結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-12-09 02:22:55
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,051 bytes
コンパイル時間 113 ms
コンパイル使用メモリ 11,208 KB
実行使用メモリ 9,508 KB
最終ジャッジ日時 2023-10-12 22:41:27
合計ジャッジ時間 26,670 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 22 ms
8,988 KB
testcase_01 AC 23 ms
8,864 KB
testcase_02 AC 53 ms
9,220 KB
testcase_03 AC 21 ms
8,912 KB
testcase_04 AC 22 ms
8,872 KB
testcase_05 AC 26 ms
8,880 KB
testcase_06 AC 55 ms
9,064 KB
testcase_07 AC 25 ms
8,936 KB
testcase_08 AC 28 ms
9,000 KB
testcase_09 AC 23 ms
8,920 KB
testcase_10 AC 23 ms
8,980 KB
testcase_11 AC 23 ms
8,872 KB
testcase_12 AC 763 ms
9,004 KB
testcase_13 AC 755 ms
9,092 KB
testcase_14 AC 1,510 ms
9,172 KB
testcase_15 AC 1,509 ms
8,992 KB
testcase_16 AC 1,520 ms
9,112 KB
testcase_17 AC 1,529 ms
9,172 KB
testcase_18 AC 778 ms
9,144 KB
testcase_19 AC 774 ms
9,040 KB
testcase_20 AC 817 ms
9,412 KB
testcase_21 AC 826 ms
9,368 KB
testcase_22 AC 1,582 ms
9,500 KB
testcase_23 AC 1,577 ms
9,508 KB
testcase_24 AC 1,532 ms
9,152 KB
testcase_25 AC 1,578 ms
9,436 KB
testcase_26 AC 1,530 ms
9,152 KB
testcase_27 AC 1,506 ms
9,124 KB
testcase_28 AC 1,612 ms
9,500 KB
testcase_29 TLE -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
testcase_34 -- -
testcase_35 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import array

mod = 1000000007
a,b,p = input().split()
p = int(p)

memo = [[[] for col in range(8)] for row in range(10)]
def gen_memo():
	global memo
	for k in range(10) :
		for sml in range(2) :
			for mod3 in range(4) :
				s = (sml<<2)|mod3
				nx = [0 for i in range(8)]
				for d in range(10) :
					if sml==0 and d>k :
						continue
					sml_ = 1 if (d<k or sml==1) else 0
					mod3_ = 3 if (mod3==3 or d==3) else (mod3+d)%3
					s_ = (sml_<<2) | mod3_
					nx[s_] += 1

				for s_ in range(8) :
					if nx[s_] > 0 :
						memo[k][s].append( (s_, nx[s_]) )


def calc(x):
	sz = len(x)
	dp = [[[0 for col in range(8)] for row in range(p)] for hoge in range(2)]

	dp[0][0][0] = 1

	u = 0
	v = 1

	for i in range(sz) :
		k = ord(x[i]) - ord('0')
		for j in range(1 if i<sz-4 else p):
			for sml in range(2):
				for mod3 in range(4):
					s = (sml<<2)|mod3
					val = dp[u][j][s]
					dp[u][j][s] = 0
					if val == 0:
						continue
					if i<sz-5:
						for c in range(len(memo[k][s])):
							s_ = memo[k][s][c][0]
							dp[v][0][ s_ ] += val * memo[k][s][c][1]
							if dp[v][0][s_] >= mod:
								dp[v][0][s_] %= mod
					else:
						for d in range(10) :
							if sml==0 and d>k :
								break
							sml_ = 1 if (sml==1 or d<k) else 0
							mod3_ = 3 if (mod3==3 or d==3) else (mod3+d)%3
							s_ = (sml_<<2) | mod3_
							j_ = (j*10 + d) % p
							dp[v][j_][s_] += val
							if dp[v][j_][s_] >= mod:
								dp[v][j_][s_] -= mod
		u,v = v,u

	ret = 0
	for j in range(p) : 
		if j%p == 0 :
			continue
		for sml in range(2) :
			for mod3 in range(4) :
				if mod3==0 or mod3==3:
					s = (sml<<2)|mod3
					ret += dp[u][j][s];
					ret += -mod if ret>=mod else 0
	return ret


gen_memo()

ans = calc(b)
ans = ans - calc(a)

ans += mod if ans<0 else 0

def hoge():
	global ans
	mod3__ = 0
	modp__ = 0
	for i in range(len(a)):
		k = ord(a[i])-ord('0')
		mod3__ = 3 if mod3__==3 or k==3 else (mod3__ + k)%3
		modp__ = (modp__ * 10 + k) % p

	if (mod3__==0 or mod3__==3) and modp__ != 0 :
		ans += 1

	ans %= mod

hoge()

print(ans)

0