結果

問題 No.491 10^9+1と回文
ユーザー cherrypi59cherrypi59
提出日時 2019-04-15 14:01:07
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 6 ms / 1,000 ms
コード長 860 bytes
コンパイル時間 1,577 ms
コンパイル使用メモリ 170,244 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-09-22 07:53:43
合計ジャッジ時間 4,139 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 103
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
typedef long long lint;

lint N, p[10];
string S, cur;

int rec(int idx){
    if(idx==(cur.size()+1)/2){
        if(stoi(cur)<=N) return 1;
        return 0;
    }

    int res = 0;
    for(char i='0';i<='9';i++){
        cur[idx] = cur[cur.size()-1-idx] = i;
        res += rec(idx+1);
        cur[idx] = cur[cur.size()-1-idx] = '0';
    }
    return res;
}

int main(){
    cin >> N;

    p[0] = 1;
    for(int i=1;i<10;i++) p[i] = p[i-1] * 10;

    N = N / lint(1e9+1);
    S = to_string(N);

    for(int i=0;i<S.size();i++) cur += '0';

    lint ans = 0;
    int k = S.size();
    for(int i=1;i<k;i++) ans += 9 * p[(i-1)/2];

    for(char i='1';i<='9';i++){
        cur[0] = cur[cur.size()-1] = i;
        ans += rec(1);
        cur[0] = cur[cur.size()-1] = '0';
    }

    cout << ans << endl;
    return 0;
}
0