結果

問題 No.5021 Addition Pyramid
ユーザー Sor4chi
提出日時 2025-02-25 22:59:06
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 1,903 ms / 2,000 ms
コード長 2,842 bytes
コンパイル時間 1,821 ms
コンパイル使用メモリ 170,504 KB
実行使用メモリ 6,820 KB
スコア 44,515,942
最終ジャッジ日時 2025-02-25 23:02:29
合計ジャッジ時間 101,267 ms
ジャッジサーバーID
(参考情報)
judge6 / judge3
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

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

static const int MOD = 100000000;
static const int N = 50;
static const double TIME_LIMIT = 1.9;

static long long A[N + 1][N + 1];

long long diff_error(long long x, long long y) {
    long long d = llabs(x - y);
    if (d >= MOD) d %= MOD;
    return min(d, MOD - d);
}

long long compute_cost(const vector<long long> &c) {
    static long long b[N + 1][N + 1];
    for (int j = 1; j <= N; j++) {
        b[N][j] = c[j - 1];
    }
    for (int i = N - 1; i >= 1; i--) {
        for (int j = 1; j <= i; j++) {
            long long tmp = b[i + 1][j] + b[i + 1][j + 1];
            if (tmp >= MOD) tmp -= MOD;
            b[i][j] = tmp;
        }
    }
    long long mx = 0;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= i; j++) {
            long long e = diff_error(A[i][j], b[i][j]);
            if (e > mx) mx = e;
        }
    }
    return mx;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    int tmpN;
    cin >> tmpN;
    for (int i = 1; i <= N; i++) {
        for (int j = 1; j <= i; j++) {
            cin >> A[i][j];
        }
    }

    std::random_device rd;
    std::mt19937_64 mt(rd());

    uniform_int_distribution<long long> distC(0, MOD - 1);
    vector<long long> c(N);
    for (int i = 0; i < N; i++) {
        c[i] = distC(mt);
    }

    long long current_cost = compute_cost(c);
    cerr << "initial cost: " << current_cost << endl;

    vector<long long> best_c = c;
    long long best_cost = current_cost;

    auto start_clock = chrono::system_clock::now();

    const double T0 = 1e7;
    const double T_end = 1e3;
    int iter = 0;

    while (true) {
        auto now_clock = chrono::system_clock::now();
        double elapsed = chrono::duration<double>(now_clock - start_clock).count();
        if (elapsed > TIME_LIMIT) break;

        double progress_ratio = elapsed / TIME_LIMIT;
        double T = T0 * pow(T_end / T0, progress_ratio);

        // ランダムな位置を選び、違う値に入れ替える近傍
        int p = uniform_int_distribution<int>(0, N - 1)(mt);
        int prev = c[p];
        c[p] = distC(mt);

        long long new_cost = compute_cost(c);

        long long diff = new_cost - current_cost;

        if (diff < 0 || uniform_real_distribution<double>(0, 1)(mt) < exp(-diff / T)) {
            cerr << "update: " << current_cost << endl;
            current_cost = new_cost;
            if (current_cost < best_cost) {
                best_cost = current_cost;
                best_c = c;
            }
        } else {
            c[p] = prev;
        }
        iter++;
    }

    cerr << "best cost: " << best_cost << endl;
    cerr << "iteration: " << iter << endl;

    for (int i = 0; i < N; i++) {
        cout << best_c[i] << (i + 1 < N ? ' ' : '\n');
    }

    return 0;
}
0