結果

問題 No.493 とても長い数列と文字列(Long Long Sequence and a String)
ユーザー antaanta
提出日時 2017-03-10 22:52:04
言語 C++14
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 2 ms / 800 ms
コード長 2,764 bytes
コンパイル時間 1,736 ms
コンパイル使用メモリ 168,760 KB
実行使用メモリ 6,948 KB
最終ジャッジ日時 2024-06-24 08:26:55
合計ジャッジ時間 4,025 ms
ジャッジサーバーID
(参考情報)
judge3 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 115
権限があれば一括ダウンロードができます

ソースコード

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

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

	ModInt &operator+=(ModInt that) { if ((x += that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator-=(ModInt that) { if ((x += MOD - that.x) >= MOD) x -= MOD; return *this; }
	ModInt &operator*=(ModInt that) { x = (unsigned long long)x * that.x % MOD; return *this; }

	ModInt operator+(ModInt that) const { return ModInt(*this) += that; }
	ModInt operator-(ModInt that) const { return ModInt(*this) -= that; }
	ModInt operator*(ModInt that) const { return ModInt(*this) *= that; }
};
typedef ModInt<1000000007> mint;
const mint inverse[11] = {
	0,1,500000004,333333336,250000002,400000003,166666668,142857144,125000001,111111112,700000005
};

struct Sum {
	ll len;
	ll sum;
	mint prod;
	mint invProd;
	Sum() : len(0), sum(0), prod(1), invProd(1) { }
	explicit Sum(char digit) : len(1) {
		int d = digit == '0' ? 10 : digit - '0';
		sum = d;
		prod = d;
		invProd = inverse[d];
	}
	Sum &operator+=(const Sum &that) {
		len += that.len;
		sum += that.sum;
		prod *= that.prod;
		invProd *= that.invProd;
		return *this;
	}
};

Sum dp[100];
Sum solve(ll R) {
	if (R == 0)
		return Sum();
	int n = 1;
	for (; dp[n + 1].len <= R; ++ n);
	Sum res = dp[n];
	R -= dp[n].len;
	string str = to_string((n + 1) * (n + 1));
	for (int k = 0; k != str.size() && R > 0; -- R, ++ k)
		res += Sum(str[k]);
	if (R > 0)
		res += solve(R);
	return res;
}

int main() {
	for (int n = 1; dp[n - 1].len < (ll)1e18; ++ n) {
		dp[n] = dp[n - 1];
		string str = to_string(n * n);
		for (char c : str)
			dp[n] += Sum(c);
		dp[n] += dp[n - 1];
	}
	int K; long long L; long long R;
	while (~scanf("%d%lld%lld", &K, &L, &R)) {
		-- L;
		if (K < 60 && dp[K].len < R) {
			puts("-1");
			continue;
		}
		Sum sumL = solve(L);
		Sum sumR = solve(R);
		ll totalSum = sumR.sum - sumL.sum;
		mint totalProd = sumR.prod * sumL.invProd;
		printf("%lld %d\n", totalSum, totalProd.get());
	}
	return 0;
}
0