BZOJ 2219 数论之神

Link

Solution

xx的个数 P=i=1cpikiP=\sum\limits_{i=1}^{c}p_i^{k^i},根据 转化为cc 的解的积

Case #1 bpkb|p^k

x=λpt,gcd(λ,p)==1x=\lambda p^{t},\gcd(\lambda,p)==1 可以知道atkat\ge k tkat\ge \lceil \dfrac ka\rceil 所以解数为pkkap^{k-\lceil \frac ka\rceil}

Case #2

b=λpt,gcd(λ,p)==1b=\lambda p^{t},\gcd(\lambda,p)==1

如果 ,假设存在解x0x_0pp的次数为ss (ps)a=psa=pt(p^s)^a=p^{sa}=p^t,与 矛盾,所以必定无解 当ata|t时,方程可以化为 x=xpta,b=λ,p=pktx'=\dfrac x{p^{\frac ta}},b'=\lambda,p'=p^{k-t}x[0,pkta)x'\in [0,p^{k-\frac ta}) 但是在新的方程中,x[0,pkt)x'\in[0,p^{k-t}),所以对新方程求出答案之后需要×ptta\times p^{t-\frac ta}

对这个方程取 现在需要把指标项代换,需要保证 xx一一对应,取pp'的原根作为底数即可,而pp'一定存在原根。 令 现在转化为求方程 的解数

对于 的解数 Ax+Cy=gcd(A,C)Ax+Cy=\gcd(A,C) 还原到原方程的解就是X=Bgcd(A,C)xX=\dfrac B{\gcd(A,C)}x,设第一个非负XXX0X_0X=X0+kCgcd(A,C)X=X_0+\dfrac {kC}{\gcd(A,C)} 所以在 意义下有gcd(A,C)\gcd(A,C)个解 这么显然的东西我居然还想了这么久 就好了

Code

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
174
175
176
177
178
179
180
181
182
183
184
//Code by Lucida
#include<bits/stdc++.h>
#define get(x) scanf("%d",&x)
#define put(x) printf("%d",x)
typedef long long ll;
template <class T> inline bool chkmx(T &a,const T &b){return a<b?a=b,1:0;}
template <class T> inline bool chkmn(T &a,const T &b){return a>b?a=b,1:0;}

using std::pair;
using std::make_pair;

const int SQRT=1e6;

struct Hash
{
static const int SIZE=SQRT,P=99929;
struct Node
{
int key,val;
Node *pre;
Node(int key,int val,Node *pre):key(key),val(val),pre(pre){}
}*POOL,*ME,*id[P];
Hash()
{
POOL=ME=(Node*)malloc(SIZE*sizeof(Node));
memset(id,0,sizeof(id));
}
void Reset()
{
memset(id,0,sizeof(id));
ME=POOL;
}
Node *FindKey(int key)
{
for(Node *p=id[key%P];p;p=p->pre)
if(p->key==key) return p;
return 0;
}
int &operator [](int x)
{
Node *re=FindKey(x);
if(re)
return re->val;
else
{
int ha=x%P;
return (id[ha]=new(ME++) Node(x,-1,id[ha]))->val;
}
}
int Find(int x)
{
Node *re=FindKey(x);
return re?re->val:-1;
}
};
int pow(int base,int index)
{
int res;
for(res=1;index;index>>=1,base*=base)
if(index&1) res*=base;
return res;
}
int pow(int base,int index,int P)
{
int res;
for(res=1;index;index>>=1,base=(ll)base*base%P)
if(index&1) res=(ll)res*base%P;
return res;
}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
int Div(int x,pair<int,int> *p)
{
int m=(int)sqrt(x+0.5)+1,c=0;
for(int i=2;i<=m;++i)
if(x%i==0)
{
int t=0;
do ++t,x/=i;
while(x%i==0);
p[++c]=make_pair(i,t);
}
if(x!=1)
p[++c]=make_pair(x,1);
return c;
}
int BSGS(int a,int b,int P)//a^x equiv b mod P
{
static Hash S;
int modu=P;
a%=P,b%=P;
ll t=1;int dep=0;
for(int d=gcd(a,P);d!=1;d=gcd(a,P))
{
if(b%d) return -1;
++dep;
b/=d,P/=d,(t*=a/d)%=modu;
}
S.Reset();
int m=(int)sqrt(P+0.5)+1;
ll baj=b;
for(int j=0;j<=m;++j)
{
S[baj]=j;
(baj*=a)%=modu;
}
int asp=pow(a,m,modu);
ll aspi=asp;
for(int i=1,j;i<=m;++i)
{
if((j=S.Find(aspi))!=-1)
return i*m-j+dep;
(aspi*=asp)%=modu;
}
return -1;
}
int phi(int x)
{
static pair<int,int> sub[SQRT];
int dc=Div(x,sub);
int res=x;
for(int i=1;i<=dc;++i)
(res/=sub[i].first)*=(sub[i].first-1);
return res;
}
int ind(int a,int b,int P){ return BSGS(a,b,P);}
int prim_root(int P)
{
static pair<int,int> sub[SQRT];
int ph=phi(P),dc=Div(ph,sub);
for(int g=2;g<P;++g)
{
bool check=1;
for(int i=1;i<=dc && check;++i)
if(pow(g,ph/sub[i].first,P)==1)
check=0;
if(check)
return g;
}
return -1;
}
int Calc(int a,int b,int p,int k)
{
int pk=pow(p,k);
//a%=pk????
b%=pk;
if(b)
{
int b1=b,t=0;
while(b1%p==0) ++t,b1/=p;
if(t%a!=0) return 0;
int p1=pow(p,k-t),
ph=phi(p1),
ind_b=ind(prim_root(p1),b1,p1);
int d=gcd(a,ph);
return ind_b%d?0:d*pow(p,t-t/a);
}
else
return pow(p,k-(k/a+(k%a?1:0)));
}
void Work()
{
static pair<int,int> sub[SQRT];
int A,B,K,P;
get(A),get(B);get(K);P=K*2+1;
int Ans=1,dc=Div(P,sub);
for(int i=1;i<=dc;++i)
{
int res=Calc(A,B,sub[i].first,sub[i].second);
Ans*=res;
//put(res),putchar('\n');
}
put(Ans),putchar('\n');
}
int main()
{
//freopen("input","r",stdin);
int T;get(T);
while(T--)
Work();
return 0;
}
/* AC Record(Bugs)

*/