题面
题解
既然题目诚心诚意地给出了式子,那我们就老老实实地推式子吧:
(
a
+
c
)
(
a
−
c
)
+
b
(
2
a
+
b
)
a
c
+
b
c
−
(
c
+
a
+
b
)
(
a
+
b
)
+
c
2
c
2
=
a
2
−
c
2
+
b
2
+
2
a
b
(
a
+
b
)
c
−
(
a
+
b
)
2
+
c
(
a
+
b
)
+
c
2
c
2
=
(
a
+
b
)
2
−
c
2
(
a
+
b
)
c
−
(
a
+
b
)
2
+
c
(
a
+
b
)
+
c
2
c
2
\frac{(a+c)(a-c)+b(2a+b)}{ac+bc}-\frac{(c+a+b)(a+b)+c^2}{c^2}\\ =\frac{a^2-c^2+b^2+2ab}{(a+b)c}-\frac{(a+b)^2+c(a+b)+c^2}{c^2}\\ =\frac{(a+b)^2-c^2}{(a+b)c}-\frac{(a+b)^2+c(a+b)+c^2}{c^2}
ac+bc(a+c)(a−c)+b(2a+b)−c2(c+a+b)(a+b)+c2=(a+b)ca2−c2+b2+2ab−c2(a+b)2+c(a+b)+c2=(a+b)c(a+b)2−c2−c2(a+b)2+c(a+b)+c2
我们令
A
=
a
+
b
A=a+b
A=a+b ,那么原式等于
A
2
−
c
2
A
c
−
A
2
+
A
c
c
2
−
1
=
A
c
−
c
A
−
A
2
c
2
−
A
c
−
1
=
−
(
A
c
)
−
1
−
(
A
c
)
2
−
1
\frac{A^2-c^2}{Ac}-\frac{A^2+Ac}{c^2}-1\\ =\frac{A}{c}-\frac{c}{A}-\frac{A^2}{c^2}-\frac{A}{c}-1=-(\frac{A}{c})^{-1}-(\frac{A}{c})^2-1
AcA2−c2−c2A2+Ac−1=cA−Ac−c2A2−cA−1=−(cA)−1−(cA)2−1
于是,这是个只跟 a + b c \frac{a+b}{c} ca+b 有关的式子,我们可以先用 N T T NTT NTT 求出 a + b = i a+b=i a+b=i 的方案数,然后再求出 p p p 的原根,利用原根将 a + b a+b a+b 和 c − 1 c^{-1} c−1 对数化,把乘法卷积转化为加法卷积。最后挨个统计 a + b c = i \frac{a+b}{c}=i ca+b=i 时计算结果是否在 T T T 中出现。这题就完了。
总时间复杂度 O ( n log n ) O(n\log n) O(nlogn) 。
CODE
#include<map>
#include<set>
#include<cmath>
#include<ctime>
#include<queue>
#include<stack>
#include<random>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
#define MAXN 200005
#define LL long long
#define ULL unsigned long long
#define ENDL putchar('\n')
#define DB double
#define lowbit(x) (-(x) & (x))
#define FI first
#define SE second
int xchar() {
static const int maxn = 1000000;
static char b[maxn];
static int pos = 0,len = 0;
if(pos == len) pos = 0,len = fread(b,1,maxn,stdin);
if(pos == len) return -1;
return b[pos ++];
}
//#define getchar() xchar()
LL read() {
LL f = 1,x = 0;int s = getchar();
while(s < '0' || s > '9') {if(s<0)return -1;if(s=='-')f=-f;s = getchar();}
while(s >= '0' && s <= '9') {x = (x<<1) + (x<<3) + (s^48);s = getchar();}
return f*x;
}
void putpos(LL x) {if(!x)return ;putpos(x/10);putchar((x%10)^48);}
void putnum(LL x) {
if(!x) {putchar('0');return ;}
if(x<0) putchar('-'),x = -x;
return putpos(x);
}
void AIput(LL x,int c) {putnum(x);putchar(c);}
const int MOD = 998244353;
int n,m,s,o,k;
int qkpow(int a,int b,int MOD) {
int res = 1;
while(b > 0) {
if(b & 1) res = res *1ll* a % MOD;
a = a *1ll* a % MOD; b >>= 1;
}return res;
}
int om,xm[MAXN<<2],rev[MAXN<<2];
void NTT(int *s,int n,int op) {
for(int i = 1;i < n;i ++) {
rev[i] = (rev[i>>1]>>1) | ((i&1) ? (n>>1):0);
if(rev[i] < i) swap(s[i],s[rev[i]]);
}
om = qkpow(3,(MOD-1)/n,MOD); xm[0] = 1;
if(op < 0) om = qkpow(om,MOD-2,MOD);
for(int i = 1;i <= n;i ++) xm[i] = xm[i-1] *1ll* om % MOD;
for(int k = 2,t = (n>>1);k <= n;k <<= 1,t >>= 1) {
for(int j = 0;j < n;j += k) {
for(int i = j,l = 0;i < j+(k>>1);i ++,l += t) {
int A = s[i],B = s[i+(k>>1)];
s[i] = (xm[l]*1ll*B + A) % MOD;
s[i+(k>>1)] = (A +MOD- xm[l]*1ll*B%MOD) % MOD;
}
}
}
if(op < 0) {
int iv = qkpow(n,MOD-2,MOD);
for(int i = 0;i < n;i ++) s[i] = s[i] *1ll* iv % MOD;
}return ;
}
int P = 1,prt;
int inv[MAXN];
int a[MAXN],b[MAXN],lo[MAXN],po[MAXN];
int A0[MAXN<<2],A[MAXN<<2],B[MAXN<<2];
int ct[MAXN];
bool f2[MAXN];
int getprimeroot(int P) {
int nn = P-1;
static int p[105],cnt; cnt = 0;
for(int i = 2;i*i <= nn;i ++) {
if(nn % i == 0) {
p[++ cnt] = i;
while(nn % i == 0) nn /= i;
}
}
if(nn > 1) p[++ cnt] = nn;
for(int i = 1;i <= P;i ++) {
bool flag = 1;
for(int j = 1;j <= cnt;j ++) {
if(qkpow(i,(P-1)/p[j],P) == 1) {flag = 0;break;}
}
if(flag) return i;
}return 0;
}
int main() {
freopen("equation.in","r",stdin);
freopen("equation.out","w",stdout);
P = read(); prt = getprimeroot(P);
inv[0] = inv[1] = 1;
for(int i = 2;i < P;i ++) inv[i] = (P-inv[P%i]) *1ll* (P/i) % P;
po[0] = 1;
for(int i = 0;i < P-1;i ++) {
po[i+1] = po[i] *1ll* prt % P;
lo[po[i]] = i;
}
n = read(); m = read();
for(int i = 1;i <= n;i ++) {
a[i] = read(); A0[a[i]] ++;
if(a[i]) B[lo[inv[a[i]]]] ++;
}
int le = 1; while(le <= (P-1)*2) le <<= 1;
NTT(A0,le,1);
for(int i = 0;i < le;i ++) A0[i] = A0[i] *1ll* A0[i] % MOD;
NTT(A0,le,-1);
for(int i = 0;i < le;i ++) {
if(i%P) {
(A[lo[i%P]] += A0[i]) %= MOD;
}
}
NTT(A,le,1); NTT(B,le,1);
for(int i = 0;i < le;i ++) A[i] = A[i] *1ll* B[i] % MOD;
NTT(A,le,-1);
for(int i = 0;i < le;i ++) {
(ct[po[i%(P-1)]] += A[i]) %= MOD;
}
for(int i = 1;i <= m;i ++) {
b[i] = read(); f2[b[i]] = 1;
}
int ans = 0;
for(int i = 1;i < P;i ++) {
int rs = (0ll+P-1 - inv[i] +P- i*1ll*i%P) % P;
if(f2[rs]) {
(ans += ct[i]) %= MOD;
}
}
AIput(ans,'\n');
return 0;
}