結果

問題 No.528 10^9と10^9+7と回文
ユーザー 👑 はまやんはまやんはまやんはまやん
提出日時 2017-06-10 12:32:46
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 67 ms / 1,000 ms
コード長 3,158 bytes
コンパイル時間 1,499 ms
コンパイル使用メモリ 166,532 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-10-01 00:58:44
合計ジャッジ時間 3,378 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 1 ms
4,376 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 2 ms
4,376 KB
testcase_07 AC 1 ms
4,376 KB
testcase_08 AC 2 ms
4,376 KB
testcase_09 AC 2 ms
4,380 KB
testcase_10 AC 66 ms
4,376 KB
testcase_11 AC 67 ms
4,380 KB
testcase_12 AC 66 ms
4,376 KB
testcase_13 AC 66 ms
4,376 KB
testcase_14 AC 39 ms
4,380 KB
testcase_15 AC 34 ms
4,376 KB
testcase_16 AC 31 ms
4,380 KB
testcase_17 AC 26 ms
4,376 KB
testcase_18 AC 63 ms
4,376 KB
testcase_19 AC 17 ms
4,376 KB
testcase_20 AC 45 ms
4,376 KB
testcase_21 AC 25 ms
4,380 KB
testcase_22 AC 2 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,380 KB
testcase_25 AC 7 ms
4,380 KB
testcase_26 AC 66 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 n;
//---------------------------------------------------------------------------------------------------
int to_integer(string s, int mod) {
    int res = 0;
    rep(i, 0, s.length()) {
        int c = s[i] - '0';
        res = (1LL * res * 10 + c) % mod;
    }
    return res;
}
//---------------------------------------------------------------------------------------------------
int count(int mod) {
    int len = (n + 1) / 2;

    string pre = N.substr(0, len);
    int ma = to_integer(pre, mod);

    string line = "1";
    rep(i, 0, len - 1) line += "0";
    int mi = to_integer(line, mod);

    return (ma - mi + mod) % mod;
}
//---------------------------------------------------------------------------------------------------
int solve(int mod) {
    n = N.length();
    int ans = 0;

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

    // 2-1
    ans = add(mod, ans, count(mod));

    // 2-2
    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() {
    cin >> N;
    cout << solve(1000000000) << endl;
    cout << solve(1000000007) << endl;
}
0