JZOJ-senior-5945. 【NOIP2018模拟11.02】昆特牌(gwent)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/HuangXinyue1017/article/details/83821890

Time Limits: 1000 ms Memory Limits: 524288 KB Detailed Limits

Description

作为一个资深OIer,你被邀请到位于波兰的CDPR总部参观。但没想到你刚一到就遇到了麻烦。昆特牌的数据库发生了故障。原本昆特牌中有 k种卡牌和n 种阵营,为了平衡,每个阵营拥有的卡牌种数都是相等的,并且每个阵营的数据顺序排列。由于故障,卡牌数据被打乱了,每个阵营现在有ai 种卡牌。因为昆特牌即将迎来重大更新,每种牌的所属阵营并不重要,工程师只想尽快让每个阵营拥有相同数量的卡牌。由于数据库的结构原因,你每单位时间只能将一种牌向左边或右边相邻的一个阵营移动。作为OI选手,这自然是难不倒你,但作为一名卡牌游戏爱好者,你想知道最终的卡牌分布有多少种方案。两种方案不同当且仅当存在一种卡牌,它在两种方案中所属阵营不同。对998244353取模

Input

第一行一个整数T,表示数据组数。
接下来每组数据,第一行一个整数n ,第二行n个数,第i个数为ai ,意义见题目描述

Output

T行,每行一个数表示答案。

Sample Input

Sample Input1
3
3
2 1 3
3
1 2 3
3
3 2 1

Sample Input2
4
3
8 1 0
4
5 0 1 2
4
0 4 0 0
4
1 1 6 0

Sample Output

Sample Output1
3
9
9

样例解释
对于第一组数据,初始为{{1,2}{3}{4,5,6}}
移动结束后为
{{1,2}{3,4}{5,6}},{{1,2}{3,6}{4,5}},{{1,2}{3,5}{4,6}}

Sample Output2
1120
30
24
270

Data Constraint

在这里插入图片描述

Solution

考虑贪心的过程,发现形成了个DAG图,用拓扑序转移,组合数求答案

Code

#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cctype>

#define fo(i,a,b) for(int i=a;i<=b;++i)
#define fd(i,a,b) for(int i=a;i>=b;--i)
#define ll long long

using namespace std;

const int N=1010,K=1e6,P=998244353;
int T,n,num;
int a[N],b[N],d[N],q[N],last[N];
int jc[K+5],ny[K+5];
struct edge{int to,next,v;}e[2*N];

inline void read(int &n)
{
	int x=0,w=0; char ch=0;
	while(!isdigit(ch)) w|=ch=='-',ch=getchar();
	while(isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=getchar();
	n=w?-x:x;
}

int ksm(int a,int b)
{
	int s=1;
	for(;b;a=(ll)a*(ll)a%P,b>>=1)
		if(b&1) s=(ll)s*(ll)a%P;
	return s;
}

void pre()
{
	jc[0]=1;
	fo(i,1,K) jc[i]=(ll)jc[i-1]*(ll)i%P; 
	ny[K]=ksm(jc[K],P-2);
	fd(i,K-1,0) ny[i]=(ll)ny[i+1]*(ll)(i+1)%P;
}

int C(int n,int m)
{
	return (ll)jc[n]*(ll)ny[m]%P*(ll)ny[n-m]%P;
}

void link(int x,int y,int z)
{
	e[++num]=(edge){y,last[x],z},last[x]=num,d[y]++;
}

int main()
{
	freopen("gwent.in","r",stdin);
	freopen("gwent.out","w",stdout);
	pre();
	read(T);
	while(T--)
	{
		num=0,memset(last,0,sizeof(last));
		read(n);
		int s=0;
		fo(i,1,n) read(a[i]),s+=a[i];
		int v=s/n;
		fo(i,1,n) b[i]=a[i]-v;
		fo(i,1,n)
		{
			if(b[i]<0)
			{
				link(i+1,i,-b[i]);
				b[i+1]-=abs(b[i]);
				b[i]=0;
			}
			if(b[i]>0)
			{
				link(i,i+1,b[i]);
				b[i+1]+=b[i];
				b[i]=0;
			}
		}
		int l=0,r=0,ans=1;
		fo(i,1,n) if(!d[i]) q[++r]=i;
		while(l<r)
		{
			int x=q[++l];
			for(int w=last[x];w;w=e[w].next)
			{
				int y=e[w].to;
				ans=(ll)ans*(ll)C(a[x],e[w].v)%P;
				a[x]-=e[w].v,a[y]+=e[w].v,--d[y];
				if(!d[y]) q[++r]=y;
			}
		}
		printf("%d\n",ans);
	}
}

猜你喜欢

转载自blog.csdn.net/HuangXinyue1017/article/details/83821890