結果

問題 No.435 占い(Extra)
ユーザー nebukuro09nebukuro09
提出日時 2017-08-30 17:22:15
言語 D
(dmd 2.107.1)
結果
AC  
実行時間 556 ms / 2,000 ms
コード長 1,321 bytes
コンパイル時間 578 ms
コンパイル使用メモリ 104,908 KB
実行使用メモリ 43,288 KB
最終ジャッジ日時 2023-09-03 16:09:25
合計ジャッジ時間 9,105 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 1 ms
4,384 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 1 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 6 ms
4,380 KB
testcase_11 AC 6 ms
4,376 KB
testcase_12 AC 46 ms
6,712 KB
testcase_13 AC 46 ms
6,344 KB
testcase_14 AC 46 ms
6,612 KB
testcase_15 AC 47 ms
6,688 KB
testcase_16 AC 56 ms
4,376 KB
testcase_17 AC 171 ms
4,380 KB
testcase_18 AC 44 ms
7,172 KB
testcase_19 AC 43 ms
6,440 KB
testcase_20 AC 424 ms
41,964 KB
testcase_21 AC 413 ms
12,488 KB
testcase_22 AC 410 ms
6,436 KB
testcase_23 AC 413 ms
6,712 KB
testcase_24 AC 425 ms
6,984 KB
testcase_25 AC 556 ms
4,384 KB
testcase_26 AC 391 ms
43,288 KB
testcase_27 AC 405 ms
12,144 KB
testcase_28 AC 1 ms
4,380 KB
testcase_29 AC 396 ms
12,760 KB
testcase_30 AC 198 ms
42,444 KB
testcase_31 AC 360 ms
35,612 KB
testcase_32 AC 370 ms
15,564 KB
testcase_33 AC 445 ms
6,984 KB
testcase_34 AC 501 ms
7,712 KB
testcase_35 AC 421 ms
6,680 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio, std.array, std.string, std.conv, std.algorithm;
import std.typecons, std.range, std.random, std.math, std.container;
import std.numeric, std.bigint, core.bitop;

immutable int[] inv = [0, 1, 5, 0, 7, 2, 0, 4, 8];


void solve() {
    auto input = readln.split.map!(to!long);
    auto N = input[0].to!int;
    auto x = input[1];
    auto a = input[2];
    auto b = input[3];
    auto m = input[4];
    auto S = new int[](N);

    S[0] = x % 10;
    
    foreach (i; 1..N) {
        x = ((x ^ a) + b) % m;
        S[i] = x % 10;
    }
    

    if (S.map!(s => s == 0).all) {
        writeln(0);
        return;
    }

    
    int C = 1;
    int C3 = 0;
    int ans = 0;

    foreach (i; 0..N) {
        int tmp_3 = 0;
        
        while (S[i] > 0 && S[i] % 3 == 0)
            tmp_3 += 1, S[i] /= 3;
        
        int C3C3 = C3 + tmp_3 >= 2 ? 0 : C3 + tmp_3 == 1 ? 3 : 1;

        ans += C % 9 * C3C3 % 9 * S[i] % 9;
        ans %= 9;

        
        int num = N - 1 - i;
        int den = i + 1;

        while (num > 0 && num % 3 == 0)
            C3 += 1, num /= 3;
        while (den % 3 == 0)
            C3 -= 1, den /= 3;

        C = C * num % 9 * inv[den % 9] % 9;
    }

    writeln(ans ? ans : 9);
}


void main() {
    auto T = readln.chomp.to!int;
    while (T--)
        solve;
}
0