[CSP-S Simulation Test]: Industrial title / a (math)

Topic Portal (internal title 39)


Input Format

The first line: four positive integer $ n $, $ m $, $ a $, $ b $.
Second row: $ n $ positive integers, represents one of the $ I $ $ f (i, 0) $ .
Third row: $ m $ positive integers, represents one of the $ I $ $ f (0, i) $ .


Output Format

The first line: an integer representing $ f (n, m) \ mod 998244353 $.


Sample

Sample input:

4 4 3 2
1 3 5 7
2 4 6 8

Sample output:

50807


Data range and tips

$ 20 \% $ data: $ n, m \ leqslant 10 , a, b \ leqslant 3, f (i, 0), f (0, i) \ leqslant 10 $.
$ 50 \% $ data: $ n, m \ leqslant { 10} ^ 3 $.
Further $ 10 \% $ data: $ n = 1 $.
Further $ 10 \% $ data: $ a = b = 1 $ .
Further $ 10 \% $ data: $ f (i, 0) = f (0, i) = 1 $.
$ 100 \% $ data: $ n, m \ leqslant 3 \ times {10} ^ 5 $, all other inputs are the data in the $ long \ long $ range.


answer

For each point $ (i, j) $, which point $ (n, m) to each direction the number of steps required to go $ is determined moves may be calculated by the number of combinations.

So the point $ (i, j) $ contribution to the answer is $ f (i, j) \ times $ walked several law $ \ times a ^ {mi} \ times b ^ {ni} $.

Pretreatment number of combinations, with rapid statistical power to answer.

Time complexity: $ \ Theta (n \ log n + m \ log m) $.

Expectations score: $ 100 $ points.

Actual score: $ 100 $ points.


Code time

#include<bits/stdc++.h>
using namespace std;
const long long mod=998244353;
long long n,m;
long long a,b;
long long p[300010],q[300010];
long long fac[600010],inv[600010];
long long ans;
long long qpow(long long x,long long y)
{
	long long res=1;
	while(y)
	{
		if(y&1)res=res*x%mod;
		x=x*x%mod;
		y>>=1;
	}
	return res;
}
long long C(long long x,long long y){return 1LL*fac[x]*inv[x-y]%mod*inv[y]%mod;}
int main()
{
	scanf("%lld%lld%lld%lld",&n,&m,&a,&b);
	a=(a+mod)%mod;
	b=(b+mod)%mod;
	for(long long i=1;i<=n;i++){scanf("%lld",&p[i]);p[i]=(p[i]+mod)%mod;}
	for(long long i=1;i<=m;i++){scanf("%lld",&q[i]);q[i]=(q[i]+mod)%mod;}
	fac[0]=inv[0]=1;
	for(long long i=1;i<=n+m;i++)
		fac[i]=1LL*fac[i-1]*i%mod;
	inv[n+m]=qpow(fac[n+m],mod-2);
	for(long long i=n+m;i;i--)
		inv[i-1]=1LL*inv[i]*i%mod;
	for(long long i=1;i<=n;i++)
		ans=(ans+C(n+m-1-i,m-1)*qpow(a,m)%mod*qpow(b,n-i)%mod*p[i]%mod)%mod;
	for(long long i=1;i<=m;i++)
		ans=(ans+C(n+m-1-i,n-1)*qpow(b,n)%mod*qpow(a,m-i)%mod*q[i]%mod)%mod;
	printf("%lld",ans);
	return 0;
}

rp ++

Guess you like

Origin www.cnblogs.com/wzc521/p/11498454.html