結果

問題 No.3506 All Distance is Square Number
コンテスト
ユーザー はじっこゆーれー
提出日時 2026-04-18 05:54:15
言語 C++23
(gcc 15.2.0 + boost 1.89.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
WA  
実行時間 -
コード長 1,419 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 1,308 ms
コンパイル使用メモリ 186,120 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-18 05:54:24
合計ジャッジ時間 2,535 ms
ジャッジサーバーID
(参考情報)
judge3_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other WA * 29
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>
#include <set>
#include <cmath>
using namespace std;
bool is_square(long long x) {
    if (x < 0) return false;
    long long r = round(sqrt(x));
    return r * r == x;
}
int N;
vector<int> R;
vector<int> W;
vector<long long> D;
vector<bool> used(205, false);
bool found_ans = false;
void dfs(int k) {
    if (found_ans) return;
    if (k > N) {
        found_ans = true;
        return;
    }
    for (int w = 2; w <= 200; ++w) {
        if (used[w]) continue;
        bool ok = true;
        for (int u = 2; u < k; ++u) {
            long long w_u = (u == 2) ? R[1] : W[u];
            bool sq = false;
            if (is_square(D[k] - D[u])) sq = true;
            else if (is_square(w_u + D[k])) sq = true;
            else if (is_square(D[u] + w)) sq = true;
            else if (is_square(w_u + w)) sq = true;
            if (!sq) {
                ok = false;
                break;
            }
        }
        if (ok) {
            W[k] = w;
            used[w] = true;
            dfs(k + 1);
            if (found_ans) return;
            used[w] = false;
        }
    }
}
int main() {
    cin >> N;
    R.assign(N + 1, 0);
    W.assign(N + 1, 0);
    D.assign(N + 1, 0);
    for (int i = 1; i <= N; ++i) {
        D[i] = (i - 1) * (i - 1);
        if (i > 1) {
            R[i - 1] = D[i] - D[i - 1];
            used[R[i - 1]] = true;
        }
    }
    dfs(3);
}
0