码迷,mamicode.com
首页 > 其他好文 > 详细

hdu-2685I won't tell you this is about number theory(数论)

时间:2016-04-27 22:15:01      阅读:311      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:

I won‘t tell you this is about number theory

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
 
题意
 
求gcd(a^m-1,a^n-1)%k的值;
 
思路
 
有定理gcd(a^m-b^m,a^n-b^n)=a^gcd(n,m)-b^gcd(n,m);
所以 gcd(a^m-1,a^n-1)%k == (a^gcd(n,m)-1)%k;
 
AC代码
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
//const LL mod=1e9+7;
const int N=1e5+6;
int gcd(int x,int y)
{
        if( y == 0 )return x;
        return gcd( y,x%y );
}
int fastpow(int x,int y,int z)
{
    int s = 1, base = x;
    while(y)
    {
        if(y&1)
        {
            s *= base;
            s %= z;
        }
        base *= base;
        base %= z;
        y = (y>>1);
    }
    return s;
}
int fun( int x,int y,int z,int mod)
{
    int temp = gcd( y, z);
    return (fastpow(x,temp,mod)-1+mod)%mod;
}
int main()
{
    int t;
    scanf( "%d",&t );
    int a,n,m,k;
    while( t -- )
    {
        scanf( "%d%d%d%d",&a,&n,&m,&k );
        printf( "%d\n",fun( a, n, m, k));
    }
    return 0;
}

 

 

hdu-2685I won't tell you this is about number theory(数论)

标签:

原文地址:http://www.cnblogs.com/zhangchengc919/p/5440527.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!