题解 LGP7489【「Stoi2031」手写的从前】

problem

\(f_0(S)=\dfrac{\sigma(S)}{\pi(S)}\)\(f_k(S)=\sum\limits_{T \subseteq S}f_{k-1}(T)\)

其中 \(\sigma(S)=\sum\limits_{x \in S}x\)\(S\) 中所有元素之和, \(\pi(S)=\prod\limits_{x \in S}x\)\(S\) 中所有元素之积。

给定 \(n,k,p\) 和集合 \(S\),求 \(f_k(S) \bmod{p}\) 的值。

对于 \(100\%\) 的数据,\(1 \le n \le 7 \times 10^6,1 \le k \le 10^{18},1 \le x_i<p,1<p<2^{31},p\) 是质数,\(x_i\) 互不相同。

solution

\[\begin{aligned} f_1(S)=\sum_{T\subseteq S}f_0(T)&=\sum_{T\subseteq S}\dfrac{\sum_{x\in T} x}{\prod_{x\in T}x}\\ &=\sum_{T\subseteq S}\sum_{x\in T} x\prod_{x\in T}\frac{1}{x}\\ &=\sum_{T\subseteq S}\sum_{x\in T}\prod_{y\in T,x\neq y}\frac{1}{y}\\ &=\sum_{x\in S}\sum_{T\subseteq S,x\in T}\prod_{y\in T,x\neq y}\frac{1}{y}\\ &=\sum_{x\in S}\sum_{T\subseteq S\backslash\{x\}}\prod_{y\in T}\frac{1}{y}\\ &=\sum_{x\in S}\prod_{y\in S,x\neq y}\left(1+\frac{1}{y}\right)\\ &=\sum_{x\in S}\frac{z}{1+\frac{1}{x}},\text{where }z=\prod_{x\in S}\left(1+\frac{1}{x}\right)\\ &=\prod_{x\in S}\left(1+\frac{1}{x}\right)\sum_{x\in S}\frac{1}{1+\frac{1}{x}},\\ &=\dfrac{\sum_{x\in T}h(x)}{\prod_{x\in T}h(x)},\text{where }h(x)=\frac{1}{1+\frac{1}{x}}=\frac{x}{x+1}. \end{aligned}\]

\[\begin{aligned} f_2(S)=\sum_{T\subseteq S}f_1(T)&=\sum_{T\subseteq S}\dfrac{\sum_{x\in T}h(x)}{\prod_{x\in T}h(x)}\\ &=\dfrac{\sum_{x\in T}h(h(x))}{\prod_{x\in T}h(h(x))}.\\ &\Rightarrow f_k(S)=f_{k-1}(h(S))\Rightarrow f_k(S)=f_0(h^{(k)}(S)),\\ &\text{where }h^{(k)}(S)=h^{(k-1)}(\{\frac{x}{x+1}:x\in S\})=\{\frac{x}{kx+1}:x\in S\}. \end{aligned}\]

\[\begin{aligned} \therefore ans&=\dfrac{\sum\limits_{x\in S}\dfrac{x}{kx+1}}{\prod\limits_{x\in S}\dfrac{x}{kx+1}}\\ &=\dfrac{\prod\limits_{x\in S}(kx+1)\sum\limits_{x\in S}\dfrac{x}{kx+1}}{\prod\limits_{x\in S}x}\\ &=\dfrac{\sum\limits_{x\in S}x\dfrac{\prod_{y\in S}(ky+1)}{kx+1}}{\prod\limits_{x\in S}x}. \end{aligned}\]

因为题目保证有解,\(kx+1=0\) 的也只有一个,我们特判一下就好了()

code

点击查看代码
#include <cstdio>
#include <vector>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long LL;
inline char rc()
{
	static char buf[1048576],*pn=buf,*pe=buf;	
	return (pn==pe)&&(pe=(pn=buf)+fread(buf,1,1048576,stdin),pn==pe)?EOF:*pn++;
}
inline int read()
{
	int x=0;
	char cc=rc();
	while(cc<'0'||cc>'9')cc=rc();
	while(cc>='0'&&cc<='9')x=x*10+cc-'0',cc=rc();
	return x;
}
inline LL _read()
{
	LL x=0;
	char cc=rc();
	while(cc<'0'||cc>'9')cc=rc();
	while(cc>='0'&&cc<='9')x=x*10+cc-'0',cc=rc();
	return x;
}
#ifdef LOCAL
#define debug(...) fprintf(stderr,##__VA_ARGS__)
#else
#define debug(...) void(0)
#endif
LL qpow(LL a,LL b,int p){LL r=1;for(a%=p;b;b>>=1,a=a*a%p) if(b&1) r=r*a%p; return r;}
int n,P;
void red(LL&x){x=(x%P);}
LL inv(LL a){return qpow(a,P-2,P);}
LL a[7000010],b[7000010],c[7000010],mul=1,ans,k,sum[7000010];
void linear_inv(LL a[],LL b[]){
	sum[0]=1;
	for(int i=1;i<=n;i++) red(sum[i]=sum[i-1]*a[i]);
	LL isum=inv(sum[n]),tot=1;
	for(int i=n;i>=1;i--){
		b[i]=sum[i-1]*tot%P*isum%P;
		red(tot*=a[i]);
	}
}
int main(){
//	#ifdef LOCAL
//	 	freopen("input.in","r",stdin);
//	#endif
//	scanf("%d%lld%d",&n,&k,&P),red(k);
	n=read(),k=_read(),P=read(),red(k); 
	for(int i=1;i<=n;i++){
//		scanf("%lld",&a[i]);
		a[i]=read();
		red(b[i]=a[i]*k+1);
	}
	int pos=min_element(b+1,b+n+1)-b;
	swap(a[pos],a[1]),swap(b[pos],b[1]);
	for(int i=1;i<=n;i++) debug("%lld/%lld\n",a[i],b[i]);
	if(!b[1]){
		ans=a[1],mul=1;
		for(int i=1;i<=n;i++) red(mul*=a[i]);
		for(int i=2;i<=n;i++) red(ans*=b[i]);
		printf("%lld\n",ans*inv(mul)%P);
	}else{
		linear_inv(b,c);
		ans=0,mul=inv(sum[n]);
		for(int i=1;i<=n;i++) red(ans+=a[i]*c[i]%P),red(mul*=a[i]);
//		for(int i=1;i<=n;i++) red(ans+=a[i]*inv(b[i])%P),red(mul*=a[i]*inv(b[i])%P);
		printf("%lld\n",ans*inv(mul)%P);
	}
	return 0;
}

posted @ 2022-11-23 13:04  caijianhong  阅读(28)  评论(0编辑  收藏  举报