結果
| 問題 |
No.528 10^9と10^9+7と回文
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2017-06-10 00:04:09 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 27 ms / 1,000 ms |
| コード長 | 2,316 bytes |
| コンパイル時間 | 728 ms |
| コンパイル使用メモリ | 70,676 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-23 18:29:43 |
| 合計ジャッジ時間 | 1,699 ms |
|
ジャッジサーバーID (参考情報) |
judge1 / judge5 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
#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;
}