結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-12-01 02:19:13
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,963 ms / 2,000 ms
コード長 2,197 bytes
コンパイル時間 282 ms
コンパイル使用メモリ 87,300 KB
実行使用メモリ 81,708 KB
最終ジャッジ日時 2023-10-12 07:03:34
合計ジャッジ時間 31,693 ms
ジャッジサーバーID
(参考情報)
judge14 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 81 ms
76,620 KB
testcase_01 AC 145 ms
78,248 KB
testcase_02 AC 208 ms
79,180 KB
testcase_03 AC 99 ms
77,184 KB
testcase_04 AC 100 ms
77,296 KB
testcase_05 AC 150 ms
78,440 KB
testcase_06 AC 210 ms
79,108 KB
testcase_07 AC 148 ms
78,472 KB
testcase_08 AC 156 ms
78,276 KB
testcase_09 AC 99 ms
77,204 KB
testcase_10 AC 99 ms
77,124 KB
testcase_11 AC 99 ms
77,076 KB
testcase_12 AC 592 ms
78,500 KB
testcase_13 AC 586 ms
78,692 KB
testcase_14 AC 986 ms
79,624 KB
testcase_15 AC 998 ms
79,684 KB
testcase_16 AC 1,070 ms
79,964 KB
testcase_17 AC 1,052 ms
79,980 KB
testcase_18 AC 689 ms
80,800 KB
testcase_19 AC 688 ms
80,440 KB
testcase_20 AC 755 ms
80,940 KB
testcase_21 AC 746 ms
81,084 KB
testcase_22 AC 1,122 ms
80,804 KB
testcase_23 AC 1,099 ms
80,936 KB
testcase_24 AC 1,051 ms
80,180 KB
testcase_25 AC 1,119 ms
81,072 KB
testcase_26 AC 1,035 ms
80,236 KB
testcase_27 AC 1,000 ms
79,596 KB
testcase_28 AC 1,025 ms
81,020 KB
testcase_29 AC 1,704 ms
81,708 KB
testcase_30 AC 1,725 ms
79,716 KB
testcase_31 AC 1,572 ms
78,764 KB
testcase_32 AC 1,899 ms
81,160 KB
testcase_33 AC 1,205 ms
81,088 KB
testcase_34 AC 1,589 ms
81,228 KB
testcase_35 AC 1,963 ms
80,340 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