結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-12-01 05:51:17
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 198 ms / 2,000 ms
コード長 1,417 bytes
コンパイル時間 573 ms
コンパイル使用メモリ 65,596 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-25 02:31:15
合計ジャッジ時間 5,125 ms
ジャッジサーバーID
(参考情報)
judge12 / judge13
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 7 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 8 ms
4,380 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 2 ms
4,380 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 2 ms
4,380 KB
testcase_11 AC 2 ms
4,380 KB
testcase_12 AC 42 ms
4,376 KB
testcase_13 AC 43 ms
4,380 KB
testcase_14 AC 84 ms
4,384 KB
testcase_15 AC 83 ms
4,376 KB
testcase_16 AC 84 ms
4,380 KB
testcase_17 AC 84 ms
4,380 KB
testcase_18 AC 44 ms
4,376 KB
testcase_19 AC 44 ms
4,380 KB
testcase_20 AC 49 ms
4,384 KB
testcase_21 AC 49 ms
4,376 KB
testcase_22 AC 90 ms
4,380 KB
testcase_23 AC 90 ms
4,380 KB
testcase_24 AC 85 ms
4,384 KB
testcase_25 AC 90 ms
4,380 KB
testcase_26 AC 84 ms
4,380 KB
testcase_27 AC 84 ms
4,376 KB
testcase_28 AC 83 ms
4,380 KB
testcase_29 AC 149 ms
4,384 KB
testcase_30 AC 146 ms
4,380 KB
testcase_31 AC 145 ms
4,380 KB
testcase_32 AC 173 ms
4,380 KB
testcase_33 AC 90 ms
4,376 KB
testcase_34 AC 129 ms
4,376 KB
testcase_35 AC 198 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <string>
using namespace std;

constexpr long long MOD = 1000000007;

long long calc(string& s, int p){
	vector<vector<long long>> dp(800, vector<long long>(16, 0));
	vector<vector<long long>> dp_(800, vector<long long>(16, 0));
	dp[0][0] = 1;
	int sz = s.size();
	for(int i=0; i<sz; i++){
		int k = s[i] - '0';
		for(int j=0; j<((i<sz-5)?1:p); j++){
			for(int a=0; a<2; a++)
			for(int b=0; b<3; b++)
			for(int c=0; c<2; c++){
				int s=(a<<3)|(b<<1)|c;
				long long val = dp[j][s];
				dp[j][s] = 0;
				for(int d=0; d<10; d++){
					if(a==0 && d>k) continue;
					int a_ = a | (d<k);
					int b_ = (b+d)%3;
					int c_ = c | (d==3);
					int s_ = (a_<<3)|(b_<<1)|c_;
					int j_ = (i<sz-5)?0:(j*10+d)%p;
					dp_[j_][s_] += val;
					if(dp_[j_][s_]>=MOD) dp_[j_][s_]-=MOD;
				}
			}
		}
		swap(dp,dp_);
	}
	long long ret = 0;
	for(int j=1; j<p; j++)
	for(int a=0; a<2; a++)
	for(int b=0; b<3; b++)
	for(int c=0; c<2; c++){
		if(b!=0 && c==0) continue;
		int s=(a<<3)|(b<<1)|c;
		ret += dp[j][s];
		if(ret>=MOD) ret-=MOD;
	}
	return ret;
}

int main(){
	string a,b;
	int p;
	cin >> a >> b >> p;
	long long ans = calc(b, p);
	ans = (ans - calc(a,p) + MOD) % MOD;

	int x=0;
	int y=0;
	int z=0;
	for(auto c:a){
		int k = c-'0';
		x = (x+k)%3;
		y |= k==3;
		z = (z*10+k)%p;
	}
	if((x==0 || y==1) && z!=0){
		ans++;
		ans %= MOD;
	}
	cout << ans << endl;
	return 0;
}
0