题目链接:超级次方
一开始直接用快速幂,结果直接超时。
long long FastPow(long long a,long long c)
{
long long res=1;
while(c)
{
if(c&1)
{
res*=a;
res%=1337;
}
a*=a;
a%=1337;
c>>=1;
}
return res;
}
int superPow(int a, int* b, int bSize){
long long count,c=0;
for(int i=0;i<bSize;i++)
c+=(b[i]*pow(10,(bSize-i-1)));
//printf("%d ",c);
count=FastPow(a,c);
return count;
}
欧拉降幂公式:
Φ(c)是欧拉函数
详情请看:欧拉函数
求法如下:
int eular(int n)
{
int ret=1,i;
for(i=2; i*i<=n; i++)
{
if(n%i==0)
{
n/=i;
ret*=i-1;
while(n%i==0)
{
n/=i;
ret*=i;
}
}
}
if(n>1)
ret*=n-1;
return ret;
}
欧拉函数求出来了,直接A题,代码如下:
long long FastPow(long long a,long long c)
{
long long res=1;
a%=1337;
while(c)
{
if(c&1)
{
res*=a;
res%=1337;
}
a*=a;
a%=1337;
c>>=1;
}
return res;
}
int eular(int n)
{
int ret=1,i;
for(i=2; i*i<=n; i++)
{
if(n%i==0)
{
n/=i;
ret*=i-1;
while(n%i==0)
{
n/=i;
ret*=i;
}
}
}
if(n>1)
ret*=n-1;
return ret;
}
int superPow(int a, int* b, int bSize){
int n = 0,temp;
temp=eular(1337);
if(bSize==0){
return 0;
}
for(int i=0;i<bSize;i++){
n*=10;
n+=b[i];
n%=temp;
n+=temp;
}
return FastPow(a,n);
}