Infoplane in Tina town
Time limit:14000/7000 MS (java/others) Memory limit:524288/524288 K (java/others)
Total submission (s): 805 Accepted Submission (s): 168
Problem Descriptionthere is a big stone with smooth surface in Tina town. When people go towards it, the stone surface is lighted and show its usage. This stone is a legacy and also the center of Tina Town ' s calculation and control system. Also, it can display events in Tina Town and contents that pedestrians is interested in, and it can is used as public com Puter. It makes people ' s life more convenient (especially for who forget to take a device).
Tina and Town were playing a game on this stone. First, a permutation of numbers from 1 To n were displayed on the stone. Town exchanged some numbers randomly and town recorded this process by macros. Town asked Tine, "does know how many times it need to turn these numbers into the original permutation by executing this Macro? Tina didn ' t know the answer so she asked your to find out the answer for her.
Since the answer is very large, you have need to output the answer modulo 3? 2 +1=3221225473 (a prime).
Inputthe first line was an integer T Representing the number of test cases. T≤5
For each test case, the first line was an integer n Representing the length of permutation. N≤3? ten 6
The second line contains n Integers representing a permutation A 1 ... A n . It is guaranteed, that numbers be different each, and all a i Satisfies ( 1≤ A i ≤N ).
Outputfor each test case, print a number ans Representing the answer.
Sample Input
231 3 262 3 4 5 6 1
Sample Output
26
Sourcebestcoder Round #51 (Div.2)
Recommendhujie | We have carefully selected several similar problems for you:5395 5394 5393 5390 5389
Given a sequence, the transformation can be returned to its 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.
The practice is to decompose the loop 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 <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < Algorithm>using namespace Std;typedef Long long ll;typedef unsigned long long ull;int a[3000010];int Vis[3000010];int b [3000010];const ll Mod=3221225473;inline int read () {char ch; For (Ch=getchar (); ch<48| | ch>57;) Ch=getchar (); int d=0; for (; ch>47&&ch<58; Ch=getchar ()) d=d*10+ch-48; return D;} ll GCD (ll A,ll b) {if (b==0) return A; else return gcd (b,a%b);} ll LCM (ll A,ll b) {return a/gcd (b) *b;} int main () {int t,n,i,j; scanf ("%d", &t); while (t--) {scanf ("%d", &n); for (I=1; i<=n; i++) A[i]=read (); memset (vis,0,sizeof (VIS)); Memset (b,0,sizeof (b)); for (I=1; i<=n; i++) {if (!vis[i]) {int t=i; int s=0; while (!vis[t]) {s++; Vis[t]=1; T=A[T]; } for (j=2; j*j<=s; J + +) {int cnt=0; while (s%j==0) {cnt++; S/=j; } b[j]=max (B[J],CNT); Statistics of the public quality factors. } if (s>1) B[s]=max (b[s],1); }}//COUT<<LCM (121,11) <<endl; Ull Ans=1; for (i=2; i<=n; i++) for (j=1; j<=b[i]; j + +) Ans=ull (ans) *i%mod; cout<<ans<<endl; }}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
HDU 5392 Infoplane in Tina town