이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h> //:3
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define sz(x) (int)((x).size())
#define int long long
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
const ll inf = 2e9;
const ll mod = 1e9 + 7;
const int N = 2e5 + 11;
const ll INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);
//ifstream in(".in");
//ofstream out(".out");
int dp[20][11][11][2];
int f(int x){
if(x < 10)return x + 1;
memset(dp, 0, sizeof(dp));
vector<int>v;
string s = to_string(x);
for(auto it : s)v.push_back(it - '0');
//i == 0
for(int cur = 1; cur <= 9; cur++){
if(cur > v[0])break;
dp[0][10][cur][(cur == v[0])] = 1;
}
//i == 1
for(int cur = 1; cur <= 9; cur++){
dp[1][10][cur][0] = 1;
}
for(int prev = 1; prev <= 9; prev++){
for(int all_equal_till_now = 0; all_equal_till_now <= 1; all_equal_till_now++){
for(int cur = 0; cur <= 9; cur++){
if((all_equal_till_now & v[1] < cur))break;
if(prev == cur)continue;
int ALL = (all_equal_till_now & (v[1] == cur));
dp[1][prev][cur][ALL] += dp[0][10][prev][all_equal_till_now];
}
}
}
//dp
for(int i = 2; i < sz(v); i++){//digit 10 means nothing
for(int cur = 1; cur <= 9; cur++){
dp[i][10][cur][0] = 1;
}
for(int a = 0; a <= 10; a++){
for(int b = 0; b <= 9; b++){
for(int all_equal_till_now = 0; all_equal_till_now <= 1; all_equal_till_now++){
for(int cur = 0; cur <= 9; cur++){
if((all_equal_till_now & v[i] < cur) || (b == cur) || (a == cur))continue;
int ALL = (all_equal_till_now & (v[i] == cur));
dp[i][b][cur][ALL] += dp[i - 1][a][b][all_equal_till_now];
}
}
}
}
}
int ans = 0;
for(int a = 0; a <= 10; a++){
for(int eq = 0; eq <= 1; eq++){
for(int b = 0; b <= 10; b++){
ans += dp[sz(v) - 1][a][b][eq];
}
}
}
return ans;
}
void solve(){
int l, r;
cin >> l >> r;
l--;
cout << f(r) - f(l) << '\n';
}
int32_t main(){
ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
int T = 1;
//cin >> T;
while(T--){
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
numbers.cpp: In function 'long long int f(long long int)':
numbers.cpp:53:47: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
53 | if((all_equal_till_now & v[1] < cur))break;
numbers.cpp:72:55: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
72 | if((all_equal_till_now & v[i] < cur) || (b == cur) || (a == cur))continue;
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |