結果

問題 No.315 世界のなんとか3.5
ユーザー yuppe19 😺yuppe19 😺
提出日時 2019-01-25 16:12:08
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 1,321 ms / 2,000 ms
コード長 2,083 bytes
コンパイル時間 1,250 ms
コンパイル使用メモリ 92,888 KB
実行使用メモリ 4,352 KB
最終ジャッジ日時 2023-10-14 09:51:05
合計ジャッジ時間 15,479 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 2 ms
4,352 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 1 ms
4,352 KB
testcase_05 AC 2 ms
4,352 KB
testcase_06 AC 2 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,348 KB
testcase_10 AC 2 ms
4,352 KB
testcase_11 AC 2 ms
4,352 KB
testcase_12 AC 114 ms
4,348 KB
testcase_13 AC 114 ms
4,352 KB
testcase_14 AC 229 ms
4,352 KB
testcase_15 AC 227 ms
4,352 KB
testcase_16 AC 265 ms
4,352 KB
testcase_17 AC 265 ms
4,352 KB
testcase_18 AC 135 ms
4,348 KB
testcase_19 AC 135 ms
4,348 KB
testcase_20 AC 341 ms
4,352 KB
testcase_21 AC 341 ms
4,348 KB
testcase_22 AC 664 ms
4,348 KB
testcase_23 AC 665 ms
4,348 KB
testcase_24 AC 267 ms
4,352 KB
testcase_25 AC 669 ms
4,352 KB
testcase_26 AC 266 ms
4,348 KB
testcase_27 AC 227 ms
4,348 KB
testcase_28 AC 659 ms
4,352 KB
testcase_29 AC 1,294 ms
4,352 KB
testcase_30 AC 517 ms
4,348 KB
testcase_31 AC 440 ms
4,348 KB
testcase_32 AC 1,315 ms
4,352 KB
testcase_33 AC 684 ms
4,352 KB
testcase_34 AC 1,297 ms
4,348 KB
testcase_35 AC 1,321 ms
4,348 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

constexpr int mod = static_cast<int>(powl(10, 9)) + 7;

int f(const string &s, const int P) {
  const int length = static_cast<int>(log10(P)) + 1;
  const int n = static_cast<int>(s.size());
  // dp[未満フラグ][has3][mod 3][mod P] := パターン数
  vector<vector<vector<vector<int>>>> dp(2, vector<vector<vector<int>>>(2, vector<vector<int>>(3, vector<int>(P, 0))));
  vector<vector<vector<vector<int>>>> ndp;
  dp[0][0][0][0] = 1;
  for(int i=0; i<n; ++i) {
    const bool upper = i < n-(2+length);
    const int LIM = upper ? 1 : P;
    ndp.assign(2, vector<vector<vector<int>>>(2, vector<vector<int>>(3, vector<int>(P, 0))));
    for(int less=0; less<2; ++less) {
      for(int has3=0; has3<2; ++has3) {
        for(int rem3=0; rem3<3; ++rem3) {
          for(int remP=0; remP<LIM; ++remP) {
            if(!dp[less][has3][rem3][remP]) { continue; }
            for(int d=0; d<=(less ? 9 : s[i]-'0'); ++d) {
              int nless = less || d < s[i] - '0',
                  nhas3 = has3 || d == 3,
                  nrem3 = (rem3 + d) % 3,
                  nremP = remP;
              if(!upper) { nremP = (remP * 10 + d) % P; }
              ndp[nless][nhas3][nrem3][nremP] += dp[less][has3][rem3][remP];
              ndp[nless][nhas3][nrem3][nremP] %= mod;
            }
          }
        }
      }
    }
    swap(dp, ndp);
  }
  int res = 0;
  for(int less=0; less<2; ++less) {
    for(int remP=1; remP<P; ++remP) {
      res += dp[less][0][0][remP];
      res %= mod;
      for(int rem3=0; rem3<3; ++rem3) {
        res += dp[less][1][rem3][remP];
        res %= mod;
      }
    }
  }
  return res;
}

void minus1(string *s) {
  int n = static_cast<int>(s->size());
  for(int i=n-1; i>=0; --i) {
    if(s->at(i) == '0') {
      s->at(i) = '9';
    } else {
      --s->at(i);
      break;
    }
  }
}

int main(void) {
  string a, b; cin >> a >> b;
  minus1(&a);
  int P; scanf("%d", &P);
  int res = f(b, P) - f(a, P);
  res += mod;
  res %= mod;
  printf("%d\n", res);
  return 0;
}
0