Multiplication method + two hnu13547 Lily ' game

Source: Internet
Author: User

Portal: Click to open link

Test instructions: Gives an original string A, and a permutation B. For each operation, the string A is transformed by permutation B, and the number of odd digits is multiplied by 2. Ask the original string a after many operations, can get c*2^d. If available, the minimum number of operations is output, otherwise the output-1

Idea: We analyze, first of all, for the problem of permutation transformation, of course, the first step is to transform the transformation into a graph model, that is, a side i->b[i]. We can see that we get a lot of rings.

The answers to the different rings are independent. We consider each ring as a line of thought. If A[i] transformation can become c*2^d, multiplied by x 2, then the position I according to the graph walk, just go through x points, the number of these points is odd.

We use the multiplication method to deal with starting from a point u, down the 1th point, the 2nd point, the 4th point ... The position of the 2^k point, and saves the number of odd numbered points on the path.

If there are y odd points on the whole ring, we need x, we'll use x%y to simplify, then we'll go to the second answer in the last lap.

The final total complexity is O (n (logn) ^2), should be able to do O (Nlogn), but I multiply the method to write a comparison rub, give a more Logn

But the trick point of the problem is too many, leading to the time of the game has been card-

1. The answer will explode int

2. If the number of odd points on the ring is 0, a special sentence, or divide 0, will be re

3. First remove the 2 in C and add it to D

4. In the x%y, but also to pay special attention to the division of the time, divide the number of steps may not walk the complete X/y rings, the number of steps may be less.

#include <map> #include <set> #include <cmath> #include <ctime> #include <stack> #include <queue> #include <cstdio> #include <cctype> #include <bitset> #include <string> #include <vector> #include <cstring> #include <iostream> #include <algorithm> #include <functional > #define FUCK (x) cout<< "[" <<x<< "]"; #define FIN freopen ("Input.txt", "R", stdin); #define FOUT Freopen ("Output.txt", "w+", stdout); using namespace Std;typedef long Long ll;typedef pair<int, int> pii;const int MX = 1e5 + 5;const LL INF = 0x3f3f3f3f;int n;int z[mx], a[mx], Nlen;int nxt[mx][20], Val[mx][20];int belong[mx], Clen[MX], CV AL[MX], Bcnt;bool vis[mx];void BFS (int u, int dfn) {queue<int> Q;    Q.push (U); while (! Q.empty ()) {u = Q.front ();        Q.pop (); clen[dfn]++;        Vis[u] = 1;        if (U & 1) cval[dfn]++;        Belong[u] = DFN;        Nxt[u][0] = Z[u];        Val[u][0] = Z[u]% 2; if (!vis[z[u]]) {Vis[z[u]] = 1;        Q.push (Z[u]);    }}}void Presolve () {bcnt = 0;    memset (Vis, 0, sizeof (VIS));            for (int i = 1; I <= n; i++) {if (!vis[i]) {++bcnt;            CLEN[BCNT] = cval[bcnt] = 0;        BFS (i, bcnt); }} for (int i = 1, i <= nlen; i++) {for (int j = 1; J <= N; j + +) {Nxt[j][i] = Nxt[nxt[j][i            -1]][I-1];        Val[j][i] = Val[j][i-1] + val[nxt[j][i-1]][i-1];    }}}int get (int m, int u) {int ret = 0;            for (int i = 0; I <= nlen; i++) {if (M >> I & 1) {ret + = Val[u][i];        U = nxt[u][i]; }} return ret;}    LL Solve (int u, int d) {int block = Belong[u];    if (d = = 0) return 0;    if (cval[block] = = 0) return-1;    int k = (d-1)/cval[block];    LL ret = (ll) k * Clen[block];    D-= k * Cval[block];    int L = 0, r = clen[block], m;        while (L <= r) {m = (L + r) >> 1; int temp = Get (M, u);        if (temp >= d) r = m-1;    else L = m + 1; } return ret + R + 1;}    int Getmax (int n) {for (int i = 1; I >= 0; i--) {if (n >> I &) return i;    }}int Main () {//fin;        while (~SCANF ("%d", &n)) {for (int i = 1; I <= n; i++) {scanf ("%d", &a[i]);        } for (int i = 1; I <= n; i++) {scanf ("%d", &z[i]);        } Nlen = Getmax (n);        Presolve ();        int C, D;        LL ans =-1;        scanf ("%d%d", &c, &d);        while (c% 2 = = 0) d++, c/= 2;            for (int i = 1; I <= n; i++) {if (A[i]% c! = 0) Continue;            A[i]/= C;            int s = 0;            While (A[i]% 2 = = 0) a[i]/= 2, s++;            if (a[i]! = 1 | | s > D) continue;            LL ret = Solve (I, D-s);                if (ret! =-1) {if (ans = =-1) ans = ret;            else ans = min (ans, ret);    }} printf ("%d\n", ans); }    return 0;} 


Multiplication method + two hnu13547 Lily ' game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.