結果

問題 No.434 占い
ユーザー r_dream0r_dream0
提出日時 2017-02-21 21:11:05
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 209 ms / 2,000 ms
コード長 1,314 bytes
コンパイル時間 625 ms
コンパイル使用メモリ 73,348 KB
実行使用メモリ 26,408 KB
最終ジャッジ日時 2023-08-28 23:40:31
合計ジャッジ時間 4,932 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
25,812 KB
testcase_01 AC 73 ms
25,796 KB
testcase_02 AC 75 ms
25,792 KB
testcase_03 AC 76 ms
25,796 KB
testcase_04 AC 76 ms
25,936 KB
testcase_05 AC 77 ms
25,808 KB
testcase_06 AC 74 ms
25,988 KB
testcase_07 AC 73 ms
25,932 KB
testcase_08 AC 76 ms
25,796 KB
testcase_09 AC 76 ms
25,676 KB
testcase_10 AC 77 ms
25,932 KB
testcase_11 AC 75 ms
25,724 KB
testcase_12 AC 87 ms
25,816 KB
testcase_13 AC 74 ms
25,824 KB
testcase_14 AC 76 ms
25,872 KB
testcase_15 AC 75 ms
26,216 KB
testcase_16 AC 74 ms
25,932 KB
testcase_17 AC 76 ms
25,800 KB
testcase_18 AC 81 ms
25,792 KB
testcase_19 AC 90 ms
25,856 KB
testcase_20 AC 209 ms
25,856 KB
testcase_21 AC 77 ms
26,408 KB
testcase_22 AC 76 ms
25,796 KB
testcase_23 AC 76 ms
25,864 KB
testcase_24 AC 80 ms
25,864 KB
testcase_25 AC 79 ms
26,084 KB
testcase_26 AC 78 ms
26,056 KB
testcase_27 AC 76 ms
25,868 KB
testcase_28 AC 78 ms
25,796 KB
testcase_29 AC 93 ms
25,820 KB
testcase_30 AC 176 ms
25,808 KB
権限があれば一括ダウンロードができます

ソースコード

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;
  string S;
  for(int i = 0; i < T; i++) {
    cin >> S;
    bool allzero = true;
    int ans = 0;
    for(int i = 0; i < S.size(); i++) {
      ans = (ans + (S[i] - '0') * comb(S.size() - 1, i)) % 9;
      if(S[i] != '0')allzero = false;
    }
    ans %= 9;
    if(ans == 0 && !allzero) ans = 9;
    cout << ans << endl;
  }
}
0