結果

問題 No.528 10^9と10^9+7と回文
ユーザー ei1333333ei1333333
提出日時 2017-06-10 00:15:16
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 71 ms / 1,000 ms
コード長 1,552 bytes
コンパイル時間 1,368 ms
コンパイル使用メモリ 145,892 KB
実行使用メモリ 12,344 KB
最終ジャッジ日時 2023-10-01 00:55:28
合計ジャッジ時間 3,044 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 5 ms
7,268 KB
testcase_03 AC 4 ms
7,532 KB
testcase_04 AC 4 ms
7,264 KB
testcase_05 AC 5 ms
7,316 KB
testcase_06 AC 4 ms
7,308 KB
testcase_07 AC 5 ms
7,324 KB
testcase_08 AC 4 ms
7,208 KB
testcase_09 AC 5 ms
7,324 KB
testcase_10 AC 71 ms
12,024 KB
testcase_11 AC 71 ms
12,100 KB
testcase_12 AC 71 ms
12,240 KB
testcase_13 AC 70 ms
12,344 KB
testcase_14 AC 44 ms
10,208 KB
testcase_15 AC 39 ms
9,824 KB
testcase_16 AC 37 ms
9,664 KB
testcase_17 AC 32 ms
9,228 KB
testcase_18 AC 68 ms
11,788 KB
testcase_19 AC 23 ms
8,560 KB
testcase_20 AC 51 ms
10,640 KB
testcase_21 AC 31 ms
9,216 KB
testcase_22 AC 5 ms
7,196 KB
testcase_23 AC 5 ms
7,216 KB
testcase_24 AC 5 ms
7,460 KB
testcase_25 AC 11 ms
7,756 KB
testcase_26 AC 71 ms
12,344 KB
testcase_27 AC 18 ms
12,212 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>

using namespace std;

typedef long long int64;

string S;
int64 dp[100001][2][2];
int64 dp2[100001];

int64 rec2(int idx, const int mod)
{
  if(idx <= 0) return (1);
  if(idx % 2 == 1) return (rec2(idx - 1, mod) * 10 % mod);
  if(~dp2[idx]) return (dp2[idx]);
  return (dp2[idx] = rec2(idx - 2, mod) * 10 % mod);
}

int64 rec(int idx, bool free, bool less, const int mod)
{
  if(~dp[idx][free][less]) return (dp[idx][free][less]);
  if(S.size() / 2 == idx) {
    if(less && !free) {
      if(S.size() % 2 == 0) return (0);
      if(S[idx] == '0') return (0);
      return (S[idx] - '0');
    }
    if(S.size() % 2 == 1) return (free ? 10 : S[idx] - '0' + 1);
    else return (1);
  }
  int64 ret = 0;
  int end = free ? 9 : S[idx] - '0';
  for(int i = 0; i <= end; i++) {
    if(idx == 0 && i == 0) continue;
    bool nt = free | (i != end);
    if(!nt && S[S.size() - idx - 1] - '0' < i) {
      (ret += rec(idx + 1, free | (i != end), true, mod)) %= mod;
    } else {
      (ret += rec(idx + 1, free | (i != end), less & (S[S.size() - idx - 1] - '0' <= i), mod)) %= mod;
    }
  }
  return (dp[idx][free][less] = ret);
}

int64 ct(int mod)
{
  memset(dp, -1, sizeof(dp));
  memset(dp2, -1, sizeof(dp2));
  int64 ret = 0;
  for(int i = 0; i < S.size() - 1; i++) {
    ret += rec2(i - 1, mod) * 9;
    ret %= mod;
  }
  return ((ret + rec(0, 0, 0, mod)) % mod);
}

int main()
{
  cin >> S;
  if(S.size() == 1) {
    cout << S << endl << S << endl;
    return (0);
  }
  cout << ct(1e9) << endl;
  cout << ct(1e9 + 7) << endl;
}
0