大意:给定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]);
}

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

  1. CF 1042 E. Vasya and Magic Matrix

    E. Vasya and Magic Matrix http://codeforces.com/contest/1042/problem/E 题意: 一个n*m的矩阵,每个位置有一个元素,给定一个起点 ...

  2. Codeforces 28C [概率DP]

    /* 大连热身D题 题意: 有n个人,m个浴室每个浴室有ai个喷头,每个人等概率得选择一个浴室. 每个浴室的人都在喷头前边排队,而且每个浴室内保证大家都尽可能均匀得在喷头后边排队. 求所有浴室中最长队 ...

  3. codeforces 148D 概率DP

    题意: 原来袋子里有w仅仅白鼠和b仅仅黑鼠 龙和王妃轮流从袋子里抓老鼠. 谁先抓到白色老师谁就赢. 王妃每次抓一仅仅老鼠,龙每次抓完一仅仅老鼠之后会有一仅仅老鼠跑出来. 每次抓老鼠和跑出来的老鼠都是随 ...

  4. Vasya And The Matrix CodeForces - 1016D (思维+构造)

    Now Vasya is taking an exam in mathematics. In order to get a good mark, Vasya needs to guess the ma ...

  5. codeforces 540D 概率dp

    传送门 大概可以这样理解, 一开始有r个石头, p个布, s个剪刀, 每一天有其中的两个相遇, 如果两个是相同的种类, 什么都不会发生, 否则的话有一个会挂掉, 问最后每一种生存的概率. dp[i][ ...

  6. CodeForces 398B 概率DP 记忆化搜索

    题目:http://codeforces.com/contest/398/problem/B 有点似曾相识的感觉,记忆中上次那个跟这个相似的 我是用了 暴力搜索过掉的,今天这个肯定不行了,dp方程想了 ...

  7. Codeforces 931 概率DP

    A #include <bits/stdc++.h> #define PI acos(-1.0) #define mem(a,b) memset((a),b,sizeof(a)) #def ...

  8. Codeforces - 518D 概率DP初步

    #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #i ...

  9. CF1042E Vasya and Magic Matrix 题解

    题目链接 思路分析 看到题目中 \(n,m \leq 1000\) ,故直接考虑 \(O(n^2)\) 级别做法. 我们先把所有的点按照 \(val\) 值从小到大排序,这样的话二维问题变成序列问题. ...

随机推荐

  1. From 7.29 To 8.4

    From 7.29 To 8.4 大纲 英语按时背 做点思维题 可能还有时间学点东西, 这周我也不知道应该干什么 7.29 上午考试, 终于有一回不是自闭的考试了 题目比较简单, 就不说了 7.30 ...

  2. 1.4 JAVA日期处理

    一.JAVA日期 参考链接:https://www.runoob.com/java/java-date-time.html 1.日期两个构造函数 1.第一个构造函数使用当前日期和时间来初始化对象.Da ...

  3. 提高组刷题班 DAY 1 上午

    低仿机器人(robo,1s,64M) 题解 大模拟 代码 #include <cstdio> #include <cstring> #include <iostream& ...

  4. springBoot2.X---过滤器,监听器,拦截器

    过滤器,监听器,拦截器 一.画图理解 过滤器(filter),监听器(listener),拦截器(Interceptor). 通过两幅图我们可以理解拦截器和过滤器的特点 1.过滤器 过滤器是在请求进入 ...

  5. javascript 数据分组

    一.静态数据 [ {"id":"1001","name":"值1","value":"11 ...

  6. 初步理解js作用域

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. jQuery九类选择器

    目的:通过选择器,能定位web页面(HTML/JSP/XML)中的任何标签, 注意:项目中,通常是多种选择器一起使用 基本选择器 <html> <head> <meta ...

  8. js复选框实现全选、全不选、反选

    复选框为checkbox对象 通过input就可以将一个简单的复选框呈现在页面上 <input type="checkbox" /> 要实现的大概就是这样一个页面 思路 ...

  9. 图片滚动js代码

          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...

  10. List去重为什么要写equals(),hashCode()方法

    一,各个集合的特点: Collection(集合):容器,用于存放对象(引用类型.基本类型需要自动装箱) List(列表):元素有序,元素可以重复 (有索引). 通过元素的equals()方法判断是否 ...