HDU 2685 I won't tell you this is about number theory 数学结论

题意:已知a,m,n,k,求 S = gcd(a^m-1,a^n-1)%k


想法:根据公式gcd(A^m-B^m,A^n-B^n)= A^gcd(m,n) - B^gcd(m,n)。在此处B^m和B^n值为1,所以就将原公式的B代换为1。

所以S=a^gcd(n,m)-1;


#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int gcd(int x,int y)
{
    if(!y) return x;
    return gcd(y,x%y);
}
int quickpow(int x,int y,int k)
{
    int res=1;
    while(y)
    {
        if(y&1) res=res*x%k;
        y/=2;
        x=x*x%k;
    }
    return res%k;
}
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int a,m,n,k;
        cin>>a>>m>>n>>k;
        int nm=gcd(m,n);
        int res=quickpow(a,nm,k)-1;
        res=(res+k)%k;
        printf("%d\n",res);
    }
    return 0;
 } 



你可能感兴趣的:(HDU 2685 I won't tell you this is about number theory 数学结论)