답안 #1008179

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1008179 2024-06-26T08:11:16 Z ezzzay Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
741 ms 512 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long 
#define ss second
#define pb push_back
int find(int n){
    if(n<=0)return n;
    string s= to_string(n);
    
    int N=s.size();
    int dp[20][10][10][2];
    for(int i=0;i<20;i++){
        for(int j=0;j<10;j++){
            for(int p=0;p<10;p++){
                for(int z=0;z<2;z++)dp[i][j][p][z]=0;
            }
            
        }
    }
    
    for(int i=1;i<10;i++){
        for(int j=0;j<10;j++){
            if(i>s[0]-'0')break;
            if(i==j)continue;
            if(i==s[0]-'0' and j==s[1]-'0'){
                dp[1][i][j][1]+=1;
                break;
            }
            dp[1][i][j][0]++;
        }
    }
    for(int i=2;i<N;i++){
        for(int j=0;j<10;j++){
            for(int p=0;p<10;p++){
                for(int k=0;k<10;k++){
                    for(int z=0;z<2;z++){
                       if(z==1 and k> s[i]-'0')continue;
                       if(k!=p and j!=k)dp[i][p][k][z and (s[i]-'0'==k)]+=dp[i-1][j][p][z];
                    }
                }
            }
        }
    }
    int ans=0;
    for(int i=0;i<10;i++){
        for(int j=0;j<10;j++){
            for(int z=0;z<2;z++){
                ans+=dp[N-1][i][j][z];
            }
        }
    }
    
    return ans;
    
}
int fun(int n){
    int k=0;
    int g= to_string(n).size();
    while(n>0){
        k+=find(n);
        //cout<<n<<" "<<endl;
        g--;
        n=0;
        for(int i=0;i<g;i++){
            n*=10;
            n+=9;
        }
    }
    return k;
}
bool check(string s){
    for(int i=0;i<s.size()-1;i++){
        if(s[i]==s[i+1]){
            return 0;
        }
    }
    for(int i=0;i<s.size()-2;i++){
        if(s[i]==s[i+2]){
            return 0;
        }
    }
    return 1;
}
void sbtsk(int a, int b){
    int ans=0;
    for(int i=a; i<=b; i++) {
        bool ok = 1;
        string s = to_string(i);
        for(int j=0; j+1<s.size(); j++) {
            if(s[j] == s[j+1]) ok = 0;
            if(j+2 < s.size() && s[j] == s[j+2]) ok = 0;
        }
        ans += ok;
    }
    cout<<ans;
    return;
    int t=0;
    for(int i=a;i<=b;i++){
        if(check(to_string(i))){
            t++;
        }
    }
    cout<<t;
}
signed main(){
    int a,b;
    cin>>a>>b;
    if(b-a<=5e7){
        sbtsk(a,b);
        return 0;
    }
    cout<<fun(b)-fun(a-1);
}

Compilation message

numbers.cpp: In function 'bool check(std::string)':
numbers.cpp:72:18: 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]
   72 |     for(int i=0;i<s.size()-1;i++){
      |                 ~^~~~~~~~~~~
numbers.cpp:77:18: 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]
   77 |     for(int i=0;i<s.size()-2;i++){
      |                 ~^~~~~~~~~~~
numbers.cpp: In function 'void sbtsk(long long int, long long int)':
numbers.cpp:89:25: 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]
   89 |         for(int j=0; j+1<s.size(); j++) {
      |                      ~~~^~~~~~~~~
numbers.cpp:91:20: 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]
   91 |             if(j+2 < s.size() && s[j] == s[j+2]) ok = 0;
      |                ~~~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 348 KB Output is correct
2 Correct 0 ms 348 KB Output is correct
3 Correct 6 ms 348 KB Output is correct
4 Correct 2 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 0 ms 348 KB Output is correct
7 Correct 0 ms 348 KB Output is correct
8 Correct 0 ms 348 KB Output is correct
9 Correct 0 ms 348 KB Output is correct
10 Correct 0 ms 348 KB Output is correct
11 Correct 0 ms 348 KB Output is correct
12 Correct 0 ms 344 KB Output is correct
13 Correct 0 ms 348 KB Output is correct
14 Correct 2 ms 348 KB Output is correct
15 Correct 1 ms 348 KB Output is correct
16 Correct 0 ms 348 KB Output is correct
17 Correct 1 ms 348 KB Output is correct
18 Correct 0 ms 348 KB Output is correct
19 Correct 4 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 158 ms 348 KB Output is correct
2 Correct 1 ms 348 KB Output is correct
3 Correct 0 ms 348 KB Output is correct
4 Correct 0 ms 348 KB Output is correct
5 Correct 0 ms 348 KB Output is correct
6 Correct 1 ms 344 KB Output is correct
7 Correct 49 ms 348 KB Output is correct
8 Correct 35 ms 344 KB Output is correct
9 Correct 64 ms 348 KB Output is correct
10 Correct 132 ms 344 KB Output is correct
11 Correct 132 ms 408 KB Output is correct
12 Correct 741 ms 412 KB Output is correct
13 Correct 143 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 348 KB Output is correct
17 Correct 1 ms 512 KB Output is correct
18 Correct 1 ms 348 KB Output is correct
19 Correct 1 ms 348 KB Output is correct
20 Correct 1 ms 348 KB Output is correct
21 Correct 1 ms 348 KB Output is correct
22 Correct 1 ms 456 KB Output is correct
23 Correct 1 ms 348 KB Output is correct
24 Correct 1 ms 348 KB Output is correct
25 Correct 1 ms 348 KB Output is correct
26 Correct 0 ms 348 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 348 KB Output is correct
30 Correct 1 ms 348 KB Output is correct
31 Correct 1 ms 348 KB Output is correct
32 Correct 1 ms 348 KB Output is correct
33 Correct 1 ms 348 KB Output is correct
34 Correct 1 ms 348 KB Output is correct
35 Correct 1 ms 348 KB Output is correct
36 Correct 1 ms 348 KB Output is correct
37 Correct 1 ms 348 KB Output is correct
38 Correct 1 ms 344 KB Output is correct
39 Correct 1 ms 348 KB Output is correct
40 Correct 1 ms 348 KB Output is correct
41 Correct 1 ms 348 KB Output is correct
42 Correct 1 ms 348 KB Output is correct
43 Correct 1 ms 348 KB Output is correct
44 Correct 1 ms 348 KB Output is correct
45 Correct 1 ms 348 KB Output is correct