結果

問題 No.528 10^9と10^9+7と回文
ユーザー finefine
提出日時 2017-06-10 00:18:22
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 29 ms / 1,000 ms
コード長 966 bytes
コンパイル時間 1,717 ms
コンパイル使用メモリ 170,432 KB
実行使用メモリ 4,380 KB
最終ジャッジ日時 2023-10-01 00:55:32
合計ジャッジ時間 3,059 ms
ジャッジサーバーID
(参考情報)
judge15 / judge11
このコードへのチャレンジ(β)

テストケース

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

ソースコード

diff #

#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;
}
0