题目链接
思路
首先要明白一个模运算的性质
(a ^ b) % p = ((a % p)^b) % p
令
e
=
2
30
+
3
,
r
=
(
q
−
1
)
(
p
−
1
)
令e=2^{30}+3,r=(q-1)(p-1)
令e=230+3,r=(q−1)(p−1),
找
到
e
在
模
n
下
的
逆
元
d
,
d
e
=
1
(
m
o
d
n
)
找到e在模n下的逆元d,de=1(mod\ n)
找到e在模n下的逆元d,de=1(mod n),然后因为
c
=
f
2
30
+
3
(
m
o
d
n
)
c=f^{2^{30}+3}(mod\ n)
c=f230+3(mod n),此处用欧拉降幂就得到
c
=
f
d
−
1
(
m
o
d
n
)
c=f^{d^{-1}}(mod\ n)
c=fd−1(mod n),运用上面第四条性质就可以得到
c
d
=
f
(
m
o
d
n
)
c^d=f(mod\ n)
cd=f(mod n)因为此处的e与n不一定互质,所以用扩展欧几里得求逆元,然后快速幂求解即可,然后中间用上快速乘就可以过了
代码实现
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
const int mod=1e9+7;
inline ll mul(ll a,ll b,ll c){return (a*b-(ll)((ld)a*b/c)*c+c)%c;}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll g = exgcd(b,a%b,y,x);y-=a/b*x;return g;}
inline ll quick_pow(ll a,ll b,ll mod){ll res=1;while(b){if(b&1)res=mul(res,a,mod);a=mul(a,a,mod);b>>=1;}return res;}
inline ll quick_pow(ll a,ll b){ll res=1;while(b){if(b&1)res=mul(res,a,mod);a=mul(a,a,mod);b>>=1;}return res;}
inline ll inv(ll x){return quick_pow(x,mod-2);}
inline ll inv(ll x,ll mod){return quick_pow(x,mod-2,mod);}
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int main(){
int t;
scanf("%d",&t);
ll a = (1<<30)+3;
for(int cas=1;cas<=t;cas++){
ll n,c;
scanf("%lld %lld",&n,&c);
ll p = sqrt(n);
while(n%p!=0)p--;
ll q = n/p;
ll r = (p-1)*(q-1);
ll x,y;
exgcd(a,r,x,y);
x=((x%r)+r)%r;
printf("Case %d: %lld\n",cas,quick_pow(c,x,n));
}
}