結果

問題 No.528 10^9と10^9+7と回文
ユーザー mamekinmamekin
提出日時 2017-06-10 00:28:15
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 38 ms / 1,000 ms
コード長 1,850 bytes
コンパイル時間 918 ms
コンパイル使用メモリ 103,724 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-10-01 00:56:15
合計ジャッジ時間 2,475 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 1 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,380 KB
testcase_09 AC 1 ms
4,376 KB
testcase_10 AC 37 ms
4,376 KB
testcase_11 AC 37 ms
4,376 KB
testcase_12 AC 37 ms
4,376 KB
testcase_13 AC 38 ms
4,380 KB
testcase_14 AC 23 ms
4,376 KB
testcase_15 AC 20 ms
4,376 KB
testcase_16 AC 19 ms
4,376 KB
testcase_17 AC 16 ms
4,380 KB
testcase_18 AC 36 ms
4,376 KB
testcase_19 AC 11 ms
4,380 KB
testcase_20 AC 26 ms
4,376 KB
testcase_21 AC 16 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 1 ms
4,380 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 5 ms
4,380 KB
testcase_26 AC 38 ms
4,376 KB
testcase_27 AC 33 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#define _USE_MATH_DEFINES
#include <cstdio>
#include <iostream>
#include <sstream>
#include <fstream>
#include <iomanip>
#include <algorithm>
#include <cmath>
#include <complex>
#include <string>
#include <vector>
#include <list>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <bitset>
#include <numeric>
#include <limits>
#include <climits>
#include <cfloat>
#include <functional>
#include <iterator>
using namespace std;

int solve(const string& s, int mod)
{
    int n = s.size();

    long long ans = 0;
    long long tmp = 9;
    for(int i=1; i<n; ++i){
        ans += tmp;
        ans %= mod;
        if(i % 2 == 0){
            tmp *= 10;
            tmp %= mod;
        }
    }

    vector<int> dp(4, 0);
    dp[2] = 1;
    for(int i=0; i<(n+1)/2; ++i){
        vector<int> nextDp(4, 0);
        for(int a=0; a<4; ++a){
            for(int x=0; x<=9; ++x){
                if(i == 0 && x == 0)
                    continue;
                if((a & 2) && x > s[i] - '0')
                    continue;

                int b = a;
                if(x < s[i] - '0')
                    b &= ~2;
                if(x < s[n-1-i] - '0')
                    b &= ~1;
                else if(x > s[n-1-i] - '0')
                    b |= 1;
                nextDp[b] += dp[a];
                nextDp[b] %= mod;
            }
        }
        dp.swap(nextDp);
    }

    for(int i=0; i<3; ++i){
        ans += dp[i];
        ans %= mod;
    }
    return ans;
}

int solve(int n)
{
    int ans = 0;
    for(int i=1; i<=n; ++i){
        string s = to_string(i);
        string t = s;
        reverse(t.begin(), t.end());
        if(s == t)
            ++ ans;
    }
    return ans;
}

int main()
{
    string s;
    cin >> s;

    cout << solve(s, 1000000000) << endl
         << solve(s, 1000000007) << endl;

    return 0;
}
0