結果

問題 No.3271 PQ Dot Product
ユーザー apricity
提出日時 2025-07-30 00:21:33
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 34 ms / 1,000 ms
コード長 1,778 bytes
コンパイル時間 2,759 ms
コンパイル使用メモリ 114,992 KB
実行使用メモリ 6,824 KB
最終ジャッジ日時 2025-09-12 01:31:20
合計ジャッジ時間 8,794 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 46
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <cassert>
#include <numeric>
#include <algorithm>
using namespace std;

int main() {
    long long n, k; cin >> n >> k;

    long long mn = 0, mx = 0;
    for (long long i = 1; i <= n; i++) {
        mn += i * (n + 1 - i);
        mx += i * i;
    }
    if (k < mn or mx < k) {
        cout << "No" << "\n";
        return 0;
    }

    vector<int> p(n);

    if (n < 4) {
        iota(p.begin(), p.end(), 1);
        do {
            long long s = 0;
            for (long long i = 0; i < n; i++) s += (i + 1) * p[i];
            if (s == k) {
                cout << "Yes" << "\n";
                for (int i = 1; i <= n; i++) cout << i << " \n"[i == n];
                for (int i = 0; i < n; i++) cout << p[i] << " \n"[i == n - 1];
                return 0;
            }
        } while (next_permutation(p.begin(), p.end()));
        cout << "No" << "\n";
        return 0;
    }

    vector<int> delta(n + 1);
    for (long long m = n; m > 4; m--) {
        if (k <= m + (m - 1) * m * (m + 1) / 3) {
            p[n - m] = m;
            k -= m * (m + 1) / 2;
        }
        else {
            p[n - m] = 1;
            delta[n - m + 1] = 1;
            k -= m * m;
        }
    }
    vector<int> r{1, 2, 3, 4};
    do {
        int s = 0;
        for (int i = 0; i < 4; i++) s += (i + 1) * r[i];
        if (s == k) {
            for (int i = 0; i < 4; i++) p[n - 4 + i] = r[i];
            break;
        }
    } while (next_permutation(r.begin(), r.end()));

    for (int i = 0; i < n; i++) {
        p[i] += delta[i];
        delta[i + 1] += delta[i];
    }

    cout << "Yes" << "\n";
    for (int i = 1; i <= n; i++) cout << i << " \n"[i == n];
    for (int i = 0; i < n; i++) cout << p[i] << " \n"[i == n - 1];
}
0