QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#722196#2576. 麻将nullptr_qwq100 ✓94ms45356kbC++207.3kb2024-11-07 18:08:582024-11-07 18:08:58

Judging History

This is the latest submission verdict.

  • [2024-11-07 18:08:58]
  • Judged
  • Verdict: 100
  • Time: 94ms
  • Memory: 45356kb
  • [2024-11-07 18:08:58]
  • Submitted

answer

// Problem: #466. 【ZJOI2019】麻将
// Contest: UOJ
// URL: https://uoj.ac/problem/466
// Memory Limit: 512 MB
// Time Limit: 2000 ms
// Author: nullptr_qwq
// 
// Powered by CP Editor (https://cpeditor.org)

// 私は猫です

#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define mkp make_pair
#define fi first
#define se second
#define inf 1000000000
#define infll 1000000000000000000ll
#define pii pair<int,int>
#define rep(i,a,b,c) for(int i=(a);i<=(b);i+=(c))
#define per(i,a,b,c) for(int i=(a);i>=(b);i-=(c))
#define F(i,a,b) for(int i=a,i##end=b;i<=i##end;i++)
#define dF(i,a,b) for(int i=a,i##end=b;i>=i##end;i--)
#define cmh(sjy) while(sjy--)
#define lowbit(x) (x&(-x))
#define HH printf("\n")
#define eb emplace_back
#define poly vector<int>
#define SZ(x) ((int)x.size())
using namespace std;
ll read(){
	ll x=0,f=1;char c=getchar();
	while(c<'0'||c>'9'){if(c=='-') f=-1;c=getchar();}
	while(c>='0'&&c<='9') x=(x<<3)+(x<<1)+(c^48),c=getchar();
	return x*f;
}
namespace Fastio{struct Reader{template<typename T>Reader&operator>>(T&x){x=0;short f=1;char c=getchar();while(c<'0'||c>'9'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+(c^48),c=getchar();x*=f;return*this;}Reader&operator>>(double&x){x=0;double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(long double&x){x=0;long double t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(__float128&x){x=0;__float128 t=0;short f=1,s=0;char c=getchar();while((c<'0'||c>'9')&&c!='.'){if(c=='-')f*=-1;c=getchar();}while(c>='0'&&c<='9'&&c!='.')x=x*10+(c^48),c=getchar();if(c=='.')c=getchar();else{x*=f;return*this;}while(c>='0'&&c<='9')t=t*10+(c^48),s++,c=getchar();while(s--)t/=10.0;x=(x+t)*f;return*this;}Reader&operator>>(char&c){c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();return*this;}Reader&operator>>(char*str){int len=0;char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str[len++]=c,c=getchar();str[len]='\0';return*this;}Reader&operator>>(string&str){str.clear();char c=getchar();while(c==' '||c=='\n'||c=='\r')c=getchar();while(c!=' '&&c!='\n'&&c!='\r')str.push_back(c),c=getchar();return*this;}Reader(){}}cin;const char endl='\n';struct Writer{const int Setprecision=6;typedef int mxdouble;template<typename T>Writer&operator<<(T x){if(x==0){putchar('0');return*this;}if(x<0)putchar('-'),x=-x;static short sta[40];short top=0;while(x>0)sta[++top]=x%10,x/=10;while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(long double x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(long double)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(__float128 x){if(x<0)putchar('-'),x=-x;mxdouble _=x;x-=(__float128)_;static short sta[40];short top=0;while(_>0)sta[++top]=_%10,_/=10;if(top==0)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;putchar('.');for(int i=0;i<Setprecision;i++)x*=10;_=x;while(_>0)sta[++top]=_%10,_/=10;for(int i=0;i<Setprecision-top;i++)putchar('0');while(top>0)putchar(sta[top]+'0'),top--;return*this;}Writer&operator<<(char c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(const char*str){int cur=0;while(str[cur])putchar(str[cur++]);return*this;}Writer&operator<<(string str){int st=0,ed=str.size();while(st<ed)putchar(str[st++]);return*this;}Writer(){}}cout;}using namespace Fastio;
#define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
template<typename T>inline void chkmax(T &x,const T &y){ x=std::max(x,y); }
template<typename T>inline void chkmin(T &x,const T &y){ x=std::min(x,y); }
const int mod=998244353,maxn=1005;
inline int qpow(int x,ll y){
	int rt=1;
	for(;y;y>>=1,x=1ll*x*x%mod) if(y&1) rt=1ll*rt*x%mod;
	return rt;
}
inline void inc(int &x,const int y){ x=(x+y>=mod)?(x+y-mod):(x+y); }
inline void dec(int &x,const int y){ x=(x>=y)?(x-y):(x+mod-y); }
inline void mul(int &x,const int y){ x=1ll*x*y%mod; }
inline int add(const int x,const int y){ return (x+y>=mod)?(x+y-mod):(x+y); }
inline int sub(const int x,const int y){ return (x>=y)?(x-y):(x+mod-y); }
inline int prod(const int x,const int y){ return 1ll*x*y%mod; }
namespace combi{
	int fac[maxn],ifac[maxn],inv[maxn];
	void init(const int N){
		fac[0]=ifac[0]=inv[0]=1;
		F(i,1,N) fac[i]=1ll*fac[i-1]*i%mod;
		ifac[N]=qpow(fac[N],mod-2);
		dF(i,N-1,1) ifac[i]=1ll*ifac[i+1]*(i+1)%mod;
		F(i,1,N) inv[i]=1ll*ifac[i]*fac[i-1]%mod;
	}
	inline int C(const int n,const int m){
		if(m>n||n<0||m<0) return 0;
		return 1ll*fac[n]*ifac[m]%mod*ifac[n-m]%mod;
	}
}
using namespace combi;
struct State{
	int f[3][3][2],cnt=0;
	void R(){ memset(f,-1,sizeof f),f[0][0][0]=cnt=0; }
	bool operator<(const State &rhs)const{
		if(cnt^rhs.cnt)return cnt<rhs.cnt;
		F(i,0,2)F(j,0,2)F(k,0,1)if(f[i][j][k]!=rhs.f[i][j][k])return f[i][j][k]<rhs.f[i][j][k];
		return 0;
	}
	bool check(){
		if(cnt>=7)return 1;
		F(i,0,2)F(j,0,2)if(f[i][j][1]>=4)return 1;
		return 0;
	}
	State Trans(int x){
		State res; res.R(),res.cnt=cnt+(x>=2);
		F(i,0,2)F(j,0,2)F(k,0,1){
			const int val=f[i][j][k];
			if(val<0)continue;
			F(t,0,2)if(i+j+t<=x)chkmax(res.f[j][t][k],val+i+(x-i-j-t>=3));
			if(k)continue;
			F(t,0,2)if(i+j+t<=x-2)chkmax(res.f[j][t][1],val+i);
		}
		F(i,0,2)F(j,0,2)F(k,0,1)chkmin(res.f[i][j][k],4);
		return res;
	}
};
map<State,int>mp;
State q[5005];
int to[5005][5],tot;
void BFS(){
	State st; st.R();
	int qL=1,qR=0;
	q[++qR]=st,mp[st]=++tot;
	while(qL<=qR){
		State u=q[qL++];
		F(i,0,4){
			State v=u.Trans(i);
			if(v.check())continue;
			if(!mp.count(v))mp[v]=++tot,q[++qR]=v;
			to[mp[u]][i]=mp[v];
		}
	}
}
int n,siz[405],f[105][405][4005];
void solve(){
	cin>>n,init(maxn-3),BFS();
	F(_,1,13){ int x,y; cin>>x>>y,++siz[x]; }
	f[0][0][1]=1;
	F(i,1,n)F(j,0,i<<2)F(k,1,tot){
		const int val=f[i-1][j][k];
		if(!val)continue;
		F(l,siz[i],4)if(to[k][l])
			inc(f[i][j+l-siz[i]][to[k][l]],1ll*val*C(4-siz[i],l-siz[i])%mod*C(j+l-siz[i],l-siz[i])%mod*fac[l-siz[i]]%mod);
	}
	int ans=0;
	const int m=(n<<2)-13;
	F(i,0,m)F(j,1,tot)inc(ans,1ll*f[n][i][j]*fac[m-i]%mod);
	mul(ans,ifac[m]),cout<<ans;
}
signed main(){
	int zsy=1;
	F(____,1,zsy)solve();
}

详细


Pretests


Final Tests

Test #1:

score: 10
Accepted
time: 4ms
memory: 4408kb

input:

5
1 1
1 2
1 3
1 4
2 1
3 1
3 2
3 3
3 4
5 1
5 2
5 3
5 4

output:

570425346

result:

ok single line: '570425346'

Test #2:

score: 10
Accepted
time: 4ms
memory: 4456kb

input:

5
1 3
2 4
4 3
5 3
3 1
5 4
4 2
1 4
4 4
3 3
3 2
1 1
2 2

output:

713031682

result:

ok single line: '713031682'

Test #3:

score: 10
Accepted
time: 5ms
memory: 5240kb

input:

13
1 3
12 1
12 2
1 4
13 3
13 2
3 2
13 1
1 1
3 4
3 3
1 2
12 3

output:

868236068

result:

ok single line: '868236068'

Test #4:

score: 10
Accepted
time: 5ms
memory: 5068kb

input:

13
1 1
13 1
2 3
1 3
3 1
3 4
2 1
13 4
1 4
12 2
13 3
12 3
3 2

output:

234399821

result:

ok single line: '234399821'

Test #5:

score: 10
Accepted
time: 5ms
memory: 5068kb

input:

13
2 3
2 1
12 2
13 2
2 2
1 2
13 3
12 4
13 4
3 4
3 1
12 3
1 3

output:

892522858

result:

ok single line: '892522858'

Test #6:

score: 10
Accepted
time: 19ms
memory: 21540kb

input:

81
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 1
12 1
13 1

output:

152635587

result:

ok single line: '152635587'

Test #7:

score: 10
Accepted
time: 40ms
memory: 26000kb

input:

94
1 1
2 1
3 1
4 1
5 1
6 1
7 1
8 1
9 1
10 1
11 1
12 1
13 1

output:

339459976

result:

ok single line: '339459976'

Test #8:

score: 10
Accepted
time: 25ms
memory: 20672kb

input:

96
1 2
1 3
1 4
1 1
2 2
2 3
2 4
2 1
3 2
3 3
3 4
3 1
4 2

output:

230028597

result:

ok single line: '230028597'

Test #9:

score: 10
Accepted
time: 18ms
memory: 18360kb

input:

83
1 2
1 3
1 4
1 1
2 2
2 3
2 4
2 1
3 2
3 3
3 4
3 1
4 2

output:

967250236

result:

ok single line: '967250236'

Test #10:

score: 10
Accepted
time: 94ms
memory: 45356kb

input:

100
67 1
26 4
12 4
11 4
30 4
29 1
1 4
13 3
30 1
64 2
63 1
20 2
32 4

output:

345132636

result:

ok single line: '345132636'

Extra Test:

score: 0
Extra Test Passed