結果

問題 No.315 世界のなんとか3.5
ユーザー antaanta
提出日時 2015-12-08 00:23:55
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 276 ms / 2,000 ms
コード長 3,225 bytes
コンパイル時間 645 ms
コンパイル使用メモリ 80,948 KB
実行使用メモリ 29,444 KB
最終ジャッジ日時 2023-10-12 20:19:16
合計ジャッジ時間 6,027 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,352 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 9 ms
4,348 KB
testcase_03 AC 4 ms
4,348 KB
testcase_04 AC 4 ms
4,348 KB
testcase_05 AC 3 ms
4,352 KB
testcase_06 AC 3 ms
4,348 KB
testcase_07 AC 3 ms
4,352 KB
testcase_08 AC 23 ms
4,352 KB
testcase_09 AC 23 ms
4,352 KB
testcase_10 AC 15 ms
4,352 KB
testcase_11 AC 23 ms
4,348 KB
testcase_12 AC 91 ms
15,836 KB
testcase_13 AC 89 ms
15,880 KB
testcase_14 AC 150 ms
16,424 KB
testcase_15 AC 148 ms
16,188 KB
testcase_16 AC 149 ms
16,112 KB
testcase_17 AC 150 ms
16,380 KB
testcase_18 AC 91 ms
15,992 KB
testcase_19 AC 91 ms
15,876 KB
testcase_20 AC 91 ms
15,888 KB
testcase_21 AC 90 ms
15,956 KB
testcase_22 AC 151 ms
16,384 KB
testcase_23 AC 148 ms
16,180 KB
testcase_24 AC 150 ms
16,264 KB
testcase_25 AC 148 ms
16,092 KB
testcase_26 AC 149 ms
16,144 KB
testcase_27 AC 151 ms
16,224 KB
testcase_28 AC 133 ms
16,104 KB
testcase_29 AC 253 ms
29,268 KB
testcase_30 AC 252 ms
29,160 KB
testcase_31 AC 254 ms
29,328 KB
testcase_32 AC 276 ms
29,444 KB
testcase_33 AC 158 ms
29,048 KB
testcase_34 AC 231 ms
29,160 KB
testcase_35 AC 255 ms
29,276 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>
#include <set>
#include <map>
#include <queue>
#include <iostream>
#include <sstream>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <cstring>
#include <cctype>
#include <cassert>
#include <limits>
#include <functional>
#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))
#if defined(_MSC_VER) || __cplusplus > 199711L
#define aut(r,v) auto r = (v)
#else
#define aut(r,v) __typeof(v) r = (v)
#endif
#define each(it,o) for(aut(it, (o).begin()); it != (o).end(); ++ it)
#define all(o) (o).begin(), (o).end()
#define pb(x) push_back(x)
#define mp(x,y) make_pair((x),(y))
#define mset(m,v) memset(m,v,sizeof(m))
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
using namespace std;
typedef vector<int> vi; typedef pair<int,int> pii; typedef vector<pair<int,int> > vpii; typedef long long ll;
template<typename T, typename U> inline void amin(T &x, U y) { if(y < x) x = y; }
template<typename T, typename U> inline 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;

vector<int> digits;
int P;
vector<mint> memo;
bool eqOK;
mint rec(int i, int m, bool three, bool lt) {
	mint &r = memo[((i + 1) * 3 + m) * 4 + three * 2 + lt];
	if(r.x != -1) return r;
	if(i <= 4) {
		r = mint();
		int p = 1;
		int es = 0;
		rep(j, i + 1) {
			es += p * digits[j];
			p *= 10;
		}
		rep(ds, p) if(lt || ds <= es) {
			bool nlt = lt || ds < es;
			int nm = (m * p + ds) % 3;
			bool nthree = three;
			int t = ds;
			rep(j, i+1) {
				nthree |= t % 10 == 3;
				t /= 10;
			}
			bool ok = true;
			ok &= nlt || eqOK;
			ok &= nm == 0 || nthree;
			ok &= ds % P != 0;
			if(ok) r += 1;
		}
		return r;
	}
	r = mint();
	int e = digits[i];
	rep(d, 10) if(lt || d <= e)
		r += rec(i-1, (m * 10 + d) % 3, three || d == 3, lt || d < e);
	return r;
}
mint solve(const char *A) {
	int len = strlen(A);
	digits.resize(len);
	rep(i, len)
		digits[i] = A[len - 1 - i] - '0';
	mint r;
	mint undef; undef.x = -1;
	memo.assign((len+1) * 3 * 4, undef);
	return rec(len - 1, 0, false, false);
}

int main() {
	char *A = new char[200002], *B = new char[200002];
	while(~scanf("%s%s%d", A, B, &P)) {
		mint ans;
		eqOK = true;
		ans += solve(B);
		eqOK = false;
		ans -= solve(A);
		printf("%d\n", ans.get());
	}
	return 0;
}
0