結果

問題 No.434 占い
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-04-21 09:36:13
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 18 ms / 2,000 ms
コード長 1,119 bytes
コンパイル時間 1,518 ms
コンパイル使用メモリ 169,388 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-09-27 10:49:22
合計ジャッジ時間 3,289 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 1 ms
4,384 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 AC 2 ms
4,376 KB
testcase_12 AC 3 ms
4,376 KB
testcase_13 AC 1 ms
4,380 KB
testcase_14 AC 2 ms
4,376 KB
testcase_15 AC 3 ms
4,380 KB
testcase_16 AC 3 ms
4,380 KB
testcase_17 AC 3 ms
4,380 KB
testcase_18 AC 3 ms
4,376 KB
testcase_19 AC 5 ms
4,376 KB
testcase_20 AC 18 ms
4,376 KB
testcase_21 AC 3 ms
4,380 KB
testcase_22 AC 3 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 3 ms
4,380 KB
testcase_25 AC 2 ms
4,380 KB
testcase_26 AC 3 ms
4,380 KB
testcase_27 AC 3 ms
4,380 KB
testcase_28 AC 3 ms
4,376 KB
testcase_29 AC 6 ms
4,380 KB
testcase_30 AC 15 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<b;i++)




const int inv[9] = { 0, 1, 5, 0, 7, 2, 0, 4, 8 };
int modpow(int a, int n, int mod = 9) {
    a %= mod;
    if (a == 0) return 0;
    int r = 1;
    while (n) r = r*((n % 2) ? a : 1) % mod, a = a*a%mod, n >>= 1;
    return r;
}
int sol(string S) {
    vector<int> v;
    for (char c : S) v.push_back(c - '0');
    int sm = 0;
    for (int i : v) sm += i;
    if (sm == 0) return 0;

    int a = 1, b = 1, t = 0;
    int res = 0;
    int n = v.size();
    rep(i, 0, n) {
        int c = a * inv[b] * modpow(3, t) % 9;
        res = (res + v[i] * c) % 9;
        a *= (n - 1 - i);
        b *= (i + 1);
        while (a % 3 == 0 && 0 < a) a /= 3, t++;
        while (b % 3 == 0 && 0 < b) b /= 3, t--;
        a %= 9; b %= 9;
    }
    if (res == 0) res = 9;
    return res;
}
//-----------------------------------------------------------------------------------
int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int T; cin >> T;
    rep(t, 0, T) {
        string s; cin >> s;
        printf("%d\n", sol(s));
    }
}
0