結果

問題 No.434 占い
ユーザー tnakao0123tnakao0123
提出日時 2016-10-30 18:29:15
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 151 ms / 2,000 ms
コード長 1,714 bytes
コンパイル時間 610 ms
コンパイル使用メモリ 84,560 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-08-16 11:25:21
合計ジャッジ時間 3,583 ms
ジャッジサーバーID
(参考情報)
judge13 / judge11
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 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 4 ms
4,380 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 5 ms
4,376 KB
testcase_09 AC 4 ms
4,376 KB
testcase_10 AC 3 ms
4,376 KB
testcase_11 AC 4 ms
4,376 KB
testcase_12 AC 17 ms
4,376 KB
testcase_13 AC 5 ms
4,376 KB
testcase_14 AC 4 ms
4,376 KB
testcase_15 AC 39 ms
4,376 KB
testcase_16 AC 33 ms
4,376 KB
testcase_17 AC 25 ms
4,376 KB
testcase_18 AC 12 ms
4,380 KB
testcase_19 AC 22 ms
4,376 KB
testcase_20 AC 151 ms
4,376 KB
testcase_21 AC 39 ms
4,376 KB
testcase_22 AC 33 ms
4,376 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 33 ms
4,380 KB
testcase_25 AC 39 ms
4,380 KB
testcase_26 AC 17 ms
4,380 KB
testcase_27 AC 24 ms
4,380 KB
testcase_28 AC 23 ms
4,380 KB
testcase_29 AC 30 ms
4,380 KB
testcase_30 AC 125 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 434.cc: No.434 占い - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

const int MAX_N = 100000;
const int MOD = 9;

/* typedef */

typedef long long ll;

/* global variables */

int dvs[MAX_N + 1];

/* subroutines */

void gen_dvs(int maxp) {
  for (int p = 2; p * p <= maxp; p++)
    if (! dvs[p])
      for (int q = p * p; q <= maxp; q += p) dvs[q] = p;
}

ll powmod(ll a, int b) {
  ll pm = 1;
  while (b > 0) {
    if (b & 1) pm = pm * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return pm;
}

ll pfs2ll(int pfs[]) {
  if (pfs[0] > 0) return 0;
  ll r = 1;
  for (int a = 1; a < MOD; a++)
    r = r * powmod((ll)a, pfs[a]) % MOD;
  return r;
}

/* main */

// nCr = n(n-1)..(n-r+1)/r!
// nC(r+1) = n(n-1)..(n-r)/(r+1)!
// -> nC(r+1) = nCr*(n-r)/(r+1)

int main() {
  gen_dvs(MAX_N);
  
  int tn;
  cin >> tn;
  while (tn--) {
    string s;
    cin >> s;
    int n = s.size() - 1;

    ll ans = 0;
    bool nz = false;
    int pfs[MOD] = {0};

    for (int i = 0; i <= n; i++) {
      int d = s[i] - '0';
      if (d > 0) nz = true;
      ans = (ans + pfs2ll(pfs) * d) % MOD;

      if (i < n) {
	int j = n - i;
	while (dvs[j]) pfs[dvs[j] % MOD]++, j /= dvs[j];
	pfs[j % MOD]++;

	int k = i + 1;
	while (dvs[k]) pfs[dvs[k] % MOD]--, k /= dvs[k];
	pfs[k % MOD]--;
      }
    }

    if (nz && ans == 0) ans = MOD;
    printf("%lld\n", ans);
  }
  return 0;
}
0