結果

問題 No.528 10^9と10^9+7と回文
ユーザー HachimoriHachimori
提出日時 2017-06-10 00:50:45
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 124 ms / 1,000 ms
コード長 2,073 bytes
コンパイル時間 1,023 ms
コンパイル使用メモリ 54,276 KB
実行使用メモリ 4,540 KB
最終ジャッジ日時 2023-10-01 00:56:41
合計ジャッジ時間 2,853 ms
ジャッジサーバーID
(参考情報)
judge12 / judge11
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 2 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 2 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 124 ms
4,452 KB
testcase_11 AC 124 ms
4,540 KB
testcase_12 AC 124 ms
4,376 KB
testcase_13 AC 124 ms
4,384 KB
testcase_14 AC 70 ms
4,376 KB
testcase_15 AC 62 ms
4,376 KB
testcase_16 AC 56 ms
4,376 KB
testcase_17 AC 46 ms
4,376 KB
testcase_18 AC 118 ms
4,420 KB
testcase_19 AC 31 ms
4,380 KB
testcase_20 AC 84 ms
4,376 KB
testcase_21 AC 46 ms
4,380 KB
testcase_22 AC 1 ms
4,384 KB
testcase_23 AC 2 ms
4,380 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 11 ms
4,376 KB
testcase_26 AC 124 ms
4,380 KB
testcase_27 AC 124 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
using namespace std;
const int DIGIT = 100005;
const int MOD_10_9   = 1000000000;
const int MOD_10_9_7 = 1000000007;

int nDigit;
int digit[DIGIT];

void read() {
    string s;
    cin >> s;
    nDigit = s.size();
    for (int i = 0; i < nDigit; ++i) {
        digit[i] = s[i] - '0';
    }
}

int mypow(int p, int n, int mod) {
    if (n == 0) return 1;
    int t = mypow(p, n / 2, mod);
    return n & 1 ? 1LL * t * t % mod * p % mod : 1LL * t * t % mod;
}

void work() {
    int remainDigit = nDigit;
    int sum_10_9   = 0;
    int sum_10_9_7 = 0;
    
    for (int idx = 0; remainDigit > 0; remainDigit -= 2, ++idx) {
        if (idx == 0) {
            sum_10_9   = (sum_10_9   + 1LL * (digit[idx] - 1) * mypow(10, (remainDigit - 1) / 2, MOD_10_9  )) % MOD_10_9  ;
            sum_10_9_7 = (sum_10_9_7 + 1LL * (digit[idx] - 1) * mypow(10, (remainDigit - 1) / 2, MOD_10_9_7)) % MOD_10_9_7;
        } else {
            sum_10_9   = (sum_10_9   + 1LL * (digit[idx]    ) * mypow(10, (remainDigit - 1) / 2, MOD_10_9  )) % MOD_10_9  ;
            sum_10_9_7 = (sum_10_9_7 + 1LL * (digit[idx]    ) * mypow(10, (remainDigit - 1) / 2, MOD_10_9_7)) % MOD_10_9_7;
        }
    }

    for (int curNDigit = 1; curNDigit < nDigit; ++curNDigit) {
        sum_10_9   = (sum_10_9   + 1LL * 9 * mypow(10, (curNDigit - 1) / 2, MOD_10_9  )) % MOD_10_9  ;
        sum_10_9_7 = (sum_10_9_7 + 1LL * 9 * mypow(10, (curNDigit - 1) / 2, MOD_10_9_7)) % MOD_10_9_7;
    }


    bool lastPalindrome = true;
    int toCompare[DIGIT];
    for (int i = 0; i < (nDigit + 1) / 2; ++i) {
        toCompare[i] = digit[i];
        toCompare[nDigit - 1 - i] = digit[i];
    }
    
    for (int i = 0; i < nDigit; ++i) {
        if (toCompare[i] > digit[i]) {
            lastPalindrome = false;
            break;
        } else if (toCompare[i] < digit[i]) {
            break;
        }
    }
    
    cout << (sum_10_9   + lastPalindrome) % MOD_10_9   << endl;
    cout << (sum_10_9_7 + lastPalindrome) % MOD_10_9_7 << endl;
}


int main() {
    read();
    work();
    return 0;
}
0