結果
問題 | No.315 世界のなんとか3.5 |
ユーザー |
![]() |
提出日時 | 2017-04-25 11:49:08 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
AC
|
実行時間 | 183 ms / 2,000 ms |
コード長 | 3,714 bytes |
コンパイル時間 | 1,665 ms |
コンパイル使用メモリ | 173,092 KB |
実行使用メモリ | 6,944 KB |
最終ジャッジ日時 | 2024-09-13 08:31:54 |
合計ジャッジ時間 | 5,341 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 36 |
ソースコード
#include<bits/stdc++.h>using namespace std;#define rep(i,a,b) for(int i=a;i<b;i++)int mod = 1000000007;int add(int x, int y) { return (x += y) >= mod ? x - mod : x; }template<class... T> int add(int x, T... y) { return add(x, add(y...)); }int mul(int x, int y) { return 1LL * x * y % mod; }template<class... T> int mul(int x, T... y) { return mul(x, mul(y...)); }int sub(int x, int y) { return add(x, mod - y); }int modpow(int a, long long b) { int ret = 1; while (b > 0) { if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1; } return ret; }int modinv(int a) { return modpow(a, mod - 2); }int fac[201010], ifac[201010];void initfac() {fac[0] = ifac[0] = 1;rep(i, 1, 201010) fac[i] = 1LL * i * fac[i - 1] % mod;rep(i, 1, 201010) ifac[i] = modinv(fac[i]);}int aCb(int a, int b) {if (b < 0 || a < b) return 0;return (1LL * fac[a] * ifac[a - b] % mod) * ifac[b] % mod;}//-----------------------------------------------------------------------------------int P;int dp[2][2][3][800][2];int count(string S) {int n = S.length();rep(dig, 0, 2) rep(up, 0, 2) rep(m3, 0, 3) rep(m800, 0, 800) rep(is3, 0, 2) dp[dig][up][m3][m800][is3] = 0;dp[0][1][0][0][0] = 1;rep(i, 0, n) {int dig = i % 2, ddig = (i + 1) % 2;int c = S[i] - '0';if (5 < abs(n - i)) {int m800 = 0;rep(up, 0, 2) rep(m3, 0, 3) rep(is3, 0, 2) dp[ddig][up][m3][m800][is3] = 0;rep(up, 0, 2) rep(m3, 0, 3) rep(is3, 0, 2) {int ma = 10;if (up == 1) ma = c + 1;rep(j, 0, ma) {int uup;int mm3 = (m3 * 10 + j) % 3;int mm800 = 0;int iis3 = is3 | (j == 3);if (up == 1 && c == j) uup = 1;else uup = 0;dp[ddig][uup][mm3][mm800][iis3] =add(dp[ddig][uup][mm3][mm800][iis3], dp[dig][up][m3][m800][is3]);}}} else {rep(up, 0, 2) rep(m3, 0, 3) rep(m800, 0, 800) rep(is3, 0, 2) dp[ddig][up][m3][m800][is3] = 0;rep(up, 0, 2) rep(m3, 0, 3) rep(m800, 0, 800) rep(is3, 0, 2) {int ma = 10;if (up == 1) ma = c + 1;rep(j, 0, ma) {int uup;int mm3 = (m3 * 10 + j) % 3;int mm800 = (m800 * 10 + j) % 800;int iis3 = is3 | (j == 3);if (up == 1 && c == j) uup = 1;else uup = 0;dp[ddig][uup][mm3][mm800][iis3] =add(dp[ddig][uup][mm3][mm800][iis3], dp[dig][up][m3][m800][is3]);}}}}int res = 0;int dig = n % 2;rep(up, 0, 2) {rep(m3, 0, 3) rep(m800, 0, 800) if(m800 % P != 0){res = add(res, dp[dig][up][m3][m800][1]);}rep(m3, 0, 3) rep(m800, 0, 800) if (m3 == 0 && m800 % P != 0) {res = add(res, dp[dig][up][m3][m800][0]);}}return res;}//-----------------------------------------------------------------------------------bool chk(string S) {int mo = 0;for (char c : S) mo = (mo * 10 + c - '0') % 2400;bool aho = (mo % 3 == 0);for (char c : S) if (c == '3') aho = true;bool seishun = (mo % P == 0);return aho && !seishun;}//-----------------------------------------------------------------------------------int main() {string A, B; cin >> A >> B >> P;int ans = sub(count(B), count(A));if (chk(A)) ans = add(ans, 1);cout << ans << endl;}