結果

問題 No.434 占い
ユーザー 🐬hec🐬hec
提出日時 2016-05-05 19:21:21
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 2,120 bytes
コンパイル時間 1,640 ms
コンパイル使用メモリ 164,664 KB
実行使用メモリ 8,468 KB
最終ジャッジ日時 2024-04-15 13:22:47
合計ジャッジ時間 5,125 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 6 ms
8,144 KB
testcase_01 AC 7 ms
8,204 KB
testcase_02 AC 6 ms
8,080 KB
testcase_03 AC 6 ms
8,120 KB
testcase_04 AC 6 ms
8,036 KB
testcase_05 AC 5 ms
8,468 KB
testcase_06 AC 6 ms
8,128 KB
testcase_07 AC 6 ms
8,016 KB
testcase_08 AC 9 ms
7,916 KB
testcase_09 AC 8 ms
8,040 KB
testcase_10 AC 7 ms
8,248 KB
testcase_11 AC 7 ms
8,068 KB
testcase_12 AC 7 ms
8,188 KB
testcase_13 AC 8 ms
8,028 KB
testcase_14 AC 8 ms
8,092 KB
testcase_15 RE -
testcase_16 RE -
testcase_17 RE -
testcase_18 RE -
testcase_19 RE -
testcase_20 RE -
testcase_21 RE -
testcase_22 RE -
testcase_23 AC 6 ms
8,048 KB
testcase_24 RE -
testcase_25 RE -
testcase_26 RE -
testcase_27 RE -
testcase_28 RE -
testcase_29 RE -
testcase_30 RE -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

#define _overload(_1,_2,_3,name,...) name
#define _rep(i,n) _range(i,0,n)
#define _range(i,a,b) for(int i=int(a);i<int(b);++i)
#define rep(...) _overload(__VA_ARGS__,_range,_rep,)(__VA_ARGS__)

#define _rrep(i,n) _rrange(i,n,0)
#define _rrange(i,a,b) for(int i=int(a)-1;i>=int(b);--i)
#define rrep(...) _overload(__VA_ARGS__,_rrange,_rrep,)(__VA_ARGS__)

using namespace std;

const int limit=100000;



int divisor[limit+1],prime[limit+1];

int factor[limit+1][10];

void init(){
    int total=0;
    for(int i=2;i<=limit;++i) divisor[i]=i;

    for(int p=2;p<=limit;++p){
        if(divisor[p]==p)
            prime[total++]=p;
        for(int i=0;i<total && prime[i]<=divisor[p];++i){
            int tmp=prime[i]*p;
            if(tmp<=limit)
                divisor[tmp]=prime[i];
            else
                break;
        }
    }

    for(int i=2;i<=limit;++i){
        if(divisor[i]==i){
            factor[i][i%9]++;
        }else{
            rep(j,10) factor[i][j]=factor[i/divisor[i]][j];
            factor[i][divisor[i]%9]++;
        }
    }
    return;
}

int power(int a,int n,int mod){
    int b=1;
    while(n){
        if(n&1) b=b*a%mod;
        a=a*a%mod;
        n>>=1;
    }
    return b;
}

const int t_limit=10000;
const int n_limit=10000;

int main(void){
    init();

    int t=0,all=0;
    scanf("%d",&t);
    assert(1<=t&&t<=t_limit);

    rep(loop,t){
        char s[limit+1];
        scanf("%s",s);
        int n=strlen(s);
        assert(1<=n);
        all+=n;
        

        bool allzero=true;
        rep(i,n) if(s[i]!='0') allzero=false;
        
        if(allzero){
            puts("0");
            continue;
        }

        int ans=0,f[10];
        fill(f,f+10,0);
        int num=n-1,den=1;
        rep(i,n){
            int cur=s[i]-'0';
            rep(j,10) cur=(cur*power(j,f[j],9))%9;
            ans=(ans+cur)%9;
            rep(j,10) f[j]+=factor[num][j];
            rep(j,10) f[j]-=factor[den][j];
            num--,den++;
        }
        if(ans==0) ans=9;
        printf("%d\n",ans);
    }
    assert(all<=n_limit);
    return 0;
}
0