結果

問題 No.435 占い(Extra)
ユーザー kimiyukikimiyuki
提出日時 2016-10-15 00:40:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 438 ms / 2,000 ms
コード長 2,062 bytes
コンパイル時間 752 ms
コンパイル使用メモリ 78,248 KB
実行使用メモリ 42,240 KB
最終ジャッジ日時 2024-04-17 02:39:05
合計ジャッジ時間 5,631 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
5,248 KB
testcase_01 AC 1 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 1 ms
5,376 KB
testcase_05 AC 1 ms
5,376 KB
testcase_06 AC 4 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 25 ms
7,296 KB
testcase_13 AC 17 ms
5,376 KB
testcase_14 AC 15 ms
5,376 KB
testcase_15 AC 19 ms
5,376 KB
testcase_16 AC 45 ms
5,376 KB
testcase_17 AC 305 ms
5,376 KB
testcase_18 AC 27 ms
7,236 KB
testcase_19 AC 17 ms
5,376 KB
testcase_20 AC 227 ms
42,240 KB
testcase_21 AC 149 ms
7,168 KB
testcase_22 AC 144 ms
5,376 KB
testcase_23 AC 142 ms
5,376 KB
testcase_24 AC 165 ms
5,376 KB
testcase_25 AC 438 ms
5,376 KB
testcase_26 AC 229 ms
42,240 KB
testcase_27 AC 147 ms
7,148 KB
testcase_28 AC 2 ms
5,376 KB
testcase_29 AC 146 ms
7,168 KB
testcase_30 AC 230 ms
42,236 KB
testcase_31 AC 161 ms
21,888 KB
testcase_32 AC 142 ms
10,808 KB
testcase_33 AC 154 ms
5,376 KB
testcase_34 AC 248 ms
5,376 KB
testcase_35 AC 143 ms
5,400 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <algorithm>
#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))
#define whole(f,x,...) ([&](decltype((x)) y) { return (f)(begin(y), end(y), ## __VA_ARGS__); })(x)
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 };
}
int pack(int k, int z) {
    return z * 9 + k;
}
pair<int,int> unpack(int n) {
    return { n % 9, n / 9 };
}
const int inv[9] = { 0, 1, 5, 0, 7, 2, 0, 4, 8 };
int choose(int n, int r) {
    static vector<int> fact(1, pack(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) = unpack(fact[i-1]);
            int k, z; tie(k, z) = zero(i);
            fact[i] = pack(pk * k % 9, pz + z);
        }
    }
    r = min(r, n - r);
    int pk, pz; tie(pk, pz) = unpack(fact[n]);
    int qk, qz; tie(qk, qz) = unpack(fact[r]);
    int rk, rz; tie(rk, rz) = unpack(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 --) {
        int n, x, a, b, m; cin >> n >> x >> a >> b >> m;
        bool zero = true;
        int acc = x % 10;
        if (x % 10 != 0) zero = false;
        repeat_from (i,1,n) {
            x = ((x ^ a) + b) % m;
            acc = (acc + x % 10 * choose(n-1, i)) % 9;
            if (x % 10 != 0) zero = false;
        }
        if (not zero and acc == 0) acc = 9;
        cout << acc << endl;
    }
    return 0;
}
0