Description has only two Sentences HDU - 3307(欧拉函数)(数论定理)

a n = X*a n-1 + Y and Y mod (X-1) = 0.
Your task is to calculate the smallest positive integer k that a k mod a 0 = 0.
Input
Each line will contain only three integers X, Y, a 0 ( 1 < X < 2 31, 0 <= Y < 2 63, 0 < a 0 < 2 31).
Output
For each case, output the answer in one line, if there is no such k, output “Impossible!”.
Sample Input
2 0 9
Sample Output
1

数论的定理见一个记一个。。。。
自己做的就不注释了。。。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<algorithm>
using namespace std;
typedef long long ll;
ll gcd(ll x,ll y)
{
    return x%y?gcd(y,x%y):y;
}
ll euler(ll n)
{
    ll res=n,a=n;
    for(int i=2;i*i<a;i++)
        if(a%i==0)
    {
        res=res/i*(i-1);
        while(a%i==0)
        a/=i;
    }
    if(a>1)
        res=res/a*(a-1);
    return res;
}
ll poww(ll a,ll b,int mod)
{
    ll ans=1;
    while(b>0)
    {
        if(b&1)
            ans=ans*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return ans;
}
int main()
{

    ll x,a0;
    ll y;
    while(scanf("%lld%lld%lld",&x,&y,&a0)==3)
    {

            ll m=y/(x-1);
           ll d=gcd(m,a0);
           a0/=d;
           if(gcd(x,a0)!=1)
            cout<<"Impossible!"<<endl;
           else
            {
                ll ans=euler(a0);
                ll up=ans;
                for(int i=2;i*i<=up;i++)
                    if(up%i==0)
                {

                    if(poww(x,i,a0)==1)ans=min(ans,(ll)i);
                    if(poww(x,up/i,a0)==1)ans=min(ans,up/i);
                }
                cout<<ans<<endl;
            }

    }

    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值