【UOJ #62】【UR #5】怎样跑得更快(莫比乌斯反演)

传送门

以下用 C , D C,D C,D表示文内的 c , d c,d c,d
显然可以替换 l c m lcm lcm后枚举 g c d gcd gcd得到
b i i D ≡ ∑ d ∣ i d C − D ∑ d ∣ j [ g c d ( i d , j d ) = 1 ] x j j D \frac{b_i}{i^D}\equiv\sum_{d|i}d^{C-D}\sum_{d|j}[gcd(\frac i d,\frac j d)=1]x_jj^D iDbididCDdj[gcd(di,dj)=1]xjjD
b i = b i i D , x j = x j j D b_i=\frac{b_i}{i^D},x_j=x_jj^D bi=iDbi,xj=xjjD
b i ≡ ∑ d ∣ i d C − D ∑ k ∣ i d μ ( k ) ∑ d k ∣ j n x j b_i\equiv \sum_{d|i}d^{C-D}\sum_{k|\frac i d}\mu(k)\sum_{dk|j}^nx_j bididCDkdiμ(k)dkjnxj
f ( i ) = ∑ d ∣ i d C − D μ ( i d ) f(i)=\sum_{d|i}d^{C-D}\mu(\frac i d) f(i)=didCDμ(di)
b i ≡ ∑ d ∣ i f ( d ) ∑ d ∣ j n x j b_i\equiv \sum_{d|i}f(d)\sum_{d|j}^nx_j bidif(d)djnxj
g ( d ) = ∑ d ∣ j n x j g(d)=\sum_{d|j}^nx_j g(d)=djnxj
b i ≡ ∑ d ∣ i f ( d ) g ( d ) b_i\equiv \sum_{d|i}f(d)g(d) bidif(d)g(d)
f ( i ) g ( i ) = b i − ∑ d ∣ i , d ≠ i f ( d ) g ( d ) f(i)g(i)=b_i-\sum_{d|i,d\not=i}f(d)g(d) f(i)g(i)=bidi,d=if(d)g(d)
求出 g g g后考虑
g ( i ) = ∑ i ∣ j n x j g(i)=\sum_{i|j}^nx_j g(i)=ijnxj
x i = g ( i ) − ∑ i ∣ j , i ≠ j x j x_i=g(i)-\sum_{i|j,i\not=j}x_j xi=g(i)ij,i=jxj
复杂度 O ( n l o g n ) O(nlogn) O(nlogn)

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
    static char ibuf[RLEN],*ib,*ob;
    (ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
    return (ib==ob)?EOF:*ib++;
}
inline int read(){
    char ch=gc();
    int res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
inline ll readll(){
    char ch=gc();
    ll res=0;bool f=1;
    while(!isdigit(ch))f^=ch=='-',ch=gc();
    while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
    return f?res:-res;
}
inline int readstring(char *s){
	int top=0;char ch=gc();
	while(isspace(ch))ch=gc();
	while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
	return top;
}
template<typename tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<typename tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int mod=998244353;
inline int add(int a,int b){return (a+=b)>=mod?(a-mod):a;}
inline int dec(int a,int b){a-=b;return a+(a>>31&mod);}
inline int mul(int a,int b){static ll r;r=1ll*a*b;return (r>=mod)?(r%mod):r;}
inline void Add(int &a,int b){(a+=b)>=mod?(a-=mod):0;}
inline void Dec(int &a,int b){a-=b,a+=a>>31&mod;}
inline void Mul(int &a,int b){static ll r;r=1ll*a*b;a=(r>=mod)?(r%mod):r;}
inline int ksm(int a,int b,int res=1){if(a==0&&b==0)return 0;for(;b;b>>=1,Mul(a,a))(b&1)&&(Mul(res,a),1);return res;}
inline int Inv(int x){return ksm(x,mod-2);}
inline int fix(int x){return (x<0)?x+mod:x;}
int n,q,c,d;
cs int N=100005;
int vis[N],pr[N],mu[N],tot,f[N],pc[N],pd[N];
inline void init_sieve(cs int n=N-5){
	mu[1]=1;
	for(int i=2;i<=n;i++){
		if(!vis[i])pr[++tot]=i,mu[i]=mod-1;
		for(int j=1;j<=tot&&i*pr[j]<=n;j++){
			vis[i*pr[j]]=1;
			if(i%pr[j]==0)break;
			mu[i*pr[j]]=mod-mu[i];
		}
	}
	for(int i=1;i<=n;i++)pc[i]=ksm(i,c),pd[i]=ksm(i,d);
	for(int i=1;i<=n;i++){
		int tp=mul(pc[i],Inv(pd[i]));
		for(int j=1;i*j<=n;j++)
		Add(f[i*j],mul(tp,mu[j]));
	}
}
int b[N],g[N],x[N];
int main(){
	#ifdef Stargazer
	freopen("lx.in","r",stdin);
	#endif
	n=read(),c=read(),d=read(),q=read();
	init_sieve();
	while(q--){int fg=1;
		for(int i=1;i<=n;i++)b[i]=mul(read(),Inv(pd[i]));
		for(int i=1;i<=n;i++){
			if(!f[i]&&b[i]){fg=0;break;}
			g[i]=mul(Inv(f[i]),b[i]);
			for(int j=2;i*j<=n;j++)
			Dec(b[i*j],b[i]);
		}
		if(!fg){puts("-1");continue;}
		for(int i=n;i;i--){
			x[i]=g[i];
			for(int j=i+i;j<=n;j+=i)
			Dec(x[i],x[j]);
		}
		for(int i=1;i<=n;i++)cout<<mul(x[i],Inv(pd[i]))<<" ";puts("");
	}return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值