結果

問題 No.528 10^9と10^9+7と回文
ユーザー yuppe19 😺yuppe19 😺
提出日時 2019-07-01 18:04:39
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 72 ms / 1,000 ms
コード長 1,819 bytes
コンパイル時間 729 ms
コンパイル使用メモリ 76,144 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-10-01 01:00:31
合計ジャッジ時間 2,493 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,380 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 1 ms
4,380 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,380 KB
testcase_06 AC 2 ms
4,380 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 72 ms
4,376 KB
testcase_11 AC 58 ms
4,376 KB
testcase_12 AC 58 ms
4,376 KB
testcase_13 AC 58 ms
4,380 KB
testcase_14 AC 44 ms
4,376 KB
testcase_15 AC 39 ms
4,376 KB
testcase_16 AC 36 ms
4,380 KB
testcase_17 AC 24 ms
4,380 KB
testcase_18 AC 55 ms
4,376 KB
testcase_19 AC 17 ms
4,376 KB
testcase_20 AC 41 ms
4,380 KB
testcase_21 AC 30 ms
4,380 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 7 ms
4,384 KB
testcase_26 AC 58 ms
4,380 KB
testcase_27 AC 20 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>
using namespace std;
using i64 = int64_t;

constexpr int mod0 = 1'000'000'000,
              mod1 = 1'000'000'007;

int cmp(int x, int y) {
  if(x <  y) { return 0; }
  if(x == y) { return 1; }
  return 2;
}

i64 f(const string &s, int mod) {
  // dp[未満/丁度/超過 上から][未満/丁度/超過 下から] := パターン数
  int n = static_cast<int>(s.size());
  vector<vector<i64>> dp(3, vector<i64>(3, 0)), ndp;
  dp[1][1] = 1;
  for(int i=0; i<=n-1-i; ++i) {
    ndp.assign(3, vector<i64>(3, 0));
    for(int state0=0; state0<3; ++state0) {
      for(int state1=0; state1<3; ++state1) {
        if(!dp[state0][state1]) { continue; }
        for(int d=(i==0); d<10; ++d) { // ちょうど n 桁にする
          int nstate0 = state0,
              nstate1 = cmp(d, s[n-1-i]-'0');
          if(nstate0 == 1) { nstate0 = cmp(d, s[i]-'0'); }
          if(nstate1 == 1) { nstate1 = state1; }
          if(nstate0 == 2) { continue; }
          ndp[nstate0][nstate1] += dp[state0][state1];
          ndp[nstate0][nstate1] %= mod;
        }
      }
    }
    swap(dp, ndp);
  }
  i64 res = 0;
  for(int state1=0; state1<3; ++state1) {
    res += dp[0][state1];
    res %= mod;
  }
  for(int state1=0; state1<2; ++state1) {
    res += dp[1][state1];
    res %= mod;
  }
  return res;
}

vector<i64> p10;

i64 solve(const string &s, int mod) {
  int n = static_cast<int>(s.size());
  p10.assign(n+5, 0);
  p10[0] = 1;
  for(int i=0; i<n; ++i) {
    p10[i+1] = p10[i] * 10 % mod;
  }
  i64 res = f(s, mod);
  for(int k=1; k<n; ++k) {
    i64 val = p10[(k-1)/2] * 9 % mod;
    res += val;
    res %= mod;
  }
  return res;
}

int main(void) {
  string s; cin >> s;
  i64 res0 = solve(s, mod0),
      res1 = solve(s, mod1);
  cout << res0 << '\n' << res1 << '\n';
  return 0;
}
0