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 ...
随机推荐
- ELK的使用
首先安装jdk,我这里使用open-jdk yum list all | grep jdk yum -y install java-1.8.0-openjdk-devel, java-1.8.0-op ...
- 关于jvm中的常量池和String.intern()理解
1. 首先String不属于8种基本数据类型,String是一个对象. 因为对象的默认值是null,所以String的默认值也是null:但它又是一种特殊的对象,有其它对象没有的一些特性. 2. ne ...
- $().bind()的返回值
var eleMenus = $("#choMenu a").bind("click", function (event){}); 此时eleMeuns的值是$ ...
- 代码题(1)—lower_bound和upper_bound算法
1.lower_bound:查找序列中的第一个出现的值大于等于val的位置 这个序列中可能会有很多重复的元素,也可能所有的元素都相同,为了充分考虑这种边界条件,STL中的lower_bound算法总体 ...
- appium-DesiredCapability详解与实战
DesiredCapability对启动app至关重要,是启动app前的准备工作.如果配置错误,app不会成功启动. DesiredCapability有appium公共健值对.Android专有和I ...
- linux命令学习笔记(12):more命令
more命令,功能类似 cat ,cat命令是整个文件的内容从上到下显示在屏幕上. more会以一页一页的显示方便 使用者逐页阅读,而最基本的指令就是按空白键(space)就往下一页显示,按 b 键就 ...
- 【leetcode刷题笔记】N-Queens
The n-queens puzzle is the problem of placing n queens on an n×n chessboard such that no two queens ...
- Linux syslogd
/********************************************************************************** * Linux syslogd ...
- DP小合集
1.Uva1625颜色的长度 dp[i][j]表示前一个串选到第i个 后一个串选到第j个 的最小价值 记一下还有多少个没有结束即dp2 记一下每个数开始和结束的位置 #include<cstdi ...
- 求解范围中 gcd(a,b)== prime 的有序对数
题目: 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的数对(x,y)有多少对. 输入: 一个整数N. 输出: 如题. Sample Input 4 Sample Output ...