uva10692 - Huge Mods poj2164: Remainder Calculator 指数循环节
2014-06-27 20:54
513 查看
Problem X
Huge Mod
Input: standard input
Output: standard output
Time Limit: 1 second
The operator for exponentiation is different from the addition, subtraction, multiplication or division operators in the sense that the default associativity
for exponentiation goes right to left instead of left to right. So unless we mess it up by placing parenthesis,
should mean
not
. This leads to the obvious fact that if we take the levels of exponents higher (i.e., 2^3^4^5^3), the numbers can become quite big. But let's not make
life miserable. We being the good guys would force the ultimate value to be no more than 10000.
Given a1, a2, a3, ... , aN and m(=10000)
you only need to compute a1^a2^a3^...^aN mod m.
Input
There can be multiple (not more than 100) test cases. Each test case will be presented in a single line. The first line of each test case would contain the value for M(2<=M<=10000). The next number of that line would be N(1<=N<=10). Then N numbers - the valuesfor a1, a2, a3, ... , aN would follow. You can safely assume that 1<=ai<=1000. The end of input is marked by a line containing a single hash ('#') mark.
Output
For each of the test cases, print the test case number followed by the value of a1^a2^a3^...^aN mod m on one line. The sample output shows the exact format for printing the test case number.Sample Input | Sample Output |
10 4 2 3 4 5 100 2 5 2 53 3 2 3 2 # | Case #1: 2 Case #2: 25 Case #3: 35 |
A^x % m = A^(x%phi(m)+phi(m)) % m (x >= phi(m))
这个证明没怎么看懂
点击打开链接 ,但是我感觉应该是这样的:如果A和m不互质的话,最小循环节可能小于phi[m],并且phi[m]是这个最小循环节的倍数,也就是phi[m]也是它的一个循环节,只是不是最小的。A和m互质的话,根据欧拉定理,最小循环节就是phi[m]。所以不管怎样,phi[m]都是循环节。那为什么要加上phi[m]呢?因为不一定从一开始就构成循环节,应该从phi[m]开始构成循环节,比如A=2,m=4,那么序列式2,0,0,0...不知道除了M是A的幂的情况还有没有别的情况也不符合,感觉应该是没有了。
用这个公式递归计算,递归函数的含义是算当前底数的上面那一堆数的次方的部分,如果大于等于M,就对M取余再加上M。
#include<iostream> #include<queue> #include<cstring> #include<cstdio> #include<cmath> #include<set> #include<map> #include<vector> #include<stack> #include<algorithm> #define eps 1e-9 #define MAXN 10010 #define MAXM 1300 #define MOD 1000000007 typedef long long LL; using namespace std; char str[MAXN<<2]; int M,N,K,prime[MAXM],vis[MAXN],a[MAXN]; void prime_table(){ memset(vis,0,sizeof(vis)); for(int i=2;i<MAXN;i++){ if(!vis[i]) prime[K++]=i; for(int j=0;j<K&&i*prime[j]<MAXN;j++){ vis[i*prime[j]]=1; if(i%prime[j]==0) break; } } } int euler(int n){ int ret=n; for(int i=0;i<K&&prime[i]*prime[i]<=n;i++) if(n%prime[i]==0){ ret=ret/prime[i]*(prime[i]-1); while(n%prime[i]==0) n/=prime[i]; } if(n>1) ret=ret/n*(n-1); return ret; } //如果x^n<M 返回x^n,否则返回x^n%M+M LL bigpow(int x,int n,int M){ LL m=1; int flag=1; for(int i=0;i<n;i++){ m*=x; if(m>=M){ flag=0; break; } } if(flag) return m; LL ret=1,t=x%M; while(n){ if(n&1) ret=ret*t%M; t=t*t%M; n>>=1; } return ret+M; } int solve(int cur,int M){ if(cur==N-1){ if(a[cur]>M) return a[cur]%M+M; return a[cur]; } int p=solve(cur+1,euler(M)); return bigpow(a[cur],p,M); } int main(){ freopen("in.txt","r",stdin); prime_table(); int cas=0; while(scanf("%s",str)!=EOF&&str[0]!='#'){ sscanf(str,"%d",&M); scanf("%d",&N); for(int i=0;i<N;i++) scanf("%d",&a[i]); printf("Case #%d: %d\n",++cas,solve(0,M)%M); } return 0; }
2164: Remainder Calculator
Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 106 Solved: 23
[Submit][Status][Web Board]
Description
In this problem, you are given two positive integers n and m and then n positive integer a1,a2,a3...an .Suppose that for each 1<=i<=n ,bi=(ai)! . You should find
mod m
Input
The number of test cases comes in the first line.For each test case, first there are two integers n and m such that 1<=n<=100 . Then you are given a1,a2,a3...an.
Output
For each test case, printmod m
Sample Input
12 1003 8
Sample Output
76
这个麻烦一点,要注意的是如果a[i]>=M,那么肯定对M取余是0,直接返回M。否则算阶乘的话会超时。
#include<iostream> #include<queue> #include<cstring> #include<cstdio> #include<cmath> #include<set> #include<map> #include<vector> #include<stack> #include<algorithm> #define eps 1e-9 #define MAXN 110 #define MAXM 1300 #define MOD 1000000007 typedef long long LL; using namespace std; LL T,N,M,a[MAXN]; LL euler(LL n){ LL ret=n; for(LL i=2;i*i<=n;i++) if(n%i==0){ ret=ret/i*(i-1); while(n%i==0) n/=i; } if(n>1) ret=ret/n*(n-1); return ret; } //如果x^n<M 返回x^n,否则返回x ^n%M+M LL bigpow(LL f,LL n,LL M){ if(M==1||f>=M) return M; LL flag=1,x=1; for(LL i=2;i<=f;i++){ x*=i; if(x>=M){ flag=0; x%=M; } } if(flag){ LL m=1; flag=0; for(LL i=0;i<n;i++){ m*=x; if(m>=M){ flag=0; break; } } if(flag) return m; } LL ret=1,t=x%M; while(n){ if(n&1) ret=ret*t%M; t=t*t%M; n>>=1; } return ret+M; } LL solve(LL cur,LL M){ if(a[cur]>=M) return M; if(cur==N-1){ LL t=1,flag=1; for(LL i=2;i<=a[cur];i++){ t*=i; if(t>=M){ flag=0; t%=M; } } if(flag) return t; return t%M+M; } LL p=solve(cur+1,euler(M)); return bigpow(a[cur],p,M); } int main(){ freopen("in.txt","r",stdin); scanf("%lld",&T); while(T--){ scanf("%lld%lld",&N,&M); for(LL i=0;i<N;i++) scanf("%d",&a[i]); printf("%lld\n",solve(0,M)%M); } return 0; }
相关文章推荐
- uva 10692 - Huge Mods(指数循环节)
- uva 10692 - Huge Mods 指数循环节
- UVa 10692 - Huge Mods(指数循环节)
- uva10692-指数循环节
- UVa10692,Huge Mod,数论,欧拉定理,指数循环节
- UVa 10692 Huge Mods (指数循环节)
- 指数循环节 uva 10692
- 10692 - Huge Mods(指数循环节)
- uva 10692 - Huge Mods(数论)
- 指数循环节 处理A^B 问题 Super A^B mod C + Calculation
- UVA - 10298 Power Strings (KMP求字符串循环节)
- UVa10692 Huge Mods
- uva11582 斐波拉切数列循环节+欧拉定理降幂
- UVA - 10298 Power Strings (KMP求字符串循环节)
- FZU 1759 Super A^B mod C 指数循环节
- 数学专项number_theory:UVa 10692
- 【关于 A^x = A^(x % Phi(C) + Phi(C)) (mod C) 的若干证明】【指数循环节】
- 【白书之路】UVa 202 Repeating Decimals 计算小数循环节
- UVA 215 Spreadsheet Calculator (模拟)
- UVa 11549 - Calculator Conundrum(计算循环节)