結果

問題 No.528 10^9と10^9+7と回文
ユーザー kimiyukikimiyuki
提出日時 2017-06-10 00:04:09
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 27 ms / 1,000 ms
コード長 2,316 bytes
コンパイル時間 578 ms
コンパイル使用メモリ 68,912 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-10-01 00:54:43
合計ジャッジ時間 2,041 ms
ジャッジサーバーID
(参考情報)
judge13 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 2 ms
4,376 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 1 ms
4,376 KB
testcase_07 AC 1 ms
4,380 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 1 ms
4,384 KB
testcase_10 AC 26 ms
4,376 KB
testcase_11 AC 26 ms
4,376 KB
testcase_12 AC 26 ms
4,380 KB
testcase_13 AC 26 ms
4,376 KB
testcase_14 AC 15 ms
4,380 KB
testcase_15 AC 14 ms
4,380 KB
testcase_16 AC 13 ms
4,380 KB
testcase_17 AC 11 ms
4,376 KB
testcase_18 AC 25 ms
4,380 KB
testcase_19 AC 8 ms
4,376 KB
testcase_20 AC 18 ms
4,376 KB
testcase_21 AC 10 ms
4,376 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 4 ms
4,380 KB
testcase_26 AC 27 ms
4,376 KB
testcase_27 AC 26 ms
4,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <string>
#include <algorithm>
#include <cassert>
#define repeat(i, n) for (int i = 0; (i) < int(n); ++(i))
#define repeat_reverse(i, n) for (int i = (n)-1; (i) >= 0; --(i))
#define whole(f, x, ...) ([&](decltype((x)) whole) { return (f)(begin(whole), end(whole), ## __VA_ARGS__); })(x)
using ll = long long;
using namespace std;

ll powmod(ll x, ll y, ll p) { // O(log y)
    assert (0 <= x and x < p);
    assert (0 <= y);
    ll z = 1;
    for (ll i = 1; i <= y; i <<= 1) {
        if (y & i) z = z * x % p;
        x = x * x % p;
    }
    return z;
}

constexpr int mod1 = 1e9;
constexpr int mod2 = 1e9+7;
int main() {
    string s; cin >> s;
    whole(reverse, s);
    int n = s.length();
    ll acc1 = 0;
    ll acc2 = 0;
    repeat (i, n-1) {
        acc1 += 9 *(ll) powmod(10, i/2, mod1);
        acc2 += 9 *(ll) powmod(10, i/2, mod2);
    }
    if (n == 1) {
        acc1 += s[0] - '0';
        acc2 += s[0] - '0';
    } else if (n == 2) {
        acc1 += min(s[0], s[1]) - '0';
        acc2 += min(s[0], s[1]) - '0';
    } else {
        acc1 += (s[n-1] - '1') * (ll) powmod(10, (n-1)/2, mod1) % mod1;
        acc2 += (s[n-1] - '1') * (ll) powmod(10, (n-1)/2, mod2) % mod2;
        for (int i = n-2; i > n/2; -- i) {
            acc1 += (s[i] - '0') *(ll) powmod(10, i-n/2, mod1) % mod1;
            acc2 += (s[i] - '0') *(ll) powmod(10, i-n/2, mod2) % mod2;
        }
        int i = n/2;
        assert (i != n-1);
        bool correction = false;
        if (n % 2 == 0) {
            repeat (di, n/2) {
                if (s[i+di] > s[i-di-1]) {
                    correction = true;
                    break;
                } else if (s[i+di] < s[i-di-1]) {
                    break;
                }
            }
        } else {
            repeat (di, n/2+1) {
                if (s[i+di] > s[i-di]) {
                    correction = true;
                    break;
                } else if (s[i+di] < s[i-di]) {
                    break;
                }
            }
        }
        acc1 += s[i] - '0' + 1 - int(correction);
        acc2 += s[i] - '0' + 1 - int(correction);
    }
    acc1 %= mod1;
    acc2 %= mod2;
    if (acc1 < 0) acc1 += mod1;
    if (acc2 < 0) acc2 += mod2;
    printf("%lld\n", acc1);
    printf("%lld\n", acc2);
    return 0;
}
0