結果

問題 No.435 占い(Extra)
ユーザー kimiyukikimiyuki
提出日時 2016-10-15 00:14:58
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,661 bytes
コンパイル時間 609 ms
コンパイル使用メモリ 75,840 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-17 02:38:17
合計ジャッジ時間 2,119 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
testcase_10 WA -
testcase_11 WA -
testcase_12 WA -
testcase_13 WA -
testcase_14 WA -
testcase_15 WA -
testcase_16 WA -
testcase_17 WA -
testcase_18 WA -
testcase_19 WA -
testcase_20 WA -
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 WA -
testcase_35 WA -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
#include <tuple>
#include <cassert>
#define repeat(i,n) for (int i = 0; (i) < (n); ++(i))
#define repeat_from(i,m,n) for (int i = (m); (i) < (n); ++(i))
using namespace std;
template <class T> void setmax(T & a, T const & b) { if (a < b) a = b; }
template <typename T, typename X> auto vectors(T a, X x) { return vector<T>(x, a); }
template <typename T, typename X, typename Y, typename... Zs> auto vectors(T a, X x, Y y, Zs... zs) { auto cont = vectors(a, y, zs...); return vector<decltype(cont)>(x, cont); }
pair<int,int> zero(int k) {
    int z = 0;
    while (k % 3 == 0) { k /= 3; z += 1; }
    return { k, z };
}
const int inv[9] = { 0, 1, 5, 0, 7, 2, 0, 4, 8 };
int choose(int n, int r) {
    const int mod = 9;
    static vector<pair<int,int> > fact(1, make_pair(1, 0));
    if (fact.size() <= n) {
        int l = fact.size();
        fact.resize( n + 1);
        repeat_from (i,l,n+1) {
            int pk, pz; tie(pk, pz) = fact[i-1];
            int k, z; tie(k, z) = zero(i);
            fact[i] = { pk * k % 9, pz + z };
        }
    }
    r = min(r, n - r);
    int pk, pz; tie(pk, pz) = fact[n];
    int qk, qz; tie(qk, qz) = fact[r];
    int rk, rz; tie(rk, rz) = fact[n-r];
    int p = pk * inv[qk] * inv[rk] % 9;
    int z = pz - qz - rz;
    assert (z >= 0);
    return z == 0 ? p : z == 1 ? p * 3 % 9 : 0;
}
int main() {
    int t; cin >> t;
    while (t --) {
        string s; cin >> s;
        int n = s.length();
        int acc = 0;
        repeat (j,n) acc += (s[j] - '0') * choose(n-1, j);
        while (acc >= 10) acc = acc / 10 + acc % 10;
        cout << acc << endl;
    }
    return 0;
}
0