結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-12-01 02:19:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,932 ms / 2,000 ms
コード長 2,197 bytes
コンパイル時間 313 ms
コンパイル使用メモリ 82,944 KB
実行使用メモリ 80,432 KB
最終ジャッジ日時 2024-09-14 06:20:17
合計ジャッジ時間 30,485 ms
ジャッジサーバーID
(参考情報)
judge1 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 54 ms
62,976 KB
testcase_01 AC 130 ms
76,984 KB
testcase_02 AC 196 ms
78,524 KB
testcase_03 AC 81 ms
71,552 KB
testcase_04 AC 76 ms
71,936 KB
testcase_05 AC 130 ms
77,528 KB
testcase_06 AC 192 ms
78,088 KB
testcase_07 AC 129 ms
77,320 KB
testcase_08 AC 137 ms
77,168 KB
testcase_09 AC 75 ms
72,192 KB
testcase_10 AC 77 ms
72,548 KB
testcase_11 AC 74 ms
71,168 KB
testcase_12 AC 558 ms
76,888 KB
testcase_13 AC 579 ms
77,200 KB
testcase_14 AC 999 ms
77,856 KB
testcase_15 AC 977 ms
78,100 KB
testcase_16 AC 1,067 ms
78,900 KB
testcase_17 AC 1,050 ms
79,444 KB
testcase_18 AC 686 ms
78,752 KB
testcase_19 AC 674 ms
78,476 KB
testcase_20 AC 751 ms
79,480 KB
testcase_21 AC 739 ms
79,816 KB
testcase_22 AC 1,101 ms
79,820 KB
testcase_23 AC 1,091 ms
79,888 KB
testcase_24 AC 1,021 ms
79,176 KB
testcase_25 AC 1,105 ms
80,252 KB
testcase_26 AC 1,031 ms
79,296 KB
testcase_27 AC 977 ms
77,960 KB
testcase_28 AC 1,015 ms
79,512 KB
testcase_29 AC 1,714 ms
79,876 KB
testcase_30 AC 1,658 ms
78,488 KB
testcase_31 AC 1,581 ms
78,080 KB
testcase_32 AC 1,880 ms
79,692 KB
testcase_33 AC 1,195 ms
80,432 KB
testcase_34 AC 1,581 ms
79,692 KB
testcase_35 AC 1,932 ms
79,684 KB
権限があれば一括ダウンロードができます

ソースコード

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)]
	dp_ = [[0 for col in range(2*4*2)] for row in range(p)]

	dp[0][0] = 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_[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_ = (mod3 * 10 + d) % 3
							m3_ = has3 | (1 if d==3 else 0)
							s_ = (m1_<<3)|(m2_<<1)|m3_

							dp_[0][s_] += dp[0][s]
							if(dp_[0][s_] >= mod):
								dp_[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_[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_ = (mod3 * 10 + d) % 3
								m3_ = has3 | (1 if d==3 else 0)
								s_ = (m1_<<3)|(m2_<<1)|m3_
							
								j_ = (j*10 + d) % p

								dp_[j_][s_] += dp[j][s]
								if(dp_[j_][s_] >= mod):
									dp_[j_][s_] -= mod

		dp, dp_ = dp_, dp

	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[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_ * 10 + k) % 3
	modp_ = (modp_ * 10 + k) % p

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

ans %= mod

print(ans)

0