結果

問題 No.434 占い
ユーザー vwxyzvwxyz
提出日時 2023-05-19 06:17:56
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 468 ms / 2,000 ms
コード長 3,055 bytes
コンパイル時間 256 ms
コンパイル使用メモリ 11,176 KB
実行使用メモリ 13,672 KB
最終ジャッジ日時 2023-08-22 16:26:55
合計ジャッジ時間 6,419 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
12,056 KB
testcase_01 AC 75 ms
12,020 KB
testcase_02 AC 75 ms
11,992 KB
testcase_03 AC 76 ms
12,096 KB
testcase_04 AC 75 ms
12,004 KB
testcase_05 AC 75 ms
12,172 KB
testcase_06 AC 79 ms
12,148 KB
testcase_07 AC 77 ms
12,120 KB
testcase_08 AC 84 ms
12,136 KB
testcase_09 AC 85 ms
11,936 KB
testcase_10 AC 84 ms
11,996 KB
testcase_11 AC 88 ms
12,116 KB
testcase_12 AC 114 ms
12,320 KB
testcase_13 AC 84 ms
12,144 KB
testcase_14 AC 85 ms
11,880 KB
testcase_15 AC 179 ms
13,672 KB
testcase_16 AC 175 ms
12,316 KB
testcase_17 AC 175 ms
11,944 KB
testcase_18 AC 170 ms
12,144 KB
testcase_19 AC 200 ms
12,116 KB
testcase_20 AC 468 ms
12,364 KB
testcase_21 AC 177 ms
13,484 KB
testcase_22 AC 176 ms
12,356 KB
testcase_23 AC 76 ms
11,996 KB
testcase_24 AC 166 ms
12,240 KB
testcase_25 AC 94 ms
13,492 KB
testcase_26 AC 122 ms
12,792 KB
testcase_27 AC 168 ms
11,880 KB
testcase_28 AC 170 ms
12,208 KB
testcase_29 AC 213 ms
12,200 KB
testcase_30 AC 404 ms
12,388 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
readline=sys.stdin.readline

def Extended_Euclid(n,m):
    stack=[]
    while m:
        stack.append((n,m))
        n,m=m,n%m
    if n>=0:
        x,y=1,0
    else:
        x,y=-1,0
    for i in range(len(stack)-1,-1,-1):
        n,m=stack[i]
        x,y=y,x-(n//m)*y
    return x,y

class MOD:
    def __init__(self,p,e=None):
        self.p=p
        self.e=e
        if self.e==None:
            self.mod=self.p
        else:
            self.mod=self.p**self.e

    def Pow(self,a,n):
        a%=self.mod
        if n>=0:
            return pow(a,n,self.mod)
        else:
            #assert math.gcd(a,self.mod)==1
            x=Extended_Euclid(a,self.mod)[0]
            return pow(x,-n,self.mod)

    def Build_Fact(self,N):
        assert N>=0
        self.factorial=[1]
        if self.e==None:
            for i in range(1,N+1):
                self.factorial.append(self.factorial[-1]*i%self.mod)
        else:
            self.cnt=[0]*(N+1)
            for i in range(1,N+1):
                self.cnt[i]=self.cnt[i-1]
                ii=i
                while ii%self.p==0:
                    ii//=self.p
                    self.cnt[i]+=1
                self.factorial.append(self.factorial[-1]*ii%self.mod)
        self.factorial_inve=[None]*(N+1)
        self.factorial_inve[-1]=self.Pow(self.factorial[-1],-1)
        for i in range(N-1,-1,-1):
            ii=i+1
            while ii%self.p==0:
                ii//=self.p
            self.factorial_inve[i]=(self.factorial_inve[i+1]*ii)%self.mod

    def Build_Inverse(self,N):
        self.inverse=[None]*(N+1)
        assert self.p>N
        self.inverse[1]=1
        for n in range(2,N+1):
            if n%self.p==0:
                continue
            a,b=divmod(self.mod,n)
            self.inverse[n]=(-a*self.inverse[b])%self.mod
    
    def Inverse(self,n):
        return self.inverse[n]

    def Fact(self,N):
        if N<0:
            return 0
        retu=self.factorial[N]
        if self.e!=None and self.cnt[N]:
            retu*=pow(self.p,self.cnt[N],self.mod)%self.mod
            retu%=self.mod
        return retu

    def Fact_Inve(self,N):
        if self.e!=None and self.cnt[N]:
            return None
        return self.factorial_inve[N]

    def Comb(self,N,K,divisible_count=False):
        if K<0 or K>N:
            return 0
        retu=self.factorial[N]*self.factorial_inve[K]%self.mod*self.factorial_inve[N-K]%self.mod
        if self.e!=None:
            cnt=self.cnt[N]-self.cnt[N-K]-self.cnt[K]
            if divisible_count:
                return retu,cnt
            else:
                retu*=pow(self.p,cnt,self.mod)
                retu%=self.mod
        return retu

T=int(readline())
mod=9
MD=MOD(3,2)
MD.Build_Fact(10**5)
for t in range(T):
    S=list(map(int,list(readline().rstrip())))
    N=len(S)
    if all(s==0 for s in S):
        ans=0
    else:
        ans=0
        for i in range(N):
            ans+=S[i]*MD.Comb(N-1,i)%mod
            ans%=mod
        ans-=1
        ans%=mod
        ans+=1
    print(ans)
0