답안 #631260

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
631260 2022-08-18T01:34:47 Z bachhoangxuan Palindrome-Free Numbers (BOI13_numbers) C++17
100 / 100
1 ms 340 KB
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define maxl 20
int dp[maxl][10][10],power[maxl];
int f(int len,int p,int q){
    if(p==q) return 0;
    if(len==2){
        if(p==q) return 0;
        else return 1;
    }
    if(dp[len][p][q]!=-1) return dp[len][p][q];
    int res=0;
    for(int i=0;i<=9;i++){
        if(i==q || i==p) continue;
        res+=f(len-1,q,i);
    }
    //cout << len << ' ' << p << ' ' << q << ' ' << res << '\n';
    return dp[len][p][q]=res;
}
int solve(int n){
    int sum=0;
    if(n<=9) return n+1;
    else sum=10;
    if(n==1e18) n--;
    string s=to_string(n);reverse(s.begin(),s.end());
    int len=s.length();
    for(int i=2;i<len;i++){
        for(int p=1;p<=9;p++){
            for(int q=0;q<=9;q++) sum+=f(i,p,q);
        }
    }
    int pre1=-1,pre2=-1,lst=len;
    for(int i=1;i<(s[len-1]-'0');i++){
        for(int p=0;p<=9;p++) sum+=f(len,i,p);
    }
    pre1=s[len-1]-'0';
    for(int i=len-1;i>=2;i--){
        int x=s[i-1]-'0';
        for(int p=0;p<x;p++){
            for(int q=0;q<=9;q++){
                if(p==pre2 || p==pre1 || q==pre1) continue;
                sum+=f(i,p,q);
            }
        }
        if(x==pre1 || x==pre2) break;
        pre2=pre1;pre1=x;lst=i;
    }
    if(lst==2){
        for(int i=0;i<=s[0]-'0';i++){
            if(i==pre1 || i==pre2) continue;
            sum++;
        }
    }
    return sum;
}
signed main(){
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int a,b;cin >> a >> b;power[0]=1;
    for(int i=0;i<=18;i++){
        for(int p=0;p<=9;p++){
            for(int q=0;q<=9;q++) dp[i][p][q]=-1;
        }
    }
    for(int i=1;i<=18;i++) power[i]=power[i-1]*10;
    cout << solve(b)-solve(a-1) << '\n';
}
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 212 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 324 KB Output is correct
8 Correct 1 ms 212 KB Output is correct
9 Correct 1 ms 212 KB Output is correct
10 Correct 1 ms 328 KB Output is correct
11 Correct 1 ms 212 KB Output is correct
12 Correct 1 ms 340 KB Output is correct
13 Correct 1 ms 320 KB Output is correct
14 Correct 1 ms 212 KB Output is correct
15 Correct 1 ms 212 KB Output is correct
16 Correct 0 ms 320 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 212 KB Output is correct
2 Correct 1 ms 212 KB Output is correct
3 Correct 1 ms 324 KB Output is correct
4 Correct 1 ms 212 KB Output is correct
5 Correct 1 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 1 ms 340 KB Output is correct
11 Correct 1 ms 340 KB Output is correct
12 Correct 1 ms 212 KB Output is correct
13 Correct 1 ms 212 KB Output is correct
14 Correct 1 ms 324 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 1 ms 340 KB Output is correct
17 Correct 1 ms 212 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 212 KB Output is correct
22 Correct 0 ms 212 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 1 ms 340 KB Output is correct
25 Correct 1 ms 324 KB Output is correct
26 Correct 1 ms 212 KB Output is correct
27 Correct 1 ms 256 KB Output is correct
28 Correct 1 ms 212 KB Output is correct
29 Correct 1 ms 212 KB Output is correct
30 Correct 1 ms 212 KB Output is correct
31 Correct 1 ms 324 KB Output is correct
32 Correct 1 ms 212 KB Output is correct
33 Correct 1 ms 212 KB Output is correct
34 Correct 1 ms 324 KB Output is correct
35 Correct 1 ms 212 KB Output is correct
36 Correct 1 ms 212 KB Output is correct
37 Correct 1 ms 324 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 0 ms 324 KB Output is correct
40 Correct 1 ms 324 KB Output is correct
41 Correct 1 ms 328 KB Output is correct
42 Correct 0 ms 340 KB Output is correct
43 Correct 1 ms 340 KB Output is correct
44 Correct 1 ms 212 KB Output is correct
45 Correct 0 ms 212 KB Output is correct