HDU 2685 I won't tell you this is about number theory (數論 公式 快速冪取模)
I won't tell you this is about number theory
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 376 Accepted Submission(s): 103
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.
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).
Each case contain four integers a,m,n,k(1<=a,m,n,k<=10000).
One line with a integer S.
1
1 1 1 1
0
Source
題目大意:求gcd(a^m-1,a^n-1)%k
題目分析:
更一般的公式:
gcd(A, B) = 1 => gcd(A^m - B^m,A^n - B^n)= A^gcd(m,n) - B^gcd(m,n)
#include <cstdio>
int a, m, n, MOD;
int gcd(int a, int b)
{
return b ? gcd(b, a % b) : a;
}
int qpow(int x, int n)
{
int res = 1;
while(n)
{
if(n & 1)
res = (res * x) % MOD;
x = (x * x) % MOD;
n >>= 1;
}
return res;
}
int main()
{
int T;
scanf("%d", &T);
while(T --)
{
scanf("%d %d %d %d", &a, &m, &n, &MOD);
printf("%d\n", (MOD + qpow(a, gcd(n, m)) % MOD - 1 % MOD) % MOD);
}
}
相關文章
- HDU 1575 Tr A【矩陣快速冪取模】矩陣
- HDU 4686 (推公式+矩陣快速冪)公式矩陣
- HDU 1005 Number Sequence(矩陣快速冪)矩陣
- HDU 1005 Number Sequence:矩陣快速冪矩陣
- FZU1759Super A^B mod C(快速冪取模) 公式公式
- Raising Modulo (快速冪取模)AI
- HDU 3221Brute-force Algorithm(降冪公式 神似hdu4549)Go公式
- HDU 1575 Tr A(矩陣快速冪)矩陣
- HDU 4565 So Easy!(矩陣快速冪)矩陣
- 學習筆記----快速冪取模演算法筆記演算法
- HDU 4965 Fast Matrix Calculation(矩陣快速冪)AST矩陣
- git中提示 please tell me who you areGit
- HDU3221Brute-force Algorithm(矩陣快速冪&&指數降冪)Go矩陣
- HDU 2157 How many ways??:矩陣快速冪【i到j共經過k個節點的方法數】矩陣
- 100 Things You Probably Didn't Know About Oracle DatabaseOracleDatabase
- HDU 4279 2012網路賽Number(數論 尤拉函式結論約數個數)函式
- HDU 2157 How many ways?? (矩陣快速冪)矩陣
- HDU 2256Problem of Precision(矩陣快速冪)矩陣
- HDU5411CRB and Puzzle(矩陣快速冪)矩陣
- 圖論(Graph Theory)圖論Graph Theory
- i tell you 微軟各種 作業系統 應用程式 開發工具 下載微軟作業系統
- HDU 4549 M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- HDU 2276 - Kiki & Little Kiki 2 (矩陣快速冪)矩陣
- HDU 4291 A Short problem(矩陣快速冪+迴圈節)矩陣
- HDU 4549M斐波那契數列(矩陣快速冪+費馬小定理)矩陣
- How to Tell if the I/O of the Database is Slow - 1Database
- I can't truly give chinese sex pills you a conclusive remedyREM
- 2008 5 19: I can't make exception for youException
- What you should know about JavaJava
- 【Codeforces Round 362 (Div 2)E】【公式推導+快速冪+費馬小定理】PLEASE a[i]=(1-a[i-1])除2下n次項 n為連乘數公式
- 計蒜客模擬賽D2T1 蒜頭君的兔子:矩陣快速冪矩陣
- HDU1792(公式)公式
- 快速冪
- 尤拉計劃717:取模公式之和公式
- 冪的一個公式(一)公式
- 冪的一個公式(二)公式
- POJ 3233-Matrix Power Series( S = A + A^2 + A^3 + … + A^k 矩陣快速冪取模)矩陣
- hdu 1792 A New Change Problem(數論)