hdu 3307

题解

  1. a1moda0=y
    a2moda0=xy+y
    ….
  2. anmoda0=i=0n1xiy=y(xn1)/(x1)
  3. 若y / (x - 1) % a0 = 0答案为1, 否则n要满足(x^n - 1) mod t = 0
    t = a0 / gcd(a0, y / (x - 1))(扩展欧几里得可证)
    即x^n mod t = 1
  4. 若gcd(x, t) != 1无解, 否则必存在 xϕ(t)=1(modt)
    此时对phi(t)分解因子找最小的k满足x^k % t = 1就可.

code:

#include <iostream>
#include <cstdio>
using namespace std;
typedef long long ll;

ll x, y, a0;

ll FastPowMod(ll a, ll b, ll p){
    ll ret = 1 % p;
    while(b){
        if(b & 1) ret = ret * a % p;
        a = a * a % p;
        b >>= 1;
    }
    return ret;
}

ll getPhi(ll x){
    ll ret = x;
    for(ll i = 2; i * i <= x; ++i)
        if(x % i == 0){
            while(x % i== 0) x /= i;
           ret -= ret / i;
        }
    if(x > 1) ret -= ret / x;
    return ret;
}

ll cal(ll x, ll t){
    ll p = getPhi(t);
    ll tmp = p;
    for(ll i = 2; i * i <= p; ++i){
        if(p % i == 0){
            while(p % i == 0) p /= i;
            while(tmp % i == 0 && FastPowMod(x, tmp / i, t) == 1) tmp /= i;
        }
    }
    if(p > 1){
         while(tmp % p == 0 && FastPowMod(x, tmp / p, t) == 1) tmp /= p;
    }
    return tmp;
}

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

int main(){
   // freopen("in.txt", "r", stdin);
    while(cin >> x >> y >> a0){
        y /= x - 1;
        if(y % a0 == 0) {
            cout << "1" << endl;
            continue;
        }
        ll t = a0 / gcd(a0, y);
        if(gcd(x, t) == 1){
            cout << cal(x, t) << endl;
            continue;
        }
        cout << "Impossible!" << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值