Submission #270376

# Submission time Handle Problem Language Result Execution time Memory
270376 2020-08-17T14:28:13 Z Hehehe Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
1 ms 384 KB
#include<bits/stdc++.h> //:3
using namespace std;
typedef long long ll;
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define pb push_back
#define mp make_pair
#define pi pair<int, int>
#define sz(x) (int)((x).size())
#define int long long

const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};

const ll inf = 2e9;
const ll mod = 1e9 + 7;
const int N = 2e5 + 11;
const ll INF64 = 3e18 + 1;
const double eps = 1e-14;
const double PI = acos(-1);

//ifstream in(".in");
//ofstream out(".out");

int dp[20][11][11][2];

int f(int x){

    if(x < 10)return x + 1;

    memset(dp, 0, sizeof(dp));

    vector<int>v;

    string s = to_string(x);
    for(auto it : s)v.push_back(it - '0');

    //i == 0
    dp[0][10][10][0] = 1;
    
    for(int cur = 1; cur <= 9; cur++){
        if(cur > v[0])break;
        dp[0][10][cur][(cur == v[0])] = 1;
    }

    //i == 1
    dp[1][10][10][0] = 1;
    
    for(int cur = 1; cur <= 9; cur++){
        dp[1][10][cur][0] = 1;
    }

    for(int prev = 1; prev <= 9; prev++){
        for(int all_equal_till_now = 0; all_equal_till_now <= 1; all_equal_till_now++){
            for(int cur = 0; cur <= 9; cur++){
                if((all_equal_till_now & v[1] < cur))break;
                if(prev == cur)continue;

                int ALL = (all_equal_till_now & (v[1] == cur));
                dp[1][prev][cur][ALL] += dp[0][10][prev][all_equal_till_now];
            }
        }
    }

    //dp
    for(int i = 2; i < sz(v); i++){//digit 10 means nothing
        dp[i][10][10][0] = 1;
            
        for(int cur = 1; cur <= 9; cur++){
            dp[i][10][cur][0] = 1;
        }

        for(int a = 0; a <= 10; a++){
            for(int b = 0; b <= 9; b++){
                for(int all_equal_till_now = 0; all_equal_till_now <= 1; all_equal_till_now++){
                    for(int cur = 0; cur <= 9; cur++){
                        if((all_equal_till_now & v[i] < cur) || (b == cur) || (a == cur))continue;

                        int ALL = (all_equal_till_now & (v[i] == cur));
                        dp[i][b][cur][ALL] += dp[i - 1][a][b][all_equal_till_now];
                    }
                }
            }
        }
    }

    int ans = 0;

    for(int a = 0; a <= 10; a++){
        for(int eq = 0; eq <= 1; eq++){
            for(int b = 0; b <= 10; b++){
                ans += dp[sz(v) - 1][a][b][eq];
            }
        }
    }


    return ans;
}

void solve(){
    int l, r;

    cin >> l >> r;
    l--;
    cout << f(r) - f(l) << '\n';
}

int32_t main(){
    ios_base :: sync_with_stdio(0); cin.tie(0); cout.tie(0);
    int T = 1;
    //cin >> T;
    while(T--){
        solve();
    }
}

Compilation message

numbers.cpp: In function 'long long int f(long long int)':
numbers.cpp:57:47: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
   57 |                 if((all_equal_till_now & v[1] < cur))break;
numbers.cpp:78:55: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
   78 |                         if((all_equal_till_now & v[i] < cur) || (b == cur) || (a == cur))continue;
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 0 ms 384 KB Output is correct
10 Correct 0 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 1 ms 384 KB Output is correct
15 Correct 0 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 384 KB Output is correct
2 Correct 1 ms 384 KB Output is correct
3 Correct 1 ms 384 KB Output is correct
4 Correct 1 ms 384 KB Output is correct
5 Correct 1 ms 384 KB Output is correct
6 Correct 1 ms 384 KB Output is correct
7 Correct 1 ms 384 KB Output is correct
8 Correct 1 ms 384 KB Output is correct
9 Correct 1 ms 384 KB Output is correct
10 Correct 1 ms 384 KB Output is correct
11 Correct 1 ms 384 KB Output is correct
12 Correct 1 ms 384 KB Output is correct
13 Correct 1 ms 384 KB Output is correct
14 Correct 0 ms 384 KB Output is correct
15 Correct 1 ms 384 KB Output is correct
16 Correct 1 ms 384 KB Output is correct
17 Correct 1 ms 384 KB Output is correct
18 Correct 1 ms 384 KB Output is correct
19 Correct 1 ms 384 KB Output is correct
20 Correct 1 ms 384 KB Output is correct
21 Correct 1 ms 384 KB Output is correct
22 Correct 1 ms 384 KB Output is correct
23 Correct 1 ms 384 KB Output is correct
24 Correct 1 ms 384 KB Output is correct
25 Correct 1 ms 384 KB Output is correct
26 Correct 1 ms 384 KB Output is correct
27 Correct 1 ms 384 KB Output is correct
28 Correct 1 ms 384 KB Output is correct
29 Correct 1 ms 384 KB Output is correct
30 Correct 1 ms 384 KB Output is correct
31 Correct 1 ms 384 KB Output is correct
32 Correct 1 ms 384 KB Output is correct
33 Correct 1 ms 384 KB Output is correct
34 Correct 1 ms 384 KB Output is correct
35 Correct 1 ms 384 KB Output is correct
36 Correct 1 ms 384 KB Output is correct
37 Correct 1 ms 384 KB Output is correct
38 Correct 1 ms 384 KB Output is correct
39 Correct 1 ms 384 KB Output is correct
40 Correct 1 ms 384 KB Output is correct
41 Correct 1 ms 384 KB Output is correct
42 Correct 1 ms 384 KB Output is correct
43 Correct 1 ms 384 KB Output is correct
44 Correct 1 ms 384 KB Output is correct
45 Correct 1 ms 384 KB Output is correct