結果

問題 No.434 占い
ユーザー chakkuchakku
提出日時 2016-10-15 17:23:13
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 170 ms / 2,000 ms
コード長 2,544 bytes
コンパイル時間 1,546 ms
コンパイル使用メモリ 154,088 KB
実行使用メモリ 10,672 KB
最終ジャッジ日時 2023-08-14 14:07:18
合計ジャッジ時間 4,600 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 19 ms
10,128 KB
testcase_01 AC 19 ms
10,084 KB
testcase_02 AC 18 ms
10,400 KB
testcase_03 AC 18 ms
10,228 KB
testcase_04 AC 18 ms
10,092 KB
testcase_05 AC 18 ms
10,388 KB
testcase_06 AC 19 ms
10,184 KB
testcase_07 AC 17 ms
10,136 KB
testcase_08 AC 20 ms
10,016 KB
testcase_09 AC 20 ms
10,120 KB
testcase_10 AC 18 ms
10,216 KB
testcase_11 AC 20 ms
10,208 KB
testcase_12 AC 34 ms
10,176 KB
testcase_13 AC 20 ms
10,136 KB
testcase_14 AC 20 ms
10,288 KB
testcase_15 AC 49 ms
10,672 KB
testcase_16 AC 42 ms
10,248 KB
testcase_17 AC 35 ms
10,132 KB
testcase_18 AC 28 ms
10,084 KB
testcase_19 AC 39 ms
10,244 KB
testcase_20 AC 170 ms
10,212 KB
testcase_21 AC 49 ms
10,640 KB
testcase_22 AC 41 ms
10,248 KB
testcase_23 AC 18 ms
10,088 KB
testcase_24 AC 40 ms
10,176 KB
testcase_25 AC 19 ms
10,600 KB
testcase_26 AC 31 ms
10,488 KB
testcase_27 AC 34 ms
10,128 KB
testcase_28 AC 33 ms
10,128 KB
testcase_29 AC 45 ms
10,180 KB
testcase_30 AC 140 ms
10,184 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

#define REP(i,n) for(int i=0;i<n;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define INF 1<<29
#define LINF LLONG_MAX/3
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(v) (v).begin(),(v).end()
#define debug(x) cerr<<#x<<":"<<x<<endl
#define debug2(x,y) cerr<<#x<<","<<#y":"<<x<<","<<y<<endl
#define CININIT cin.tie(0),ios::sync_with_stdio(false)
template<typename T> ostream& operator<<(ostream& os,const vector<T>& vec){ os << "["; for(const auto& v : vec){ os << v << ","; } os << "]"; return os; }

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vvi;

constexpr ll mod = 1e9+7;

constexpr ll max_s = 100010;
vector<vector<int>> f;

int mod_pow(int x,int n,int m){
    //cerr << "in pow" << endl;
    int res=1;
    if(x==0) return 1;
    x%=m;
    //cerr << x << "  " << n << endl;
    while(n){
        //cerr << n << endl;
        if(n&1) res=(res*x)%m;
        x=(x*x)%m;
        n>>=1;
    }
    //cerr << "out pow" << endl;
    return res;
}

vector<vector<int>> init(){
    vi prime;
    vector<bool> u(max_s,true);
    u[0] = u[1] = false;
    for(int i=2;i<max_s;i++){
        if(!u[i]) continue;
        prime.push_back(i);
        for(int j=2;i*j<max_s;j++) u[i*j] = false;
    }

    vector<vector<int>> f(max_s,vector<int>(9,0));
    for(int p : prime){
        for(int num=p;num<max_s;num+=p){
            int tmp=num;
            int cnt=0;
            while(tmp%p==0){
                cnt++;
                tmp/=p;
            }
            f[num][p%9]+=cnt;
        }
    }
    return f;
}

void solve(){
    string s;cin>>s;
    int n=s.size();
    vector<int> v(n);
    rep(i,n) v[i]=s[i]-'0';

    if(*max_element(ALL(v))==0){
        cout << "0" << endl;
        return;
    }

    ll ans=0;
    vector<int> nCk(9,0);
    for(int i=0;i<n;i++){
        //cout << "check"  << endl;
        ll tmp=1;
        // tmp := n-1Ci = 0*nCk[0] * 1*nCk[1] * 2*nCk[2] ... * 8*nCk[8]
        //rep(j,9) tmp = (tmp*mod_pow(j,nCk[j],9))%9;
        rep(j,9){
            ll k = mod_pow(j,nCk[j],9);
            tmp = tmp * k %9;
        }
        tmp = tmp*v[i]%9;
        ans=(ans+tmp)%9;
//        debug(nCk);

        // calc n-1Ci+1 = n-1Ci * (n-(i+1)) / (i+1)
        rep(j,9) nCk[j] += f[n-i-1][j];
        rep(j,9) nCk[j] -= f[i+1][j];
    }
    if(ans==0) ans=9;
    cout << ans << endl;
}

int main(){
    int T;cin>>T;
    f = init();
    rep(i,T){
        solve();
    }
}
0