結果

問題 No.435 占い(Extra)
ユーザー kimiyukikimiyuki
提出日時 2016-10-15 00:40:59
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 451 ms / 2,000 ms
コード長 2,062 bytes
コンパイル時間 807 ms
コンパイル使用メモリ 78,260 KB
実行使用メモリ 42,264 KB
最終ジャッジ日時 2023-07-30 00:22:45
合計ジャッジ時間 5,955 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 1 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 5 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,376 KB
testcase_11 AC 3 ms
4,380 KB
testcase_12 AC 28 ms
6,976 KB
testcase_13 AC 18 ms
4,376 KB
testcase_14 AC 17 ms
4,380 KB
testcase_15 AC 20 ms
4,376 KB
testcase_16 AC 47 ms
4,376 KB
testcase_17 AC 306 ms
4,380 KB
testcase_18 AC 28 ms
6,996 KB
testcase_19 AC 18 ms
4,384 KB
testcase_20 AC 258 ms
42,180 KB
testcase_21 AC 166 ms
7,048 KB
testcase_22 AC 155 ms
4,380 KB
testcase_23 AC 154 ms
4,384 KB
testcase_24 AC 180 ms
4,380 KB
testcase_25 AC 451 ms
4,380 KB
testcase_26 AC 254 ms
42,248 KB
testcase_27 AC 165 ms
6,876 KB
testcase_28 AC 2 ms
4,376 KB
testcase_29 AC 162 ms
6,996 KB
testcase_30 AC 256 ms
42,264 KB
testcase_31 AC 176 ms
21,760 KB
testcase_32 AC 153 ms
11,400 KB
testcase_33 AC 170 ms
4,384 KB
testcase_34 AC 278 ms
4,380 KB
testcase_35 AC 161 ms
5,300 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