逆推期望

#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. list.remove的使用分析

    场景描述 在做需求中,有很多情况会出现 对一个list遍历并过滤掉其中特定的数据 这种场景 .但是按照平常的使用方式,发现报错了. public static void main(String[] a ...

  2. jdk 8 日期处理。

    ZoneId id = ZoneId.systemDefault(); LocalDateTime dateTime = LocalDateTime.now(id); System.out.print ...

  3. java网络编程-单线程服务端与客户端通信

    该服务器一次只能处理一个客户端请求;p/** * 利用Socket进行简单服务端与客户端连接 * 这是服务端 */public class EchoServer { private ServerSoc ...

  4. ElasicSearch(3) 安装elasticsearch-head

    https://github.com/mobz/elasticsearch-head 1.git install git 2.git clone git://github.com/mobz/elast ...

  5. i386 x86_64 armv7 arm64

    arm7: Used in the oldest iOS 7-supporting devices arm7s: As used in iPhone 5 and 5C arm64: For the 6 ...

  6. 2018 pycharm最近激活码

    今天更新了一下pycharm,结果之前的激活就不能用了,下面是新的激活方法: 1.mac下在终端进入etc目录: cd /etc 2.编辑hosts文件: vi hosts 将“0.0.0.0 acc ...

  7. Javascript中 toFixed

    javascript中toFixed使用的是银行家舍入规则. 银行家舍入:所谓银行家舍入法,其实质是一种四舍六入五取偶(又称四舍六入五留双)法. 简单来说就是:四舍六入五考虑,五后非零就进一,五后为零 ...

  8. Linux redhat 7 进入单用户模式

    redhat  7 进入单用户模式修复系统故障 1.启动机器,grub界面选择第一个,按e 2.往下翻,找到Linux16 开头的那一行 3.将ro改为"rw init=/sysroot/b ...

  9. 8-13、Python 散列复习

    1.{} 输入是花括号 myCat = { 'size':'fat', 'color':'gray', 'disposition':'loud'}   键:值 myCat['size'] = fat ...

  10. 图片识别文字, OCR

    文章引用自: https://www.cnblogs.com/stone_w/archive/2011/10/08/2202397.html 方式一.Asprise-OCR的使用. Asprise-O ...