QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#425745 | #990. Colorful Components | mazihang2022 | AC ✓ | 60ms | 3752kb | C++14 | 5.1kb | 2024-05-30 16:28:51 | 2024-05-30 16:28:52 |
Judging History
answer
#include<bits/stdc++.h>
#define ll long long
#define fir first
#define sec second
#define pii pair<int,int>
#define isz(v) (int)(v.size())
using namespace std;
const int maxn=305;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
namespace Solve {
struct mint {
int val;
mint():val(0) {}
mint(ll tval) {
if(-mod<=tval&&tval<2*mod) {
if(tval>=mod) {
tval-=mod;
}
if(tval<0) {
tval+=mod;
}
} else {
tval%=mod;
if(tval<0) {
tval+=mod;
}
}
val=tval;
}
mint& operator += (const mint &b) {
val=val+b.val>=mod?val+b.val-mod:val+b.val;
return *this;
}
mint& operator -= (const mint &b) {
val=val-b.val<0?val-b.val+mod:val-b.val;
return *this;
}
mint& operator *= (const mint &b) {
val=1ll*val*b.val>=mod?1ll*val*b.val%mod:val*b.val;
return *this;
}
mint& operator /= (const mint &b) {
*this*=b.inv();
return *this;
}
friend mint operator + (const mint &a,const mint &b) {
mint ans=a;
ans+=b;
return ans;
}
friend mint operator - (const mint &a,const mint &b) {
mint ans=a;
ans-=b;
return ans;
}
friend mint operator * (const mint &a,const mint &b) {
mint ans=a;
ans*=b;
return ans;
}
friend mint operator / (const mint &a,const mint &b) {
mint ans=a;
ans/=b;
return ans;
}
friend bool operator ! (const mint &v) {
return !v.val;
}
friend mint operator - (const mint &v) {
return mod-v;
}
friend mint& operator ++ (mint &v) {
v+=1;
return v;
}
friend mint operator ++ (mint &v,signed _) {
mint t=v;
v+=1;
return t;
}
friend mint& operator -- (mint &v) {
v-=1;
return v;
}
friend mint operator -- (mint &v,signed _) {
mint t=v;
v-=1;
return t;
}
friend bool operator < (const mint &a,const mint &b) {
return a.val<b.val;
}
friend bool operator == (const mint &a,const mint &b) {
return a.val==b.val;
}
friend bool operator != (const mint &a,const mint &b) {
return a.val!=b.val;
}
friend istream& operator >> (istream &is,mint &x) {
ll val;
is>>val;
x=mint(val);
return is;
}
friend ostream& operator << (ostream &os,const mint &x) {
os<<x.val;
return os;
}
mint qpow(ll y) const {
mint x=*this,ans=1;
if(y<0) {
y=-y;
x=inv();
}
while(y) {
if(y&1) {
ans*=x;
}
x*=x;
y>>=1;
}
return ans;
}
mint inv() const {
// mod must be a prime
return qpow(mod-2);
}
mint sqrt() {
mint a=*this;
auto check=[&](mint x) {
return x.qpow((mod-1)/2)==1;
};
static mt19937 rnd(73385);
mint b=rnd()%mod;
while(check(b*b-a)) {
b=rnd()%mod;
}
static mint val=b*b-a;
struct Complex {
mint real,imag;
Complex(mint treal=0,mint timag=0):real(treal),imag(timag) {}
Complex operator * (const Complex &rhs) {
return {real*rhs.real+imag*rhs.imag*val,real*rhs.imag+imag*rhs.real};
}
Complex& operator *= (const Complex &rhs) {
return *this=*this*rhs;
}
};
auto qpow=[&](Complex x,int y) {
Complex ans={1};
while(y) {
if(y&1) {
ans*=x;
}
x*=x;
y>>=1;
}
return ans;
};
mint ans=qpow({b,1},(mod+1)/2).real;
return min(ans,mod-ans);
}
};
mint qpow(mint x,ll y) {
return x.qpow(y);
}
mint inv(mint x) {
return x.inv();
}
mint invf[maxn];
mint fact[maxn];
int n,k,sn;
int cnt[maxn];
mint w[maxn];
mint f[maxn];
mint v[maxn];
mint g[maxn];
void clear() {
}
void prepare(int n) {
fact[0]=invf[0]=1;
for(int i=1;i<=n;i++) {
fact[i]=fact[i-1]*i;
}
invf[n]=inv(fact[n]);
for(int i=n-1;i>=1;i--) {
invf[i]=invf[i+1]*(i+1);
}
}
mint C(int n,int m) {
// assert(n>=0&&m>=0);
return n<m?0:fact[n]*invf[m]*invf[n-m];
}
mint A(int n,int m) {
// assert(n>=0&&m>=0);
return n<m?0:fact[n]*invf[n-m];
}
mint invs(int x) {
// assert(x>=0);
return !x?0:invf[x]*fact[x-1];
}
mint calc(int n) {
memset(g,0,sizeof(g));
g[0]=1;
for(int i=1;i<=n;i++) {
for(int j=1;j<=i;j++) {
g[i]+=g[i-j]*C(i-1,j-1)*sn*v[j]*j;
}
}
// cerr<<g[n]<<"?\n";
return g[n];
}
void main(int tid) {
cin>>n>>k,sn=n;
for(int i=1;i<=n;i++) {
int c;
cin>>c;
cnt[c]++;
}
// if(*max_element(cnt+1,cnt+n+1)==n) {
// cout<<(k==n?qpow(n,n-2):0)<<"\n";
// return ;
// }
w[1]=1;
for(int i=2;i<=n;i++) {
w[i]=qpow(i,i-2);
}
for(int n=1;n<=sn;n++) {
memset(f,0,sizeof(f));
f[0]=-1;
for(int i=1;i<=n;i++) {
for(int j=1;j<=k&&j<=i;j++) {
f[i]-=f[i-j]*C(i-1,j-1)*j*n*w[j];
}
}
v[n]=f[n]/n/n;
}
// cerr<<v[1]<<" "<<v[2]<<" "<<v[3]<<"\n";
mint ans=1;
for(int i=1;i<=n;i++) {
ans*=calc(cnt[i]);
}
// cout<<ans<<"\n";
cout<<ans/n/n<<"\n";
}
void init() {
prepare(maxn-1);
}
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int T=1;
// cin>>T;
Solve::init();
for(int t=1;t<=T;t++) {
Solve::main(t);
Solve::clear();
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 3752kb
input:
5 3 1 1 3 1 5
output:
125
result:
ok "125"
Test #2:
score: 0
Accepted
time: 0ms
memory: 3668kb
input:
4 2 2 1 1 1
output:
7
result:
ok "7"
Test #3:
score: 0
Accepted
time: 11ms
memory: 3684kb
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: 13ms
memory: 3696kb
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: 0ms
memory: 3616kb
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: 1ms
memory: 3700kb
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: 50ms
memory: 3684kb
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: 59ms
memory: 3628kb
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: 0
Accepted
time: 60ms
memory: 3668kb
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:
0
result:
ok "0"
Test #10:
score: 0
Accepted
time: 37ms
memory: 3752kb
input:
300 80 1 3 3 3 1 1 2 3 2 3 2 3 1 2 2 3 3 3 3 1 1 1 3 3 3 2 2 1 1 2 3 2 3 3 2 3 2 1 1 2 1 3 1 3 1 3 1 3 1 1 3 1 1 2 1 3 1 3 2 2 1 3 2 2 3 2 1 3 1 2 1 1 2 3 1 1 3 1 2 3 1 1 1 1 1 2 1 1 2 1 3 2 2 2 1 3 1 2 3 3 1 3 3 1 3 1 2 2 2 2 1 1 2 1 1 1 1 2 1 3 2 2 1 1 1 3 1 3 1 1 3 1 2 2 1 3 1 1 3 1 2 3 2 2 1 3 2...
output:
428950103
result:
ok "428950103"
Test #11:
score: 0
Accepted
time: 28ms
memory: 3620kb
input:
300 50 3 3 1 1 2 3 2 3 2 3 1 2 2 3 3 3 3 1 1 1 3 3 3 2 2 1 1 2 3 2 3 3 2 3 2 1 1 2 1 3 1 3 1 3 1 3 1 1 3 1 1 2 1 3 1 3 2 2 1 3 2 2 3 2 1 3 1 2 1 1 2 3 1 1 3 1 2 3 1 1 1 1 1 2 1 1 2 1 3 2 2 2 1 3 1 2 3 3 1 3 3 1 3 1 2 2 2 2 1 1 2 1 1 1 1 2 1 3 2 2 1 1 1 3 1 3 1 1 3 1 2 2 1 3 1 1 3 1 2 3 2 2 1 3 2 2 3...
output:
283683661
result:
ok "283683661"
Test #12:
score: 0
Accepted
time: 23ms
memory: 3632kb
input:
300 50 1 1 2 3 2 3 2 3 1 2 2 3 3 3 3 1 1 1 3 3 3 2 2 1 1 2 3 2 3 3 2 3 2 1 1 2 1 3 1 3 1 3 1 3 1 1 3 1 1 2 1 3 1 3 2 2 1 3 2 2 3 2 1 3 1 2 1 1 2 3 1 1 3 1 2 3 1 1 1 1 1 2 1 1 2 1 3 2 2 2 1 3 1 2 3 3 1 3 3 1 3 1 2 2 2 2 1 1 2 1 1 1 1 2 1 3 2 2 1 1 1 3 1 3 1 1 3 1 2 2 1 3 1 1 3 1 2 3 2 2 1 3 2 2 3 2 1...
output:
280919911
result:
ok "280919911"
Test #13:
score: 0
Accepted
time: 59ms
memory: 3688kb
input:
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 300 300 ...
output:
394671363
result:
ok "394671363"
Test #14:
score: 0
Accepted
time: 60ms
memory: 3684kb
input:
300 299 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:
0
result:
ok "0"