QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#275560 | #7257. Paint | PetroTarnavskyi# | AC ✓ | 2ms | 3936kb | C++20 | 3.1kb | 2023-12-04 20:35:49 | 2023-12-04 20:35:50 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define RFOR(i, b, a) for(int i = (b) - 1; i >= (a); i--)
#define SZ(a) (int)a.size()
#define ALL(a) a.begin(), a.end()
#define PB push_back
#define MP make_pair
#define F first
#define S second
typedef long long LL;
#define int LL
typedef vector<int> VI;
typedef pair<int, int> PII;
typedef double db;
const int mod = 1e9 + 7;
void updAdd(int& a, int b)
{
a += b;
if(a >= mod)
a -= mod;
}
void updSub(int& a, int b)
{
a -= b;
if(a < 0)
a += mod;
}
int mult(int a, int b)
{
return (LL)a * b % mod;
}
int binpow(int a, int n)
{
int res = 1;
while(n)
{
if(n & 1)
res = mult(res, a);
a = mult(a, a);
n /= 2;
}
return res;
}
int inv(int x)
{
return binpow(x, mod - 2);
}
int C(int n, int m)
{
int ans = 1;
FOR(i, 0, m)
{
ans = mult(ans, n - i);
ans = mult(ans, inv(i + 1));
}
return ans;
}
int solve(int n, VI lens)
{
n -= SZ(lens) - 1;
FOR(i, 0, SZ(lens))
n -= lens[i];
if(n < 0)
return 0;
int cnt = 2 * SZ(lens) + 1;
return C(n + cnt - 1, cnt - 1);
}
set<vector<PII>> calculated;
int solve(int n, vector<PII> lens)
{
if(calculated.count(lens) == 1)
return 0;
calculated.insert(lens);
int ans = 0;
FOR(mask, 0, 1 << SZ(lens))
{
VI cur(SZ(lens));
FOR(i, 0, SZ(lens))
{
if((mask >> i) & 1)
cur[i] = lens[i].S;
else
cur[i] = lens[i].F;
}
int val = solve(n, cur);
if(__builtin_popcount(mask) % 2 == 0)
updAdd(ans, val);
else
updSub(ans, val);
}
return ans;
}
vector<PII> possible[4];
vector<PII> genVec;
int resRecalc = 0;
void rec(int pos, int sz, int n)
{
if(pos == sz)
{
updAdd(resRecalc, solve(n, genVec));
return;
}
for(auto val : possible[pos])
{
genVec.PB(val);
rec(pos + 1, sz, n);
genVec.pop_back();
}
}
VI vals;
int recalc(int n, vector<PII> lens)
{
resRecalc = 0;
FOR(i, 0, SZ(lens))
{
possible[i].clear();
int prev = lens[i].F;
FOR(j, 0, SZ(vals))
{
if(vals[j] <= prev)
continue;
possible[i].PB(MP(prev, vals[j]));
prev = vals[j];
if(vals[j] == lens[i].S)
break;
}
}
rec(0, SZ(lens), n);
return resRecalc;
}
int32_t main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout << fixed << setprecision(15);
int n, k;
cin >> n >> k;
VI a(k);
FOR(i, 0, k)
cin >> a[i];
FOR(mask, 1, 1 << k)
{
int sum = 0;
FOR(i, 0, k)
if((mask >> i) & 1)
sum += a[i];
vals.PB(sum);
vals.PB(sum + 1);
}
sort(ALL(vals));
int ans = 0;
VI idx(k);
iota(ALL(idx), 0);
do{
FOR(mask, 0, 1 << (k - 1))
{
vector<PII> lens = {MP(a[idx[0]], a[idx[0]] + 1)};
FOR(i, 0, k - 1)
{
if((mask >> i) & 1)
{
lens.back().F = max(lens.back().F, a[idx[i + 1]]);
lens.back().S += a[idx[i + 1]];
}
else
lens.PB(MP(a[idx[i + 1]], a[idx[i + 1]] + 1));
}
updAdd(ans, recalc(n, lens));
}
}while(next_permutation(ALL(idx)));
cout << ans << endl;
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3572kb
input:
10 2 1 1
output:
55
result:
ok 1 number(s): "55"
Test #2:
score: 0
Accepted
time: 1ms
memory: 3588kb
input:
1000000000 4 2015 2015 123456789 27
output:
782767239
result:
ok 1 number(s): "782767239"
Test #3:
score: 0
Accepted
time: 0ms
memory: 3824kb
input:
1000000000 4 158260522 877914575 602436426 24979445
output:
660970994
result:
ok 1 number(s): "660970994"
Test #4:
score: 0
Accepted
time: 1ms
memory: 3680kb
input:
1000000000 4 861648772 623690081 433933447 476190629
output:
285087118
result:
ok 1 number(s): "285087118"
Test #5:
score: 0
Accepted
time: 1ms
memory: 3900kb
input:
1000000000 4 262703497 211047202 971407775 628894325
output:
705252352
result:
ok 1 number(s): "705252352"
Test #6:
score: 0
Accepted
time: 1ms
memory: 3928kb
input:
1000000000 4 731963982 822804784 450968417 430302156
output:
442542649
result:
ok 1 number(s): "442542649"
Test #7:
score: 0
Accepted
time: 1ms
memory: 3716kb
input:
1000000000 4 982631932 161735902 880895728 923078537
output:
918022647
result:
ok 1 number(s): "918022647"
Test #8:
score: 0
Accepted
time: 1ms
memory: 3580kb
input:
1000000000 4 707723857 189330739 910286918 802329211
output:
647369460
result:
ok 1 number(s): "647369460"
Test #9:
score: 0
Accepted
time: 1ms
memory: 3648kb
input:
1000000000 4 404539679 303238506 317063340 492686568
output:
212860747
result:
ok 1 number(s): "212860747"
Test #10:
score: 0
Accepted
time: 1ms
memory: 3896kb
input:
1000000000 4 773361868 125660016 650287940 839296263
output:
89880693
result:
ok 1 number(s): "89880693"
Test #11:
score: 0
Accepted
time: 1ms
memory: 3932kb
input:
1000000000 4 462224593 492601449 384836991 191890310
output:
650499095
result:
ok 1 number(s): "650499095"
Test #12:
score: 0
Accepted
time: 1ms
memory: 3900kb
input:
1000000000 4 576823355 782177068 404011431 818008580
output:
633872265
result:
ok 1 number(s): "633872265"
Test #13:
score: 0
Accepted
time: 1ms
memory: 3916kb
input:
1000000000 4 954291757 160449218 155374934 840594328
output:
800323543
result:
ok 1 number(s): "800323543"
Test #14:
score: 0
Accepted
time: 1ms
memory: 3676kb
input:
1000000000 4 164163676 797829355 138996221 501899080
output:
107066835
result:
ok 1 number(s): "107066835"
Test #15:
score: 0
Accepted
time: 1ms
memory: 3568kb
input:
1000000000 4 353195922 545531545 910748511 350034067
output:
250355397
result:
ok 1 number(s): "250355397"
Test #16:
score: 0
Accepted
time: 1ms
memory: 3708kb
input:
1000000000 4 913575467 470338674 824284691 533206504
output:
55628645
result:
ok 1 number(s): "55628645"
Test #17:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
1000000000 4 180999835 31262034 138344965 677959980
output:
742514685
result:
ok 1 number(s): "742514685"
Test #18:
score: 0
Accepted
time: 1ms
memory: 3892kb
input:
1000000000 4 131381221 846045895 208032501 346948152
output:
140767748
result:
ok 1 number(s): "140767748"
Test #19:
score: 0
Accepted
time: 1ms
memory: 3704kb
input:
1000000000 4 973708325 506147731 893229302 816248153
output:
124170944
result:
ok 1 number(s): "124170944"
Test #20:
score: 0
Accepted
time: 2ms
memory: 3684kb
input:
1000000000 4 298309896 37119022 455797489 215208399
output:
384985131
result:
ok 1 number(s): "384985131"
Test #21:
score: 0
Accepted
time: 1ms
memory: 3896kb
input:
1000000000 4 190870607 189766466 554374432 137831502
output:
706408699
result:
ok 1 number(s): "706408699"
Test #22:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
1000000000 4 694053968 47628333 469187475 409233792
output:
946069861
result:
ok 1 number(s): "946069861"
Test #23:
score: 0
Accepted
time: 0ms
memory: 3840kb
input:
810900084 1 804759886
output:
6140199
result:
ok 1 number(s): "6140199"
Test #24:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
799979694 1 44444714
output:
755534981
result:
ok 1 number(s): "755534981"
Test #25:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
575207354 1 412717848
output:
162489507
result:
ok 1 number(s): "162489507"
Test #26:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
666538409 1 7351644
output:
659186766
result:
ok 1 number(s): "659186766"
Test #27:
score: 0
Accepted
time: 0ms
memory: 3600kb
input:
597902643 2 68231372 24256196
output:
851233803
result:
ok 1 number(s): "851233803"
Test #28:
score: 0
Accepted
time: 0ms
memory: 3512kb
input:
14723492 2 13906140 3621469
output:
33369643
result:
ok 1 number(s): "33369643"
Test #29:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
234828607 2 163301522 43053043
output:
578991015
result:
ok 1 number(s): "578991015"
Test #30:
score: 0
Accepted
time: 0ms
memory: 3832kb
input:
537560717 2 139395612 788466
output:
719681904
result:
ok 1 number(s): "719681904"
Test #31:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
741106986 3 4170134 346022697 55882702
output:
612119221
result:
ok 1 number(s): "612119221"
Test #32:
score: 0
Accepted
time: 0ms
memory: 3520kb
input:
279509806 3 81928315 44340994 261281371
output:
947454307
result:
ok 1 number(s): "947454307"
Test #33:
score: 0
Accepted
time: 0ms
memory: 3860kb
input:
322056553 3 151054155 57012055 247450876
output:
713098369
result:
ok 1 number(s): "713098369"
Test #34:
score: 0
Accepted
time: 0ms
memory: 3608kb
input:
27172325 3 14239685 24435929 18116814
output:
935612802
result:
ok 1 number(s): "935612802"
Test #35:
score: 0
Accepted
time: 1ms
memory: 3936kb
input:
751627327 4 390801259 338744059 458549842 211527743
output:
559576491
result:
ok 1 number(s): "559576491"
Test #36:
score: 0
Accepted
time: 1ms
memory: 3640kb
input:
940778720 4 213618218 550622799 492668232 613881610
output:
440703144
result:
ok 1 number(s): "440703144"
Test #37:
score: 0
Accepted
time: 1ms
memory: 3568kb
input:
203226467 4 4512149 196127038 79016766 169450893
output:
45766908
result:
ok 1 number(s): "45766908"
Test #38:
score: 0
Accepted
time: 0ms
memory: 3720kb
input:
206347466 4 5397973 121481549 96300411 89338511
output:
644458563
result:
ok 1 number(s): "644458563"
Test #39:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
10 1 5
output:
6
result:
ok 1 number(s): "6"
Test #40:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
8 1 4
output:
5
result:
ok 1 number(s): "5"
Test #41:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
7 1 4
output:
4
result:
ok 1 number(s): "4"
Test #42:
score: 0
Accepted
time: 0ms
memory: 3640kb
input:
8 1 8
output:
1
result:
ok 1 number(s): "1"
Test #43:
score: 0
Accepted
time: 0ms
memory: 3628kb
input:
10 2 1 9
output:
3
result:
ok 1 number(s): "3"
Test #44:
score: 0
Accepted
time: 0ms
memory: 3624kb
input:
8 2 8 2
output:
1
result:
ok 1 number(s): "1"
Test #45:
score: 0
Accepted
time: 0ms
memory: 3604kb
input:
3 2 3 3
output:
1
result:
ok 1 number(s): "1"
Test #46:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
6 2 6 2
output:
1
result:
ok 1 number(s): "1"
Test #47:
score: 0
Accepted
time: 0ms
memory: 3564kb
input:
2 3 2 2 1
output:
1
result:
ok 1 number(s): "1"
Test #48:
score: 0
Accepted
time: 0ms
memory: 3852kb
input:
2 3 1 2 1
output:
1
result:
ok 1 number(s): "1"
Test #49:
score: 0
Accepted
time: 0ms
memory: 3632kb
input:
1 3 1 1 1
output:
1
result:
ok 1 number(s): "1"
Test #50:
score: 0
Accepted
time: 0ms
memory: 3636kb
input:
10 3 4 10 2
output:
1
result:
ok 1 number(s): "1"
Test #51:
score: 0
Accepted
time: 1ms
memory: 3652kb
input:
7 4 2 6 6 2
output:
3
result:
ok 1 number(s): "3"
Test #52:
score: 0
Accepted
time: 1ms
memory: 3672kb
input:
10 4 6 3 7 5
output:
10
result:
ok 1 number(s): "10"
Test #53:
score: 0
Accepted
time: 0ms
memory: 3568kb
input:
9 4 2 2 2 3
output:
86
result:
ok 1 number(s): "86"
Test #54:
score: 0
Accepted
time: 1ms
memory: 3592kb
input:
6 4 3 3 6 5
output:
1
result:
ok 1 number(s): "1"