Vasya and Magic Matrix CodeForces - 1042E (概率dp)

大意:给定n*m矩阵, 初始位置(r,c), 每一步随机移动到权值小于当前点的位置, 得分为移动距离的平方, 求得分期望.

直接暴力dp的话复杂度是O(n^4), 把距离平方拆开化简一下, 可以O(n^2logn).

#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, P2 = 998244353, 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(P2%x)*(P2-P2/x)%P2;}
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 = 1e3+10;
int n, m, r, c, cnt;
struct _ {
	int x,y,w;
	bool operator < (const _ &rhs) const {
		return w<rhs.w;
	}
} a[N*N];
int dp[N][N];

int main() {
	scanf("%d%d", &n, &m);
	REP(i,1,n) REP(j,1,m) {
		int t;
		scanf("%d", &t);
		a[++cnt] = {i,j,t};
	}
	sort(a+1,a+1+cnt);
	a[cnt+1].w=-1;
	ll sum_dp = 0, sum_2 = 0, sum_x = 0, sum_y = 0;
	REP(R,1,cnt) {
		int L = R;
		while (a[R].w==a[R+1].w) ++R;
		if (L!=1) {
			REP(i,L,R) {
				ll t = (sum_dp+sum_2-a[i].x*sum_x-a[i].y*sum_y+(L-1)*((ll)a[i].x*a[i].x+(ll)a[i].y*a[i].y))%P2;
				t = t*inv(L-1)%P2;
				if (t<0) t += P2;
				dp[a[i].x][a[i].y] = t%P2;
			}
		}
		REP(i,L,R) {
			(sum_dp += dp[a[i].x][a[i].y]) %= P2;
			(sum_2 += (ll)a[i].x*a[i].x+(ll)a[i].y*a[i].y) %= P2;
			(sum_x += 2*a[i].x) %= P2;
			(sum_y += 2*a[i].y) %= P2;
		}
	}
	scanf("%d%d", &r, &c);
	printf("%d\n", dp[r][c]);
}

原文地址:https://www.cnblogs.com/uid001/p/10913537.html

时间: 2024-08-28 09:39:27

Vasya and Magic Matrix CodeForces - 1042E (概率dp)的相关文章

UVA - 10559 Blocks 和 Vasya and Binary String CodeForces - 1107E (dp OR 记忆化搜索)

UVA - 10559 Blocks 题意:消消乐,每次连续相同的可以消除,分数加上长度的平方,问最多可以获得几分全部消完 题解: 区间dp + 记忆化搜索 dp[i][j][k] : (区间 [i,  j] 后面带上一段和 j 颜色相同的且长度为 k )的消消乐最大积分 1.消最后一段颜色和 j 颜色相同的 dp[i][j][k] <-- dp[i][j-1][0] + (k+1)^2 2.对于i <= l < j, 如果 l 和 j 的颜色相同, 那么可以把 [l+1, j-1]消掉

Codeforces 28C [概率DP]

/* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队伍的期望. 思路: 概率dp dp[i][j][k]代表前i个浴室有j个人最长队伍是k的概率. 枚举第i个浴室的人数.然后转移的时候其实是一个二项分布. */ #include<bits/stdc++.h> using namespace std; int jilu[55]; double dp[

Problem A CodeForces 148D 概率dp

题意:袋子里有w只白鼠和b只黑鼠.龙和公主轮流从袋子里抓老鼠.谁先抓到白色老师谁就赢.公主每次抓一只老鼠,龙每次抓完一只老鼠之后会有一只老鼠跑出来.每次抓老鼠和跑出来的老鼠都是随机的.如果两个人都没有抓到白色老鼠则龙赢.公主先抓.问公主赢的概率. 做了这么多概率dp的题目了,本来接的差不多了,结果一做还是不会...... 下面是看了别人的思路 win[i][j] = i * 1.0 / (i + j); //i只白老鼠j只黑老鼠时公主选白老鼠 win[i][j] += lost[i][j-1]

CodeForces 148D 概率dp

//思路:通过博弈的思想设计出状态,dp[w][b] 表示当公主面对 w 只白鼠和 b 只黑鼠时获胜的概率 1 #include "iostream" 2 #include "cstdio" 3 #include "cstring" 4 #include "algorithm" 5 using namespace std; 6 double dp[1010][1010]; 7 int w, b; 8 9 int main()

CF1042E Vasya and Magic Matrix

感觉不会期望. 首先把所有格子按照权值从小到大排一下序,这样一共有$n * m$个元素,每个元素有三个属性$x, y, val$. 下文中的下标均为排序后的下标. 这样子我们就可以推出公式: $f_i = \frac{1}{k}\sum_{j = 1}^{k}(f_j + (x_j - x_i)^2 + (y_j - y_i)^2)$    $($保证$val_j < val_i$并且这样的元素一共有$k$个$)$. 暴力转移是$n^2$的,但是我们可以把这个式子拆开: $f_i = \frac

Codeforces Div.301D Bad Luck Island(概率dp+记忆化搜索)

一道概率dp问题. 题目链接:http://codeforces.com/contest/540/problem/D 题目大意:一个岛上有r个石头,s个剪子,p个布,他们之间随机挑出两个相遇,如果不是相同物种,就会有一个消失,分别求出最后这座岛上只剩下一个物种的概率. 我们用dp[i][j][k]来存储i个石头,j个剪刀,k个布时,某物种的存活概率,共dp三次,算出三个物种分别的概率. 首先,我们需要把对应想求的物种概率初始化,这里以石头为例,那么对于i从1到r,不难理解dp[i][0][0]=

CodeForces 540D Bad Luck Island 概率dp

CodeForces 540D 应该是简单概率dp,由于写得少显得十分蠢萌 求期望逆推,求概率正推,大概是这么个意思,贴一发留恋 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define db double const int maxn=108; db dp[maxn][maxn][maxn]; int main() { int i,j,n,m,k,p; whi

Codeforces 148D Bag of mice (概率dp)

D. Bag of mice time limit per test:2 seconds memory limit per test:256 megabytes The dragon and the princess are arguing about what to do on the New Year's Eve. The dragon suggests flying to the mountains to watch fairies dancing in the moonlight, wh

Codeforces 518D Ilya and Escalator (概率dp)

Ilya and Escalator time limit per test: 2 seconds memory limit per test: 256 megabytes Ilya got tired of sports programming, left university and got a job in the subway. He was given the task to determine the escalator load factor. Let's assume that