AtCoder Beginner Contest 199 F - Graph Smoothing

https://atcoder.jp/contests/abc199/tasks/abc199_f

我们发现可以知道每一次随机选边后的值的变化

假设i与deg条边,那么ai= \sum{(ai+aj)/2 /m} + (m-deg)/m * ai

那么就相当于每次随机选边后都有一个与上一次的值有关的等式

k=1e9,所以用矩阵快速幂,复杂度是n^3 logk

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

const int maxl=110;
const int mod=1e9+7;

int n,m,k,cnt,tot,cas;
ll a[maxl],deg[maxl],ans[maxl];
bool g[maxl][maxl];
bool vis[maxl];
char s[maxl];

struct matrix
{
	ll a[110][110];
	matrix()
	{
		memset(a,0,sizeof(a));
	}
	inline void clear()
	{
		memset(a,0,sizeof(a));
	}
	matrix operator * (const matrix &b)const
	{
		matrix c;
		for(int i=1;i<=n;i++)
			for(int j=1;j<=n;j++)
				for(int k=1;k<=n;k++)
					c.a[i][k]=(c.a[i][k]+a[i][j]*b.a[j][k])%mod;
		return c;
	}
};

inline ll qp(ll a,ll b)
{
	ll ans=1,cnt=a;
	while(b)
	{
		if(b&1)
			ans=ans*cnt%mod;
		cnt=cnt*cnt%mod;
		b>>=1;
	}
	return ans;
}
inline matrix qp(matrix a,ll b)
{
	matrix ans,cnt=a;
	for(int i=1;i<=n;i++) ans.a[i][i]=1;
	while(b)
	{
		if(b&1)
			ans=ans*cnt;
		cnt=cnt*cnt;
		b>>=1;
	}
	return ans;
}

inline void prework()
{
	scanf("%d%d%d",&n,&m,&k);
	for(int i=1;i<=n;i++)
		scanf("%lld",&a[i]);
	for(int i=1;i<=m;i++)
	{
		int u,v;
		scanf("%d%d",&u,&v);
		g[u][v]=g[v][u]=true;
		deg[u]++;deg[v]++;
	}
}

inline void mainwork()
{
	matrix c;
	for(int j=1;j<=n;j++)
	{
		c.a[j][j]=1ll*(2*m-deg[j])*qp(2*m,mod-2)%mod;
		for(int i=1;i<=n;i++)
		if(g[j][i])
			c.a[i][j]=qp(2*m,mod-2);
	}
	c=qp(c,k);
	for(int j=1;j<=n;j++)
	{
		for(int i=1;i<=n;i++)
			ans[j]=(ans[j]+a[i]*c.a[i][j])%mod;
		printf("%lld\n",ans[j]);
	}
}

inline void print()
{
		

}

int main()
{
	int t=1;
	//scanf("%d",&t);
	for(cas=1;cas<=t;cas++)
	{
		prework();
		mainwork();
		print();
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值