結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-12-01 02:38:31
言語 PyPy3
(7.3.15)
結果
TLE  
実行時間 -
コード長 2,231 bytes
コンパイル時間 527 ms
コンパイル使用メモリ 87,168 KB
実行使用メモリ 81,968 KB
最終ジャッジ日時 2023-10-12 07:06:33
合計ジャッジ時間 36,424 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 95 ms
76,336 KB
testcase_01 AC 165 ms
78,356 KB
testcase_02 AC 222 ms
79,664 KB
testcase_03 AC 103 ms
76,900 KB
testcase_04 AC 103 ms
77,192 KB
testcase_05 AC 161 ms
78,752 KB
testcase_06 AC 238 ms
79,436 KB
testcase_07 AC 168 ms
78,756 KB
testcase_08 AC 177 ms
78,568 KB
testcase_09 AC 114 ms
77,208 KB
testcase_10 AC 113 ms
77,388 KB
testcase_11 AC 111 ms
77,228 KB
testcase_12 AC 696 ms
78,504 KB
testcase_13 AC 720 ms
78,632 KB
testcase_14 AC 1,191 ms
79,804 KB
testcase_15 AC 1,158 ms
80,028 KB
testcase_16 AC 1,235 ms
80,528 KB
testcase_17 AC 1,267 ms
80,792 KB
testcase_18 AC 789 ms
80,348 KB
testcase_19 AC 788 ms
80,568 KB
testcase_20 AC 888 ms
81,300 KB
testcase_21 AC 868 ms
81,244 KB
testcase_22 AC 1,351 ms
81,924 KB
testcase_23 AC 1,279 ms
80,448 KB
testcase_24 AC 1,204 ms
81,084 KB
testcase_25 AC 1,284 ms
80,716 KB
testcase_26 AC 1,225 ms
80,320 KB
testcase_27 AC 1,158 ms
79,828 KB
testcase_28 AC 1,202 ms
81,180 KB
testcase_29 TLE -
testcase_30 AC 1,982 ms
80,408 KB
testcase_31 AC 1,955 ms
79,360 KB
testcase_32 TLE -
testcase_33 AC 1,414 ms
81,164 KB
testcase_34 AC 1,798 ms
80,804 KB
testcase_35 TLE -
権限があれば一括ダウンロードができます

ソースコード

diff #

import array

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

#dp small mod3 has3 mod p
def calc(x):
	sz = len(x)
	dp = [[[0 for col in range(2*4*2)] for row in range(p)] for hoge in range(2)]

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

	mooooood3 = [i%3 for i in range(12)]

	u = 0
	v = 1

	for i in range(sz) :
		k = ord(x[i]) - ord('0')

		if i<sz-5 :
			#initialize
			for small in range(2) :
				for mod3 in range(3) :
					for has3 in range(2) :
						s = (small<<3)|(mod3<<1)|has3
						dp[v][0][s] = 0

			for small in range(2) :
				for mod3 in range(3) :
					for has3 in range(2) :
						s = (small<<3)|(mod3<<1)|has3
						for d in range(10) :
							if small==0 and d>k :
								continue
							m1_ = small | (1 if d<k else 0)
							m2_ = mooooood3[mod3 + d]
							m3_ = has3 | (1 if d==3 else 0)
							s_ = (m1_<<3)|(m2_<<1)|m3_

							dp[v][0][s_] += dp[u][0][s]
							if(dp[v][0][s_] >= mod):
								dp[v][0][s_] -= mod

		else :
			for j in range(p) : 
				for small in range(2) :
					for mod3 in range(3) :
						for has3 in range(2) :
							s = (small<<3)|(mod3<<1)|has3
							dp[v][j][s] = 0
			for j in range(p) :

				for small in range(2) :
					for mod3 in range(3) :
						for has3 in range(2) :
							s = (small<<3)|(mod3<<1)|has3
							for d in range(10) :
								if small==0 and d>k :
									continue
								m1_ = small | (1 if d<k else 0)
								m2_ = mooooood3[mod3 + d]
								m3_ = has3 | (1 if d==3 else 0)
								s_ = (m1_<<3)|(m2_<<1)|m3_
							
								j_ = (j*10 + d) % p

								dp[v][j_][s_] += dp[u][j][s]
								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 small in range(2) :
			for mod3 in range(3) :
				for has3 in range(2) :
					if (has3==0 and mod3 != 0):
						continue
					s = (small<<3)|(mod3<<1)|has3
					ret += dp[u][j][s];
					ret += -mod if ret>=mod else 0

	return ret




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

ans += mod if ans<0 else 0

has3_ = 0
mod3_ = 0
modp_ = 0
for i in range(len(a)):
	k = ord(a[i])-ord('0')
	has3_ |= 1 if k == 3 else 0
	mod3_ = (mod3_ + k) % 3
	modp_ = (modp_ * 10 + k) % p

if (has3_!=0 or mod3_ == 0) and modp_ != 0 :
	ans += 1

ans %= mod

print(ans)

0