-
Notifications
You must be signed in to change notification settings - Fork 0
/
POWERMUL(precalc).cpp
130 lines (122 loc) · 2.33 KB
/
POWERMUL(precalc).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
#include <bits/stdc++.h>
#define M 100005
#define L 100005
#define LIM 320
#define sz 66
#define MOD 1000000007
using namespace std;
typedef long long ll;
typedef pair<int,int> ii;
vector<int> primes;
struct fact
{
ll power[sz];
ll extra;
fact()
{
for(int i = 0 ; i<sz ; i++)
power[i] = 0;
extra = 0;
}
};
fact f[M];
int mark[L];
int pos[L];
ll pow(int base, ll exponent, int modulus)
{
int result = 1;
base = base % modulus;
while (exponent > 0)
{
if (exponent%2 == 1)
result = (ll)(result * base)% modulus;
exponent = exponent >> 1;
base = (ll)(base * base) % modulus;
}
return result;
}
void markmultiples(int n , int k)
{
for(int i = k ; i<n ; i+=k)
mark[i] = k;
}
void sieve(int n)
{
memset(mark , false , sizeof(mark));
for(int i = 2 ; i<n ; i++)
{
if(!mark[i])
{
primes.push_back(i);
markmultiples(n , i);
}
}
}
void factor(int n)
{
int keep = n;
while(n>1)
{
if(mark[n] > LIM)
f[keep].extra += keep*mark[n];
else
f[keep].power[pos[mark[n]]] += keep;
n /= mark[n];
}
}
int main()
{
freopen("E://pp.txt" , "r" , stdin);
sieve(L);
int prime[] = {2 , 3 , 5 , 7 , 11 , 13 , 17 , 19 , 23 , 29 , 31 , 37 , 41 , 43 , 47 , 53 , 59 , 61 , 67 , 71 , 73 , 79 , 83 , 89 , 97 , 101 , 103 , 107 , 109 , 113 , 127 , 131 , 137 , 139 , 149 , 151 , 157 , 163 , 167 , 173 , 179 , 181 , 191 , 193 , 197 , 199 , 211 , 223 , 227 , 229 , 233 , 239 , 241 , 251 , 257 , 263 , 269 , 271 , 277 , 281 , 283 , 293 , 307 , 311 , 313 , 317};
for(int i = 0 ; i<sz ; i++)
{
pos[prime[i]] = i;
}
for(int i = 2 ; i<M ; i++)
{
factor(i);
}
for(int i = 3 ; i<M ; i++)
{
for(int j = 0 ; j<sz ; j++)
{
f[i].power[j] += f[i-1].power[j];
}
}
int Cases;
scanf("%d",&Cases);
int N , r , mod , q;
ll ans;
while(Cases--)
{
scanf("%d %d %d",&N , &mod , &q);
if(mod == 1)
{
while(q--)
{
scanf("%d" ,&r);
printf("0\n");
}
}
else
{
while(q--)
{
scanf("%d" ,&r);
ans = 1LL;
for(int i = 0 ; i<sz ; i++)
{
ans = (ans * pow(prime[i] , f[N].power[i] - f[r].power[i] - f[N-r].power[i] , mod)) %mod;
}
cout<<ans<<endl;
for(int i = sz ; primes[i] < N ; i++)
{
ans = (ans * pow(prime[i] , prime[i] , mod)) %mod;
cout<<i<<" "<<primes[i]<<" "<<ans<<endl;
}
printf("%lld\n" , ans);
}
}
}
}