結果

問題 No.315 世界のなんとか3.5
ユーザー vwxyzvwxyz
提出日時 2023-02-02 14:35:19
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,630 bytes
コンパイル時間 527 ms
コンパイル使用メモリ 11,244 KB
実行使用メモリ 17,508 KB
最終ジャッジ日時 2023-09-14 20:51:39
合計ジャッジ時間 10,059 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
14,988 KB
testcase_01 AC 35 ms
10,460 KB
testcase_02 AC 145 ms
10,612 KB
testcase_03 AC 60 ms
10,608 KB
testcase_04 AC 66 ms
10,548 KB
testcase_05 AC 51 ms
10,576 KB
testcase_06 AC 58 ms
10,548 KB
testcase_07 AC 52 ms
10,748 KB
testcase_08 AC 244 ms
10,460 KB
testcase_09 AC 223 ms
10,644 KB
testcase_10 AC 167 ms
10,588 KB
testcase_11 AC 234 ms
10,644 KB
testcase_12 AC 1,892 ms
12,196 KB
testcase_13 AC 1,872 ms
12,116 KB
testcase_14 TLE -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import bisect
import copy
import decimal
import fractions
import heapq
import itertools
import math
import random
import sys
import time
from collections import Counter,deque,defaultdict
from functools import lru_cache,reduce
from heapq import heappush,heappop,heapify,heappushpop,_heappop_max,_heapify_max
def _heappush_max(heap,item):
    heap.append(item)
    heapq._siftdown_max(heap, 0, len(heap)-1)
def _heappushpop_max(heap, item):
    if heap and item < heap[0]:
        item, heap[0] = heap[0], item
        heapq._siftup_max(heap, 0)
    return item
from math import gcd as GCD
read=sys.stdin.read
readline=sys.stdin.readline
readlines=sys.stdin.readlines
write=sys.stdout.write

A,B,P=readline().split()
A=list(map(int,list(A)))
B=list(map(int,list(B)))
P=int(P)
mod=10**9+7
def solve(N):
    if len(N)<=6:
        ans=0
        N=int("".join(map(str,N)))
        for n in range(1,N):
            if (n%3==0 or "3" in str(n)) and n%P:
                ans+=1
        return ans
    dp0=[0]*3
    dp1=[0]*3
    for i in range(1,N[0]):
        if i==3:
            dp1[i%3]+=1
        else:
            dp0[i%3]+=1
    s=N[0]
    bl=(N[0]==3)
    for n in N[1:-5]:
        prev0=dp0
        prev1=dp1
        dp0=[0]*3
        dp1=[0]*3
        for p in range(3):
            for i in range(10):
                if i==3:
                    dp1[(p+i)%3]+=prev0[p]
                    dp1[(p+i)%3]+=prev1[p]
                    dp1[(p+i)%3]%=mod
                else:
                    dp0[(p+i)%3]+=prev0[p]
                    dp1[(p+i)%3]+=prev1[p]
                    dp0[(p+i)%3]%=mod
                    dp1[(p+i)%3]%=mod
        for i in range(n):
            if bl|(i==3):
                dp1[(s+i)%3]+=1
            else:
                dp0[(s+i)%3]+=1
        for i in range(1,10):
            if i==3:
                dp1[i%3]+=1
            else:
                dp0[i%3]+=1
        s=(s+n)%3
        bl|=(n==3)
    X=N[-5]*10000+N[-4]*1000+N[-3]*100+N[-2]*10+N[-1]
    ans=0
    for i in range(3):
        for x in range(100000):
            if x%P==0:
                continue
            if (i+x)%3==0 or "3" in str(x):
                ans+=dp0[i]
            ans+=dp1[i]
    for x in range(1,X):
        if x%P==0:
            continue
        if (s+x)%3==0 or "3" in str(x) or bl:
            ans+=1
    for x in range(1,100000):
        if x%P==0:
            continue
        if x%3==0 or "3" in str(x):
            ans+=1
    return ans%mod
ans=(solve(B)-solve(A))%mod
B=[0]*4+B
if (3 in B or sum(B)%3==0) and (B[-5]*10000+B[-4]*1000+B[-3]*100+B[-2]*10+B[-1])%P!=0:
    ans+=1
    ans%=mod
print(ans)
0