-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdu_3221_Brute-force Algorithm.cpp
173 lines (154 loc) · 2.98 KB
/
hdu_3221_Brute-force Algorithm.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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<cmath>
using namespace std;
#define eps 1e-8
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define N 1000010
ll p,phi;
int pri[N];
bool vis[N];
int tot;
struct Matrix{
ll m[2][2];
void init()
{
m[0][0] = 1;
m[1][0] = 1;
m[0][1] = 1;
m[1][1] = 0;
}
void E()
{
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
m[i][j] = (i == j);
}
};
void init()
{
int i,j; tot = 0;
memset(vis,0,sizeof(vis));
for(i=2; i<N; i++)
{
if(vis[i]) continue;
pri[tot++] = i;
for(j=2*i; j<N; j+=i)
vis[j]=true;
}
}
ll eular(ll p)
{
int i;
ll t = p;
for(i=0; pri[i] * pri[i] <= t & i < tot; i++)
{
if(t % pri[i] == 0)
{
p = p - p / pri[i];
while(t % pri[i] == 0) t /= pri[i];
}
}
if(t > 1) p = p - p / t;
return p;
/*
ll i,t = p;
for(i = 2; i * i <= t; i ++)
{
if(t % i == 0)
{
while(t % i == 0) t /= i;
p = p - p / i;
}
}
if(t > 1) p = p - p / t;
return p;
*/
}
void MatMul(Matrix &mat0, Matrix mat1, ll mod)
{
Matrix mat2;
for(int i=0; i<2; i++)
for(int j=0; j<2; j++)
{
mat2.m[i][j] = 0;
for(int k=0; k<2; k++)
{
mat2.m[i][j] += mat0.m[i][k] * mat1.m[k][j];
if(mat2.m[i][j] >= mod)
mat2.m[i][j] = mat2.m[i][j] % mod + mod;
}
}
mat0 = mat2;
}
Matrix MatPow(Matrix mat0, ll p, ll mod)
{
Matrix mat,mat1 = mat0;
mat.E();
while(p)
{
if(p & 1) MatMul(mat,mat1,mod);
MatMul(mat1,mat1,mod);
p >>= 1;
}
return mat;
}
ll qpow(ll a, ll b, ll mod)
{
ll res = 1;
//cout<<a<<"^"<<b<<" % "<<mod<<" : ";
while(b)
{
if(b & 1) res = res * a % mod;
a = a * a % mod;
b >>= 1;
}
//res = (res % mod + mod) % mod;
//cout<<res<<endl;
return res;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out-2.txt","w",stdout);
#endif
int T,i,j,cas;
ll a,b,n,p;
init();
scanf("%d",&T);
for(cas = 1; cas <= T; cas ++)
{
cin>>a>>b>>p>>n;
cout<<"Case #"<<cas<<": ";
if(n == 1)
{
cout<<a%p<<endl;
continue;
}
if(n == 2)
{
cout<<b%p<<endl;
continue;
}
phi = eular(p);
//cout<<"phi = "<<phi<<endl;
Matrix mat;
mat.init();
mat = MatPow(mat,n-2,phi);
ll pa = mat.m[0][1], pb = mat.m[0][0];
//cout<<"pa = "<<pa<<"\tpb = "<<pb<<endl;
a = qpow(a,pa,p);
b = qpow(b,pb,p);
cout<<(a * b) % p<<endl;
}
return 0;
}