結果

問題 No.434 占い
コンテスト
ユーザー himueu
提出日時 2026-10-20 10:55:04
言語 C++23(gcc16)
(gcc 16.2.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 8 ms / 2,000 ms
+ 475µs
コード長 953 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 3,241 ms
コンパイル使用メモリ 168,480 KB
実行使用メモリ 9,904 KB
最終ジャッジ日時 2026-10-20 10:55:19
合計ジャッジ時間 5,397 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <iostream>
#include <vector>

void solve() {
    using namespace std;
    string s;
    cin >> s;
    int n = s.size() - 1;
    constexpr int inv[] = {0, 1, 5, 0, 7, 2, 0, 4, 8};
    bool all_zero = true;
    int ans = 0, comb = 1, e = 0;
    for (int i = 0; i <= n; i++) {
        int c = s[i] - '0';
        all_zero &= (c == 0);
        if (e == 0) { ans += c * comb; }
        if (e == 1) { ans += c * comb * 3; }
        ans %= 9;

        if (i == n) { break; }
        int k = n - i;
        while (k % 3 == 0) {
            k /= 3;
            e++;
        }
        comb *= k;
        k = i + 1;
        while (k % 3 == 0) {
            k /= 3;
            e--;
        }
        comb *= inv[k % 9];
        comb %= 9;
    }
    if (!all_zero && ans == 0) { ans = 9; }
    cout << ans << "\n";
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    int t;
    std::cin >> t;
    while (t--) { solve(); }
}
0