結果

問題 No.435 占い(Extra)
ユーザー r_dream0r_dream0
提出日時 2017-02-21 21:06:50
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
WA  
実行時間 -
コード長 1,444 bytes
コンパイル時間 800 ms
コンパイル使用メモリ 72,704 KB
実行使用メモリ 26,112 KB
最終ジャッジ日時 2024-10-08 12:20:29
合計ジャッジ時間 7,733 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 28 WA * 4
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;

const int MAX_N = 10000001;
// (3n)! = 3^thress[n]k (kは3の倍数ではない)
vector<int> threes;
vector<char> fact;
vector<int> inv;
void init() {
  threes.resize((MAX_N + 2) / 3);
  threes[0] = 0;
  for(size_t i = 1; i < threes.size(); i++) {
    size_t j = i * 3;
    threes[i] = threes[i - 1];
    while(j % 3 == 0) {
      threes[i]++;
      j /= 3;
    }
  }
  fact.resize(MAX_N);
  fact[0] = 1;
  for(size_t i = 1; i < fact.size(); i++) {
    int j = i;
    while(j % 3 == 0) j /= 3;
    fact[i] = (fact[i - 1] * j) % 9;
  }
  inv.resize(9);
  for(int i = 0; i < 9; i++) {
    for(int j = 0; j < 9; j++) {
      if(i * j % 9 == 1) inv[i] = j;
    }
  }
}

int comb(int n, int k) {
  int t = threes[n/3] - threes[(n - k) / 3] - threes[k / 3];
  if(t >= 2) return 0;
  int ret = fact[n] * inv[fact[n - k]] * inv[fact[k]] % 9;
  if(t == 1) ret = ret * 3 % 9;
  return ret;
}

int main() {
  init();
  int T;
  cin >> T;
  for(int i = 0; i < T; i++) {
    int n, x, a, b, m;
    cin >> n >> x >> a >> b >> m;
    int lastS = x;
    bool allzero = lastS == 0;
    int ans = (lastS % 10) * comb(n - 1, 0) % 9;
    for(int i = 1; i < n; i++) {
      int nextS = ((lastS ^ a) + b) % m;
      ans = (ans + (nextS % 10) * comb(n - 1, i)) % 9;
      if(nextS)allzero = false;
      lastS = nextS;
    }
    ans %= 9;
    if(ans == 0 && !allzero) ans = 9;
    cout << ans << endl;
  }
}
0