期望——Vasya and Magic Matrix

题解:

对于当前点来说,权值比他小的每一点都是等概率的。所以对当前点的权值进行从小到大排序,然后概率dp,状态转移的方程很好就求出来了。

#include <bits/stdc++.h>
//#define int long long
using namespace std;
using namespace std;
const int N=10000010;
int n, m, sumx, sumy, sump, dp[N], sumdp, x, y, tot;
struct point { int x, y, val; } p[N];
const int mod = 998244353;
void up(int &x, int y) { if ((x += y) >= mod) x -= mod; }
using LL = long long;
int pow(int x, int y) {
    int ans = 1;
    for (; y; y >>= 1, x = static_cast<LL> (x) * x % mod)
        if (y & 1) ans = static_cast<LL> (ans) * x % mod;
    return ans;
}
signed main() {
    std::scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) for (int j = 1; j <= m; j++) std::scanf("%d", &p[++tot].val), p[tot].x = i, p[tot].y = j;
    std::scanf("%d%d", &x, &y);
    std::sort(p + 1, p + tot + 1, [] (point A, point B) { return A.val < B.val; });
    for (int i = 1, l = 1; i <= tot; i++) {
        dp[i] = 0;
        if (p[i].val != p[l].val)
            for (; l < i; l++) up(sumx, p[l].x), up(sumy, p[l].y), up(sump, p[l].x * p[l].x + p[l].y * p[l].y), up(sumdp, dp[l]);
        int n = l - 1;
        up(dp[i], static_cast<LL> (n) * (p[i].x * p[i].x + p[i].y * p[i].y) % mod);
        up(dp[i], mod - 2 * (static_cast<LL> (p[i].x) * sumx % mod + static_cast<LL> (p[i].y) * sumy % mod) % mod);
        up(dp[i], (sump + sumdp) % mod);
        dp[i] = static_cast<LL> (dp[i]) * pow(n, mod - 2) % mod;
        if (p[i].x == x && p[i].y == y) {
            std::printf("%d\n", dp[i]);
            return 0;
        }
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值