結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-12-09 02:23:19
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 555 ms / 2,000 ms
コード長 2,051 bytes
コンパイル時間 791 ms
コンパイル使用メモリ 87,088 KB
実行使用メモリ 80,200 KB
最終ジャッジ日時 2023-10-12 22:42:43
合計ジャッジ時間 11,833 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 76 ms
71,260 KB
testcase_01 AC 89 ms
75,928 KB
testcase_02 AC 127 ms
78,256 KB
testcase_03 AC 81 ms
75,636 KB
testcase_04 AC 83 ms
76,184 KB
testcase_05 AC 107 ms
76,632 KB
testcase_06 AC 138 ms
78,160 KB
testcase_07 AC 105 ms
76,800 KB
testcase_08 AC 108 ms
76,816 KB
testcase_09 AC 85 ms
75,804 KB
testcase_10 AC 84 ms
75,660 KB
testcase_11 AC 83 ms
75,808 KB
testcase_12 AC 182 ms
76,916 KB
testcase_13 AC 191 ms
77,096 KB
testcase_14 AC 283 ms
78,456 KB
testcase_15 AC 279 ms
77,904 KB
testcase_16 AC 304 ms
78,076 KB
testcase_17 AC 308 ms
77,972 KB
testcase_18 AC 224 ms
77,980 KB
testcase_19 AC 234 ms
77,828 KB
testcase_20 AC 280 ms
79,972 KB
testcase_21 AC 264 ms
78,732 KB
testcase_22 AC 348 ms
79,244 KB
testcase_23 AC 344 ms
78,716 KB
testcase_24 AC 319 ms
78,132 KB
testcase_25 AC 351 ms
79,236 KB
testcase_26 AC 321 ms
78,740 KB
testcase_27 AC 295 ms
77,704 KB
testcase_28 AC 357 ms
79,008 KB
testcase_29 AC 508 ms
80,148 KB
testcase_30 AC 462 ms
78,536 KB
testcase_31 AC 449 ms
78,152 KB
testcase_32 AC 516 ms
80,064 KB
testcase_33 AC 344 ms
79,340 KB
testcase_34 AC 500 ms
80,200 KB
testcase_35 AC 555 ms
80,012 KB
権限があれば一括ダウンロードができます

ソースコード

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