FZU - 1759 Super A^B mod C(扩展欧拉定理)

传送门


网上很多题解都是错的(单纯求 b ≥ φ ( p ) b\geq \varphi(p) bφ(p)是不对的),可以去洛谷做一下扩展欧拉定理的模板,题解区的OI爷说的才是对的

欧拉定理

g c d ( a , p ) = 1 gcd(a,p)=1 gcd(a,p)=1时,有 a φ ( x ) ≡ 1    ( m o d    p ) a^{\varphi(x)} \equiv 1~~(mod~~p) aφ(x)1  (mod  p)

推论:当 g c d ( a , p ) = 1 gcd(a,p)=1 gcd(a,p)=1时,有 a b = a b % φ ( p )    ( m o d    p ) a^b=a^{b\%\varphi(p)}~~(mod~~p) ab=ab%φ(p)  (mod  p)

扩展欧拉定理

a , p a,p a,p均为正整数时(无需互质):

a b ≡ { a b    , b < φ ( p ) a b % φ ( p ) + φ ( p )    , b ≥ φ ( p )    ( m o d    p ) a^b \equiv \left\{\begin{array}{rcl} a^b~~,b< \varphi(p) \\ a^{b\% \varphi(p)+\varphi(p)}~~, b\geq \varphi(p)\end{array}\right.~~(mod~~p) ab{ ab  ,b<φ(p)ab%φ(p)+φ(p)  ,bφ(p)  (mod  p)

那么本题就迎刃而解了:

//
// Created by Happig on 2020/8/26
//
#include <iostream>
#include <math.h>
#include <string>
#include <cstring>

using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define Vector Point
#define lowbit(x) (x&(-x))
#define mkp(x, y) make_pair(x,y)
#define mem(a, x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;
const double eps = 1e-8;
const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double dinf = 1e300;
const ll INF = 1e18;
const int Mod = 1e9 + 7;
const int maxn = 2e5 + 10;

ll gcd(ll a, ll b) {
     
    return b == 0 ? a : gcd(b, a % b);
}

inline ll mul(ll a, ll b, ll p) {
     
    if (p <= 1000000000) return a * b % p;
    else if (p <= 1000000000000LL) return (((a * (b >> 20) % p) << 20) + (a * (b & ((1 << 20) - 1)))) % p;
    else {
     
        ll d = (ll) floor(a * (long double) b / p + 0.5);
        ll ret = (a * b - d * p) % p;
        if (ret < 0) ret += p;
        return ret;
    }
}

inline ll slow_mul(ll a, ll b, ll Mod) {
     
    ll ans = 0;
    while (b) {
     
        if (b & 1) ans = (ans + a) % Mod;
        a = (a + a) % Mod;
        b >>= 1;
    }
    return ans;
}

ll euler_phi(ll n) {
     
    int m = sqrt(n + 0.5);
    ll ans = n;
    for (int i = 2; i <= m; i++) {
     
        if (n % i == 0) {
     
            ans = ans / i * (i - 1);
            while (n % i == 0) n /= i;
        }
    }
    if (n > 1) ans = ans / n * (n - 1);
    return ans;
}



ll qkp(ll x, ll n, ll p) {
     
    ll ans = 1;
    x %= p;
    while (n) {
     
        if (n & 1) ans = mul(ans, x, p);
        x = mul(x, x, p);
        n >>= 1;
    }
    return ans;
}

string s;

int main() {
     
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    ll a, b, c;
    while (cin >> a >> c >> s) {
     
        ll phi = euler_phi(c);
        ll ans = 0;
        for (int i = 0; i < s.size(); i++){
     
            ans = ans * 10 + s[i] - '0';
            if(ans>=phi) break;
        }
        if(ans<phi) cout<<qkp(a,ans,c)<<endl;
        else{
     
            ans = 0;
            for (int i = 0; i < s.size(); i++)
                ans = (ans * 10 + s[i] - '0')%phi;
            ans+=phi;
            cout << qkp(a, ans, c) << endl;
        }
    }
    return 0;
}

你可能感兴趣的:(数论)