QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#637126 | #8285. Shell Sort | wangjunrui | WA | 0ms | 3812kb | C++14 | 2.0kb | 2024-10-13 09:52:51 | 2024-10-13 09:52:56 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
int main() {
static const long long mod = 1000000007;
static const int maxn = 30, maxm = 10;
int n, m;
static int d[maxm], msk[maxm][maxn];
scanf("%d %d", &n, &m);
for (int i = 0; i < m; i++) scanf("%d", d + i);
int p = 0;
while (d[p] >= n) p++, m--;
for (int i = 0; i < m; i++) d[i] = d[i + p];
for (int i = 0; i < m; i++)
for (int j = 0; j < n; j++) {
int r = j % d[i], x = 0;
while (r < n) x ^= 1 << r, r += d[i];
msk[i][j] = x;
}
struct node {
int mx;
long long cnt;
inline node operator+(const node rhs) const {
return { max(mx, rhs.mx), ((mx >= rhs.mx) * cnt + (rhs.mx >= mx) * rhs.cnt) % mod };
}
};
function<node(const vector<int>&)> dfs = [&dfs, n, m](const vector<int>& v) {
static unordered_map<int, node> dp;
if (v[0] == (1 << n) - 1)
return (node){ 0, 1 };
if (dp.count(v[0]))
return dp[v[0]];
node ret = { 0, 0 };
for (int i = 0; i < d[0]; i++) {
int p = i + __builtin_popcount(v[0] & msk[0][i]) * d[0];
if (p >= n)
continue;
int cnt = 0;
auto nxt = v;
nxt[0] ^= 1 << p;
for (int r = 1; r < m; r++) {
int s = nxt[r - 1] & msk[r][p];
cnt += __builtin_popcount(s >> p) - 1;
// p = p % d[r] + (__builtin_popcount(s) - 1) * d[r];
nxt[r] ^= 1 << p;
}
node cur = dfs(nxt);
cur.mx += cnt, ret = ret + cur;
}
return dp[v[0]] = ret;
};
int cnt = 0;
for (int i = 0; i < d[0]; i++) {
int pop = __builtin_popcount(msk[0][i]);
cnt += pop * (pop - 1) >> 1;
}
vector<int> v(m);
node ans = dfs(v);
ans.mx += cnt, printf("%d %lld\n", ans.mx, ans.cnt);
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3808kb
input:
2 2 2 1
output:
1 1
result:
ok 2 number(s): "1 1"
Test #2:
score: -100
Wrong Answer
time: 0ms
memory: 3812kb
input:
5 4 5 4 2 1
output:
9 4
result:
wrong answer 1st numbers differ - expected: '6', found: '9'