forked from hackerkid/LightOJ-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
1054-Efficient-Pseudo-Code.cpp
115 lines (86 loc) · 1.71 KB
/
1054-Efficient-Pseudo-Code.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#include <iostream>
#include <stdio.h>
#include <vector>
#define sz 1000000
using namespace std;
vector <int> pr;
int sv[1000005];
long long MOD = 1000000007;
long long mod = 1000000007;
void extEuclid(int a, int b, int &x, int &y, int &gcd) {
x = 0; y = 1; gcd = b;
int m, n, q, r;
for (int u=1, v=0; a != 0; gcd=a, a=r) {
q = gcd / a; r = gcd % a;
m = x-u*q; n = y-v*q;
x=u; y=v; u=m; v=n;
}
}
long long modInv(long long n, long long m) {
int x, y, gcd;
extEuclid(n, m, x, y, gcd);
if (gcd == 1) return x % m;
return 0;
}
long long mod_pow(long long x, long long y)
{
long long ans;
ans = 1;
while(y) {
if(y & 1) {
ans = (ans * x) % MOD;
}
x = (x * x) % MOD;
y = y >> 1;
}
return ans;
}
int main()
{
pr.push_back(2);
for (int i = 3; i * i <= sz; i += 2) {
if(!sv[i]) {
for (int j = i * i; j <= sz; j += 2 * i) {
sv[j] = 1;
}
}
}
for (int i = 3; i <= sz; i += 2) {
if(!sv[i]) {
pr.push_back(i);
}
}
int t;
int n;
int m;
int k;
long long ans;
scanf("%d", &t);
for (int cs = 1; cs <= t; cs++) {
scanf("%d %d", &n, &m);
vector < pair < int, int> > fact;
for (int i = 0; i < pr.size() and pr[i] * pr[i] <= n ; i++) {
k = 0;
while(n % pr[i] == 0) {
k++;
n = n / pr[i];
}
if(k != 0) {
fact.push_back(make_pair(pr[i], k));
}
}
if(n != 1) {
fact.push_back(make_pair(n, 1));
}
ans = 1;
long long p;
long long e;
for (int i = 0; i < fact.size(); i++) {
p = fact[i].first;
e = fact[i].second;
ans = (ans * (mod_pow(p, e * m + 1LL) - 1LL + mod)) % mod;
ans = (ans * mod_pow(p - 1LL, mod - 2)) % mod;
}
printf("Case %d: %lld\n", cs , ans);
}
}