QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#422835 | #990. Colorful Components | CharlieVinnie | AC ✓ | 102ms | 4932kb | C++20 | 4.2kb | 2024-05-27 19:42:44 | 2024-05-27 19:42:44 |
Judging History
answer
#include "bits/stdc++.h"
#ifdef DEBUG
#include "PrettyDebug.hpp"
#else
#define debug(...) [](){}()
#define debuga(...) [](){}()
#endif
#define For(i,a,b) for(int i=a;i<=b;i++)
#define Rev(i,a,b) for(int i=a;i>=b;i--)
#define Fin(file) freopen(file,"r",stdin)
#define Fout(file) freopen(file,"w",stdout)
using namespace std; typedef long long ll;
constexpr int N=305,mod=1e9+7;
namespace ModInt{
int qpow(int x,int y){int res=1;while(y){if(y&1)res=1ll*res*x%mod;x=1ll*x*x%mod;y>>=1;}return res;}
class Int{
int x;
public:
explicit Int(int _x=0) : x(_x) {}
Int& operator+= (const Int& O) { unsigned t=x+O.x; x=min(t,t-mod); return *this; }
Int& operator-= (const Int& O) { unsigned t=x-O.x; x=min(t,t+mod); return *this; }
Int& operator*= (const Int& O) { x=1ll*x*O.x%mod; return *this; }
Int operator- () { return Int(x==0?0:mod-x); }
friend Int operator+ (Int A,const Int& B) { return A+=B; }
friend Int operator- (Int A,const Int& B) { return A-=B; }
friend Int operator* (Int A,const Int& B) { return A*=B; }
friend Int inv(const Int& A) { return Int(qpow(A.x,mod-2)); }
template<class ...Args> void add(const Int& A,const Int& B,Args... rest) { add(rest...,Int(1ll*A.x*B.x%mod)); }
void add(const Int& A,const Int& B) { x=(x+1ll*A.x*B.x)%mod; }
template<class ...Args> friend Int mul(const Int& A,const Int& B,Args... rest) { return mul(rest...,Int(1ll*A.x*B.x%mod)); }
Int mul(const Int& A,const Int& B) { return Int(1ll*A.x*B.x%mod); }
friend istream& operator>> (istream& os,Int& O) { return os>>O.x; }
friend ostream& operator<< (ostream& os,const Int& O){
constexpr int LIM=1000;
if(O.x<=LIM) os<<O.x;
else if(O.x>=mod-LIM) os<<O.x<<"(-"<<mod-O.x<<")";
else{
int found=0;
For(v,1,LIM){
int iv=qpow(v,mod-2);
For(u,1,LIM) if(1ll*u*iv%mod==O.x) { found=1; os<<O.x<<'('<<u<<"/"<<v<<')'; goto OUT; }
For(u,1,LIM) if(mod-1ll*u*iv%mod==O.x) { found=1; os<<O.x<<"(-"<<u<<"/"<<v<<')'; goto OUT; }
}
OUT: if(!found) os<<O.x<<"(?)";
}
return os;
}
friend Int qpow(Int x,int y) { return Int(qpow(x.x,y)); }
friend Int ID(Int x) { return x.x&1?Int(mod-1):Int(1); }
int operator()() const { return x; }
};
Int ID(int x) { return x&1?Int(mod-1):Int(1); }
Int Norm(ll x) { unsigned t=x%mod; return Int(min(t,t+mod)); }
}
using namespace ModInt;
int n,K,cnt[N]; Int cp[N],ci[N],g[N],f[N][N],ans[N][N],tmp[N][N];
signed main(){
atexit([](){cerr<<"Time = "<<clock()<<" ms"<<endl;});
cin>>n>>K; For(i,1,n) { int x; cin>>x; cnt[x]++; }
cp[0]=Int(1); For(i,1,n) cp[i]=cp[i-1]*Int(i);
ci[n]=inv(cp[n]); Rev(i,n-1,0) ci[i]=ci[i+1]*Int(i+1);
For(j,1,K) tmp[1][j]=qpow(Int(j),j-1)*ci[j];
For(i,1,n) For(j,1,n) if(tmp[i][j]()) {
g[j]+=tmp[i][j]*(i==1?inv(Int(j)):qpow(Int(j),i-2))*ci[i];
if(i==n) continue;
For(k,1,min(n-j,K)){
tmp[i+1][j+k]-=qpow(Int(k),k-1)*ci[k]*tmp[i][j];
}
}
debuga(g,1,n);
For(k,1,n) if(cnt[k]) {
For(i,0,n+1) For(j,0,n+1) tmp[i][j]=Int(0);
tmp[0][0]=Int(1);
For(i,0,cnt[k]) For(j,0,cnt[k]) if(tmp[i][j]()) {
debug(i,j,tmp[i][j]);
if(j==cnt[k]) f[k][i]+=tmp[i][j]*cp[cnt[k]]*ci[i];
For(t,1,cnt[k]-j) tmp[i+1][j+t]+=Int(t)*g[t]*tmp[i][j];
}
debuga(f[k],1,cnt[k]);
}
ans[0][0]=Int(1);
For(k,0,n-1){
if(!cnt[k+1]) { For(i,0,n) ans[k+1][i]=ans[k][i];; continue; }
For(i,0,n) if(ans[k][i]()) {
For(j,1,min(n-i,cnt[k+1])) ans[k+1][i+j]+=f[k+1][j]*ans[k][i];
}
}
For(k,0,n) For(i,0,n) if(ans[k][i]()) debug(k,i,ans[k][i]);
Int Ans(0);
For(i,1,n) Ans+=ans[n][i]*(i==1?inv(Int(n)):qpow(Int(n),i-2));
debug(Ans);
cout<<Ans()<<'\n';
return 0;
}
// CONTINUE, NON-STOPPING, FOR CHARLIEY
// START TYPING IF YOU DON'T KNOW WHAT TO DO
// STOP TYPING IF YOU DON'T KNOW WHAT YOU'RE DOING
// Started Coding On: May 27 Mon, 19 : 07 : 05
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 4800kb
input:
5 3 1 1 3 1 5
output:
125
result:
ok "125"
Test #2:
score: 0
Accepted
time: 0ms
memory: 4756kb
input:
4 2 2 1 1 1
output:
7
result:
ok "7"
Test #3:
score: 0
Accepted
time: 10ms
memory: 4808kb
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: 4684kb
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: 4768kb
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: 3ms
memory: 4912kb
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: 84ms
memory: 4892kb
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: 97ms
memory: 4788kb
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: 97ms
memory: 4728kb
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: 52ms
memory: 4932kb
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: 34ms
memory: 4848kb
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: 34ms
memory: 4864kb
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: 101ms
memory: 4788kb
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: 102ms
memory: 4856kb
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"