逆推期望

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pb(x) push_back(x)
const int maxn = 1e3+5;
const ll mod = 998244353;
struct node
{
ll x,y;
ll val;
bool operator < ( const node &b) const
{
return val < b.val;
}
};
node a[maxn*maxn];
ll sumr,sumr2,sumc,sumc2,sumdp;
//ll arr[maxn];
ll dp[maxn][maxn];
ll mul(ll a,ll b)
{
return (a*b)%mod;
}
ll ksm(ll a,ll b)
{
ll res = 1;
while(b > 0)
{
if(b & 1) res = mul(res,a);
a = mul(a,a);
b >>= 1;
}
return res;
}
ll add(ll a,ll b)
{
a += b;
while(a >= mod) a -= mod;
while(a < 0) a += mod;
return a;
}
ll inv(ll a)
{
ll ia = ksm(a,mod-2);
assert(mul(a,ia) == 1);
return ia;
}
int main()
{
ll n,m;
ll i,j,k;
ll len;
scanf("%lld %lld",&n,&m);
len = 0;
for(i=1;i<=n;++i)
{
for(j=1;j<=m;++j)
{
a[len].x = i;
a[len].y = j;
scanf("%lld",&a[len].val);
len ++;
}
}
sort(a,a+len);
//for(i=0;i<len;++i)
// printf("%lld %lld %lld\n",a[i].x,a[i].y,a[i].val);
memset(dp,0,sizeof(dp));
ll l,r;
l = 0;
sumr = sumr2 = sumc2 = sumc = sumdp = 0;
while(l < n*m)
{
r = l;
while(a[r].val == a[l].val && r < n*m) r ++;
//cout << l << " " << r << endl;
ll il = -1;
if(l != 0) il = inv(l); for(i=l;i<r;++i)
{
ll rr,cc;
rr = a[i].x; cc = a[i].y;
if(il == -1)
{
dp[rr][cc] = 0;
continue;
}
dp[rr][cc] = add(dp[rr][cc],mul(sumdp,il));
dp[rr][cc] = add(dp[rr][cc],mul(rr,rr));
dp[rr][cc] = add(dp[rr][cc],mul(cc,cc));
dp[rr][cc] = add(dp[rr][cc],mul(sumr2,il));
dp[rr][cc] = add(dp[rr][cc],mul(sumc2,il));
dp[rr][cc] = add(dp[rr][cc],mul(mul(-2*rr,sumr),il));
dp[rr][cc] = add(dp[rr][cc],mul(mul(-2*cc,sumc),il)); }
for(i = l; i < r; ++i)
{
int rr,cc;
rr = a[i].x; cc = a[i].y;
sumdp = add(sumdp,dp[rr][cc]);
sumr2 = add(sumr2,mul(rr,rr));
sumc2 = add(sumc2,mul(cc,cc));
sumr = add(sumr,rr);
sumc = add(sumc,cc);
}
l = r;
}
ll c,b;
scanf("%lld %lld",&c,&b);
// cout << endl;
cout << dp[c][b] << endl;
}
/*
1 4
1 1 2 1
1 3 2 3
1 5 7
2 3 1
1 2
*/

  

这题是真的痛苦

从各个val低于指定位置val的点,向指定位置去推

至于为什么要用x、x²等前缀和,写下公式多看下就懂了

codeforces 1042 e的更多相关文章

  1. CodeForces 1042 F Leaf Sets 贪心

    Leaf Sets 题意:给你一棵树,树上有n个点,只有一条边的点叫做叶子,现在要求把所有的叶子分组,每个组内的所有叶子的距离都不能大于k. 题解: 我们可以随意找一个不是叶子的节点当做这颗树的根节点 ...

  2. Codeforces Round #510 (Div. 2)

    Codeforces Round #510 (Div. 2) https://codeforces.com/contest/1042 A 二分 #include<iostream> usi ...

  3. Codeforces Round #510 (Div. 2) D. Petya and Array(离散化+反向树状数组)

    http://codeforces.com/contest/1042/problem/D 题意 给一个数组n个元素,求有多少个连续的子序列的和<t (1<=n<=200000,abs ...

  4. CF 1042 E. Vasya and Magic Matrix

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

  5. CF 1042 F. Leaf Sets

    F. Leaf Sets http://codeforces.com/contest/1042/problem/F 题意: 将所有的叶子节点分配到尽量少的集合,一个可行的集合中两两叶子节点的距离< ...

  6. Codeforces Round #510 (Div. 2) B. Vitamins

    B. Vitamins 题目链接:https://codeforces.com/contest/1042/problem/B 题意: 给出几种药,没种可能包含一种或多种(最多三种)维生素,现在问要吃到 ...

  7. Codeforces Round #510 (Div. 2) D. Petya and Array(树状数组)

    D. Petya and Array 题目链接:https://codeforces.com/contest/1042/problem/D 题意: 给出n个数,问一共有多少个区间,满足区间和小于t. ...

  8. Codeforces Round #510 #C Array Product

    http://codeforces.com/contest/1042/problem/C 给你一个有n个元素序列,有两个操作:1,选取a[i]和a[j],删除a[i],将$a[i]*a[j]$赋值给a ...

  9. Codeforces Round #510 #B

    http://codeforces.com/contest/1042/problem/B 题意: 给出n种饮料,每种饮料还有一种或多种维生素(A或B或C),某人想集齐三种维生素,问最少需要花费多少? ...

随机推荐

  1. for...in的改进版for...of

    for...in 用起来似乎还不错,为什么又弄个 for...of 呢? 来看个例子: 'user strict' var arr = [12,13,14,15,16]; for(var i in a ...

  2. windows清空电脑的DNS缓存

    清空电脑的DNS缓存 按"Win+R"系统热键打开"运行"窗口,进入terminal终端 输入"ipconfig /flushdns"命令后 ...

  3. fastjson的@JSONField注解

    @JSONField作用:在字段和方法上1.Field:@JSONField作用在Field时,name可以定义输入key的名字,反序列化的时 值不会赋值到属性上2.作用在setter和getter方 ...

  4. javaee_正则表达式基础和常用表达式

    正则基础: 1.1 -字符集 [ ] : 代表单个字符. [^] : 除了该字符外的所有单个字符. [a-zA-Z] : [a-z] || [A-Z]. [a-d[m-p]] :[a, d] || [ ...

  5. saltstack的封装和内网使用

    0.客户端使用 linux:把linux的ragent文件夹拷贝到内网linux /opt目录下,运行初始化脚本 salt服务端:# @Master:"/opt/ragent/python/ ...

  6. 在centos上面编译安装python

    前言 因为在学习storm的过程中需要安装python,storm是部署在linux上面的,所以需要将python安装在linux上面. 安装准备 python下载 官网链接:https://www. ...

  7. centos7下 nginx配置upstream 不能直接代理到本机tomcat的解决

    Nginx代理本地tomcat时404. upstream是直接配置server localhost:10000 的.单独访问tomcat是可以的. 查看/var/log/nginx/error.lo ...

  8. 关于 web 页面 占满全屏

    页面一般可以分成三部分,头部,底部,中间内容部分. 一般不用考虑中间高度部分,因为可以靠内容撑开,然后让底部到达底部.但是当中间内容太少时,底部就会顶不到底部. 方法1.中间部分给一个最小高度(min ...

  9. TZOJ 5101 A Game(区间DP)

    描述 Consider the following two-player game played with a sequence of N positive integers (2 <= N & ...

  10. Python基础-python简介(一)

    一.简介: python是一种面向对象的解释性计算机程序设计语言,由荷兰人Guido  von  Rossum于1989年的圣诞节发明. Python语言的特色: 1.python是一门解释性语言 解 ...