Student's Camp CodeForces - 708E (dp, prefix and optimization)

Effect: $ n $ $ m $ column brick row, each block has a left-side boundary day $ p $ probabilities are destroyed, the right edge of the evening has been destroyed $ p $ probability, the probability of communicating the required final upper and lower boundaries.

 

Note $ {dp} _ {i, l, r} $ as $ t $ traverse to the first row, the first row of bricks range $ t $ $ [l, r] $ probability.

There are $ {dp} _ {i, l, r} = p_ {l, r} \ sum {dp} _ {i-1, l ', r'} $ ( to meet the $ [l ', r'] $ and $ [l, r] $ intersect)

$ P_ {l, r} $ $ k $ days represents the remaining tiles are $ [l, r] $ probability.

Consider a two-dimensional optimization prefix, denoted $ f_ {i, l, r } = \ sum \ limits_ {1 \ le x \ le l} \ sum \ limits_ {1 \ le y \ le r} {dp} _ {i, x, y} $,  there

$$\begin{align}  {dp}_{i,l,r} &=p_{l,r}(f_{i-1,m,r}+f_{i-1,r,m}-f_{i-1,m,l-1}-f_{i-1,r,r}) \notag\\ &=p_{l,r}(f_{i-1,r,m}-f_{i-1,l-1,l-1}) \notag\end{align}$$

Finally ask the answer is $ f_ {n, m, m} $. However, this complexity is $ O (nm ^ 2) $

It may be noted $ dp $ equation in the required $ f $ value is very small.

记$A_{i,x}=f_{i,x,m},B_{i,x}=f_{i,x,x}$, 有

$$dp_{i,l,r}=p_{l,r}(A_{i-1,r}-B_{i-1,l-1})$$

$$A_{i,x}=A_{i,x-1}+\sum\limits_{x\le k\le m}{dp}_{i,x,k}$$

$$B_{i,x}=B_{i,x-1}+\sum\limits_{1\le k\le x}{dp}_{i,k,x}$$

Then, the prefix optimization, that is, the complexity of $ O (nm) $

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head



const int N = 1e6+10, M = 2e3+10;
int n, m, a, b, k;
int fac[N],ifac[N],p[M],suf[M],pre[M];
int A[2][M],B[2][M],s1[M],s2[M];
	I [0] = 1;

void init () {
	REP(i,1,N-1) fac[i]=fac[i-1]*(ll)i%P;
	ifac[N-1]=inv(fac[N-1]);
	PER(i,0,N-2) ifac[i]=ifac[i+1]*(i+1ll)%P;
}
int C(int n, int m) {
	if (n<m) return 0;
	return (ll)fac[n]*ifac[m]%P*ifac[n-m]%P;
}

int main() {
	init();
	cin>>n>>m>>a>>b>>k;
	int x = (ll)a*inv(b)%P, y = (ll)(P+b-a)*inv(b)%P;
	REP(i,1,min(m,k+1)) p[i] = (ll)C(k,i-1)*qpow(x,i-1)%P*qpow(y,k-i+1)%P;
	REP(i,1,m) { 
		suf[i] = (suf[i-1]+p[m-i+1])%P;
		pre[i] = (pre[i-1]+p[i])%P;
	}
	REP(i,1,m) A[0][i] = 1;
	int cur = 0;
	REP(i,1,n) {
		cur ^= 1;
		REP(k,1,m) s1[k]=(s1[k-1]+(ll)p[m-k+1]*A[!cur][k])%P;
		REP(k,1,m) s2[k]=(s2[k-1]-(ll)p[k]*B[!cur][k-1])%P;
		REP(x,1,m) {
			A[cur][x] = A[cur][x-1];
			int ret = (s1[m]-s1[x-1])%P;
			ret = (ret-(ll)(suf[m]-suf[x-1])*B[!cur][x-1])%P;
			A[cur][x] = (A[cur][x]+(ll)p[x]*ret)%P;
		}
		REP(x,1,m) {
			B[cur][x] = B[cur][x-1];
			int ret = (s2[x]+(ll)pre[x]*A[!cur][x])%P;
			B[cur][x] = (B[cur][x]+(ll)p[m-x+1]*ret)%P;
		}
	}
	int ans = B[cur][m];
	if (ans<0) ans += P;
	printf("%d\n", ans);
}

 

 

 

 

Guess you like

Origin www.cnblogs.com/uid001/p/11279175.html