結果

問題 No.315 世界のなんとか3.5
ユーザー koyumeishikoyumeishi
提出日時 2015-12-08 23:20:40
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 54 ms / 2,000 ms
コード長 2,307 bytes
コンパイル時間 685 ms
コンパイル使用メモリ 75,016 KB
実行使用メモリ 4,396 KB
最終ジャッジ日時 2023-10-12 22:11:59
合計ジャッジ時間 2,839 ms
ジャッジサーバーID
(参考情報)
judge15 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 5 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 3 ms
4,352 KB
testcase_06 AC 5 ms
4,348 KB
testcase_07 AC 3 ms
4,348 KB
testcase_08 AC 3 ms
4,352 KB
testcase_09 AC 2 ms
4,348 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 14 ms
4,352 KB
testcase_13 AC 14 ms
4,348 KB
testcase_14 AC 26 ms
4,352 KB
testcase_15 AC 27 ms
4,348 KB
testcase_16 AC 27 ms
4,348 KB
testcase_17 AC 26 ms
4,348 KB
testcase_18 AC 14 ms
4,348 KB
testcase_19 AC 14 ms
4,352 KB
testcase_20 AC 17 ms
4,352 KB
testcase_21 AC 17 ms
4,352 KB
testcase_22 AC 28 ms
4,348 KB
testcase_23 AC 29 ms
4,396 KB
testcase_24 AC 26 ms
4,348 KB
testcase_25 AC 29 ms
4,348 KB
testcase_26 AC 26 ms
4,348 KB
testcase_27 AC 26 ms
4,348 KB
testcase_28 AC 26 ms
4,348 KB
testcase_29 AC 48 ms
4,348 KB
testcase_30 AC 46 ms
4,352 KB
testcase_31 AC 46 ms
4,352 KB
testcase_32 AC 54 ms
4,348 KB
testcase_33 AC 28 ms
4,352 KB
testcase_34 AC 43 ms
4,352 KB
testcase_35 AC 53 ms
4,356 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

constexpr long long MOD = 1000000007;

vector<vector<vector<pair<int,int>>>> memo(10, vector<vector<pair<int,int>>>(16));
void make_memo(){
	for(int k=0; k<10; k++){
		for(int a=0; a<2; ++a)
		for(int c=0; c<2; ++c)
		for(int b=0; b<(c==0?3:1); ++b){
			int s = (a<<3)|(b<<1)|c;
			vector<int> next(16, 0);

			for(int d=0; d<10; ++d){
				if(a==0 && d>k) break;
				int a_ = a | (d<k);
				int b_ = (b+d)%3;
				int c_ = c | (d==3);

				if(c_) b_ = 0;

				int s_ = (a_<<3)|(b_<<1)|c_;

				next[s_]++;
			}
			for(int i=0; i<16; i++){
				if(next[i]>0) memo[k][s].push_back({i, next[i]});
			}
		}
	}
}

long long calc(string& x, 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 = x.size();
	for(int i=0; i<sz; ++i){
		int k = x[i] - '0';
		for(int j=0; j<((i<sz-4)?1:p); ++j){
			for(int a=0; a<2; ++a)
			for(int c=0; c<2; ++c)
			for(int b=0; b<(c==0?3:1); ++b){
				int s=(a<<3)|(b<<1)|c;
				long long val = dp[j][s];
				dp[j][s] = 0;
				if(i<sz-5){
					for(auto& p: memo[k][s]){
						dp_[0][p.first] += val * p.second;
						if(dp_[0][p.first] >= MOD){
							dp_[0][p.first] %= MOD;
						}
					}
				}else for(int d=0; d<10; ++d){
					if(a==0 && d>k) break;
					int a_ = a | (d<k);
					int b_ = (b+d)%3;
					int c_ = c | (d==3);
					if(c_) b_ = 0;
					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_);
	
	make_memo();

	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