結果

問題 No.260 世界のなんとか3
ユーザー koyumeishikoyumeishi
提出日時 2015-12-09 00:09:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 4 ms / 2,000 ms
コード長 2,164 bytes
コンパイル時間 1,025 ms
コンパイル使用メモリ 73,752 KB
実行使用メモリ 5,376 KB
最終ジャッジ日時 2024-04-25 07:49:54
合計ジャッジ時間 1,904 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 4 ms
5,376 KB
testcase_04 AC 4 ms
5,376 KB
testcase_05 AC 3 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 3 ms
5,376 KB
testcase_08 AC 3 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 AC 4 ms
5,376 KB
testcase_11 AC 3 ms
5,376 KB
testcase_12 AC 3 ms
5,376 KB
testcase_13 AC 3 ms
5,376 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 3 ms
5,376 KB
testcase_16 AC 3 ms
5,376 KB
testcase_17 AC 3 ms
5,376 KB
testcase_18 AC 3 ms
5,376 KB
testcase_19 AC 3 ms
5,376 KB
testcase_20 AC 3 ms
5,376 KB
testcase_21 AC 3 ms
5,376 KB
testcase_22 AC 4 ms
5,376 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 3 ms
5,376 KB
testcase_25 AC 3 ms
5,376 KB
testcase_26 AC 3 ms
5,376 KB
testcase_27 AC 2 ms
5,376 KB
testcase_28 AC 3 ms
5,376 KB
testcase_29 AC 4 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:83:15: warning: too many arguments for format [-Wformat-extra-args]
   83 |         scanf("%s %s", a_,b_, p);
      |               ^~~~~~~
main.cpp:83:14: warning: ignoring return value of ‘int scanf(const char*, ...)’ declared with attribute ‘warn_unused_result’ [-Wunused-result]
   83 |         scanf("%s %s", a_,b_, p);
      |         ~~~~~^~~~~~~~~~~~~~~~~~~

ソースコード

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>>>(8));
void make_memo(){
	for(int k=0; k<10; k++){
		for(int a=0; a<2; ++a)
		for(int b=0; b<4; ++b){
			int s = (a<<2)|b;
			vector<int> next(8, 0);

			for(int d=0; d<10; ++d){
				if(a==0 && d>k) break;
				int a_ = a | (d<k);
				int b_ = (b==3||d==3)?3:(b+d)%3;
				int s_ = (a_<<2)|b_;
				next[s_]++;
			}
			for(int i=0; i<8; 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(p, vector<long long>(8, 0));
	vector<vector<long long>> dp_(p, vector<long long>(8, 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 b=0; b<4; ++b){
				int s=(a<<2)|b;
				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==3||d==3)?3:(b+d)%3;
					int s_ = (a_<<2)|b_;
					int j_ = (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<4; ++b){
		if(b!=0 && b!=3) continue;
		int s=(a<<2)|b;
		ret += dp[j][s];
		if(ret>=MOD) ret-=MOD;
	}
	return ret;
}

int main(){
	char a_[200010];
	char b_[200010];
	int p = 8;
	//cin >> a >> b >> p;
	//scanf("%s %s %d", a_,b_, &p);
	scanf("%s %s", 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';
		y |= k==3;
		if(y==0) x = (x+k)%3;
		z = (z*10+k)%p;
	}
	if((x==0 || y==1) && z!=0){
		ans++;
		ans %= MOD;
	}
	printf("%lld\n", ans);
	//cout << ans << endl;
	return 0;
}
0