[Codeforces 1042E] Vasya and Magic Matrix(期望DP)

本文介绍了一种使用期望动态规划(DP)解决特定格子得分问题的方法。通过将格子按权值排序,利用预处理逆元,避免了直接枚举转移,实现了高效的求解。关键在于化简转移方程,利用累积和技巧减少计算复杂度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门
没事干来划个水(喂喂喂,想想你还有多少题没补w(゚Д゚)w

这题是个非常水的期望DP
我们先按格子从小到大排个序
然后用 f [ i ] f[i] f[i]表示 i i i i i i这个格子出发的期望得分

最小的那些格子的f值显然是0啦
大的格子的f值显然是从比它小的转移来的啦

转移方程大概长这个样子,假设比第 i i i个格子小的共有 s s s个, j j j是权值比 i i i小的那些格子
f [ i ] = ∑ ( f [ j ] + ( x [ i ] − x [ j ] ) 2 + ( y [ i ] − y [ j ] ) 2 ) ) s f[i]=\frac{\sum{(f[j]+(x[i]-x[j])^2+(y[i]-y[j])^2))}}{s} f[i]=s(f[j]+(x[i]x[j])2+(y[i]y[j])2))
但是这样好像并!无!卵!用!因为我们还是要枚举 j j j
于是我们可以把上面的式子的分子展开
∑ ( f [ j ] + ( x [ i ] − x [ j ] ) 2 + ( y [ i ] − y [ j ] ) 2 ) ) = ∑ ( f [ j ] + x [ i ] 2 − 2 x [ i ] x [ j ] + x [ j ] 2 + y [ i ] 2 − 2 y [ i ] y [ j ] + y [ j ] 2 ) \sum{(f[j]+(x[i]-x[j])^2+(y[i]-y[j])^2))}=\sum{(f[j]+x[i]^2-2x[i]x[j]+x[j]^2+y[i]^2-2y[i]y[j]+y[j]^2)} (f[j]+(x[i]x[j])2+(y[i]y[j])2))=(f[j]+x[i]22x[i]x[j]+x[j]2+y[i]22y[i]y[j]+y[j]2)
再化简就是(由于式子较长就只写了关于 x x x的部分, y y y同理)
s ⋅ x [ i ] 2 − 2 ⋅ x [ i ] ⋅ ∑ x [ j ] + ∑ x [ j ] 2 s\cdot x[i]^2-2 \cdot x[i] \cdot \sum{x[j]}+\sum{x[j]^2} sx[i]22x[i]x[j]+x[j]2
于是,我们就可以让 s f = ∑ f [ j ] , s x = ∑ x [ j ] , s x 2 = ∑ x [ j ] 2 sf=\sum{f[j]},sx=\sum{x[j]},sx2=\sum{x[j]^2} sf=f[j],sx=x[j],sx2=x[j]2,当然还有 s y sy sy s y 2 sy2 sy2
于是
f [ i ] = s f + s ⋅ x [ i ] 2 − 2 ⋅ x [ i ] ⋅ s x + s x 2 + s ⋅ y [ i ] 2 − 2 ⋅ y [ i ] ⋅ s y + s y 2 s f[i]=\frac{sf+s \cdot x[i]^2-2 \cdot x[i] \cdot sx+sx2+s \cdot y[i]^2-2 \cdot y[i] \cdot sy+sy2}{s} f[i]=ssf+sx[i]22x[i]sx+sx2+sy[i]22y[i]sy+sy2
欸,怎么还有个除 s s s ???
没事没事,这个一开始线性求逆元预处理一波就好啦O(∩_∩)O

还有一个需要注意的细节是,由于每个格子是由严格小于它的格子转移来的,所以要把权值相同的格子的f值全求完了之后再一起更新 s f , s x sf,sx sf,sx这些变量。

#include<bits/stdc++.h>
#define fr(i,x,y) for(int i=x;i<=y;i++)
#define rf(i,x,y) for(int i=y;i>=x;i--)
#define ll long long
using namespace std;
const int N=1001;
const int p=998244353;
int n,m;
struct data{
	int x,y,c;
}a[N*N];
int x,y;
ll inv[N*N];
ll f[N*N];
ll s,sf,sx,sx2,sy,sy2;

void read(int &x){
	char ch=getchar();x=0;
	for(;ch<'0'||ch>'9';ch=getchar());
	for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
}

int cmp(const data &q,const data &w){
	return q.c<w.c;
}

void Add(ll &x,ll y){  //手动取模
	x+=y;
	while(x<0) x+=p;
	while(x>=p) x-=p;
}

inline ll sqr(ll x){
	return x*x;
}

int main(){
	read(n);read(m);
	fr(i,1,n) fr(j,1,m){
		read(x);
		a[(i-1)*m+j]=(data){i,j,x};
	}
	read(x);read(y);
	n*=m;
	sort(a+1,a+1+n,cmp);
	inv[1]=1;
	fr(i,2,n) inv[i]=(-p/i*inv[p%i])%p;  //线性求逆元
	a[n+1].c=-1;
	fr(i,1,n){
		if (s){  //可以转移
			Add(f[i],(s*sqr(a[i].x)%p-2*sx*a[i].x%p+sx2)%p);
			Add(f[i],(s*sqr(a[i].y)%p-2*sy*a[i].y%p+sy2)%p);
			Add(f[i],sf);
			f[i]=f[i]*inv[s]%p;
		} else f[i]=0;   //已经是最小的格子了
		if (a[i].x==x&&a[i].y==y){   //算出答案就输出
			cout<<(f[i]+p)%p<<endl;
			exit(0);
		}
		if (a[i].c!=a[i+1].c)   //相同权值的格子全部处理完了就更新
		 for(int j=i;j;j--){
		 	if (a[j].c!=a[i].c) break;
		 	Add(sf,f[j]);
		 	Add(sx,a[j].x);
		 	Add(sx2,sqr(a[j].x));
		 	Add(sy,a[j].y);
		 	Add(sy2,sqr(a[j].y));
		 	s++;
		 }
	}
	return 0;
}
### Codeforces 887E Problem Solution and Discussion The problem **887E - The Great Game** on Codeforces involves a strategic game between two players who take turns to perform operations under specific rules. To tackle this challenge effectively, understanding both dynamic programming (DP) techniques and bitwise manipulation is crucial. #### Dynamic Programming Approach One effective method to approach this problem utilizes DP with memoization. By defining `dp[i][j]` as the optimal result when starting from state `(i,j)` where `i` represents current position and `j` indicates some status flag related to previous moves: ```cpp #include <bits/stdc++.h> using namespace std; const int MAXN = ...; // Define based on constraints int dp[MAXN][2]; // Function to calculate minimum steps using top-down DP int minSteps(int pos, bool prevMoveType) { if (pos >= N) return 0; if (dp[pos][prevMoveType] != -1) return dp[pos][prevMoveType]; int res = INT_MAX; // Try all possible next positions and update 'res' for (...) { /* Logic here */ } dp[pos][prevMoveType] = res; return res; } ``` This code snippet outlines how one might structure a solution involving recursive calls combined with caching results through an array named `dp`. #### Bitwise Operations Insight Another critical aspect lies within efficiently handling large integers via bitwise operators instead of arithmetic ones whenever applicable. This optimization can significantly reduce computation time especially given tight limits often found in competitive coding challenges like those hosted by platforms such as Codeforces[^1]. For detailed discussions about similar problems or more insights into solving strategies specifically tailored towards contest preparation, visiting forums dedicated to algorithmic contests would be beneficial. Websites associated directly with Codeforces offer rich resources including editorials written after each round which provide comprehensive explanations alongside alternative approaches taken by successful contestants during live events. --related questions-- 1. What are common pitfalls encountered while implementing dynamic programming solutions? 2. How does bit manipulation improve performance in algorithms dealing with integer values? 3. Can you recommend any online communities focused on discussing competitive programming tactics? 4. Are there particular patterns that frequently appear across different levels of difficulty within Codeforces contests?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值