結果

問題 No.1185 完全な3の倍数
ユーザー titia
提出日時 2020-08-22 14:48:51
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 31 ms / 2,000 ms
コード長 1,128 bytes
コンパイル時間 213 ms
コンパイル使用メモリ 12,672 KB
実行使用メモリ 10,880 KB
最終ジャッジ日時 2024-11-24 09:32:36
合計ジャッジ時間 2,661 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input = sys.stdin.readline

N=int(input())
ANS=0
STRN=str(N)
LEN=len(STRN)

def calc(keta):
    ANS=0
    
    ni=int(STRN[keta])

    if keta==LEN-1:
        if ni<=2:
            return 1
        elif ni<=5:
            return 2
        elif ni<=8:
            return 3
        else:
            return 4

    if ni%3!=0:
        if ni<=2:
            return 1*pow(4,LEN-keta-1)
        elif ni<=5:
            return 2*pow(4,LEN-keta-1)
        elif ni<=8:
            return 3*pow(4,LEN-keta-1)
        else:
            return pow(4,LEN-keta)

    else:
        if ni==0:
            return calc(keta+1)
        elif ni==3:
            return calc(keta+1)+pow(4,LEN-keta-1)
        elif ni==6:
            return calc(keta+1)+2*pow(4,LEN-keta-1)
        else:
            return calc(keta+1)+3*pow(4,LEN-keta-1)
            
ANS=calc(0)-4

for i in range(10,min(N,100)+1):
    if i%3!=0:
        continue
    
    STR=str(i)
    flag=0

    for j in STR:
        if int(j)%3!=0:
            flag=1
    if flag and (int(STR[0])+int(STR[1]))%3==0:
        ANS+=1

print(ANS)
            
    
    
    

    
    
0