CF 1042 E. Vasya and Magic Matrix
E. Vasya and Magic Matrix
http://codeforces.com/contest/1042/problem/E
题意:
一个n*m的矩阵,每个位置有一个元素,给定一个起点,每次随机往一个小于这个点位置走,走过去的值为欧几里得距离的平方,求期望的值。
分析:
逆推期望。
将所有值取出,按元素大小排序,然后最小的就是0,往所有大于它的转移即可,复杂度n^2,见下方考试代码。
考虑每个点,从所有小于它的元素转移。排序后,维护前缀和,可以做到O(1)转移。
$f[i] = \sum\limits_{j=1,val[j]<val[i]}f[j] + (x_j - x_i)^2 + (y_j - y_i) ^ 2$
$f[i] =\sum\limits_{j=1,val[j]<val[i]} f[j] + x_j^2 - 2x_jx_i + x_i^2 + y_j^2 - 2y_jy_i + y_i ^ 2$
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define fi(s) freopen(s,"r",stdin);
#define fo(s) freopen(s,"w",stdout);
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const LL mod = ;
const int N = ; struct Node {
int x, y, val;
bool zh;
bool operator < (const Node &A) const {
return val < A.val;
}
}A[N];
LL f[N], cnt[N], sumx[N], sumy[N], sumx2[N], sumy2[N]; LL ksm(LL a,LL b) {
LL ans = ;
while (b) {
if (b & ) ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod;
b >>= ;
}
return ans;
} inline void add(LL &x,LL y) { (x += y) >= mod ? (x -= mod) : x; }
inline void sub(LL &x,LL y) { (x -= y) < ? (x += mod) : x; } void solve2(int n) { A[].val = -;
for (int i=; i<=n; ++i) {
if (A[i].val == A[i - ].val) cnt[i] = cnt[i - ];
else cnt[i] = i - ;
sumx[i] = (sumx[i - ] + A[i].x) % mod;
sumy[i] = (sumy[i - ] + A[i].y) % mod;
sumx2[i] = (sumx2[i - ] + 1ll * A[i].x * A[i].x % mod) % mod;
sumy2[i] = (sumy2[i - ] + 1ll * A[i].y * A[i].y % mod) % mod;
} LL sum = , tmp = ;
for (int i=; i<=n; ++i) {
LL x2 = sumx2[cnt[i]];
LL y2 = sumy2[cnt[i]];
LL z1 = 1ll * sumx[cnt[i]] * % mod * A[i].x % mod;
LL z2 = 1ll * sumy[cnt[i]] * % mod * A[i].y % mod;
LL h1 = 1ll * cnt[i] * A[i].x % mod * A[i].x % mod;
LL h2 = 1ll * cnt[i] * A[i].y % mod * A[i].y % mod; add(f[i], x2); add(f[i], y2);
sub(f[i], z1); sub(f[i], z2);
add(f[i], h1); add(f[i], h2);
add(f[i], sum); f[i] = 1ll * f[i] * ksm(cnt[i], mod - ) % mod;
if (A[i].zh) {
cout << f[i]; return ;
}
add(tmp, f[i]); // 只有小于的时候才转移!!!
if (A[i].val < A[i + ].val) add(sum, tmp), tmp = ;
} } int main() {
int n = read(), m = read(), tot = ;
for (int i=; i<=n; ++i)
for (int j=; j<=m; ++j)
A[++tot].x = i, A[tot].y = j, A[tot].val = read(), A[tot].zh = false; int x = read(), y = read(), z = (x - ) * m + y;
A[z].zh = true; sort(A + , A + tot + ); solve2(tot);
return ;
}
比赛时代码,记录调试历程。
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
#include<cctype>
#include<set>
#include<vector>
#include<queue>
#include<map>
#define fi(s) freopen(s,"r",stdin);
#define fo(s) freopen(s,"w",stdout);
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const LL mod = ;
const int N = ; struct Node {
int x, y, val;
bool zh;
bool operator < (const Node &A) const {
return val < A.val;
}
}A[N]; LL inv[N], deg[N], f[N];
//double dp[N]; LL ksm(LL a,LL b) {
LL ans = ;
while (b) {
if (b & ) ans = 1ll * ans * a % mod;
a = 1ll * a * a % mod;
b >>= ;
}
return ans;
} LL Calc(int i,int j) {
return ((A[i].x - A[j].x) * (A[i].x - A[j].x) % mod + (A[i].y - A[j].y) * (A[i].y - A[j].y) % mod) % mod;
} void solve1(int n) {
for (int i=; i<=n; ++i) {
// cout << A[i].val << ": ";
if (deg[i]) {
// dp[i] = dp[i] / (double)(deg[i]);
f[i] = 1ll * ksm(deg[i], mod - ) * f[i] % mod;
}
if (A[i].zh) {
cout << f[i]; return ;
}
for (int j=i+; j<=n; ++j)
if (A[j].val > A[i].val) {
// dp[j] = dp[j] + dp[i] + Calc(i, j);
// cout << A[j].val << " " << Calc(i, j) <<"--";
f[j] = (f[j] + f[i] + Calc(i, j)) % mod;
deg[j] ++;
}
// puts("");
}
} int cnt[N], sumx[N], sumy[N], sumx2[N], sumy2[N]; inline void add(LL &x,LL y) { (x += y) >= mod ? (x -= mod) : x; }
inline void sub(LL &x,LL y) { (x -= y) < ? (x += mod) : x; } void solve2(int n) { A[].val = -;
for (int i=; i<=n; ++i) {
if (A[i].val == A[i - ].val) cnt[i] = cnt[i - ];
else cnt[i] = i - ;
sumx[i] = (sumx[i - ] + A[i].x) % mod;
sumy[i] = (sumy[i - ] + A[i].y) % mod;
sumx2[i] = (sumx2[i - ] + 1ll * A[i].x * A[i].x % mod) % mod;
sumy2[i] = (sumy2[i - ] + 1ll * A[i].y * A[i].y % mod) % mod;
} LL sum = , tmp = ;
for (int i=; i<=n; ++i) {
LL x2 = sumx2[cnt[i]];
LL y2 = sumy2[cnt[i]];
LL z1 = 1ll * sumx[cnt[i]] * % mod * A[i].x % mod;
LL z2 = 1ll * sumy[cnt[i]] * % mod * A[i].y % mod;
LL h1 = 1ll * cnt[i] * A[i].x % mod * A[i].x % mod;
LL h2 = 1ll * cnt[i] * A[i].y % mod * A[i].y % mod; add(f[i], x2); add(f[i], y2);
sub(f[i], z1); sub(f[i], z2);
add(f[i], h1); add(f[i], h2);
add(f[i], sum); f[i] = 1ll * f[i] * ksm(cnt[i], mod - ) % mod;
if (A[i].zh) {
cout << f[i]; return ;
}
add(tmp, f[i]); // 只有小于的时候才转移!!!
if (A[i].val < A[i + ].val) add(sum, tmp), tmp = ;
} } int main() {
int n = read(), m = read(), tot = ;
for (int i=; i<=n; ++i)
for (int j=; j<=m; ++j)
A[++tot].x = i, A[tot].y = j, A[tot].val = read(), A[tot].zh = false; int x = read(), y = read(), z = (x - ) * m + y;
A[z].zh = true; sort(A + , A + tot + ); // if (tot <= 1000) {
// solve1(tot) ;return 0;
// }
solve2(tot);
return ;
}
CF 1042 E. Vasya and Magic Matrix的更多相关文章
- CF1042E Vasya and Magic Matrix
感觉不会期望. 首先把所有格子按照权值从小到大排一下序,这样一共有$n * m$个元素,每个元素有三个属性$x, y, val$. 下文中的下标均为排序后的下标. 这样子我们就可以推出公式: $f_i ...
- Vasya and Magic Matrix CodeForces - 1042E (概率dp)
大意:给定n*m矩阵, 初始位置(r,c), 每一步随机移动到权值小于当前点的位置, 得分为移动距离的平方, 求得分期望. 直接暴力dp的话复杂度是O(n^4), 把距离平方拆开化简一下, 可以O(n ...
- CF1042E Vasya and Magic Matrix 题解
题目链接 思路分析 看到题目中 \(n,m \leq 1000\) ,故直接考虑 \(O(n^2)\) 级别做法. 我们先把所有的点按照 \(val\) 值从小到大排序,这样的话二维问题变成序列问题. ...
- 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 ...
随机推荐
- 20140322 卡迪夫城VS利物浦,拔出重剑,有惊无险
一.菱形442 起初在客战南安普顿的时候,罗杰斯启用了菱形442阵式,阵容和今天客战卡迪夫城几乎一样,只是格伦·约翰逊打左后卫,弗拉纳甘任职右后卫,目的是为了在客场抵御卢克·肖+拉拉纳.当时库蒂尼奥的 ...
- redis三节点sentinel部署
角色 ip port master 127.0.0.1 6379 slave-1 127.0.0.1 6380 slave-2 127. ...
- 4.30-5.1cf补题
//yy:拒绝转载!!! 悄悄告诉你,做题累了,去打两把斗地主就能恢复了喔~~~ //yy:可是我不会斗地主吖("'▽'") ~~~那就听两遍小苹果嘛~~~ 五一假期除了花时间建模 ...
- [原创] SiteServer 3.5 批量导入文章的SQL处理脚本
2005时做过一个小网站,当时是用ASP+Access做的,功能很简单,但里面的文章不少 现在就像把它转移到SS上来,重点就是如何导入文章 本来SS本身提供了批量导入功能,但对于在WEB上一次性导入一 ...
- BZOJ3999:[TJOI2015]旅游(树链剖分)
Description 为了提高智商,ZJY准备去往一个新世界去旅游.这个世界的城市布局像一棵树.每两座城市之间只有一条路径可 以互达.每座城市都有一种宝石,有一定的价格.ZJY为了赚取最高利益,她会 ...
- BZOJ 3744: Gty的妹子序列 【分块 + 树状数组 + 主席树】
任意门:https://www.lydsy.com/JudgeOnline/problem.php?id=3744 3744: Gty的妹子序列 Time Limit: 20 Sec Memory ...
- 【转】maven命令-P 参数引发的思考
序言: maven 命令:clean package -Dmaven.test.skip=true -P product 1.命令很简单是:清class文件,打包构建,跳过测试,注意最后一个 -P p ...
- php面试重要知识点,面试题
1.什么是引用变量,用什么符号定义引用变量? 概念:用不同的名称引用同一个变量的内容:用&符号定义. 例如: $a = range(0,100); $b = &$a; $b = ran ...
- 【翻译】Django Channels 官方文档 -- Tutorial
Django Channels 官方文档 https://channels.readthedocs.io/en/latest/index.html 前言: 最近课程设计需要用到 WebSocket,而 ...
- HDU 2018母牛的故事(类似斐波那契,找规律)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=2018 母牛的故事 Time Limit: 2000/1000 MS (Java/Others) ...