# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
26378 | samir_droubi | Palindrome-Free Numbers (BOI13_numbers) | C++14 | 0 ms | 2040 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;
long long a,b;
long long dp[20][12][12];
long long calc(int i,int lst1,int lst2)
{
if(i==2)return lst1!=lst2;
if(dp[i][lst1][lst2]!=-1)return dp[i][lst1][lst2];
long long ans=0;
for(int j=0;j<=9;++j)
{
if(j!=lst1&&j!=lst2)
ans=ans+calc(i-1,j,lst1);
}
return dp[i][lst1][lst2]=ans;
}
long long sm[20];
vector<int>v;
long long f(long long x)
{
v.clear();
int y=x;
while(x!=0)
{
v.push_back(x%10);
x/=10;
}
if(v.size()==1)
return y-1;
if(v.size()==2)
return y-1-(v[1]-1)-(v[0]-1>=v[1]);
long long ans=0;
for(int i=0;i<v.size();++i)
{
int lst1=10;
int lst2=10;
if(i<v.size()-1)lst1=v[i+1];
if(i<v.size()-2)lst2=v[i+2];
for(int j=(i==v.size()-1);j<v[i];++j)
{
if(j==lst2||j==lst1)continue;
ans=ans+calc(i+2,j,lst1);
}
}
return ans+sm[v.size()-1];
}
int main()
{
memset(dp,-1,sizeof dp);
scanf("%lld%lld",&a,&b);
sm[1]=9;
for(int i=2;i<=18;++i)
{
for(int j=1;j<=9;++j)
for(int k=0;k<=9;++k)
sm[i]+=calc(i,j,k);
sm[i]+=sm[i-1];
}
long long sa=f(a);
long long sb=f(b+1);
printf("%lld\n",sb-sa);
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |