CF1042E Vasya and Magic Matrix
感觉不会期望。
首先把所有格子按照权值从小到大排一下序,这样一共有$n * m$个元素,每个元素有三个属性$x, y, val$。
下文中的下标均为排序后的下标。
这样子我们就可以推出公式:
$f_i = \frac{1}{k}\sum_{j = 1}^{k}(f_j + (x_j - x_i)^2 + (y_j - y_i)^2)$ $($保证$val_j < val_i$并且这样的元素一共有$k$个$)$。
暴力转移是$n^2$的,但是我们可以把这个式子拆开:
$f_i = \frac{1}{k}\sum_{j = 1}^{k}f_j + x_i^2 + y_i^2 + \frac{1}{k}\sum_{j = 1}^{k}x_j^2 + \frac{1}{k}\sum_{j = 1}^{k}y_j^2 - \frac{2x_i}{k}\sum_{j = 1}^{k}x_j - \frac{2y_i}{k}\sum_{j = 1}^{k}y_j$
维护$\sum_{i = 1}^{k}x_i^2$、$\sum_{i = 1}^{k}y_i^2$、$\sum_{i = 1}^{k}y_i$、$\sum_{i = 1}^{k}x_i$、$\sum_{i = 1}^{k}f_i$五个前缀和就可以$O(n)$转移了。
要注意$val_i$可能为$0$。
加上算逆元的时间一共是$O(nmlogP)$。
Code:
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll; const int N = ;
const int M = 1e6 + ;
const ll P = 998244353LL; int n, m, tot = ;
ll a[N][N], f[M]; struct Item {
ll x, y, val;
} b[M]; bool cmp(const Item &u, const Item &v) {
return u.val < v.val;
} inline ll fpow(ll x, ll y) {
ll res = 1LL;
for(; y > ; y >>= ) {
if(y & ) res = res * x % P;
x = x * x % P;
}
return res;
} inline void up(ll &x, ll y) {
x = ((x + y) % P + P) % P;
} template <typename T>
inline void read(T &X) {
X = ; char ch = ; T op = ;
for(; ch > '' || ch < ''; ch = getchar())
if(ch == '-') op = -;
for(; ch >= '' && ch <= ''; ch = getchar())
X = (X << ) + (X << ) + ch - ;
X *= op;
} int main() {
read(n), read(m);
for(int i = ; i <= n; i++)
for(int j = ; j <= m; j++) {
read(a[i][j]);
b[++tot].x = 1LL * i, b[tot].y = 1LL * j, b[tot].val = a[i][j];
} int stx, sty, pos; read(stx), read(sty);
sort(b + , b + + tot, cmp);
for(int i = ; i <= tot; i++)
if(b[i].x == stx && b[i].y == sty) {
pos = i;
break;
} ll sumx = 0LL, sumy = 0LL, sumx2 = 0LL, sumy2 = 0LL, sumf = 0LL; int k = ;
for(int i = ; i <= pos; i++) {
for(; b[k].val < b[i].val && k <= pos; k++) {
up(sumx, b[k].x), up(sumy, b[k].y);
up(sumx2, b[k].x * b[k].x % P), up(sumy2, b[k].y * b[k].y % P);
up(sumf, f[k]);
}
if(k <= ) continue;
ll invK = fpow(k - , P - );
up(f[i], invK * sumf % P);
up(f[i], b[i].x * b[i].x % P), up(f[i], b[i].y * b[i].y % P);
up(f[i], invK * sumx2 % P), up(f[i], invK * sumy2 % P);
up(f[i], -2LL * b[i].x % P * invK % P * sumx % P), up(f[i], -2LL * b[i].y % P * invK % P * sumy % P);
} printf("%lld\n", f[pos]);
return ;
}
提醒自己:写快速幂不要把函数名写成$pow$,因为这样WA了很多次。
CF1042E Vasya and Magic Matrix的更多相关文章
- CF1042E Vasya and Magic Matrix 题解
题目链接 思路分析 看到题目中 \(n,m \leq 1000\) ,故直接考虑 \(O(n^2)\) 级别做法. 我们先把所有的点按照 \(val\) 值从小到大排序,这样的话二维问题变成序列问题. ...
- CF 1042 E. Vasya and Magic Matrix
E. Vasya and Magic Matrix http://codeforces.com/contest/1042/problem/E 题意: 一个n*m的矩阵,每个位置有一个元素,给定一个起点 ...
- Vasya and Magic Matrix CodeForces - 1042E (概率dp)
大意:给定n*m矩阵, 初始位置(r,c), 每一步随机移动到权值小于当前点的位置, 得分为移动距离的平方, 求得分期望. 直接暴力dp的话复杂度是O(n^4), 把距离平方拆开化简一下, 可以O(n ...
- Educational Codeforces Round 9 F. Magic Matrix 最小生成树
F. Magic Matrix 题目连接: http://www.codeforces.com/contest/632/problem/F Description You're given a mat ...
- Educational Codeforces Round 48 (Rated for Div. 2) D 1016D Vasya And The Matrix (构造)
D. Vasya And The Matrix time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- codeforces1016 D. Vasya And The Matrix(思维+神奇构造)
D. Vasya And The Matrix time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- Codeforces 632F Magic Matrix(bitset)
题目链接 Magic Matrix 考虑第三个条件,如果不符合的话说明$a[i][k] < a[i][j]$ 或 $a[j][k] < a[i][j]$ 于是我们把所有的$(a[i][j ...
- D. Vasya And The Matrix(Educational Codeforces Round 48)
D. Vasya And The Matrix time limit per test2 seconds memory limit per test256 megabytes inputstandar ...
- 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 ...
随机推荐
- 323 id与小数据池
a = 1000b = 1000print(a == b)== 比较的是数值is 比较的是内存地址.print(a is b)查看内存地址id()print(id(a))print(id(b)) 小数 ...
- Hadoop 运行jar包时 java.lang.ClassNotFoundException: Class com.zhen.mr.RunJob$HotMapper not found
错误如下 Error: java.lang.RuntimeException: java.lang.ClassNotFoundException: Class com.zhen.mr.RunJob$H ...
- 关于jvm中的常量池和String.intern()理解
1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...
- Spring- 异常org.xml.sax.SAXParseException; systemId: http://www.springframework.org/schema/context/; lineNumber: 1; columnNumber: 55; 在 publicId 和 systemId 之间需要有空格。
抛出异常 六月 03, 2018 7:40:44 下午 org.springframework.context.support.AbstractApplicationContext prepareRe ...
- mysql 使用过程中出现问题
1. mysql_front连接报错,sql执行错误#3167的解决方案 提示:The 'INFORMATION_SCHEMA.SESSION_VARIABLES' feature is disabl ...
- eclipse导入jar包连接mysql
Eclipse中导入 mysql--conncetor --java--jars 方法一:在工程项上右击,点Build Path->Configure Build Path-->Libr ...
- DbHelperSQL 事务写法!
try { DBUtility.CommandInfo dbcom = new DBUtility.CommandInfo(); List<DBUtility.CommandInfo> s ...
- $.ajax()方法详解(转)
以下内容转自:http://www.cnblogs.com/tylerdonet/p/3520862.html 尊重原创,请访问原创文章 jquery中的ajax方法参数总是记不住,这里记录一下. ...
- python-多线程趣味
假设一个程序员,想听歌,但是又想敲代码,于是又: 我听完歌就去敲代码: #! /usr/bin/env python #coding=utf-8 import time def matter1(mus ...
- Python基础-列表推导式
python中列表推导式有三种数据类型可用:列表,字典,集合 列表推导式书写形式: [表达式 for 变量 in 列表] 或者 [表达式 for 变量 in 列表 if 条件] 1,列表推导式 ...