逆推期望

#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. dll 恐怖的代码调整

    总结一波这998的不得不提的调整代码的心得. 调整代码的背景:现有wps美化代码分散在各个插件里面,导致每次修改一小部分代码,都要全新编译,并且只能跟版本发,所以决定将wps的美化代码整合成一个插件d ...

  2. C语言的基础输入输出

    首先来整理一下各个数据类型的输入输出格式: 1.char %c 2.int/short int %d 3.long int %ld 4.long long int %lld 5.float %f 6. ...

  3. 使用httpClient发送post请求连接restful接口

    public static String httpPost(String url,String arg){ InputStream is; BufferedReader br; StringBuild ...

  4. leetcode84

    public class Solution { public int LargestRectangleArea(int[] hist) { // The main function to find t ...

  5. mysql连接测试java脚本

    JDBC.java import java.io.IOException; import java.io.InputStream; import java.sql.*; import java.uti ...

  6. xtrabackup备份还原MySQL数据库

    mysqldump 备份鉴于其自身的某些特性(锁表,本质上备份出来insert脚本或者文本,不支持差异备份),不太适合对实时性要求比较高的情况Xtrabackup可以解决mysqldump存在的上述的 ...

  7. actuator/hystrix.stream 没有反应的方法

    http://localhost:8086/actuator/hystrix.stream 在启动类加上,就ok了 @Bean public ServletRegistrationBean hystr ...

  8. 模型介绍之FastText

    模型介绍一: 1. FastText原理及实践 前言----来源&特点 fastText是Facebook于2016年开源的一个词向量计算和文本分类工具,在学术上并没有太大创新.但是它的优点也 ...

  9. [leetcode]5. Longest Palindromic Substring最长回文子串

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  10. rsync同步命令详解

    一.rsync命令的解释sync(Synchronize,即“同步”)为UNIX操作系统的标准系统调用,功能为将内核文件系统缓冲区的所有数据(也即预定将通过低级I/O系统调用写入存储介质的数据)写入存 ...