結果

問題 No.5021 Addition Pyramid
ユーザー SnowBeenDiding
提出日時 2025-03-21 14:16:39
言語 C++23
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 2,726 bytes
コンパイル時間 3,406 ms
コンパイル使用メモリ 277,644 KB
実行使用メモリ 7,324 KB
スコア 1,610,941
最終ジャッジ日時 2025-03-21 14:16:47
合計ジャッジ時間 6,435 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
純コード判定しない問題か言語
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 50
権限があれば一括ダウンロードができます

ソースコード

diff #

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

static const int N = 50;
static const int MOD = 100000000;

typedef long long ll;

//=== Annealing parameters ===//
struct SAParams {
    double T0 = 1e4;          // initial temperature
    double Tend = 1e-4;       // final temperature
    double alpha = 0.995;     // cooling rate
    int maxIter = 1000000;    // maximum iterations
    double timeLimit = 1.9;   // seconds
    int deltaRange = 1000000; // max random change per move
} sa;

int a[N][N];
int c[N];
int best_c[N];
int b[N][N];

mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
uniform_int_distribution<int> uni_pos(0, N - 1);
uniform_int_distribution<int> uni_delta(-sa.deltaRange, sa.deltaRange);

double elapsed() {
    static auto start = chrono::high_resolution_clock::now();
    auto now = chrono::high_resolution_clock::now();
    return chrono::duration<double>(now - start).count();
}

// Build pyramid from bottom c into b
void build() {
    for (int j = 0; j < N; j++)
        b[N - 1][j] = (c[j] % MOD + MOD) % MOD;
    for (int i = N - 2; i >= 0; i--) {
        for (int j = 0; j <= i; j++) {
            b[i][j] = (b[i + 1][j] + b[i + 1][j + 1]) % MOD;
        }
    }
}

// Compute maximum modular error
int evaluate() {
    int mx = 0;
    for (int i = 0; i < N; i++)
        for (int j = 0; j <= i; j++) {
            int diff = abs(a[i][j] - b[i][j]);
            mx = max(mx, min(diff, MOD - diff));
        }
    return mx;
}

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

    for (int i = 0; i < N; i++)
        for (int j = 0; j <= i; j++)
            cin >> a[i][j];
    // initialize bottom randomly
    for (int i = 0; i < N; i++)
        c[i] = uniform_int_distribution<int>(0, MOD - 1)(rng);
    build();
    int curScore = evaluate();
    memcpy(best_c, c, sizeof(c));
    int bestScore = curScore;

    double T = sa.T0;
    int iter = 0;

    while (elapsed() < sa.timeLimit && T > sa.Tend) {
        int pos = uni_pos(rng);
        int old = c[pos];
        int delta = uni_delta(rng);
        c[pos] = (old + delta + MOD) % MOD;

        build();
        int newScore = evaluate();
        int diff = newScore - curScore;
        if (diff < 0 ||
            exp(-diff / T) > uniform_real_distribution<double>(0, 1)(rng)) {
            curScore = newScore;
            if (newScore < bestScore) {
                bestScore = newScore;
                memcpy(best_c, c, sizeof(c));
            }
        } else {
            c[pos] = old;
        }

        T *= sa.alpha;
        iter++;
    }

    // output best solution
    for (int i = 0; i < N; i++)
        cout << best_c[i] << (i + 1 < N ? ' ' : '\n');
    return 0;
}
0