QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#866590 | #9735. Japanese Bands | ucup-team3646 | TL | 2826ms | 215512kb | C++20 | 6.0kb | 2025-01-22 16:24:21 | 2025-01-22 16:24:31 |
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) {
// ll res = 0;
// for(ll b = 1; b <= a; ++b) {
// ll tmp = binom(a, b) * modpow(b, n) % MOD;
// if(a % 2 != b % 2) tmp *= -1;
// tmp = (tmp + MOD) % MOD;
// res += tmp;
// res %= MOD;
// }
// return res;
return binom(n+c-1, a+c-1);
}
ll memoX[25][25],memoY[25][25];
void solve() {
ll X, Y, M, K; cin >> X >> Y >> M >> K;
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;
rep(i,25)rep(j,25){
memoX[i][j]=func(X,i,j);
memoY[i][j]=func(Y,i,j);
}
for(int bit=(1<<M)-1;bit>=0;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;
rep(i,M){
if(((bit>>i)&1)==0)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;
vll dp(M+1);
dp[0]=1;
rep(i,M){
if(cnt0[i]+cnt1[i]>0){
vll ndp(M+1);
rep(j,sm+1){
ndp[j+cnt0[i]]+=dp[j];
ndp[j+cnt1[i]]+=dp[j];
}
sm+=cnt0[i]+cnt1[i];
rep(j,M+1)dp[j]=ndp[j]%MOD;
}
}
rep(c1,sm+1){
int c2=sm-c1;
ll val=dp[c1];
ll p = __builtin_popcount(bit);
ans += memoX[c1+p][free] * memoY[c2+p][free] % MOD * val % MOD;
ans %= MOD;
// print(c1, c2, val);
}
}
}
cout << ans << '\n';
return;
// search all subset
ll res = 0;
for(ll bit = 0; bit < (1<<M); ++bit) {
if((bit & must) != must) continue;
vector<ll> col(M, 0);
rep(i, M) {
if(bit & (1<<i)) col[i] = 3;
}
vector<array<ll, 2>> vec;
queue<ll> que;
bool ng = 0;
rep(r, M) {
if(col[r]) continue;
ll r1 = 0, r2 = 0;
col[r] = 1, r1++;
que.push(r);
while(que.size()) {
ll v = que.front();
que.pop();
for(auto nv : graph[v]) {
if(col[nv] == col[v]) {ng = 1; break;}
if(col[nv]) continue;
col[nv] = 3 - col[v];
if(col[nv] == 1) r1++;
else r2++;
}
}
if(ng) break;
vec.push_back({min(r1, r2), max(r1, r2)});
}
// cout << bitset<10>(bit) << endl;
// for(auto [r1, r2] : vec) {
// cout << r1 << ' ' << r2 << endl;
// }
if(ng) continue;
ll s = vec.size();
vector<ll> dp(M+1, 0);
dp[0] = 1;
for(auto [a, b] : vec) {
vector<ll> ndp(M+1, 0);
rep(x, M+1) {
if(x - a >= 0) ndp[x] += dp[x-a];
if(x - b >= 0) ndp[x] += dp[x-b];
ndp[x] %= MOD;
}
swap(dp, ndp);
}
ll p = __builtin_popcount(bit);
rep(x, M+1) {
ll s = x + p, t = M - x;
// ll tmp = func(X, s) * func(Y, t) % MOD * dp[x] % MOD;
res += tmp;
}
res %= MOD;
}
cout << res << endl;
return;
}
int main() {
precalc();
ll T; cin >> T;
while(T--) {
solve();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 3ms
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: 0
Accepted
time: 693ms
memory: 215484kb
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:
ok 500 lines
Test #3:
score: 0
Accepted
time: 565ms
memory: 215488kb
input:
500 54748096 75475634 8 64 3 8 3 2 3 5 5 6 5 7 5 4 2 2 5 8 5 3 3 5 7 6 1 7 3 3 6 8 3 5 3 4 1 2 7 5 7 6 4 7 8 7 7 5 4 2 3 2 8 5 7 1 4 3 4 6 3 3 8 3 6 1 5 4 1 4 2 3 5 6 1 4 5 8 8 2 1 3 8 1 5 7 1 2 3 8 4 2 5 4 7 2 4 6 5 8 4 6 3 5 5 7 4 6 4 8 6 4 7 4 3 3 5 2 1 6 4 5 3 1 1 4 5 6 4 3 3 1 44007561 94403501...
output:
48662676 1 138912221 349671067 150052712 86775188 956490545 756234965 1 567881550 726030753 1 914856825 867349578 0 784807018 388018114 433007753 524010032 507842496 282218203 442034917 668340856 1 1 1 489645337 153477090 564466420 1673 0 390234222 604892803 264163973 601602052 135055881 27720 15744...
result:
ok 500 lines
Test #4:
score: 0
Accepted
time: 682ms
memory: 215248kb
input:
500 923264237 374288891 9 36 8 8 5 8 3 6 2 4 2 6 4 7 3 8 3 4 5 5 5 1 9 3 1 9 5 4 5 8 4 3 2 8 7 6 7 3 8 9 3 4 4 1 8 1 7 3 6 3 6 2 9 6 2 3 2 5 6 1 8 5 4 5 3 1 8 7 6 5 3 2 1 1 885955146 964950238 8 59 1 3 7 7 8 1 2 7 6 5 1 3 1 2 2 3 1 2 8 2 4 1 5 6 5 8 3 1 8 3 2 3 8 3 6 5 2 5 6 7 7 2 6 3 6 5 6 7 7 8 8 ...
output:
975815187 715739872 849684680 940532257 1 181582862 672614348 487379998 1 872849956 969457677 827780523 98088 1 496360790 568133531 231661973 1 981238143 13510 8 663802864 252 107191472 18522 415132978 697199493 116414735 1 10 912343690 81 583097677 223080594 33251656 117275734 1 419400938 630591794...
result:
ok 500 lines
Test #5:
score: 0
Accepted
time: 2455ms
memory: 215468kb
input:
500 1 9 7 21 4 3 4 5 4 3 4 5 4 5 4 7 4 7 4 2 4 3 4 6 4 1 4 7 4 5 4 1 4 2 4 2 4 2 4 1 4 2 4 6 4 6 192019400 315755530 10 56 8 10 9 7 9 6 8 4 3 4 1 6 8 7 9 10 8 7 5 7 2 4 5 7 1 6 9 7 2 4 1 4 2 7 5 7 5 7 2 10 3 7 8 4 8 4 3 4 5 7 9 6 2 10 3 4 8 10 9 4 5 10 2 4 8 7 5 4 5 7 9 4 8 6 1 7 5 4 8 10 3 4 9 7 8 ...
output:
84 668356110 663215632 0 0 578736401 597267922 0 33363799 1161 80106560 13486 379410136 346687579 215079152 954964869 0 749122504 842423167 0 739926379 822901144 642136628 770357778 886 370384128 817027991 684214806 463554366 759552089 16 384293072 744192004 475 443091214 298039661 815658191 7064088...
result:
ok 500 lines
Test #6:
score: 0
Accepted
time: 2826ms
memory: 215512kb
input:
500 365329221 412106895 9 25 3 8 3 9 3 6 3 4 3 4 3 5 3 2 3 9 3 4 3 8 3 7 3 7 3 4 3 7 3 5 3 4 3 1 3 6 3 2 3 2 3 1 3 2 3 7 3 2 3 9 777109873 324284579 2 4 2 1 2 1 2 1 2 1 203265013 578140767 9 46 4 7 4 3 4 9 4 8 4 6 4 9 4 8 4 3 4 6 4 9 4 1 4 6 4 2 4 1 4 3 4 2 4 2 4 9 4 2 4 8 4 1 4 1 4 3 4 2 4 6 4 2 4 ...
output:
437314267 339909689 523663972 939772260 15 294996 873351127 420170527 361605716 10 6317 1015 698532307 716316221 827134829 526287544 433650509 256800385 596882660 574424501 589351733 505841163 517919676 378556809 150786280 1 4103867 986751324 345294966 926479261 557962762 987 133774068 568046845 778...
result:
ok 500 lines
Test #7:
score: 0
Accepted
time: 1380ms
memory: 215468kb
input:
500 643910770 5887448 4 3 2 3 4 3 2 3 483024012 714786389 2 1 2 1 735205996 288464482 7 46 4 2 3 7 5 7 4 2 3 1 5 2 4 2 5 2 3 6 3 6 3 2 3 2 3 2 5 7 5 7 4 7 5 2 5 1 4 6 4 1 5 7 5 6 3 2 4 2 3 1 5 7 3 2 4 7 4 2 3 7 3 2 5 7 4 1 3 1 3 1 5 2 5 2 5 6 4 1 4 1 4 2 4 7 4 2 5 1 3 2 4 7 143954125 56574503 3 2 1 ...
output:
772480244 118770152 108641022 615067819 872673860 900276958 951405638 705098808 308078046 912436115 266068466 270465299 858128867 591071828 345756242 253238303 226837537 15366 16188333 896137863 637986485 386483060 601850323 980044594 35 951860846 97816824 379760475 813023811 973778941 948954783 749...
result:
ok 500 lines
Test #8:
score: 0
Accepted
time: 2611ms
memory: 215468kb
input:
500 72235422 449924898 2 4 2 1 2 1 2 1 2 1 826908873 897131377 8 44 7 6 7 5 4 2 7 1 7 2 4 5 7 6 4 2 4 8 7 2 4 3 4 3 4 1 4 5 7 1 7 1 4 3 4 2 7 1 4 3 4 2 4 8 7 8 4 8 7 8 7 1 7 5 7 2 4 2 7 2 4 5 7 6 4 5 7 6 7 8 4 3 7 3 7 3 4 2 4 6 4 8 4 1 4 3 4 5 398346835 179283845 3 4 2 1 2 1 2 1 3 1 146844881 503366...
output:
169993670 63971922 447978336 415747130 17523830 520716693 376879328 858277870 165 142685959 3399 88 339812162 591663632 856960754 861425853 527624471 210 960737551 318751600 197044810 0 0 578594559 980927816 1765 215406311 230128376 3178 155563197 347921715 794781090 928438409 129 890907226 21574705...
result:
ok 500 lines
Test #9:
score: -100
Time Limit Exceeded
input:
500 940751563 43705451 3 2 2 1 1 3 4 2 2 1 2 1 756411639 690869710 9 8 3 5 3 6 3 4 5 1 1 7 2 8 3 8 7 9 892465776 834697020 7 6 6 2 4 7 2 3 1 4 5 7 4 3 3649468 246352594 5 4 5 1 5 2 3 2 2 4 927157783 349300522 2 1 2 1 283790347 262369656 8 7 8 6 2 1 7 4 4 3 4 5 2 4 2 6 290590966 415217454 6 5 6 3 4 3...