-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNo_cost_too_great2.cpp
More file actions
77 lines (64 loc) · 1.83 KB
/
No_cost_too_great2.cpp
File metadata and controls
77 lines (64 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <bits/stdc++.h>
using namespace std;
#define MOD 676767677
#define int long long
// Use inline + reserve + reference passing
inline vector<vector<int>> combinations(int sz)
{
int comb = 1LL << sz;
vector<vector<int>> all;
all.reserve(comb); // reserve memory upfront
for (int mask = 0; mask < comb; ++mask)
{
vector<int> bits;
bits.reserve(sz); // reserve memory once
// no need for sz - 1 to 0; direct 0..sz-1 is fine
for (int j = 0; j < sz; ++j)
bits.push_back(((mask >> j) & 1) ? 1 : -1);
all.emplace_back(std::move(bits)); // move instead of copy
}
return all;
}
inline void solve(const vector<int> &arr)
{
const int sz = arr.size();
int ans = 0;
vector<vector<int>> sol = combinations(sz);
// cache frequently used values
for (auto &bits : sol)
{
vector<int> prefixNeg(sz), suffixPos(sz);
prefixNeg[0] = (bits[0] == -1);
for (int i = 1; i < sz; ++i)
prefixNeg[i] = prefixNeg[i - 1] + (bits[i] == -1);
suffixPos[sz - 1] = (bits[sz - 1] == 1);
for (int i = sz - 2; i >= 0; --i)
suffixPos[i] = suffixPos[i + 1] + (bits[i] == 1);
bool correct = true;
for (int j = 0; j < sz; ++j)
{
int sum = prefixNeg[j] + suffixPos[j];
if (sum != arr[j]) { correct = false; break; }
}
ans += correct;
}
cout << (ans % MOD) << '\n';
}
int32_t main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int t;
cin >> t;
while (t--)
{
int sz;
cin >> sz;
vector<int> arr(sz);
for (int &x : arr)
cin >> x;
solve(arr);
}
return 0;
}