标签:答案 iostream out pre code bsp ble The inpu
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20133 Accepted Submission(s): 6321
#include<iostream> #include<math.h> #define ll long long using namespace std; int main() { ll n; while(~scanf("%lld",&n)) { if(n==1||n%2==0) printf("2^? mod %d = 1\n",n ); else { ll s=2; for(int i=2;;i++) { s=s*2%n; if(s==1) { printf("2^%d mod %d = 1\n",i,n ); break; } } } } return 0; }
欧拉函数:
先求欧拉函数的值phi(n),在对phi(n)进行因数分解,把phi(n)的因数存在数组e[i]里面
然后依次枚举e[i]的每一个数,同时判断这个数e[i]是否满足2e[i]%m==1,不断更新一个最小值,最后得到答案
#include <cstdio> #include <cstring> #include <cmath> #include <iostream> #include <algorithm> using namespace std; #define LL __int64 LL t,e[1000]; LL mod; LL euler_phi(LL n)//欧拉函数 { LL m=sqrt(n+0.5); LL ans=n,i; for(i=2;i<=m;i++) { if(n%i==0) { ans=ans/i*(i-1); while(n%i==0)n=n/i; } } if(n>1)ans=ans/n*(n-1); return ans; } void find(LL n)//找出m的所有因子 { LL i; e[t++]=n; for(i=2;i*i<=n;i++) { if(n%i==0) { if(i*i==n) e[t++]=i; else { e[t++]=i; e[t++]=n/i; } } } } LL pows(LL a,LL b) { LL s=1; while(b) { if(b&1) s=(s*a)%mod; a=(a*a)%mod; b=b>>1; } return s; } int main() { LL n; while(cin>>n) { if(n%2==0||n==1) cout<<"2^? mod "<<n<<" = 1"<<endl; else { LL m,ans,i; m=euler_phi(n); t=0; find(m); sort(e,e+t); mod=n; for(i=0;i<t;i++) { if(pows(2,e[i])==1) { ans=e[i]; break; } } cout<<"2^"<<ans<<" mod "<<n<<" = 1"<<endl; } } return 0; }
标签:答案 iostream out pre code bsp ble The inpu
原文地址:https://www.cnblogs.com/-citywall123/p/10084259.html