Hdu 5392 Infoplane in Tina town (mass factor decomposition for GCD)

Source: Internet
Author: User

Title: http://acm.hdu.edu.cn/showproblem.php?pid=5392

Test instructions: So far I have not understood test instructions. Guess by admin: Find out the length of each cycle and find the least common multiple of these lengths. Results%3221225473.

Analysis: First find out the length of each cycle Len, because the result is very large, with GCD to find least common multiple time can not direct mode 3221225473 (Die gcd is not correct ...), can be all the length of Len decomposition of the mass factor, recording the maximum number of each mass factor, Finally, a quick power is obtained.

Code:

#include <iostream> #include <cstdio> #include <cstring> #include <cmath>typedef long LL; Using namespace std;const LL mod = 3221225473;const int sz = 3e6+7; const int MAXN = 4000;int prime[maxn],nprime,a[sz],f[sz];bool isprime[maxn],visit[sz];void doprime () {Nprime=0;int i,j; for (i=1;i<maxn;i+=2) isprime[i]=true;isprime[1]=false;isprime[2]=true;for (i=2;i<maxn;i++) {if (Isprime[i]) { Prime[nprime++]=i;for (j=2*i;j<maxn;j+=i) Isprime[j]=false;}} void divide (int x) {int ret=0,cnt;for (int i=0;i<nprime;i++) {if (x%prime[i]==0) {x/=prime[i];cnt=1;while (X%prime[i] ==0) {x/=prime[i];cnt++;} F[prime[i]]=max (f[prime[i]],cnt); if (x==1) break;}} if (x>1) F[x]=max (f[x],1);} int Getlen (int x) {int Ret=1,cur=a[x];visit[x]=true;while (x!=cur) {visit[cur]=true;ret++;cur=a[cur];} return ret;} ll Mypow (int a,int n) {LL ret=1;while (n) {if (n&1) ret=ret*a%mod;n>>=1;a=a*a%mod;} return ret;} void Solve () {LL ans=1;for (int i=0;i<sz;i++) if (F[i]) Ans=ans*mypow (I,f[i])%mod;printf ("%lld\n", ans);} int main () {doprime (), int ncase,n,i,j;scanf ("%d", &ncase), while (ncase--) {scanf ('%d ', &n); for (i=1;i<=n;i+ +) scanf ("%d", &a[i]), memset (F,0,sizeof (f)), memset (visit,false,sizeof (visit)), for (i=1;i<=n;i++) if (!visit[ I]) divide (Getlen (i)); Solve ();} return 0;}


Hdu 5392 Infoplane in Tina town (mass factor decomposition for GCD)

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.