Submission #850820

# Submission time Handle Problem Language Result Execution time Memory
850820 2023-09-17T13:28:54 Z Essa2006 Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
1 ms 600 KB
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
#define FF first
#define SS second
#define all(a) a.begin(), a.end()
#define mod (ll)(1000000007)
int main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
    ll fir, sec;
    cin>>fir>>sec;
    fir--;
    string a=to_string(fir);
    string b=to_string(sec);
    ll ans1=0, ans2=0;
    if(fir!=-1){
        vector<vector<vector<array<ll, 2> > > >dp(19, vector<vector<array<ll, 2> > >(10, vector<array<ll, 2> >(10)));
        // 0 -> limit, 1 -> no limit
        // dig_pos, dig_val, dig_after_val
        for(int i=a.size()-1;i>=0;i--){
            if(i==a.size()-1){
                for(int j=0;j<10;j++){
                    dp[i][j][0][j<=a[i]-'0']++;
                }
                for(int j1=0;j1<10;j1++){
                    for(int j2=0;j2<10;j2++){
                        ans1+=dp[i][j1][j2][1]+(i==0?0:dp[i][j1][j2][0]);
                    }
                }
            }
            else if(i==a.size()-2){
                for(int j=0;j<10;j++){
                    for(int j1=0;j1<10;j1++){
                        if(j==j1)
                            continue;
                        if(j>a[i]-'0')
                            dp[i][j][j1][0]+=dp[i+1][j1][0][0]+dp[i+1][j1][0][1];
                        else if(j<a[i]-'0')
                            dp[i][j][j1][1]+=dp[i+1][j1][0][0]+dp[i+1][j1][0][1];
                        else
                            dp[i][j][j1][1]+=dp[i+1][j1][0][1], dp[i][j][j1][0]+=dp[i+1][j1][0][0];
                    }
                }
                for(int j1=1;j1<10;j1++){
                    for(int j2=0;j2<10;j2++){
                        ans1+=dp[i][j1][j2][1]+(i==0?0:dp[i][j1][j2][0]);
                    }
                }
            }
            else{
                for(int j=0;j<10;j++){
                    for(int j1=0;j1<10;j1++){
                        for(int j2=0;j2<10;j2++){
                            if(j1==j || j2==j)
                                continue;
                            if(j>a[i]-'0')
                                dp[i][j][j1][0]+=dp[i+1][j1][j2][0]+dp[i+1][j1][j2][1];
                            else if(j<a[i]-'0')
                                dp[i][j][j1][1]+=dp[i+1][j1][j2][0]+dp[i+1][j1][j2][1];
                            else
                                dp[i][j][j1][1]+=dp[i+1][j1][j2][1], dp[i][j][j1][0]+=dp[i+1][j1][j2][0];
                        }
                    }
                }
                for(int j1=1;j1<10;j1++){
                    for(int j2=0;j2<10;j2++){
                        ans1+=dp[i][j1][j2][1]+(i==0?0:dp[i][j1][j2][0]);
                    }
                }
            }
            
        }
    }
    a=b;
    {
        vector<vector<vector<array<ll, 2> > > >dp(19, vector<vector<array<ll, 2> > >(10, vector<array<ll, 2> >(10)));
        // 0 -> limit, 1 -> no limit
        // dig_pos, dig_val, dig_after_val
        for(int i=a.size()-1;i>=0;i--){
            if(i==a.size()-1){
                for(int j=0;j<10;j++){
                    dp[i][j][0][j<=a[i]-'0']++;
                }
                for(int j1=0;j1<10;j1++){
                    for(int j2=0;j2<10;j2++){
                        ans2+=dp[i][j1][j2][1]+(i==0?0:dp[i][j1][j2][0]);
                    }
                }
            }
            else if(i==a.size()-2){
                for(int j=0;j<10;j++){
                    for(int j1=0;j1<10;j1++){
                        if(j==j1)
                            continue;
                        if(j>a[i]-'0')
                            dp[i][j][j1][0]+=dp[i+1][j1][0][0]+dp[i+1][j1][0][1];
                        else if(j<a[i]-'0')
                            dp[i][j][j1][1]+=dp[i+1][j1][0][0]+dp[i+1][j1][0][1];
                        else
                            dp[i][j][j1][1]+=dp[i+1][j1][0][1], dp[i][j][j1][0]+=dp[i+1][j1][0][0];
                    }
                }
                for(int j1=1;j1<10;j1++){
                    for(int j2=0;j2<10;j2++){
                        ans2+=dp[i][j1][j2][1]+(i==0?0:dp[i][j1][j2][0]);
                    }
                }
            }
            else{
                for(int j=0;j<10;j++){
                    for(int j1=0;j1<10;j1++){
                        for(int j2=0;j2<10;j2++){
                            if(j1==j || j2==j)
                                continue;
                            if(j>a[i]-'0')
                                dp[i][j][j1][0]+=dp[i+1][j1][j2][0]+dp[i+1][j1][j2][1];
                            else if(j<a[i]-'0')
                                dp[i][j][j1][1]+=dp[i+1][j1][j2][0]+dp[i+1][j1][j2][1];
                            else
                                dp[i][j][j1][1]+=dp[i+1][j1][j2][1], dp[i][j][j1][0]+=dp[i+1][j1][j2][0];
                        }
                    }
                }
                for(int j1=1;j1<10;j1++){
                    for(int j2=0;j2<10;j2++){
                        ans2+=dp[i][j1][j2][1]+(i==0?0:dp[i][j1][j2][0]);
                    }
                }
            }
            
        }
    }
    cout<<ans2-ans1;

}

Compilation message

numbers.cpp: In function 'int main()':
numbers.cpp:22:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   22 |             if(i==a.size()-1){
      |                ~^~~~~~~~~~~~
numbers.cpp:32:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   32 |             else if(i==a.size()-2){
      |                     ~^~~~~~~~~~~~
numbers.cpp:81:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   81 |             if(i==a.size()-1){
      |                ~^~~~~~~~~~~~
numbers.cpp:91:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   91 |             else if(i==a.size()-2){
      |                     ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 0 ms 344 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 1 ms 348 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 348 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 348 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 508 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 348 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 1 ms 344 KB Output is correct
4 Correct 1 ms 344 KB Output is correct
5 Correct 1 ms 344 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 1 ms 344 KB Output is correct
8 Correct 1 ms 344 KB Output is correct
9 Correct 1 ms 344 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 348 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 1 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 344 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 344 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 344 KB Output is correct
21 Correct 0 ms 344 KB Output is correct
22 Correct 0 ms 344 KB Output is correct
23 Correct 1 ms 344 KB Output is correct
24 Correct 1 ms 344 KB Output is correct
25 Correct 1 ms 344 KB Output is correct
26 Correct 1 ms 344 KB Output is correct
27 Correct 1 ms 344 KB Output is correct
28 Correct 1 ms 344 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 344 KB Output is correct
32 Correct 1 ms 344 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 344 KB Output is correct
35 Correct 1 ms 504 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 1 ms 600 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 344 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 344 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 1 ms 344 KB Output is correct
45 Correct 1 ms 348 KB Output is correct