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

来源:互联网 发布:差分演化算法及其应用 编辑:程序博客网 时间:2024/06/06 03:11

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 longll 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;}
阅读全文
0 0
原创粉丝点击