結果

問題 No.319 happy b1rthday 2 me
ユーザー yosupotyosupot
提出日時 2015-12-12 00:31:39
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 2 ms / 2,000 ms
コード長 2,045 bytes
コンパイル時間 964 ms
コンパイル使用メモリ 102,252 KB
実行使用メモリ 4,476 KB
最終ジャッジ日時 2023-10-13 11:31:46
合計ジャッジ時間 2,348 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,348 KB
testcase_01 AC 2 ms
4,348 KB
testcase_02 AC 1 ms
4,356 KB
testcase_03 AC 2 ms
4,348 KB
testcase_04 AC 2 ms
4,348 KB
testcase_05 AC 1 ms
4,372 KB
testcase_06 AC 2 ms
4,368 KB
testcase_07 AC 1 ms
4,356 KB
testcase_08 AC 2 ms
4,352 KB
testcase_09 AC 2 ms
4,352 KB
testcase_10 AC 2 ms
4,348 KB
testcase_11 AC 2 ms
4,348 KB
testcase_12 AC 2 ms
4,352 KB
testcase_13 AC 1 ms
4,352 KB
testcase_14 AC 2 ms
4,348 KB
testcase_15 AC 2 ms
4,352 KB
testcase_16 AC 1 ms
4,352 KB
testcase_17 AC 1 ms
4,352 KB
testcase_18 AC 1 ms
4,348 KB
testcase_19 AC 1 ms
4,352 KB
testcase_20 AC 2 ms
4,352 KB
testcase_21 AC 1 ms
4,352 KB
testcase_22 AC 1 ms
4,356 KB
testcase_23 AC 2 ms
4,352 KB
testcase_24 AC 1 ms
4,356 KB
testcase_25 AC 2 ms
4,348 KB
testcase_26 AC 1 ms
4,476 KB
testcase_27 AC 1 ms
4,352 KB
testcase_28 AC 2 ms
4,348 KB
testcase_29 AC 1 ms
4,348 KB
testcase_30 AC 2 ms
4,348 KB
testcase_31 AC 1 ms
4,348 KB
testcase_32 AC 2 ms
4,352 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <cstdio>
#include <cassert>
#include <cstring>
#include <vector>
#include <valarray>
#include <array>
#include <queue>
#include <set>
#include <unordered_set>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <cmath>
#include <complex>
#include <random>

using namespace std;
using ll = long long;
using P = pair<ll, ll>;


string s;

P dp[20][11][2];
bool used[20][11][2];

P calc(int n, int ba, int f) {
	if (n == 0) {
		if (f) return P(0, 0);
		return P(0, 1);
	}
	if (used[n][ba][f]) return dp[n][ba][f];
	used[n][ba][f] = true;
	ll res = 0, co = 0;
	if (f == 0) {
		for (int i = 0; i <= 9; i++) {
			auto p = calc(n-1, i, 0);
			res += p.first;
			co += p.second;
			if (ba == 1 && i == 2) {
				res += p.second;
			}
		}
	} else {
		for (int i = 0; i <= s[n-1]-'0'; i++) {
			auto p = calc(n-1, i, i == s[n-1]-'0');
			res += p.first;
			co += p.second;
			if (ba == 1 && i == 2) {
				res += p.second;
			}
		}
	}
	return dp[n][ba][f] = P(res, co);
}

ll dp2[20][11][2][2];
bool used2[20][11][2][2];

ll calc2(int n, int ba, int f, int ok) {
	if (n == 0) {
		if (f) return 0;
		if (ba == 2) return 1;
		return 0;
	}
	if (used2[n][ba][f][ok]) return dp2[n][ba][f][ok];
	used2[n][ba][f][ok] = true;
	ll res = 0;
	if (f == 0) {
		for (int i = 0; i <= 9; i++) {
			if (!ok && (i != 0 && i != 2)) continue;
			res += calc2(n-1, i, 0, ok | (i == 2));
		}
	} else {
		for (int i = 0; i <= s[n-1]-'0'; i++) {
			if (!ok && (i != 0 && i != 2)) continue;
			res += calc2(n-1, i, i == s[n-1]-'0', ok | (i == 2));
		}
	}
	return dp2[n][ba][f][ok] = res;
}

//1,2,3,...,n-1
ll solve(ll x) {
	memset(used, 0, sizeof(used));
	memset(used2, 0, sizeof(used2));
	s = to_string(x);
	reverse(s.begin(), s.end());
	int n = (int)s.size();
//	cout << calc(n, 0, 1).first << " " << calc2(n, 0, 1, 0) << endl;
	return calc(n, 0, 1).first + calc2(n, 0, 1, 0);
}


int main() {
	ll a, b;
	cin >> a >> b;
	ll u = solve(b+1) - solve(a);
	if (to_string(a)[0] == '2' && a % 10 == 2) u--;
	cout << u << endl;
	return 0;
}
0