Hdu 2685 I won't tell you this is about number theory 快速幂取模+gcd

Problem Description

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.

Sample Input

 

1

1 1 1 1

Sample Output

 

0

此题需要用到一个结论....

if(a>b&&gcd(a,b)==1)

gcd(a^m-b^m,a^n-b^n)=a^gcd(m,n)-b^gcd(m,n);

代码如下:
 

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;
ll a,m,n,k;
int t;
ll Fast (int a,int b)
{
    ll sum=1;
    while (b)
    {
        if(b&1)
        {
            sum=sum*a%k;
        }
        b>>=1;
        a=a*a%k;
    }
    return sum;
}
ll gcd (ll a,ll b)
{
    return b==0? a:gcd(b,a%b);
}
int main()
{
    scanf("%d",&t);
    while (t--)
    {
        scanf("%lld%lld%lld%lld",&a,&m,&n,&k);
        a=a%k;
        ll ans=(Fast(a,gcd(m,n))-1+k)%k;
        printf("%lld\n",ans);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41410799/article/details/83056696