The main idea: give a sequence, the transformation can return to the original position several times. For example 1 3 2, 3 is not in the original position, changed to 3 position, the number of changes to 2, the number of +1. Get 2.
Problem-solving ideas: the practice is to decompose the cycle length. Then ask for the least common multiple. But can not directly use LCM to seek least common multiple. We can consider the decomposition of prime numbers to seek, that is, the common mass factor multiplied by the mass factor of each number itself.
#include <cctype>#include <cstdio>#include <algorithm>using namespace STD;Const Long LongMOD =3221225473;Const intMAXN =3000010;intA[MAXN], REC[MAXN];namespaceIO {Const Static intMAXN = $<< -;Static CharBUF[MAXN], *pbuf = buf, *end;voidInit () {intc = fread (buf,1, MAXN, stdin); End = buf + C; }int&readint () {Static intAns Ans =0; while(Pbuf! = End &&!)IsDigit(*PBUF)) Pbuf + +; while(Pbuf! = End &&IsDigit(*PBUF)) {ans = ans *Ten+ *pbuf-' 0 '; Pbuf + +; }returnAns }}intMain () {io::init ();intT T = Io::readint (); while(t--) {intN n = io::readint (); for(inti =1; I <= N; i++) A[i] = Io::readint ();intCNT =0; for(inti =1; I <= N; i++) {intpos = i, TMP =0, Next; while(A[pos]) {next = A[pos]; A[pos] =0; pos = next; tmp++; }if(TMP! =0) rec[cnt++] = tmp; }Long LongAns =1; for(inti =0; I < CNT; i++) {intTMP = Rec[i]; for(intj =2; J * J <= tmp; J + +) {intnum =0; while(tmp% J = =0) {tmp/= J; num++; } A[j] = max (a[j], num); }if(tmp >1) a[tmp] = max (1, A[tmp]); } for(inti =2; I <= N; i++) while(a[i]--) ans = (ans * i)% MOD;printf("%lld\n", ans); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU-5392 Infoplane in Tina town