zoj 2674 Strange Limit 欧拉定理应用 (3-E)
1 a,m和不一定互素的时候,欧拉定理的应用 a^phi(m)%m=a^(k*phi(m) ) %m (证明用到中国剩余定理)
2 发现A 满足的同余式以后 ,由于phi(m)<m ,可以设计一个递归函数搞定之
#include<iostream> #include<algorithm> #include<cmath> using namespace std; struct robber { int money; int id; double inf; }; int cmp(robber x,robber y) { if(x.inf<y.inf) return 1; else return 0; } int main() { int size; cin>>size; for(int l=0;l<size;l++) { int n,m,y; cin>>n>>m>>y; int *p=new int [n]; for(int i=0;i<n;i++) cin>>p[i]; int q[n]; for(int i=0;i<n;i++) { q[i]=m*p[i]/y; } int sum=0; for(int i=0;i<n;i++) sum+=q[i]; int left=m-sum; // 存在结构体中去 robber * rr=new robber[n]; for(int i=0;i<n;i++) { rr[i].id=i; rr[i].money=q[i]; rr[i].inf=abs((q[i]+1.0)/m-(p[i]*1.0)/y)-abs((q[i]*1.0)/m-(p[i]*1.0)/y); } sort(rr,rr+n,cmp); for(int i=0;i<left;i++) { q[rr[i].id]++; } for(int i=0;i<n-1;i++) cout<<q[i]<<" "; cout<<q[n-1]<<endl; if(l<size-1) cout<<endl; } }
这里求phi(m)的方式还是有点土
watashi大神的代码如下:
#include <cstdio> long long f[] = {1, 1, 2, 6, 24, 120, 720, 5040, 40320, 362880, 3628800, 39916800, 479001600}; long long p[] = {2, 3, 5, 7, 11, 13}; long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); } long long phi(long long n) { long long m = 1; for (int i = 0; n > 1; ++i) { if (n % p[i] == 0) { m *= p[i] - 1; n /= p[i]; while (n % p[i] == 0) { m *= p[i]; n /= p[i]; } } } return m; } long long powMod(long long a, long long b, long long m) { long long c = 1 % m; while (b > 0) { if ((b & 1) != 0) { c = c * a % m; } a = a * a % m; b >>= 1; } return c; } long long gao(long long a, long long b) { if (b == 1) { return 0; } else { long long d = phi(b); return powMod(a, d + gao(a, d), b); } } int main() { bool blank = false; long long a, b; while (scanf("%lld%lld", &a, &b) != EOF) { if (blank) { puts(""); } else { blank = true; } printf("%lld\n", gao(a, f[b])); } return 0; } /* Run ID Submit Time Judge Status Problem ID Language Run Time(ms) Run Memory(KB) User Name 2130779 2010-03-28 22:49:39 Accepted 2674 C++ 0 176 watashi@Zodiac */