結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-12-01 05:54:02
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 194 ms / 2,000 ms
コード長 1,521 bytes
コンパイル時間 2,073 ms
コンパイル使用メモリ 66,648 KB
実行使用メモリ 4,376 KB
最終ジャッジ日時 2023-10-12 07:22:51
合計ジャッジ時間 4,463 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 3 ms
4,352 KB
testcase_02 AC 8 ms
4,348 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 2 ms
4,348 KB
testcase_06 AC 8 ms
4,348 KB
testcase_07 AC 2 ms
4,348 KB
testcase_08 AC 2 ms
4,348 KB
testcase_09 AC 2 ms
4,372 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 41 ms
4,352 KB
testcase_13 AC 42 ms
4,348 KB
testcase_14 AC 83 ms
4,352 KB
testcase_15 AC 82 ms
4,352 KB
testcase_16 AC 83 ms
4,364 KB
testcase_17 AC 82 ms
4,352 KB
testcase_18 AC 43 ms
4,352 KB
testcase_19 AC 42 ms
4,348 KB
testcase_20 AC 48 ms
4,352 KB
testcase_21 AC 49 ms
4,352 KB
testcase_22 AC 88 ms
4,352 KB
testcase_23 AC 88 ms
4,356 KB
testcase_24 AC 82 ms
4,352 KB
testcase_25 AC 88 ms
4,352 KB
testcase_26 AC 82 ms
4,352 KB
testcase_27 AC 82 ms
4,368 KB
testcase_28 AC 83 ms
4,376 KB
testcase_29 AC 146 ms
4,348 KB
testcase_30 AC 141 ms
4,352 KB
testcase_31 AC 141 ms
4,348 KB
testcase_32 AC 168 ms
4,352 KB
testcase_33 AC 87 ms
4,352 KB
testcase_34 AC 126 ms
4,372 KB
testcase_35 AC 194 ms
4,372 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <vector>
#include <iostream>
#include <string>
#include <cstdio>
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(){
	char a_[200010];
	char b_[200010];
	int p;
	//cin >> a >> b >> p;
	scanf("%s %s %d", a_,b_, &p);
	string a(a_);
	string b(b_);
	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