結果

問題 No.528 10^9と10^9+7と回文
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-06-09 23:39:57
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
AC  
実行時間 66 ms / 1,000 ms
コード長 2,966 bytes
コンパイル時間 1,519 ms
コンパイル使用メモリ 166,820 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-10-01 00:54:36
合計ジャッジ時間 3,364 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<b;i++)
using namespace std; void _main(); int main() { cin.tie(0); ios::sync_with_stdio(false); _main(); }
//---------------------------------------------------------------------------------------------------
int add(int mod, int x, int y) { return (x += y) >= mod ? x - mod : x; }
template<class... T> int add(int mod, int x, T... y) { return add(mod, x, add(mod, y...)); }
int mul(int mod, int x, int y) { return 1LL * x * y % mod; }
template<class... T> int mul(int mod, int x, T... y) { return mul(mod, x, mul(mod, y...)); }
int sub(int mod, int x, int y) { return add(mod, x, mod - y); }
int modpow(int mod, int a, long long b) {
    int ret = 1; while (b > 0) {
        if (b & 1) ret = 1LL * ret * a % mod; a = 1LL * a * a % mod; b >>= 1;
    } return ret;
}
int modinv(int mod, int a) { return modpow(mod, a, mod - 2); }
/*---------------------------------------------------------------------------------------------------
            ∧_∧  
      ∧_∧  (´<_` )  Welcome to My Coding Space!
     ( ´_ゝ`) /  ⌒i     
    /   \     | |     
    /   / ̄ ̄ ̄ ̄/  |  
  __(__ニつ/     _/ .| .|____  
     \/____/ (u ⊃  
---------------------------------------------------------------------------------------------------*/




string N;
//---------------------------------------------------------------------------------------------------
int solve(int mod) {
    int n = N.length();
    int ans = 0;

    rep(len, 1, n) {
        int res = 9;
        if (len == 1) {
            ans = res;
            continue;
        }
        res = mul(mod, res, modpow(mod, 10, (len - 1) / 2));
        ans = add(mod, ans, res);
    }

    int sm = 0;
    rep(i, 0, (n + 1) / 2) sm = add(mod, mul(mod, sm, 10), N[i] - '0');
    int sb = 1;
    rep(i, 0, (n + 1) / 2 - 1) sb = mul(mod, sb, 10);
    sb = add(mod, sb, 0);
    ans = add(mod, ans, sub(mod, sm, sb));

    if (n % 2 == 0) {
        bool ok = true;
        rep(i, 0, n / 2) {
            if (N[n / 2 - 1 - i] > N[i + n / 2]) ok = false;
            if (N[n / 2 - 1 - i] < N[i + n / 2]) break;
        }
        if (ok) ans = add(mod, ans, 1);
    } else {
        bool ok = true;
        rep(i, 0, n / 2) {
            if (N[n / 2 - 1 - i] > N[i + n / 2 + 1]) ok = false;
            if (N[n / 2 - 1 - i] < N[i + n / 2 + 1]) break;
        }
        if (ok) ans = add(mod, ans, 1);
    }

    return ans;
}
//---------------------------------------------------------------------------------------------------
void _main() {
    /*rep(i, 999, 1112) {
        N = to_string(i);
        cout << "<<" << i << endl;
        cout << solve(1000000000) << endl;
        cout << solve(1000000007) << endl;
    }*/
    cin >> N;
    cout << solve(1000000000) << endl;
    cout << solve(1000000007) << endl;
}
0