QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#866588 | #9735. Japanese Bands | ucup-team3646 | WA | 826ms | 215520kb | C++23 | 4.6kb | 2025-01-22 16:23:20 | 2025-01-22 16:23:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i, n) for(ll i = 0; i < (n); ++i)
#define rep2(i, l, r) for(ll i = (l); i < (r); ++i)
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
#define all(A) A.begin(), A.end()
#define elif else if
using pii = pair<ll, ll>;
bool chmin(auto &a, auto b) {return a > b ? a = b, 1 : 0;}
bool chmax(auto &a, auto b) {return a < b ? a = b, 1 : 0;}
struct IOS {
IOS() {
cin.tie(0);
ios::sync_with_stdio(0);
}
} iosetup;
template<class T>
void print(vector<T> a) {
for(auto x : a) cout << x << ' ';
cout << endl;
}
void print(auto x) {
cout << x << endl;
}
template<class Head, class... Tail>
void print(Head &&head, Tail &&...tail) {
cout << head << ' ';
print(forward<Tail>(tail)...);
}
const ll MOD = 1e9 + 7;
ll modpow(ll x, ll k, ll m = MOD) {
ll v = 1;
while(k) {
if(k & 1) v = v * x % m;
x = x * x % m;
k >>= 1;
}
return v;
}
int len = 2e5;
vll fac, finv;
void precalc(ll m = MOD) {
fac.assign(len, 1), finv.assign(len, 1);
rep2(i, 2, len) fac[i] = fac[i-1] * i % m;
finv[len-1] = modpow(fac[len-1], m-2, m);
for(int i = len-2; i >= 0; i--) finv[i] = finv[i+1] * (i+1) % m;
return;
}
ll binom(ll n, ll k, ll m = MOD) {
if(n < 0 || k < 0 || k > n) return 0;
if(k < 100) {
// n * ... * (n-k+1) / k!
ll ans = 1;
rep(i, k) ans *= (n - i), ans %= m;
ans *= finv[k], ans %= m;
return ans;
}
else {
return fac[n] * finv[k] % m * finv[n-k] % m;
}
}
// n mai ari, [1, a] wo tukau toki no sousuu
ll func(ll n, ll a, ll c) {
return binom(n+c-1, a+c-1);
}
ll memofuncX[40][40];
ll memofuncY[40][40];
void solve() {
ll X, Y, M, K; cin >> X >> Y >> M >> K;
rep(a,40)rep(c,40){
memofuncX[a][c]=func(X,a,c);
memofuncY[a][c]=func(Y,a,c);
}
vector<array<ll, 2>> cards(K);
rep(i, K) {
ll a, b; cin >> a >> b;
a--, b--;
cards[i] = {min(a, b), max(a, b)};
}
sort(all(cards));
cards.erase(unique(all(cards)), cards.end());
vi deg(M,0);
for(auto [a,b]:cards){
deg[a]++;
deg[b]++;
}
vi ID(M,0);
int tmp=0;
rep(i,M){
if(deg[i]>0){
ID[i]=tmp;
tmp++;
}
}
int free=M-tmp;
M=tmp;
ll must = 0;
vector graph(M, vector<ll>());
for(auto [a, b] : cards) {
if(a == b) must |= (1<<ID[a]);
else {
graph[ID[a]].emplace_back(ID[b]);
graph[ID[b]].emplace_back(ID[a]);
}
}
vi NG(1<<M,0);
vector<vector<pair<int,int>>>memo(1<<M,vector<pair<int,int>>(M,{-1,-1}));
ll ans = 0;
for(int bit=(1<<M)-1;bit>=0;bit--){
ll p = __builtin_popcount(bit);
rep(i,M){
if(((bit>>i)&1)==0){
NG[bit]|=NG[bit|(1<<i)];
}
}
if(NG[bit])continue;
if((bit&must)!=must)continue;
if(bit!=(1<<M)-1){
int add=-1;
int ms=1000;
rep(i,M){
if(((bit>>i)&1)==0){
if(ms>graph[i].size()){
ms=graph[i].size();
add=i;
}
}
}
vi flip(M,-1);
int bit0=bit|(1<<add);
memo[bit]=memo[bit0];
for(auto v:graph[add]){
auto [grp,col]=memo[bit0][v];
if(grp==-1)continue;
if(flip[grp]==-1){
flip[grp]=1^col;
}
if(flip[grp]!=1^col){
NG[bit]=1;
}
}
memo[bit][add]={add,0};
rep(i,M){
auto [grp,col]=memo[bit][i];
if(grp==-1)continue;
if(flip[grp]!=-1){
memo[bit][i]={add,col^flip[grp]};
}
}
}
if(NG[bit]==0){
vi cnt0(M,0),cnt1(M,0);
rep(i,M){
auto [grp,col]=memo[bit][i];
if(grp!=-1){
if(col==0)cnt0[grp]++;
else cnt1[grp]++;
}
}
ll sm=0;
ll Z=M-p-free;
ll ms=0;
vll dp(M);
dp[0]=1;
rep(i,M){
if(cnt0[i]+cnt1[i]>0){
int nms=ms+max(cnt0[i],cnt1[i]);
vll ndp(nms+1);
rep(j,ms+1){
ndp[j+cnt0[i]]+=dp[j];
ndp[j+cnt1[i]]+=dp[j];
}
ms+=max(cnt0[i],cnt1[i]);
sm+=cnt0[i]+cnt1[i];
rep(j,nms+1)dp[j]=ndp[j]%MOD;
}
}
rep(c1,sm+1){
int c2=sm-c1;
ll val=dp[c1];
ans += memofuncX[c1+p][free] * memofuncY[c2+p][free] % MOD * val % MOD;
ans %= MOD;
// print(c1, c2, val);
}
}
}
cout << ans << '\n';
return;
}
int main() {
precalc();
ll T; cin >> T;
while(T--) {
solve();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 6528kb
input:
3 2 3 3 3 2 3 1 1 2 3 2 2 2 1 1 1 1 1 10 2 1 2 1 3
output:
6 4 0
result:
ok 3 lines
Test #2:
score: -100
Wrong Answer
time: 826ms
memory: 215520kb
input:
500 481199252 336470888 8 58 4 7 7 6 4 7 2 6 2 6 2 7 8 3 8 7 8 1 2 6 2 6 1 2 1 5 3 1 6 4 2 4 4 7 2 6 2 3 7 1 4 4 5 1 2 7 6 7 8 1 8 7 5 6 4 2 4 2 8 5 5 4 6 8 5 6 3 8 1 7 1 6 7 1 5 6 6 3 1 1 2 7 3 3 1 5 5 5 8 7 3 4 6 3 8 8 7 4 1 6 2 1 8 7 4 5 2 7 3 5 2 6 4 5 4 3 2 6 2 2 2 1 1 1 500330171 987581927 10 ...
output:
724920553 11 966099120 846759476 528107862 1 245313614 144990327 1 269113412 946380890 1 0 996348464 376698469 453289929 53346774 238565800 260837053 956196844 0 487514263 842710229 8376584 16 300260118 933415828 808801372 1 612901047 137099259 14974173 0 531418108 1 522718622 264859767 1 1 59318545...
result:
wrong answer 63rd lines differ - expected: '956713478', found: '409115415'