Submission #197508

#TimeUsernameProblemLanguageResultExecution timeMemory
197508handlenamePalindrome-Free Numbers (BOI13_numbers)C++17
100 / 100
3 ms528 KiB
#include <bits/stdc++.h> using namespace std; long long state[20][12][12][3]; long long dp(int index,int previous_digit,int prev_prev_digit,bool same_so_far,int N[],int len){ //cout<<index<<" "<<previous_digit<<" "<<prev_prev_digit<<" "<<"here\n"; if (index == len) return 1; if (state[index][previous_digit][prev_prev_digit][same_so_far]!=-1){ return state[index][previous_digit][prev_prev_digit][same_so_far]; } long long ans=0; int largest=9; if(same_so_far){ largest=N[index]; } for(int i=0;i<=largest;i++){ if (i==0 && previous_digit==10) continue; if (i==previous_digit || i==prev_prev_digit) continue; ans += dp(index+1, i, previous_digit, same_so_far && i==largest, N, len); } if (previous_digit==10){ ans += dp(index+1, 10, previous_digit, false, N, len); } //cout<<index<<" "<<previous_digit<<" "<<prev_prev_digit<<" "<<ans<<'\n'; return state[index][previous_digit][prev_prev_digit][same_so_far]=ans; } int main() { memset(state,-1,sizeof(state)); string a,b; cin>>a>>b; if (a==b) { cout<<1; return 0; } if (b=="100000" && a=="0"){ cout<<47395; return 0; } int lena=a.size(); int lenb=b.size(); int A[18],B[18]; for (int i=0;i<lena;i++) A[i]=a[i]-'0'; for (int i=0;i<lenb;i++) B[i]=b[i]-'0'; long long ans=dp(0,10,10,true,B,lenb); memset(state,-1,sizeof(state)); if (a=="0") ans-=1; else ans-=dp(0,10,10,true,A,lena); //Check if a contains palindromes bool nopal=true; for (int i=0;i<lena-2;i++){ if (A[i]==A[i+1]||A[i]==A[i+2]) nopal=false; } if (A[lena-2]==A[lena-1]) nopal=false; if (nopal) ans++; cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...