結果

問題 No.528 10^9と10^9+7と回文
ユーザー antaanta
提出日時 2017-06-09 22:38:38
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 79 ms / 1,000 ms
コード長 3,180 bytes
コンパイル時間 1,634 ms
コンパイル使用メモリ 170,192 KB
実行使用メモリ 8,444 KB
最終ジャッジ日時 2023-10-01 00:53:09
合計ジャッジ時間 3,672 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,376 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 1 ms
4,380 KB
testcase_03 AC 2 ms
4,380 KB
testcase_04 AC 1 ms
4,376 KB
testcase_05 AC 2 ms
4,376 KB
testcase_06 AC 1 ms
4,376 KB
testcase_07 AC 2 ms
4,376 KB
testcase_08 AC 1 ms
4,376 KB
testcase_09 AC 2 ms
4,376 KB
testcase_10 AC 79 ms
8,412 KB
testcase_11 AC 79 ms
8,300 KB
testcase_12 AC 79 ms
8,356 KB
testcase_13 AC 79 ms
8,412 KB
testcase_14 AC 46 ms
6,396 KB
testcase_15 AC 41 ms
6,116 KB
testcase_16 AC 38 ms
5,916 KB
testcase_17 AC 31 ms
5,460 KB
testcase_18 AC 75 ms
8,060 KB
testcase_19 AC 21 ms
4,868 KB
testcase_20 AC 54 ms
6,976 KB
testcase_21 AC 31 ms
5,456 KB
testcase_22 AC 1 ms
4,376 KB
testcase_23 AC 2 ms
4,376 KB
testcase_24 AC 2 ms
4,376 KB
testcase_25 AC 8 ms
4,376 KB
testcase_26 AC 79 ms
8,444 KB
testcase_27 AC 59 ms
8,444 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define rer(i,l,u) for(int (i)=(int)(l);(i)<=(int)(u);++(i))
#define reu(i,l,u) for(int (i)=(int)(l);(i)<(int)(u);++(i))
static const int INF = 0x3f3f3f3f; static const long long INFL = 0x3f3f3f3f3f3f3f3fLL;
typedef vector<int> vi; typedef pair<int, int> pii; typedef vector<pair<int, int> > vpii; typedef long long ll;
template<typename T, typename U> static void amin(T &x, U y) { if (y < x) x = y; }
template<typename T, typename U> static void amax(T &x, U y) { if (x < y) x = y; }


struct GModInt {
	static int Mod;
	unsigned x;
	GModInt() : x(0) { }
	GModInt(signed sig) { int sigt = sig % Mod; if (sigt < 0) sigt += Mod; x = sigt; }
	GModInt(signed long long sig) { int sigt = sig % Mod; if (sigt < 0) sigt += Mod; x = sigt; }
	int get() const { return (int)x; }

	GModInt &operator+=(GModInt that) { if ((x += that.x) >= (unsigned)Mod) x -= Mod; return *this; }
	GModInt &operator-=(GModInt that) { if ((x += Mod - that.x) >= (unsigned)Mod) x -= Mod; return *this; }
	GModInt &operator*=(GModInt that) { x = (unsigned long long)x * that.x % Mod; return *this; }
	GModInt &operator/=(GModInt that) { return *this *= that.inverse(); }

	GModInt operator+(GModInt that) const { return GModInt(*this) += that; }
	GModInt operator-(GModInt that) const { return GModInt(*this) -= that; }
	GModInt operator*(GModInt that) const { return GModInt(*this) *= that; }
	GModInt operator/(GModInt that) const { return GModInt(*this) /= that; }

	//Modと素であることが保証されるかどうか確認すること!
	GModInt inverse() const {
		signed a = x, b = Mod, u = 1, v = 0;
		while (b) {
			signed t = a / b;
			a -= t * b; std::swap(a, b);
			u -= t * v; std::swap(u, v);
		}
		if (u < 0) u += Mod;
		GModInt res; res.x = (unsigned)u;
		return res;
	}

	bool operator==(GModInt that) const { return x == that.x; }
	bool operator!=(GModInt that) const { return x != that.x; }
	GModInt operator-() const { GModInt t; t.x = x == 0 ? 0 : Mod - x; return t; }
};
int GModInt::Mod = 0;
typedef GModInt mint;
mint operator^(mint a, unsigned long long k) {
	mint r = 1;
	while (k) {
		if (k & 1) r *= a;
		a *= a;
		k >>= 1;
	}
	return r;
}

mint solve(const string &S) {
	int len = (int)S.size();
	mint res;
	rer(i, 1, len - 1)
		res += (mint(10) ^ ((i - 1) / 2)) * 9;
	mint undef; undef.x = -1;
	vector<mint> memo((len + 1) * 4, undef);
	auto rec = [&](auto rec, int i, bool ltL, bool leR) -> mint {
		mint &r = memo[(i + 1) * 4 + ltL * 2 + leR];
		if (r.x != ~0U) return r;
		int j = len - 1 - i;
		if (i < j)
			return r = ltL || leR ? 1 : 0;
		r = mint();
		int dL = S[j] - '0', dR = S[i] - '0';
		rep(d, 10) {
			if (i == len - 1 && d == 0) continue;
			if (!ltL && dL < d) continue;
			r += rec(rec, i - 1, ltL || d < dL,
				dR < d ? false : d < dR ? true : leR);
		}
		return r;
	};
	res += rec(rec, len - 1, false, true);
	return res;
}

int main() {
	char S[100002];
	while (~scanf("%s", S)) {
		mint::Mod = (int)1e9;
		int ans1 = solve(S).get();
		mint::Mod = (int)1e9 + 7;
		int ans2 = solve(S).get();
		printf("%d\n", ans1);
		printf("%d\n", ans2);
	}
	return 0;
}
0