[Codeforces-Gym] (101550E)Exponial ---- 广义欧拉定理降幂★

版权声明:本文为博主原创文章,转载请预先通知博主(〃'▽'〃)。 https://blog.csdn.net/m0_37624640/article/details/82954446

题目传送门

AC代码:

#include<bits/stdc++.h>
#define IO          ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)
#define pb(x)       push_back(x)
#define sz(x)       (int)(x).size()
#define sc(x)       scanf("%d",&x)
#define pr(x)       printf("%d\n",x)
#define abs(x)      ((x)<0 ? -(x) : x)
#define all(x)      x.begin(),x.end()
#define mk(x,y)     make_pair(x,y)
#define debug       printf("!!!!!!\n")
#define fin         freopen("in.txt","r",stdin)
#define fout        freopen("out.txt","w",stdout)
using namespace std;
typedef long long ll;
typedef pair<int,int> PII;
const int mod = 1e9+7;
const double PI = 4*atan(1.0);
const int maxm = 1e8+5;
const int maxn = 1e4+5;
const int INF = 0x3f3f3f3f;
const ll LINF = 1ll<<62;
ll phi(ll x)
{
    ll ans = x;
    for(int i=2;i*i<=x;i++)
    {
        if(x%i == 0){
            ans =  ans/i*(i-1);
            while(x%i == 0) x /= i;
        }
    }
    if(x>1) ans = ans/x*(x-1);
    return ans;
}
ll qpow(ll a,ll b,ll m)
{
    ll res = 1;
    while(b){
        if(1&b) res = (res*a)%m;
        a = (a*a)%m;
        b >>= 1;
    }
    return res;
}
ll solve(ll x,ll m)   // a^b mod c = a^(b mod phi(c) + phi(c))
{
    if(m == 1) return 0;
    if(x == 1) return 1;
    if(x == 2) return 2%m;
    if(x == 3) return 9%m;
    if(x == 4) return qpow(4,9,m);
    else{
        ll e = phi(m);
        ll k = solve(x-1,e);
        ll ans = qpow(x,k+e,m);
        return ans;
    }
}
int main()
{
    // fin;
    IO;
    ll n,m;
    cin>>n>>m;
    cout<<solve(n,m)<<endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_37624640/article/details/82954446