結果

問題 No.434 占い
ユーザー tossy
提出日時 2016-10-23 00:00:46
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
TLE  
実行時間 -
コード長 1,705 bytes
コンパイル時間 1,128 ms
コンパイル使用メモリ 95,204 KB
実行使用メモリ 13,644 KB
最終ジャッジ日時 2024-11-23 17:47:23
合計ジャッジ時間 26,502 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 20 TLE * 7
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <cassert>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <vector>
#include <set>
#include <map>
#include <bitset>
using namespace std;

#define repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define rep(i,n) repl(i,0,n)
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define fi first
#define se second

#define INF 2147483600

vector<int> primes;

// combinarion nCr mod M
long comb(long n, long r, long mod){
  if(r>n-r) r=n-r;
  vector<int> a(n);
  rep(i,r) a[i]=n-i;

  for(int p : primes){
    if(p>r) break;
    for(long q=p; q<=r; q*=p){
      for(int i=n%q, j=0; j<r/q; i+=q,j++){
        // r! のうち,qで割れる値はr/q個ある.また,a[i]はpでわりきれる
        a[i] /= p;
      }
    }
  }

  long mul=1;
  rep(i,r) mul = mul*a[i]%mod;
  return mul;
}

int main(){
  // sieve of Erastosthenes
  #define SZ 100000
  vector<bool> arr(SZ+2, true);
  arr[0]=arr[1]=false;
  for(int i=4;i<=SZ;i+=2) arr[i]=false;
  primes.pb(2);
  for(int i=3; i<=SZ/2; i+=2){
    if(arr[i]){
      primes.pb(i);
      for(int j=i*2; j<=SZ; j+=i) arr[j]=false;
    }
  }

  int tc;
  scanf("%d\n", &tc);
  rep(loops, tc){
    vector<int> vec;
    char c;
    while(scanf("%c", &c)){
      if(c=='\n') break;
      vec.pb(c-'0');
    }
    int n =vec.size();

    int res = 0;
    bool isZero = (vec[0]==0);
    rep(i,n){
      if(vec[i]!=0) isZero=false;
      res += vec[i] * comb(n-1, i, 9);
      res %= 9;
    }

    if(!isZero && res==0) res=9;
    printf("%d\n", res);
  }

  return 0;
}
0