結果

問題 No.435 占い(Extra)
ユーザー tottoripapertottoripaper
提出日時 2016-10-19 18:18:50
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 330 ms / 2,000 ms
コード長 2,012 bytes
コンパイル時間 1,382 ms
コンパイル使用メモリ 146,504 KB
実行使用メモリ 42,672 KB
最終ジャッジ日時 2023-07-30 00:30:11
合計ジャッジ時間 7,151 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,384 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 4 ms
4,380 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 28 ms
7,600 KB
testcase_13 AC 27 ms
4,376 KB
testcase_14 AC 27 ms
4,380 KB
testcase_15 AC 26 ms
4,380 KB
testcase_16 AC 34 ms
4,384 KB
testcase_17 AC 86 ms
4,376 KB
testcase_18 AC 29 ms
7,552 KB
testcase_19 AC 27 ms
4,380 KB
testcase_20 AC 257 ms
42,604 KB
testcase_21 AC 250 ms
7,628 KB
testcase_22 AC 246 ms
4,384 KB
testcase_23 AC 248 ms
4,380 KB
testcase_24 AC 252 ms
4,380 KB
testcase_25 AC 324 ms
4,380 KB
testcase_26 AC 256 ms
42,672 KB
testcase_27 AC 262 ms
7,596 KB
testcase_28 AC 1 ms
4,376 KB
testcase_29 AC 238 ms
7,776 KB
testcase_30 AC 142 ms
42,488 KB
testcase_31 AC 211 ms
24,092 KB
testcase_32 AC 221 ms
9,672 KB
testcase_33 AC 274 ms
4,380 KB
testcase_34 AC 330 ms
4,380 KB
testcase_35 AC 261 ms
4,908 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

#define fst(t) std::get<0>(t)
#define snd(t) std::get<1>(t)
#define thd(t) std::get<2>(t)

using ll = long long;
using P = std::tuple<int,int>;

const int dx[8] = {-1, 1, 0, 0, -1, -1, 1, 1}, dy[8] = {0, 0, -1, 1, -1, 1, -1, 1};

int S[10000100];
int inv[10];

template <typename T>
T expt(T a, T n, T mod = std::numeric_limits<T>::max()){
    T res = 1;
    while(n){
        if(n & 1){res = res * a % mod;}
        a = a * a % mod;
        n >>= 1;
    }
    return res;
}

int main(){
    //*
    std::cin.tie(nullptr);
    std::ios::sync_with_stdio(false);
    /*/
      /*/

    for(int i=0;i<9;++i){
        // generate inv
        if(i % 3 == 0){inv[i] = 1;}
        else{
            inv[i] = expt(i % 9, 5, 9);
        }
    }
    
    
    int T;
    scanf("%d", &T);

    for(int _=0;_<T;++_){
        int N, X, A, B, M;
        scanf("%d %d %d %d %d", &N, &X, &A, &B, &M);
        
        S[0] = X;
        for(int i=1;i<N;++i){
            S[i] = ((S[i-1] ^ A) + B) % M;
        }

        for(int i=0;i<N;++i){
            S[i] %= 10;
        }

        int l = N, n = l - 1;

        if(count(S, S+l, 0) == l){
            puts("0");
            continue;
        }
        
        int three_n = 0, m = 1;

        int res = 0;
        for(int i=0;i<=n;++i){
            if(i >= 1){
                int x = i;
                while(x % 3 == 0){
                    --three_n;
                    x /= 3;
                }

                m = m * (x > 1 ? inv[x % 9] : 1) % 9;
                
                x = n - i + 1;

                while(x % 3 == 0){
                    ++three_n;
                    x /= 3;
                }

                m = m * x % 9;
            }
            
            int a = S[i], three_n2 = three_n + (a % 3 == 0) + (a % 9 == 0);
            if(three_n2 < 2){
                res = (res + a * m % 9 * expt(3, three_n) % 9) % 9;
            }
        }

        printf("%d\n", res > 0 ? res : 9);
    }
}
0