結果

問題 No.3506 All Distance is Square Number
コンテスト
ユーザー takuma saito
提出日時 2026-04-18 22:00:11
言語 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  
実行時間 -
コード長 2,224 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,561 ms
コンパイル使用メモリ 335,684 KB
実行使用メモリ 6,400 KB
最終ジャッジ日時 2026-04-18 22:00:23
合計ジャッジ時間 7,492 ms
ジャッジサーバーID
(参考情報)
judge3_1 / judge2_1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 3 WA * 26
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;

int f(int n) {
    if (n < 0) return 0;
    int s = round(sqrt(n));
    return s * s == n;
}

int main() {
    int n;
    cin >> n;
    if (n == 2) {
        cout << "1\n1 2 1\n1 1" << endl;
        return 0;
    }
    int a[55], b[55], x = 1, u[205] = {0};
    u[x] = 1;
    for (int i = 3; i <= n; i++) {
        bool ok = false;
        for (int p = 1; p <= 200; p++) {
            if (u[p]) continue;
            for (int q = 1; q <= 200; q++) {
                if (u[q] || p == q) continue;
                if (!(f(p) || f(q + x))) continue;
                if (!(f(q) || f(p + x))) continue;
                bool ok_ = true;
                for (int j = 3; j < i; j++) {
                    if (f(p + a[j]) || f(q + b[j]) || f(p + x + b[j]) || f(q + x + a[j])) continue;
                    ok_ = false;
                    break;
                }
                if (ok_) {
                    a[i] = p;
                    b[i] = q;
                    u[p] = u[q] = 1;
                    ok = true;
                    break;
                }
            }
            if (ok) break;
        }
    }
    cout << 2 * n - 3 << "\n1 2 " << x << endl;
    for (int i = 3; i <= n; i++) {
        cout << "1 " << i << " " << a[i] << "\n2 " << i << " " << b[i] << endl;
    }
    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            if (i == 1 && j == 2) {
                cout << "1 1" << endl;
            } else if (i == 1) {
                if (f(a[j])) cout << "1 " << 2 * j - 4 << endl;
                else cout << "2 1 " << 2 * j - 3 << endl;
            } else if (i == 2) {
                if (f(b[j])) cout << "1 " << 2 * j - 3 << endl;
                else cout << "2 1 " << 2 * j - 4 << endl;
            } else {
                if (f(a[i] + a[j])) cout << "2 " << 2 * i - 4 << " " << 2 * j - 4 << endl;
                else if (f(b[i] + b[j])) cout << "2 " << 2 * i - 3 << " " << 2 * j - 3 << endl;
                else if (f(a[i] + x + b[j])) cout << "3 " << 2 * i - 4 << " 1 " << 2 * j - 3 << endl;
                else cout << "3 " << 2 * i - 3 << " 1 " << 2 * j - 4 << endl;
            }
        }
    }
    return 0;
}
0