Links: Http://codeforces.com/problemset/problem/542/C
Test instructions: To a function f (x), define the field [1,n], so that f (k,x) = f (f (f (f...f (x)))) (the total iterative K-times). The smallest k, so that f (k, X) satisfies the properties of g (g (x)) = g (x), i.e. F (k,f (k,x)) = f (k,x) (x belongs to [1,n]).
Idea: Although the method is simple, it is more ingenious and worth doing. Since f (k,x) is only related to the number of iterations of F, and the F array is initially given, it may be appropriate to treat all x as [1,n] separately. Consider F (1,x), F (2,x), F (3,x), F (k,x) consisting of a sequence, since F (k,f (k,x)) = f (2k, x), so translates to the number AK in the K position of the sequence, making AK = a2k. Since the last item of the sequence is an F () operation of the previous item, there must be a loop section, and the length of the loop section is less than or equal to N, it may be possible to start the sequence from the P0 position cycle, the length of the link is K0, then the value of K is {sK0, (s+1) K0, (s+2) K0 , sK0 is the decimal number greater than or equal to P0. Then x is taken [1,n], the value of the set of K is the minimum value of the intersection element is the answer, but obviously can not be asked. Because of each value of the argument, K is always in that case, the value of the Loop section K0 a multiple, it is advisable to all the K0 least common multiple, and then adjust the result so that it satisfies the P0 is greater than or equal to all, adjust the time just add all K0 least common multiple, until the conditions are met.
1 #pragmaComment (linker, "/stack:10240000,10240000")2 3#include <iostream>4#include <cstdio>5#include <algorithm>6#include <cstdlib>7#include <cstring>8#include <map>9#include <queue>Ten#include <deque> One#include <cmath> A#include <vector> -#include <ctime> -#include <cctype> the#include <Set> -#include <bitset> -#include <functional> -#include <numeric> +#include <stdexcept> -#include <utility> + A using namespacestd; at - #defineMem0 (a) memset (a, 0, sizeof (a)) - #defineMem_1 (a) memset (a,-1, sizeof (a)) - #defineLson L, M, RT << 1 - #defineRson m + 1, R, RT << 1 | 1 - #definedefine_m int m = (L + r) >> 1 in #defineRep_up0 (A, b) for (int a = 0; a < (b); a++) - #defineRep_up1 (A, b) for (int a = 1; a <= (b); a++) to #defineRep_down0 (A, b) for (int a = b-1; a >= 0; a--) + #defineRep_down1 (A, b) for (int a = b; a > 0; a--) - #defineAll (a) (a). Begin (), (a). End () the #defineLowbit (x) ((x) & (-(x))) * #defineCONSTRUCTINT4 (name, a, B, C, D) name (int a = 0, int b = 0, int c = 0, int d = 0): A (a), B (b), C (c), D (d) {} $ #defineCONSTRUCTINT3 (name, a, B, c) name (int a = 0, int b = 0, int c = 0): A (a), B (b), C (c) {}Panax Notoginseng #defineConstructInt2 (name, a, b) name (int a = 0, int b = 0): A (a), B (b) {} - #definePCHR (a) Putchar (a) the #definePstr (a) printf ("%s", a) + #defineSstr (a) scanf ("%s", a) A #defineSint (a) scanf ("%d", &a) the #defineSint2 (A, b) scanf ("%d%d", &a, &b) + #defineSint3 (A, B, c) scanf ("%d%d%d", &a, &b, &c) - #definePint (a) printf ("%d\n", a) $ #defineTest_print1 (a) cout << "var1 =" << a << Endl $ #defineTest_print2 (A, b) cout << "var1 =" << a << ", var2 =" << b << Endl - #defineTest_print3 (A, B, c) cout << "var1 =" << a << ", var2 =" << b << ", Var3 =" << c &L t;< Endl - #defineMP (A, b) Make_pair (A, B) the #definePB (a) push_back (a) - WuyitypedefLong LongLL; thetypedef pair<int,int>PII; -typedef vector<int>VI; Wu - Const intdx[8] = {0,0, -1,1,1,1, -1, -1}; About Const intdy[8] = {-1,1,0,0,1, -1,1, -1 }; $ Const intMAXN = 3e4 +7; - Const intMD =10007; - Const intINF = 1e9 +7; - ConstLL inf_l = 1e18 +7; A Const DoublePI = ACOs (-1.0); + Const DoubleEPS = 1e-6; the -template<classT>t gcd (t A, T b) {returnb==0? A:GCD (b,a%b);} $template<classT>BOOLMax_update (T &a,ConstT &b) {if(B>a) {a = B;return true;}return false;} thetemplate<classT>BOOLMin_update (T &a,ConstT &b) {if(B<a) {a = B;return true;}return false;} thetemplate<classT>t condition (BOOLF, t A, T b) {returnF?a:b;} thetemplate<classT>voidCopy_arr (t a[], T b[],intN) {rep_up0 (i,n) a[i]=b[i];} the intMAKE_ID (intXintYintN) {returnX * n +y;} - in intf[207]; the intMINV; the About intFindintx) { the intused[207], C =0; the mem0 (used); the while(!Used[f[x]]) { +C + +; -Used[x = f[x]] =C; the }Bayi max_update (MINV, used[f[x]]); the returnC-USED[F[X]] +1; the } - - intMain () { the //freopen ("In.txt", "R", stdin); the intN; theCIN >>N; the Rep_up1 (i, n) { - Sint (F[i]); the } theLL ans =1; the Rep_up1 (i, n) {94LL tmp =find (i); theAns = ans/gcd (ans, tmp) *tmp; the } theLL tmp =ans;98 while(Ans < MINV) ans + =tmp; Aboutcout << ans <<Endl; - return 0;101}
View Code
[Codeforces-542-c] Yy?