結果
| 問題 |
No.319 happy b1rthday 2 me
|
| コンテスト | |
| ユーザー |
yosupot
|
| 提出日時 | 2015-12-12 00:31:39 |
| 言語 | C++11(廃止可能性あり) (gcc 13.3.0) |
| 結果 |
AC
|
| 実行時間 | 2 ms / 2,000 ms |
| コード長 | 2,045 bytes |
| コンパイル時間 | 1,154 ms |
| コンパイル使用メモリ | 104,636 KB |
| 実行使用メモリ | 5,376 KB |
| 最終ジャッジ日時 | 2024-09-15 08:30:46 |
| 合計ジャッジ時間 | 2,114 ms |
|
ジャッジサーバーID (参考情報) |
judge3 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 4 |
| other | AC * 29 |
ソースコード
#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;
}
yosupot