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)); } }
|