Submission #741985

# Submission time Handle Problem Language Result Execution time Memory
741985 2023-05-15T09:41:50 Z irmuun Palindrome-Free Numbers (BOI13_numbers) C++17
96.25 / 100
1 ms 212 KB
#include<bits/stdc++.h>

using namespace std;

#define pb push_back
#define ll long long
#define ff first
#define ss second
#define all(s) s.begin(),s.end()

ll cnt[20],c8[20];

ll check(ll n){
    string s=to_string(n);
    for(ll i=1;i<s.size();i++){
        if(s[i]==s[i-1]){
            return 0;
        }
    }
    for(ll i=2;i<s.size();i++){
        if(s[i]==s[i-2]){
            return 0;
        }
    }
    return 1;
}

ll pal(ll n){
    if(n==0){
        return 0;
    }
    string s=to_string(n);
    ll sz=s.size();
    ll res=0;
    for(ll i=1;i<sz;i++){
        res+=cnt[i]*9;
    }
    //cout<<res<<"\n";
    for(ll i=0;i<sz;i++){
        for(ll j=(i==0?1:0);j<s[i]-48;j++){
            //cout<<"str "<<s.substr(0,i)<<char(j+48)<<"\n";
            if((i>0&&char(j+48)==s[i-1])||(i>1&&char(j+48)==s[i-2])){
                continue;
            }
            if(i==0){
                //cout<<cnt[sz-i]<<' ';
                res+=cnt[sz-i];
            }
            else{
                //cout<<c8[sz-i-1]<<' ';
                res+=c8[sz-i-1];
            }
            //cout<<"\n";
        }
        if((i>0&&s[i]==s[i-1])||(i>1&&s[i]==s[i-2])){
            break;
        }
    }
    //cout<<check(n);
    //cout<<"\n";
    return res+check(n);
}

int main(){
    ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
    cnt[0]=1;
    cnt[1]=1;
    cnt[2]=9;
    c8[0]=1;
    c8[1]=8;
    c8[2]=64;
    for(ll i=3;i<=18;i++){
        cnt[i]=cnt[i-1]*8;
        c8[i]=c8[i-1]*8;
    }
    ll a,b;
    cin>>a>>b;
    cout<<pal(b)-pal(a-1);
}

Compilation message

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