1. 程式人生 > >Vasya and Magic Matrix題解

Vasya and Magic Matrix題解

題目連結 一道期望DP題,先把矩陣的元素從小到大排序。f[i]表示期望 f[i]=j=1t(f[j]+(x[i]x[j])2+(y[i]y[j])2)t f[i]=\frac{\sum_{j=1}^{t}{(f[j]+(x[i]-x[j])^2+(y[i]-y[j])^2})}{t} 然後把式子轉換一下 f[i]=(f[j]+x[j]2+y[j]2)2x[i]×x[j]2y[i]×y[j]t+x[i]2+y[i]2 f[i]=\frac{\sum{(f[j]+x[j]^2+y[j]^2)}-2 x[i]\times \sum x[j]-2y[i]\times \sum y[j]}{t}+x[i]^2+y[i]^2

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
using namespace std;
const int mo=998244353;
const int N=1001;
struct node{int x,y;}p[N*N];
int
X,Y,n,m,a[N][N],tot,V; long long f,fs,xs,ys,fsw,xsw,ysw;//f[i]=(sigma(f[j]+(x[i]-x[j])^2+(y[i]-y[j])^2))/p int getint();//f[i]=(sigma(f[j]+x[j]^2+y[j]^2)-2*x[i]*sigma(x[j])-2*y[i]*sigma(y[j]))/p+x[i]^2+y[i]^2 bool cmp(node x,node y){return a[x.x][x.y]<a[y.x][y.y];} long long ks(long long t,long long k)
{ long long w; for(w=1;k;k>>=1,t=t*t%mo)if(k&1)w=w*t%mo; return w; } int main(){ //freopen("1.in","r",stdin); //freopen("1.out","w",stdout); int i,j,k,t; n=getint();m=getint(); for(i=1;i<=n;i++)for(j=1;j<=m;j++){ a[i][j]=getint(); p[++tot].x=i;p[tot].y=j; } X=getint();Y=getint(); sort(p+1,p+tot+1,cmp); for(i=1;i<=tot;i++)if(p[i].x==X&&p[i].y==Y){V=i;break;} for(i=1,j=0;i<=V;i++){ f=fs-2*p[i].x*xs%mo+-2*p[i].y*ys%mo+mo+mo; f%=mo; f=f*ks(j,mo-2)%mo; f+=p[i].x*p[i].x%mo*ks(j,mo-1);f%=mo; f+=p[i].y*p[i].y%mo*ks(j,mo-1);f%=mo; fsw+=f;fsw+=p[i].x*p[i].x%mo;fsw+=p[i].y*p[i].y%mo;fsw%=mo; xsw+=p[i].x;xs%=mo; ysw+=p[i].y;ys%=mo; if(a[p[i].x][p[i].y]<a[p[i+1].x][p[i+1].y]){ j=i; fs+=fsw;fs%=mo; xs+=xsw;xs%=mo; ys+=ysw;ys%=mo; fsw=xsw=ysw=0; } } printf("%lld",f); } int getint(){ int w=0,q=1; char c=getchar(); while((c>'9'||c<'0')&&c!='-')c=getchar(); if(c=='-')q=-1,c=getchar(); while(c>='0'&&c<='9')w=w*10+c-'0',c=getchar(); return w*q; }