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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值