結果
| 問題 |
No.528 10^9と10^9+7と回文
|
| ユーザー |
tsutaj
|
| 提出日時 | 2019-04-15 16:07:28 |
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
| 結果 |
AC
|
| 実行時間 | 95 ms / 1,000 ms |
| コード長 | 2,423 bytes |
| コンパイル時間 | 888 ms |
| コンパイル使用メモリ | 103,028 KB |
| 実行使用メモリ | 6,944 KB |
| 最終ジャッジ日時 | 2024-07-23 18:35:29 |
| 合計ジャッジ時間 | 2,714 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| other | AC * 28 |
ソースコード
// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
using namespace std;
#define debug(...) fprintf(stderr, __VA_ARGS__)
#define int long long int
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
typedef pair<int, int> pii;
typedef long long ll;
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const ll INF = 1001001001001001LL;
const ll MOD = 1000000007LL;
int mod_pow(int n, int k, int mod) {
int res = 1;
for(; k>0; k>>=1) {
if(k & 1) (res *= n) %= mod;
(n *= n) %= mod;
}
return res;
}
int solve(string s, int mod) {
int ans = 0;
int N = s.length();
// 1 桁から N-1 桁までの回文数
for(int i=1; i<N; i++) {
// 最上位の桁は 1 から 9
// それ以外は 0 から 9
// (残っている桁数) / 2 の切り上げ だけ自由に決められる
int rem = (max(0LL, i - 2) + 1) / 2;
(ans += 9 * mod_pow(10, rem, mod)) %= mod;
}
// N 桁の回文数
// 上位 i 桁まで上限と一致している場合について考える
for(int i=0; i<N; i++) {
// i 桁まで決めたときに残っている桁数
int rem = max(N - 2 * i, 0LL);
if(rem == 0) break;
// 最上位の桁を小さくするなら、0 は許されない
int mul1 = max(0, s[i] - '0' - (i == 0));
int mul2 = mod_pow(10, (max(0LL, rem - 2) + 1) / 2, mod);
(ans += mul1 * mul2) %= mod;
}
// 上位 ceil(N / 2) 桁がすべて元と同じ場合
string t = s;
for(int i=0; i<(N+1)/2; i++) {
t[N-1-i] = s[i];
}
if(t <= s) (ans += 1) %= mod;
return ans;
}
signed main() {
string s; cin >> s;
const int MOD1 = 1000000000LL;
const int MOD2 = 1000000007LL;
cout << solve(s, MOD1) << endl << solve(s, MOD2) << endl;
return 0;
}
tsutaj