結果

問題 No.434 占い
ユーザー しらっ亭しらっ亭
提出日時 2016-10-15 00:38:52
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 146 ms / 2,000 ms
コード長 4,018 bytes
コンパイル時間 1,860 ms
コンパイル使用メモリ 182,748 KB
実行使用メモリ 5,580 KB
最終ジャッジ日時 2023-08-14 12:29:49
合計ジャッジ時間 4,608 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 10 ms
5,092 KB
testcase_01 AC 10 ms
5,312 KB
testcase_02 AC 10 ms
5,148 KB
testcase_03 AC 10 ms
5,268 KB
testcase_04 AC 9 ms
5,144 KB
testcase_05 AC 9 ms
5,232 KB
testcase_06 AC 12 ms
5,080 KB
testcase_07 AC 10 ms
5,164 KB
testcase_08 AC 11 ms
5,080 KB
testcase_09 AC 11 ms
5,296 KB
testcase_10 AC 11 ms
5,292 KB
testcase_11 AC 12 ms
5,140 KB
testcase_12 AC 24 ms
5,084 KB
testcase_13 AC 10 ms
5,232 KB
testcase_14 AC 11 ms
5,092 KB
testcase_15 AC 18 ms
5,360 KB
testcase_16 AC 18 ms
5,264 KB
testcase_17 AC 18 ms
5,232 KB
testcase_18 AC 19 ms
5,152 KB
testcase_19 AC 31 ms
5,088 KB
testcase_20 AC 146 ms
5,244 KB
testcase_21 AC 18 ms
5,280 KB
testcase_22 AC 18 ms
5,068 KB
testcase_23 AC 10 ms
5,092 KB
testcase_24 AC 18 ms
5,180 KB
testcase_25 AC 18 ms
5,432 KB
testcase_26 AC 13 ms
5,580 KB
testcase_27 AC 19 ms
5,092 KB
testcase_28 AC 20 ms
5,140 KB
testcase_29 AC 37 ms
5,092 KB
testcase_30 AC 119 ms
5,116 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

#define _p(...) (void)printf(__VA_ARGS__)
#define forr(x,arr) for(auto&& x:arr)
#define _overload3(_1,_2,_3,name,...) name
#define _rep2(i,n) _rep3(i,0,n)
#define _rep3(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload3(__VA_ARGS__,_rep3,_rep2,)(__VA_ARGS__)
#define _rrep2(i,n) _rrep3(i,0,n)
#define _rrep3(i,a,b) for(int i=int(b)-1;i>=int(a);i--)
#define rrep(...) _overload3(__VA_ARGS__,_rrep3,_rrep2,)(__VA_ARGS__)
#define ALL(x) (x).begin(), (x).end()
#define BIT(n) (1LL<<(n))
#define SZ(x) ((int)(x).size())
#define fst first
#define snd second
using ll=long long;using pii=pair<int,int>;using vb=vector<bool>;
using vi=vector<int>;using vvi=vector<vi>;using vvvi=vector<vvi>;
using vl=vector<ll>;using vvl=vector<vl>;using vvvl=vector<vvl>;
using vd=vector<double>;using vvd=vector<vd>;using vvvd=vector<vvd>;
using vpii=vector<pii>;using vvpii=vector<vpii>;using vvvpii=vector<vvpii>;
template <typename T> T read() {T t; cin >> t; return t;}

template <typename T> map<T, int> factorize(T n) {
  map<T, int> ret;
  for (T i = 2; i * i <= n; i++) {
    while (n % i == 0) {
      ++ret[i];
      n /= i;
    }
  }
  if (n != 1) ret[n] = 1;
  return ret;
}

long long extgcd(long long a, long long b, long long& x, long long& y) {
  long long g = a; x = 1; y = 0;
  if (b != 0) g = extgcd(b, a % b, y, x), y -= (a / b) * x;
  return g;
}

long long invMod(long long x, long long m) {
  long long s, t;
  extgcd(x, m, s, t);
  return (m + s) % m;
}

struct Combination {
  struct CombinationLucas {
    long long p, q;
    long long mod;
    vector<long long> power;
    vector<long long> F, invF;
    vector<int> e;

    CombinationLucas(int n, long long p, long long q)
      : p(p), q(q), power(q + 1), F(n), invF(n), e(n) {
        mod = 1;
        for (int i = 0; i < q; i++) {
          mod *= p;
        }

        power[0] = 1;
        for (int i = 1; i <= q; i++) {
          power[i] = power[i - 1] * p % mod;
        }

        F[0] = 1;
        for (long long i = 1; i < n; i++) {
          if (i % p == 0) {
            F[i] = F[i - 1];
          } else {
            F[i] = F[i - 1] * i % mod;
          }
        }

        invF[n - 1] = invMod(F[n - 1], mod);
        for (long long i = n - 2; i >= 0; i--) {
          if ((i + 1) % p == 0) {
            invF[i] = invF[i + 1];
          } else {
            invF[i] = invF[i + 1] * (i + 1) % mod;
          }
        }

        for (int i = 1; i < n; i++) {
          F[i] = F[i] * F[i / p] % mod;
          invF[i] = invF[i] * invF[i / p] % mod;
          e[i] = i / p + e[i / p];
        }
      }

    long long operator()(int n, int r) {
      if (n < 0 || r < 0 || n < r) return 0;
      long long result = F[n] * invF[n - r] % mod * invF[r] % mod;
      result = result * power[min<int>(q, e[n] - e[n - r] - e[r])] % mod;
      return result;
    }
  };

  vector<long long> inv;
  vector<CombinationLucas> cs;

  Combination(int n, long long mod) {
    auto pf = factorize(mod);
    for (auto kv : pf) {
      cs.emplace_back(n, kv.first, kv.second);
    }

    long long m = 1;
    for (auto &c : cs) {
      inv.push_back(invMod(m, c.mod));
      m *= c.mod;
    }
  }

  long long operator()(int n, int r) {
    long long x = 0;
    long long mod = 1;

    for (int i = 0; i < cs.size(); i++) {
      long long y = cs[i](n, r);
      x += mod * (y - x) * inv[i];
      mod *= cs[i].mod;
      x %= mod;
    }

    return (x % mod + mod) % mod;
  }
};

Combination cb(101010, 9);

void Main() {
  string S = read<string>();
  int N = SZ(S);

  ll ans = 0;
  rep(i, N) {
    int a = S[i] - '0';

    int c = cb(N-1, i);
    //_p("%d,C(%d,%d):%d\n", a, N-1,i,c);
    ans += 1 + (a * c - 1) % 9;
    ans = 1 + (ans - 1) % 9;
  }

  int c0 = 0;
  forr(c, S) if (c == '0') c0++;
  if (ans == 0 && c0 != N) cout << 9 << endl;
  else cout << ans << endl;
}
int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int T = read<int>();
  while (T--) Main();
  return 0;
}
0