# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
230433 | mehrdad_sohrabi | Palindrome-Free Numbers (BOI13_numbers) | C++14 | 5 ms | 512 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>
typedef long long int ll;
typedef long double ld;
#define pb push_back
#define pii pair < ll , ll >
#define F first
#define S second
#define endl '\n'
#define int long long
#define sync ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#pragma GCC optimize ("Ofast")
#pragma GCC optimize ("unroll-loops")
#define kill(x) return cout<<x<<'\n', 0;
using namespace std;
const int N=20,M=10;
bool check(vector <int> a){
for (int i=1;i<a.size();i++){
if (a[i]==a[i-1]) return 0;
}
for (int i=2;i<a.size();i++){
if (a[i]==a[i-2]) return 0;
}
return 1;
}
ll dp[N];
ll p[N];
ll solve(ll x){
if (x<10) return x+1;
vector <int> a;
ll k=x;
while(k){
a.pb(k%10);
k/=10;
}
ll ans=0;
ll z=a.size();
reverse(a.begin(),a.end());
vector <int> b;
for (int i=0;i<a.size();i++){
z--;
for (int j=0;j<a[i];j++){
if (i==0 && j==0) continue;
b.pb(j);
if (check(b)){
if (i==0){
ans+=p[z]/8*9;
// cout << p[z]/8*9 << endl;;
b.pop_back();
continue;
}
ans+=p[z];
// cout << p[z] << endl;
}
b.pop_back();
}
b.pb(a[i]);
}
if (check(b)) ans++;
ans+=dp[(ll)a.size()-1];
return ans;
}
int32_t main(){
p[0]=1;
for (int i=1;i<N;i++){
p[i]=p[i-1]*8;
}
dp[0]=1;
dp[1]=10;
for (int i=2;i<N;i++){
dp[i]=dp[i-1]+9*9*p[i-2];
}
ll l,r;
cin >> l >> r;
ll ans=solve(r);
// cout << ans << endl;
if (l!=0) ans-=solve(l-1);
cout << ans << endl;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |