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

题目链接:

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(数论)

时间: 2024-10-14 04:39:16

hdu-2685I won't tell you this is about number theory(数论)的相关文章

hdu 2685 I won&#39;t tell you this is about number theory

#include<stdio.h>#include<string.h>#define LL __int64 LL mult_mod(LL a,LL b,LL c){    a%=c;    b%=c;    LL ret=0;    while(b)    {        if(b&1){ret+=a;ret%=c;}        a<<=1;        if(a>=c)a%=c;        b>>=1;    }    retur

HDU 1005 Number Sequence(数论)

HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple

hdu 4279&quot;Number&quot;(数论)

传送门 待参考资料: [1]:https://blog.csdn.net/u010709592/article/details/13615763 原文地址:https://www.cnblogs.com/violet-acmer/p/10676924.html

hdu 4961 Boring Sum(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4961 Problem Description Number theory is interesting, while this problem is boring. Here is the problem. Given an integer sequence a1, a2, -, an, let S(i) = {j|1<=j<i, and aj is a multiple of ai}. If S

hdu 1698 Just a Hook 线段树区间更新

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1698 Let us number the consecutive metallic sticks of the hook from 1 to N. For each operation, Pudge can change the consecutive metallic sticks, numbered from X to Y, into cupreous sticks, silver sticks

HDU - 4961 Boring Sum

Problem Description Number theory is interesting, while this problem is boring. Here is the problem. Given an integer sequence a1, a2, -, an, let S(i) = {j|1<=j<i, and aj is a multiple of ai}. If S(i) is not empty, let f(i) be the maximum integer in

HDU 2824 The Euler function 题解

求区间的euler数值,自然使用筛子法了. Problem Description The Euler function phi is an important kind of function in number theory, (n) represents the amount of the numbers which are smaller than n and coprime to n, and this function has a lot of beautiful character

HDU 3341 Lost&#39;s revenge AC自动机+dp

Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 3757    Accepted Submission(s): 1020 Problem Description Lost and AekdyCoin are friends. They always play "number game"(A bor

HDU 3341 Lost&#39;s revenge

Lost's revenge Time Limit: 5000ms Memory Limit: 65535KB This problem will be judged on HDU. Original ID: 334164-bit integer IO format: %I64d      Java class name: Main Lost and AekdyCoin are friends. They always play "number game"(A boring game

hdu 3336 Count the string

Count the stringTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4239    Accepted Submission(s): 1977 Problem Description It is well known that AekdyCoin is good at string problems as well as num