指数循环节

$$求证a^b\equiv a^{b\%\varphi(m)+\varphi(m)}(\%m),其中b\geq \varphi(m)$$

$$a^0,a^1,...,a^m(\%m)$$

$$a^0,a^1,a^2,...,a^{r-1},a^r,a^{r+1},...,a^{r+\varphi (m)-1},a^r,a^{r+1},...(\%m)$$

C同学竟然卡题了！而且还卡在一个不难的题目上。题目是这样的：

对于每组数据输出一行，包含一个整数，即a1 ^ a2 ^ … an对p取模的值。

2

5 13

2 2 2 2 2

3 9

2 3 2

3

8

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<fstream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<utility>
#include<set>
#include<bitset>
#include<vector>
#include<functional>
#include<deque>
#include<cctype>
#include<climits>
#include<complex>
//#include<bits/stdc++.h>适用于CF,UOJ，但不适用于poj

using namespace std;

typedef long long LL;
typedef double DB;
typedef pair<int,int> PII;
typedef complex<DB> CP;

#define mmst(a,v) memset(a,v,sizeof(a))
#define mmcy(a,b) memcpy(a,b,sizeof(a))
#define fill(a,l,r,v) fill(a+l,a+r+1,v)
#define re(i,a,b)  for(i=(a);i<=(b);i++)
#define red(i,a,b) for(i=(a);i>=(b);i--)
#define ire(i,x) for(typedef(x.begin()) i=x.begin();i!=x.end();i++)
#define fi first
#define se second
#define m_p(a,b) make_pair(a,b)
#define p_b(a) push_back(a)
#define SF scanf
#define PF printf
#define two(k) (1<<(k))
#define SIZE(x) (int(x.size())

template<class T>inline T sqr(T x){return x*x;}
template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;}
template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;}

inline int sgn(DB x){if(abs(x)<1e-9)return 0;return(x>0)?1:-1;}
const DB Pi=acos(-1.0);

int gint()
{
int res=0;bool neg=0;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return 0;
if(z=='-'){neg=1;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
return (neg)?-res:res;
}
LL gll()
{
LL res=0;bool neg=0;char z;
for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar());
if(z==EOF)return 0;
if(z=='-'){neg=1;z=getchar();}
for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar());
return (neg)?-res:res;
}

const int maxn=25;

int n,p,s;
int a[maxn],b[maxn],c[maxn],d[maxn];

int mul(int x)
{
int i,res=x;
for(i=2;i*i<=x;i++)
if(x%i==0)
{
while(x%i==0)x/=i;
res=res/i*(i-1);
}
if(x>1)res=res/x*(x-1);
return res;
}

int getpow(int a,int k,int modd)
{
LL res=1,x=a,flag=0;
for(;k;k>>=1)
{
if(k&1)
{
if(res*x>=modd) flag=1;
res=res*x%modd;
}
if(k==1)break;
if(x*x>=modd)flag=1;
x=x*x%modd;
}
if(flag)res+=modd;
return res;
}

int main()
{
freopen("pow.in","r",stdin);
freopen("pow.out","w",stdout);
int i,j,T=gint();
while(T--)
{
n=gint();p=gint();mmst(a,0);mmst(b,0);mmst(c,0);mmst(d,0);
re(i,1,n)a[i]=gint();
s=0;for(int t=p;t!=1;t=mul(t))b[++s]=t;
re(j,1,s)
{
c[j]=a[n]%b[j];
if(a[n]>=b[j])c[j]+=b[j];
}
red(i,n-1,1)
{
re(j,1,s)d[j]=getpow(a[i],c[j+1],b[j]);
re(j,1,s)c[j]=d[j];
}
PF("%d\n",c[1]%p);
}
return 0;
}
View Code

posted @ 2015-12-15 21:22  maijing  阅读(1976)  评论(3编辑  收藏  举报