hdu4335 (指数循环节)

来源:互联网 发布:python手机自动化脚本 编辑:程序博客网 时间:2024/05/08 00:05
 

hdu4335 (指数循环节)

分类: 数论 63人阅读 评论(0) 收藏 举报

题目:What is N?

 

本题好坑人,居然还有那种情况。。。

[cpp] view plaincopy
  1. #include <stdio.h>  
  2.   
  3. #define LLU unsigned long long  
  4.   
  5. LLU Euler(LLU n)  
  6. {  
  7.      LLU i;  
  8.      LLU ret=n;  
  9.      for(i=2;i*i<=n;i++)  
  10.      {  
  11.          if(n%i==0)  
  12.          {  
  13.              ret=ret-ret/i;  
  14.              while(n%i==0) n/=i;  
  15.          }  
  16.      }  
  17.      if(n>1)  
  18.          ret=ret-ret/n;  
  19.      return ret;  
  20.  }  
  21.   
  22.  LLU quick_mod(LLU a,LLU b,LLU m)  
  23.  {  
  24.      LLU ans=1;  
  25.      while(b)  
  26.      {  
  27.          if(b&1)  
  28.          {  
  29.              ans=ans*a%m;  
  30.              b--;  
  31.          }  
  32.          b>>=1;  
  33.          a=a*a%m;  
  34.      }  
  35.      return ans;  
  36.  }  
  37.   
  38.  LLU f[100001];  
  39.   
  40.  int main()  
  41.  {  
  42.      int t,T,i,k,l,flag;  
  43.      LLU b,p,m,phi,ans;  
  44.      scanf("%d",&T);  
  45.      for(t=1;t<=T;t++)  
  46.      {  
  47.          ans=0;flag = 0;  
  48.          scanf("%I64u%I64u%I64u",&b,&p,&m);  
  49.          if(b==0&&p==1&&m==18446744073709551615ull)  
  50.          {  
  51.              printf("Case #%d: 18446744073709551616\n",t);  
  52.              continue;  
  53.          }  
  54.          phi=Euler(p);  
  55.          f[0]=1;  
  56.          if(b==0)  
  57.              ans++;  
  58.          for(i=1;i<=m;i++)  
  59.          {  
  60.              f[i] = f[i-1]*i;  
  61.              if(f[i]>=phi)  
  62.              {  
  63.                  f[i]=f[i]%phi;  
  64.                  flag=1;  
  65.                  if(f[i]==0)  
  66.                      break;  
  67.              }  
  68.              if(flag)  
  69.              {  
  70.                  if(quick_mod(i,f[i]+phi,p)==b)  
  71.                      ans++;  
  72.              }  
  73.              else  
  74.              {  
  75.                  if(quick_mod(i,f[i],p)==b)  
  76.                      ans++;  
  77.              }  
  78.          }  
  79.          for(k=0;i<=m&&k<p;i++,k++)  
  80.              if(quick_mod(i,phi,p)==b)  
  81.                  ans = ans+1+(m-i)/p;  
  82.          printf("Case #%d: %I64u\n",t,ans);  
  83.      }  
  84.      return 0;  
  85.  }  
 

NEFU 691(欧拉函数与指数循环节)

分类: 数论 84人阅读 评论(0) 收藏 举报

题目:Remainder Calculator

 

[cpp] view plaincopy
  1. #include <iostream>  
  2. #include <string.h>  
  3. #include <stdio.h>  
  4.   
  5. using namespace std;  
  6. typedef long long LL;  
  7. const int N=105;  
  8.   
  9. LL p[N],a[N];  
  10.   
  11. LL phi(LL n)  
  12. {  
  13.     LL rea=n,i;  
  14.     for(i=2;i*i<=n;i++)  
  15.     {  
  16.         if(n%i==0)  
  17.         {  
  18.             rea=rea-rea/i;  
  19.             while(n%i==0) n/=i;  
  20.         }  
  21.     }  
  22.     if(n>1)  
  23.         rea=rea-rea/n;  
  24.     return rea;  
  25. }  
  26.   
  27. void Init(LL m)  
  28. {  
  29.     p[0]=m;  
  30.     for(LL i=1;i<105;i++)  
  31.        p[i]=phi(p[i-1]);  
  32. }  
  33.   
  34. LL Solve(LL k,LL *up)  
  35. {  
  36.     LL i,j,chk;  
  37.     LL m=p[k];  
  38.     LL res,num,next,num_is_big=0;  
  39.     if (m==1)  
  40.     {  
  41.         if(a[k]>1) *up=1;  
  42.         else *up=0;  
  43.         return 0;  
  44.     }  
  45.     if (a[k]>=m) {*up=1; return 0;}  
  46.     num=1;  
  47.     for(i=2;i<=a[k];i++)  
  48.     {  
  49.         num=num*i;  
  50.         if (num>=m) num_is_big=1, num%=m;  
  51.     }  
  52.     next=Solve(k+1,&chk);  
  53.     if (chk==1) next+=phi(m);  
  54.     res=1; *up=0;  
  55.     for(i=0;i<next;i++)  
  56.     {  
  57.         res=res*num;  
  58.         if (res>=m) *up=1,res%=m;  
  59.         if (num_is_big) *up=1;  
  60.     }  
  61.     return res;  
  62. }  
  63.   
  64. int main()  
  65. {  
  66.     LL n,t,m,i;  
  67.     cin>>t;  
  68.     while(t--)  
  69.     {  
  70.         cin>>n>>m;  
  71.         for(i=0;i<n;i++)  
  72.            cin>>a[i];  
  73.         if(m==1)  
  74.         {  
  75.             puts("0");  
  76.             continue;  
  77.         }  
  78.         Init(m);  
  79.         cout<<Solve(0,&i)<<endl;  
  80.   
  81.     }  
  82.     return 0;  
  83. }  

原创粉丝点击