Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define all(x) (x).begin(),(x).end()
- using namespace std;
- using ll = long long;
- const int N = 25000;
- void Solve() {
- int n, k;
- cin >> n >> k;
- vector<int> a(n - k + 1);
- for (int i = 0; i < a.size(); i++) {
- cin >> a[i];
- }
- bitset<2 * N> tot;
- for (int i = 0; i < n; i++) tot[i] = 1;
- bitset<2 * N> cur;
- vector<vector<ll>> dlts;
- for (int i = 0; i < k; i++) {
- dlts.push_back({0});
- }
- for (int i = 1; i < n - k + 1; i++) {
- dlts[(i - 1) % k].push_back(dlts[(i - 1) % k].back() + a[i] - a[i - 1]);
- }
- for (int i = 0; i < k; i++) {
- ll dlt = *min_element(all(dlts[i]));
- for (ll& val : dlts[i]) val -= dlt;
- if (*max_element(all(dlts[i])) >= n || *min_element(all(dlts[i])) < 0) {
- cout << "0\n";
- return;
- }
- }
- vector<int> per;
- vector<bitset<2 * N>> arr(k);
- for (int i = 0; i < k; i++) {
- for (int val : dlts[i]) {
- arr[i][val] = 1;
- }
- }
- for (int i = 0; i < k; i++) per.push_back(i);
- vector<vector<int>> ans;
- do {
- cur.reset();
- vector<int> dlt2;
- for (int i = 0; i < k; i++) {
- for (int j = 0; j < n; j++) {
- if (cur[j] == 0) {
- cur |= arr[per[i]] << j;
- dlt2.push_back(j);
- break;
- }
- }
- }
- if ((cur & tot).count() == n) {
- vector<int> curans(n);
- for (int i = 0; i < k; i++) {
- int curind = per[i];
- for (int j = 0; j < dlts[curind].size(); j++) {
- curans[j * k + curind] = dlts[curind][j] + dlt2[i];
- }
- }
- for (int j = 0; j < n; j++) {
- cur[curans[j]] = 0;
- }
- if (cur.count() == 0 && *max_element(all(curans)) < n && accumulate(curans.begin(), curans.begin() + k, 0LL) + k == a[0]) {
- ans.push_back(curans);
- }
- }
- } while(next_permutation(all(per)));
- cout << ans.size() << "\n";
- sort(all(ans));
- for (auto& v : ans) {
- for (int val : v) {
- cout << val + 1 << " ";
- }
- cout << '\n';
- }
- }
- int main() {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int q;
- cin >> q;
- while (q--) {
- Solve();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement