HDU 2685 I won't tell you this is about number theory

To think of a beautiful problem description is so hard for me that let’s just drop them off. :)
Given four integers a,m,n,k,and S = gcd(a^m-1,a^n-1)%k,calculate the S.

Input
The first line contain a t,then t cases followed.
Each case contain four integers a,m,n,k(1<=a,m,n,k<=10000).

Output
One line with a integer S.

eg:
intput
1
1 1 1 1

output
0

题意:S = gcd(a^m-1,a^n-1)%k,求s;
需要知道一个公式:
gcd(a^(m-1),a^(n-1)) = (a^gcd(m,n))-1
推广式:
若 gcd(a,b)=1
gcd(a^m-b^m,a^n-b^n) = a^gcd(m,n)-b^gcd(m,n)

代码:

#include<cstdio>
#define ll long long
ll Gcd(ll x,ll y)
{
    return y?Gcd(y,x%y):x;
}
int main()
{
    ll n,m,a,k,t;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld%lld%lld%lld",&a,&m,&n,&k);
        ll s=Gcd(m,n);
        ll Pow=1;
        for(int i=1;i<=s;i++)
        {
            Pow*=a; 
            Pow%=k;
        }
        Pow%=k;
        ll ans=(Pow-1+k)%k;
        printf("%lld\n",ans);
    }
    return 0;

}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值