# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
199449 | TadijaSebez | Palindrome-Free Numbers (BOI13_numbers) | C++11 | 9 ms | 504 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 ll long long
#define pb push_back
ll dp[10][10][2],tmp[10][20][2];
void Clear(){
for(int j=0;j<10;j++)
for(int k=0;k<10;k++)
for(int l=0;l<2;l++)
dp[j][k][l]=0;
}
void Copy(){
for(int j=0;j<10;j++)
for(int k=0;k<10;k++)
for(int l=0;l<2;l++)
tmp[j][k][l]=dp[j][k][l];
}
ll Solve(ll x){
//printf("%lld\n",x);
if(x==0)return 1;
ll y=x;
vector<int> cif;
while(x)cif.pb(x%10),x/=10;
reverse(cif.begin(),cif.end());
ll mul=1;for(int i=1;i<cif.size();i++)mul*=10;
ll ans=Solve(mul-1);
if(cif.size()==1)return y+ans;
Clear();
for(int i=1;i<=9;i++)for(int j=0;j<=9;j++)if(i!=j){
if(i==cif[0] && j==cif[1])dp[i][j][1]=1;
else if(i<cif[0] || i==cif[0] && j<cif[1])dp[i][j][0]=1;
}
for(int len=2;len<cif.size();len++){
Copy();
Clear();
for(int i=0;i<=9;i++)for(int j=0;j<=9;j++){
for(int k=0;k<=9;k++)if(j!=i && j!=k){
dp[i][j][0]+=tmp[k][i][0];
if(j<cif[len])dp[i][j][0]+=tmp[k][i][1];
}
}
if(cif[len]!=cif[len-1] && cif[len]!=cif[len-2])dp[cif[len-1]][cif[len]][1]=tmp[cif[len-2]][cif[len-1]][1];
}
for(int i=0;i<=9;i++)for(int j=0;j<=9;j++)ans+=dp[i][j][0]+dp[i][j][1];
//printf("%lld %lld\n",y,ans);
return ans;
}
int main(){
ll l,r;
scanf("%lld %lld",&l,&r);
printf("%lld\n",Solve(r)-(l>0?Solve(l-1):0));
return 0;
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |