結果

問題 No.528 10^9と10^9+7と回文
ユーザー wk1080idwk1080id
提出日時 2019-04-15 16:28:13
言語 C++11
(gcc 11.4.0)
結果
WA  
実行時間 -
コード長 3,150 bytes
コンパイル時間 911 ms
コンパイル使用メモリ 87,504 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 06:39:55
合計ジャッジ時間 2,447 ms
ジャッジサーバーID
(参考情報)
judge9 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 WA -
testcase_01 WA -
testcase_02 WA -
testcase_03 WA -
testcase_04 WA -
testcase_05 WA -
testcase_06 WA -
testcase_07 WA -
testcase_08 WA -
testcase_09 WA -
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 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
権限があれば一括ダウンロードができます

ソースコード

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' && s.size() > 1;
    loop(i,1,n)if(s[i] != '0')c = false;

    if(c){//10…0
        rep(i,n-1)t += "9";
    }else{//それ以外
        rep(i,n/2)t += s[i];
        int p = 0;
        rep(i,t.size()){
            if(p == 0 && t[t.size()-1-i] > s[(n+1)/2+i])p++;
            if(p == 0 && t[t.size()-1-i] < s[(n+1)/2+i])p--;
        }
        if(n%2){
            if(p == 1){
                if(s[(n+1)/2-1] > '0')s[(n+1)/2-1]--;
                else{
                    int ind = t.size()-1;
                    while(ind >= 0 && t[ind] == '0')ind++;
                    t[ind]--;
                    while(ind < t.size()-1){
                        ind++;
                        t[ind] = '9';
                    }
                    s[(n+1)/2-1] = '9';
                }
            }
            string rev = t;
            reverse(all(rev));
            t = t + s[(n+1)/2-1] + rev;
        }else{
            if(p == 1){
                int ind = t.size()-1;
                while(ind >= 0 && t[ind] == '0')ind++;
                t[ind]--;
                while(ind < t.size()-1){
                    ind++;
                    t[ind] = '9';
                }
            }
            string rev = t;
            reverse(all(rev));
            t = t + rev;
        }
    }
    cout << t << endl;
    cout << solve(t,1e9) << endl;
    cout << solve(t,1e9+7) << endl;

}
0