QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#275542 | #7259. Robots | PetroTarnavskyi# | TL | 0ms | 0kb | C++20 | 2.9kb | 2023-12-04 20:19:48 | 2023-12-04 20:19:49 |
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;
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;
}
VI vals;
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();
}
}
int recalc(int n, vector<PII> lens)
{
resRecalc = 0;
FOR(i, 0, SZ(lens))
{
possible[i].clear();
}
rec(0, SZ(lens), n);
return resRecalc;
}
int 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: 0
Time Limit Exceeded
input:
5 10 1 0 U 3 1 U 1 2 R 1 1 L 0 1 R