您的位置:首页 > 大数据 > 人工智能

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 values
for 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 MB

Submit: 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, print

mod 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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: