QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#37033#990. Colorful Componentszero4338WA 3ms3788kbC++2.1kb2022-06-30 09:00:062022-06-30 09:00:08

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2022-06-30 09:00:08]
  • 评测
  • 测评结果:WA
  • 用时:3ms
  • 内存:3788kb
  • [2022-06-30 09:00:06]
  • 提交

answer

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int read()
{
	int ret=0;bool f=0;char c=getchar();
	while(c>'9'||c<'0')f|=(c=='-'),c=getchar();
	while(c>='0'&&c<='9')ret=(ret<<3)+(ret<<1)+(c^48),c=getchar();
	return f?-ret:ret;
}
const int maxn=305,mod=1e9+7;
int n,k;
int cnt[maxn],ccnt[maxn];
int qpow(int a,int b){if(b<0)a=qpow(a,mod-2),b=-b;int ret=1;for(;b;b>>=1,a=(ll)a*a%mod)if(b&1)ret=(ll)ret*a%mod;return ret;}
class mint
{
	public:
	int v;
	mint()=default;
	mint(int _v):v(_v){}
	mint&operator+=(const mint &rhs){v+=rhs.v;if(v>=mod)v-=mod;return *this;}
	mint&operator-=(const mint &rhs){v-=rhs.v;if(v<0)v+=mod;return *this;}
	mint&operator*=(const mint &rhs){v=(ll)v*rhs.v%mod;return *this;}
	mint&operator/=(const mint &rhs){v=(ll)v*rhs.inv().v%mod;return *this;}	
	mint inv()const{return qpow(v,mod-2);}
	friend mint operator +(const mint &lhs,const mint &rhs){return (mint)lhs+=rhs;}
	friend mint operator -(const mint &lhs,const mint &rhs){return (mint)lhs-=rhs;}
	friend mint operator *(const mint &lhs,const mint &rhs){return (mint)lhs*=rhs;}
	friend mint operator /(const mint &lhs,const mint &rhs){return (mint)lhs/=rhs;}
	mint pow(int c){return qpow(v,c);}
};
mint ans,fac[maxn],inv[maxn],ifac[maxn];
mint solve(int c)
{
	vector<mint>ret(c+1),f(c+1);
	for(int i=1;i<=min(k,c);i++)f[i]=(n-c)*ifac[i-1]*qpow(i,i-2);
	
	for(int i=0;i<c;i++)f[i]=f[i+1]*(i+1);f.resize(c);
	ret[0]=1;
	auto add=[&](int p,mint v)
	{
		for(int i=p;i<c;i++)
			ret[i+1]+=v*f[i-p]*inv[i+1];
	};
	add(0,1);
	for(int i=1;i<c;i++)
		add(i,ret[i]);
	return ret[c]*fac[c];
}
int main()
{
	n=read();k=read();
	for(int i=1;i<=n;i++)cnt[read()]++;
	for(int i=1;i<=n;i++)ccnt[cnt[i]]++;
	if(ccnt[n]){printf("%d\n",k<=n?mint(n).pow(n-2).v:0);return 0;}
	fac[0]=1;for(int i=1;i<=n;i++)fac[i]=fac[i-1]*i;
	ifac[n]=fac[n].inv();for(int i=n-1;i>=0;i--)ifac[i]=ifac[i+1]*(i+1);
	for(int i=1;i<=n;i++)inv[i]=ifac[i]*fac[i-1];
	ans=1;
	for(int i=1;i<=n;i++)
		if(ccnt[i])ans*=solve(i).pow(ccnt[i]);
	ans*=mint(n).pow(n-2);
	for(int i=1;i<=n;i++)ans/=n-cnt[i];
	printf("%d\n",ans.v);
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3600kb

input:

5 3
1
1
3
1
5

output:

125

result:

ok "125"

Test #2:

score: 0
Accepted
time: 2ms
memory: 3668kb

input:

4 2
2
1
1
1

output:

7

result:

ok "7"

Test #3:

score: 0
Accepted
time: 3ms
memory: 3536kb

input:

300 16
2
2
2
4
5
1
5
3
5
4
2
1
4
4
4
5
2
1
5
4
3
4
5
3
5
5
1
3
1
1
2
5
5
3
3
2
5
2
3
2
2
4
2
2
2
4
4
2
2
4
1
3
3
4
1
3
3
4
3
4
3
5
5
4
3
3
1
2
1
2
5
2
2
4
3
3
5
3
2
4
3
5
1
4
5
5
2
3
2
3
4
4
5
5
5
5
4
5
3
2
4
4
4
3
5
3
1
1
3
5
5
4
5
2
5
5
5
2
2
2
3
1
5
4
1
4
3
5
1
4
4
2
5
2
2
4
5
3
4
3
3
4
2
5
1
1
3...

output:

540253743

result:

ok "540253743"

Test #4:

score: 0
Accepted
time: 2ms
memory: 3748kb

input:

300 20
2
7
4
5
1
10
3
10
9
2
6
4
9
4
5
2
6
10
9
8
4
10
8
5
5
1
3
1
1
7
5
5
3
8
7
10
2
3
2
7
4
7
7
7
9
9
2
7
9
6
3
3
4
1
8
8
4
3
4
8
5
10
9
3
3
6
7
6
7
10
7
2
9
3
3
10
3
7
4
8
10
1
4
5
10
2
3
7
3
4
9
5
10
5
10
9
5
3
2
9
4
4
3
10
3
6
1
8
5
10
4
5
7
10
5
5
7
2
7
3
1
5
4
1
9
3
5
1
9
4
7
10
2
2
4
10
8
9
...

output:

616258392

result:

ok "616258392"

Test #5:

score: 0
Accepted
time: 2ms
memory: 3600kb

input:

300 1
124
25
131
70
63
150
139
62
46
94
9
34
25
102
66
120
139
28
134
120
98
135
95
21
43
71
11
87
45
15
33
58
37
70
12
63
132
47
104
97
67
17
9
119
72
87
29
96
53
103
34
71
58
78
34
3
94
78
115
60
139
43
63
46
127
146
37
60
37
12
59
23
43
120
53
107
54
68
70
21
94
125
10
22
143
117
133
64
129
55
14...

output:

450350134

result:

ok "450350134"

Test #6:

score: 0
Accepted
time: 2ms
memory: 3656kb

input:

300 2
131
70
63
150
139
62
46
94
9
34
25
102
66
120
139
28
134
120
98
135
95
21
43
71
11
87
45
15
33
58
37
70
12
63
132
47
104
97
67
17
9
119
72
87
29
96
53
103
34
71
58
78
34
3
94
78
115
60
139
43
63
46
127
146
37
60
37
12
59
23
43
120
53
107
54
68
70
21
94
125
10
22
143
117
133
64
129
55
140
75
10...

output:

942808207

result:

ok "942808207"

Test #7:

score: 0
Accepted
time: 2ms
memory: 3716kb

input:

300 150
1
2
2
2
1
2
1
2
2
2
1
2
2
2
2
1
1
1
1
1
1
1
1
1
1
2
1
2
2
1
2
1
2
1
1
1
1
1
2
1
1
2
1
1
2
1
2
2
2
1
2
2
1
2
1
1
1
2
1
2
1
2
1
2
1
1
1
2
1
1
2
2
2
1
2
1
2
2
1
1
1
2
1
1
2
1
2
1
1
1
2
1
2
2
1
2
1
2
1
2
1
2
2
1
1
2
1
1
1
2
1
1
1
1
2
1
1
1
1
1
1
2
1
2
2
2
2
2
2
1
1
1
2
2
1
1
1
1
1
2
1
1
2
1
1
1
...

output:

169425341

result:

ok "169425341"

Test #8:

score: 0
Accepted
time: 0ms
memory: 3788kb

input:

300 290
2
2
1
2
1
2
2
2
1
2
2
2
2
1
1
1
1
1
1
1
1
1
1
2
1
2
2
1
2
1
2
1
1
1
1
1
2
1
1
2
1
1
2
1
2
2
2
1
2
2
1
2
1
1
1
2
1
2
1
2
1
2
1
1
1
2
1
1
2
2
2
1
2
1
2
2
1
1
1
2
1
1
2
1
2
1
1
1
2
1
2
2
1
2
1
2
1
2
1
2
2
1
1
2
1
1
1
2
1
1
1
1
2
1
1
1
1
1
1
2
1
2
2
2
2
2
2
1
1
1
2
2
1
1
1
1
1
2
1
1
2
1
1
1
1
2
...

output:

394671363

result:

ok "394671363"

Test #9:

score: -100
Wrong Answer
time: 2ms
memory: 3744kb

input:

300 290
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
300
...

output:

394671363

result:

wrong answer 1st words differ - expected: '0', found: '394671363'