結果

問題 No.528 10^9と10^9+7と回文
ユーザー wk1080idwk1080id
提出日時 2019-03-28 02:36:58
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 2,389 bytes
コンパイル時間 654 ms
コンパイル使用メモリ 87,492 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-04-19 23:44:19
合計ジャッジ時間 2,479 ms
ジャッジサーバーID
(参考情報)
judge6 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<cctype>
#include<math.h>
#include<string>
#include<string.h>
#include<stack>
#include<queue>
#include<vector>
#include<utility>
#include<set>
#include<map>
#include<stdlib.h>
#include<iomanip>

using namespace std;

#define ll long long
#define ld long double
#define EPS 0.0000000001
#define INF 1e9
#define LINF (ll)INF*INF
#define MOD 1000000007
#define rep(i,n) for(int i=0;i<(n);i++)
#define loop(i,a,n) for(int i=a;i<(n);i++)
#define all(in) in.begin(),in.end()
#define shosu(x) fixed<<setprecision(x)

#define int ll //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int,int> pii;
typedef vector<pii> vp;

int gcd(int a, int b){
    if(b==0) return a;
    return gcd(b,a%b);
}
int lcm(int a, int b){
    return a/gcd(a,b)*b;
}


long long beki(long long a,int n,int mod){
    long long s = 1LL;
    while(n){
        if(n % 2) s = (s * a) % mod;
        a = (a * a) % mod;
        n /= 2;
    }
    return s;
}


int solve(string s, int mod){
    int n = s.size();
    // n桁の 回文数の 数え上げ(575)
    string t = "";
    rep(i,n/2)t += s[i];
    if(n%2) t += s[n/2];
    int ret = 0;
    int m = t.size();
    if(m > 1)t.front()--;
    rep(i,m){
        ret *= 10;
        ret += t[i] - '0';
        ret %= mod;
    }
    if(m > 1)ret++;
    //n桁未満の回文数を数え上げ
    //i桁の回文数は 0~9が使える桁が(i-1)/2個、1~9が使える桁が1個

    loop(i,1,n){
        ret += 9 * beki(10, (i-1)/2, mod) % mod;
        ret %= mod;
    }

    return ret;
}

signed main(void) {
    string s;
    cin >> s;
    int n = s.size();

    string t = "";
    bool c = s.front() == '1';
    loop(i,1,n)if(s[i] != '0')c = false;

    if(c){//10…0
        rep(i,n-1)t += "9";
    }else{//それ以外
        c = false;
        for(int i = 0; i < n/2; i++){
            if(c){
                t += "9";
            }else if(s[i] <= s[n-1-i]){
                t += s[i];
            }else{
                c = true;
                t += s[i]-1;
            }
        }
        string rev = t;
        reverse(all(rev));
        if(n%2){
            if(c)t += "9";
            else t += s[n/2];
        }
        t = t + rev;
    }

    cout << solve(t,1e9) << endl;
    cout << solve(t,1e9+7) << endl;

}
0