結果
| 問題 | No.528 10^9と10^9+7と回文 | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2017-06-10 00:18:22 | 
| 言語 | C++14 (gcc 13.3.0 + boost 1.87.0) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 28 ms / 1,000 ms | 
| コード長 | 966 bytes | 
| コンパイル時間 | 1,708 ms | 
| コンパイル使用メモリ | 173,836 KB | 
| 実行使用メモリ | 6,944 KB | 
| 最終ジャッジ日時 | 2024-07-23 18:30:22 | 
| 合計ジャッジ時間 | 2,747 ms | 
| ジャッジサーバーID (参考情報) | judge1 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 28 | 
ソースコード
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll modpow(ll x, ll n, ll mod) {
	ll res = 1;
	while (n > 0) {
		if (n & 1) res = res * x % mod;
		x = x * x % mod;
		n >>= 1;
	}
	return res;
}
ll solve(string n, ll mod) {
	int sz = n.length();
	int m = (sz + 1) / 2;
	vector<ll> dp[2];
	for (int i = 0; i < 2; i++) dp[i].resize(m + 1, 0);
	dp[0][0] = 1;
	for (int i = 0; i < m; i++) {
		for (int j = 0; j < 2; j++) {
			int lim = j ? 9 : n[i] - '0';
			for (int d = 0; d <= lim; d++) {
				(dp[j || d < lim][i + 1] += dp[j][i]) %= mod;
			}
		}
	}
	ll ans = dp[1][m] - 1;
	(ans += (modpow(10, m - sz % 2, mod) + mod - 1) % mod) %= mod;
	string s1 = n.substr(0, sz / 2);
	reverse(s1.begin(), s1.end());
	string s2 = n.substr(m);
	if (s1 <= s2) (ans += dp[0][m]) %= mod;
	return ans;
}
int main() {
	cin.tie(0);
	ios::sync_with_stdio(false);
	string n;
	cin >> n;
	cout << solve(n, 1e9) << endl;
	cout << solve(n, 1e9 + 7) << endl;
	return 0;
}
            
            
            
        