hdu 2685 I won'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;
    }
    return ret;
}

LL PowerMod(LL a, LL b, LL c)
{
    
    LL ans = 1;
    LL k = a % c;
    while(b>0)    //(k*k % c)2^b %c
    {
        if(b&1)
            ans = mult_mod(ans,k,c);
        b = b>>1;
        k = mult_mod(k,k,c);
    }
    return ans;
}

LL gcd(LL a,LL b)
{    
    if(a==0){return b;}
    return gcd(b%a,a);
}
int main()
{
    int T;
    scanf("%d",&T);
    LL a,m,n,k,G;
    while(T--)
    {
        scanf("%I64d%I64d%I64d%I64d",&a,&m,&n,&k);
        if(m>n)
        {
            G = gcd(n,m);
        }
        else
        {
            G = gcd(m,n);
        }
        printf("%I64d\n",(PowerMod(a,G,k)+k-1%k)%k);
    }
}

/*
    gcd(a^m - 1,a^n - 1) = a^gcd(m,n) - 1

hint:
    计算减法的时候 为了保证结果是正数。
    a-b % k   ->  (a%k + k - b%k)%k

*/

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

时间: 2024-11-10 17:25:32

hdu 2685 I won't tell you this is about number theory的相关文章

2018HDU多校训练-3-Problem D. Euler Function

链接:http://acm.hdu.edu.cn/showproblem.php?pid=6322 Problem Description In number theory, Euler's totient function φ(n) counts the positive integers up to a given integer n that are relatively prime to n . It can be defined more formally as the number

hdu 1394 Minimum Inversion Number 逆序数/树状数组

Minimum Inversion Number Time Limit: 1 Sec  Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1394 Description The inversion number of a given number sequence a1, a2, ..., an is the number of pairs (ai, aj) that satisfy i < j and ai

HDU 5182

#include <iostream> #include <algorithm> #include <cstring> using namespace std; /************************************************************************/ /* HDU 5182题 http://acm.hdu.edu.cn/showproblem.php?pid=5182*/ /******************

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

POJ 3340 &amp; HDU 2410 Barbara Bennett&#39;s Wild Numbers(数学)

题目链接: PKU:http://poj.org/problem?id=3340 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2410 Description A wild number is a string containing digits and question marks (like 36?1?8). A number X matches a wild number W if they have the same length, and

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