你最愿意做的哪件事,才是你的天赋所在

0%

gym-102055-RSA加密-欧拉降幂

题目链接

gym102055

思路

首先要明白一个模运算的性质
(a ^ b) % p = ((a % p)^b) % p
$令e=2^{30}+3,r=(q-1)(p-1)$,$找到e在模n下的逆元d,de=1(mod n)$,然后因为$c=f^{2^{30}+3}(mod n)$,此处用欧拉降幂就得到$c=f^{d^{-1}}(mod n)$,运用上面第四条性质就可以得到$c^d=f(mod n)$因为此处的e与n不一定互质,所以用扩展欧几里得求逆元,然后快速幂求解即可,然后中间用上快速乘就可以过了

代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#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));
}
}
-------------你最愿意做的哪件事才是你的天赋所在-------------