Basic Gcd Problem
时间限制: 2 Sec 内存限制: 256 MB
题目描述
As a great ACMer, Z Y B ZYB ZYB is also good at math and number theory.
Z
Y
B
ZYB
ZYB constructs a function
f
c
(
x
)
f_c(x)
fc(x) such that:
f
c
(
x
)
=
max
i
=
1...
x
−
1
c
⋅
f
c
(
g
c
d
(
i
,
x
)
)
x
>
1
f
c
(
x
)
=
1
x
>
1
f_c(x)=\max_{i=1...x-1} c \cdot f_c(gcd(i,x)) \quad x>1\\ f_c(x)=1 \quad\quad\quad\quad\quad\quad\quad\quad\quad\quad x>1
fc(x)=i=1...x−1maxc⋅fc(gcd(i,x))x>1fc(x)=1x>1
Give some positive integer pairs
(
n
i
,
c
i
)
(n_i, c_i)
(ni,ci), ZYB wants to know
f
c
i
(
n
i
)
f_{c_i}(n_i)
fci(ni) mod (
1
0
9
+
7
10^9 +7
109+7).
输入
The input contains multiple test cases. The first line of input contains one integer
T
(
1
≤
T
≤
1
0
6
)
T(1≤T≤10^6)
T(1≤T≤106).
In the following
T
T
T lines, each line contains two integers
n
i
,
c
i
(
1
≤
n
i
,
c
i
≤
1
0
6
)
n_i, c_i(1≤n_i,c_i≤10^6)
ni,ci(1≤ni,ci≤106) describing one question.
输出
For each test case, output one integer indicating the answer.
样例输入
2
3 3
10 5
样例输出
3
25
思路
观察这个函数可得
n
=
p
1
a
1
p
2
a
2
p
3
a
3
.
.
.
p
k
a
k
m
=
∑
i
=
1
k
a
i
a
n
s
=
c
m
n=p_1^{a_1}p_2^{a_2}p_3^{a_3}...p_k^{a_k}\\ m=\sum_{i=1}^{k} a_{i}\\ ans=c^{m}
n=p1a1p2a2p3a3...pkakm=i=1∑kaians=cm
#include <iostream>
#include <cstdio>
#include <cstring>
#include <set>
using namespace std;
typedef long long ll;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;
const int maxn = 1e6+10;
int prime[maxn + 10] , vis[maxn + 10],tot;
inline void prework() {
for (int i = 2; i <= maxn; i++) {
if (vis[i] == 0) {
vis[i] = i;
prime[tot++] = i;
}
for (int j = 0; j < tot && 1ll * prime[j] * i <= maxn; j++) {
vis[i * prime[j]] = prime[j];
if (i % prime[j] == 0) break;
}
}
}
int qpow(int a,int b) {
int res = 1;
while (b) {
if (b & 1) res = 1ll * res * a % mod;
b >>= 1;
a = 1ll * a * a % mod;
}
return res;
}
int n,c;
inline void work() {
int k = 0;
scanf("%d%d", &n, &c);
while (n > 1) {
k++;
n /= vis[n];
}
printf("%d\n", qpow(c, k));
}
int main() {
prework();
int T = 1;
scanf("%d", &T);
while (T--) {
work();
}
return 0;
}