HDU2837 Calculation(扩展欧拉定理)

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 3121    Accepted Submission(s): 778

Problem Description

Assume that f(0) = 1 and 0^0=1. f(n) = (n%10)^f(n/10) for all n bigger than zero. Please calculate f(n)%m. (2 ≤ n , m ≤ 10^9, x^y means the y th power of x).

Input

The first line contains a single positive integer T. which is the number of test cases. T lines follows.Each case consists of one line containing two positive integers n and m.

Output

One integer indicating the value of f(n)%m.

Sample Input

2 24 20 25 20

Sample Output

16 5

Source

2009 Multi-University Training Contest 3 - Host by WHU

Recommend

gaojie   |   We have carefully selected several similar problems for you:  2841 2839 2838 2840 2836

$a^x \equiv a^{x \ \% \ phi(m) + phi(m)} \pmod m$

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define int long long
using namespace std;
const int MAXN = 1e5 + 10, INF = 1e9 + 10;
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M, PhiM;
int fastpow(int a, int p, int mod) {
if(a == 0) return p == 0;
int base = 1;
while(p) {
if(p & 1) base = (base * a) % mod;
p >>= 1; a = (a * a) % mod;
}
return base == 0 ? mod : (base + mod)% mod;
}
int GetPhi(int x) {
int limit = sqrt(x), ans = x;
for(int i = 2; i <= limit; i++) {
if(!(x % i)) ans = ans / i * (i - 1) ;
while(!(x % i)) x /= i;
}
if(x > 1) ans = ans / x * (x - 1);
return ans;
}
int F(int N, int mod) {
if(N < 10) return N;
return fastpow((N % 10), F(N / 10, GetPhi(mod)), mod);
}
main() {
while(QwQ--) {
printf("%I64d\n", F(N, M));
}
return 0;
}
/*
4
24 20
37 25
123456 321654
123456789 456789321
*/

0 条评论

相关文章

POJ 2773 Happy 2006（容斥原理+二分）

Happy 2006 Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 1...

30870

HDUOJ--1159Common Subsequence

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536...

27470

hdu----(5055)Bob and math problem(贪心)

Bob and math problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 327...

29550

2014---多校训练2（ZCC Loves Codefires）

ZCC Loves Codefires Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 3276...

29050

2017 Multi-University Training Contest - Team 9 1004&&HDU 6164 Dying Light【数学+模拟】

Dying Light Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/13107...

28550

HDUOJ -----1686

Oulipo Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Ja...

33370

hdu----（5045）Contest（数位dp）

Contest Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (J...

36450

hduoj----(1033)Edge

Edge                         Time Limit: 2000/1000 MS (Java/Others)    Memory Li...

31360

Key Task Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ...

36540

hdu------(4300)Clairewd’s message(kmp)

Clairewd’s message Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768...

30960