Submission #850814

# Submission time Handle Problem Language Result Execution time Memory
850814 2023-09-17T13:27:35 Z Essa2006 Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
1 ms 600 KB
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif
 
// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
 
#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
//#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);
    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:121:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  121 |             if(i==a.size()-1){
      |                ~^~~~~~~~~~~~
numbers.cpp:131:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  131 |             else if(i==a.size()-2){
      |                     ~^~~~~~~~~~~~
numbers.cpp:180:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  180 |             if(i==a.size()-1){
      |                ~^~~~~~~~~~~~
numbers.cpp:190:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
  190 |             else if(i==a.size()-2){
      |                     ~^~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 344 KB Output is correct
2 Correct 1 ms 344 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 348 KB Output is correct
7 Correct 1 ms 348 KB Output is correct
8 Correct 1 ms 348 KB Output is correct
9 Correct 1 ms 348 KB Output is correct
10 Correct 1 ms 344 KB Output is correct
11 Correct 1 ms 344 KB Output is correct
12 Correct 1 ms 344 KB Output is correct
13 Correct 0 ms 344 KB Output is correct
14 Correct 1 ms 344 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 1 ms 344 KB Output is correct
17 Correct 1 ms 452 KB Output is correct
18 Correct 1 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 1 ms 344 KB Output is correct
# 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 344 KB Output is correct
7 Correct 0 ms 348 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 600 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 0 ms 344 KB Output is correct
19 Correct 1 ms 344 KB Output is correct
20 Correct 0 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 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 348 KB Output is correct
28 Correct 1 ms 348 KB Output is correct
29 Correct 1 ms 344 KB Output is correct
30 Correct 1 ms 344 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 344 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 0 ms 344 KB Output is correct
36 Correct 1 ms 344 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 348 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 544 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 344 KB Output is correct
43 Correct 1 ms 344 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 344 KB Output is correct