이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <random>
#include <chrono>
#include <algorithm>
#include <cstring>
#define rep(i, x) for(int i = 0; i < x; i++)
using namespace std;
typedef long long ll;
ll MOD = (ll)1e9 + 7;
const int N = 1e5+5, inf = 1e9+5;
ll add(ll x, ll y) { x += y; if (x >= MOD) return x - MOD; return x; }
ll sub(ll x, ll y) { x -= y; if (x < 0) return x + MOD; return x; }
ll mult(ll x, ll y) { return (x * y) % MOD; }
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
ll dp[20][2][11][11];
vector<int> num;
// function for [0, x]
ll f(int pos, bool less, int slast, int last) {
if (pos == num.size()) return 1;
if (dp[pos][less][slast][last] != -1) return dp[pos][less][slast][last];
ll ans = 0;
for (int digit = 0; digit <= 9; digit++) {
if (!less && digit > num[pos]) break;
if (slast == digit || last == digit) continue;
// if last is a leading zero (i.e. a 10) then the current zero is also a leading zero.
ans += f(pos+1, less || digit < num[pos], last, last == 10 && digit == 0 ? 10 : digit);
}
return dp[pos][less][slast][last] = ans;
}
int main() {
ios_base::sync_with_stdio(false); cin.tie(nullptr);
ll a, b; cin >> a >> b;
while (b > 0) {
num.push_back(b%10);
b/=10;
}
reverse(begin(num), end(num));
memset(dp, -1, sizeof(dp));
ll b_ans = f(0, false, 10, 10); // 10 is leading zero
num.clear();
a--;
while (a > 0) {
num.push_back(a%10);
a/=10;
}
reverse(begin(num), end(num));
memset(dp, -1, sizeof(dp));
ll a_ans = f(0, false, 10, 10); // 10 is leading zero
cout << b_ans-a_ans << endl;
}
컴파일 시 표준 에러 (stderr) 메시지
numbers.cpp: In function 'll f(int, bool, int, int)':
numbers.cpp:21:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
if (pos == num.size()) return 1;
~~~~^~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |