# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
270336 | Hehehe | Palindrome-Free Numbers (BOI13_numbers) | C++14 | 2 ms | 384 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
using namespace std;
#define pb push_back
#define dbg(x) cout << #x << '=' << x << '\n';
#define ll long long
#define x first
#define y second
#define pi pair <int, int>
#define vi vector <int>
#define L nod<<1
#define R ((nod<<1)|1)
#define mp make_pair
const ll mod = 1000000007;
const ll nmax=1000003;
#define int ll
int a, b;
int dp[20][11][11][2];
int f(int x){
memset(dp, 0, sizeof(dp));
if(x<=9) return x+1;
int ans=0;
vector<int>v;
string s = to_string(x);
for(auto it : s)v.push_back(it - '0');
int n=v.size();
//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];
}
}
}
for(int i = 2; i < n; i++){
dp[i][10][10][0]=1;
for(int next=1; next<=9; next++){
dp[i][10][next][0]=1;
}
for(int xd2=1; xd2<=9; xd2++){
for(int next=0; next<=9; next++){
if(next!=xd2) dp[i][xd2][next][0]+=dp[i-1][10][xd2][0];
}
}
for(int xd1=0; xd1<=9; xd1++){
for(int xd2=0; xd2<=9; xd2++){
for(int eq=0; eq<2; eq++){
for(int next=0; next<=9; next++){
if(eq && next>v[i]) break;
int ctrl=(eq&(next==v[i]));
if(next!=xd1 && next!=xd2) dp[i][xd2][next][ctrl]+=dp[i-1][xd1][xd2][eq];
}
}
}
}
}
//for(int i=0; i<v.size(); i++){
for(int xd1=0; xd1<=10; xd1++){
for(int xd2=0; xd2<=9; xd2++){
for(int eq=0; eq<2; eq++){
ans+=dp[n-1][xd1][xd2][eq];
}
//}
}
}
return ans;
}
int32_t main(){
ios_base :: sync_with_stdio(0); cin.tie(); cout.tie();
//while(cin>>a) cout<<f(a)<<endl;
cin>>a>>b;
a--;
cout<<f(b)-f(a);
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |