Submission #166380

# Submission time Handle Problem Language Result Execution time Memory
166380 2019-12-01T23:53:15 Z achibasadzishvili Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
10 ms 508 KB
#include<bits/stdc++.h>
#define ll long long
#define f first
#define s second
#define pb push_back
using namespace std;
ll dp[25][12][12][3];
vector<ll>v;
ll solve(ll x){
    if(x < 10)return x + 1;
    ll xx = x;
    v.clear();
    while(x){
        v.pb(x % 10);
        x /= 10;
    }
    v.pb(-1);
    reverse(v.begin() , v.end());
    for(int i=0; i<=v.size(); i++)
        for(int x=0; x<10; x++)
            for(int y=0; y<=10; y++)
                dp[i][x][y][0] = dp[i][x][y][1] = 0;
    for(int x=1; x<10; x++)
        for(int y=0; y<10; y++)
            if(x != y || x == 0){
                if(x < v[1] || (x == v[1] && y < v[2])){
                    dp[2][y][x][1] = 1;
                }
                if(x == v[1] && y == v[2])
                    dp[2][y][x][0] = 1;
            }
    for(int i=3; i<(int)v.size(); i++){
        for(int n=0; n<10; n++)
            for(int x=0; x<10; x++)
                for(int y=0; y<10; y++){
                    if(x == y)continue;
                    if(n == x)continue;
                    if(n == y)continue;
                    dp[i][n][x][1] += dp[i - 1][x][y][1];
                    if(n < v[i])dp[i][n][x][1] += dp[i - 1][x][y][0];
                    if(n == v[i])dp[i][n][x][0] += dp[i - 1][x][y][0];
                }
    }
    ll ret = 0;
    ll n = (ll)v.size() - 1;
    for(int x=0; x<10; x++)
        for(int y=0; y<10; y++){
            ret += dp[n][x][y][1] + dp[n][x][y][0];
        }
    return ret;
}
ll count(ll x){
    ll ans = 0;
    ans += solve(x);
    ll raod = 0;
    while(x){
        raod++;
        x /= 10;
    }
    ll p = 1;
    for(int i=1; i<raod; i++){
        p *= 10;
        ans += solve(p - 1);
    }
    return ans;
}
int main(){
    ios::sync_with_stdio(false);
    ll x,y;
    cin >> x >> y;
    
    cout << count(y) - count(x - 1) << endl;
    
    
    
    
    return 0;
}

Compilation message

numbers.cpp: In function 'long long int solve(long long int)':
numbers.cpp:19:19: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i=0; i<=v.size(); i++)
                  ~^~~~~~~~~~
numbers.cpp:11:8: warning: unused variable 'xx' [-Wunused-variable]
     ll xx = x;
        ^~
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 2 ms 380 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 2 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 508 KB Output is correct
15 Correct 2 ms 376 KB Output is correct
16 Correct 2 ms 376 KB Output is correct
17 Correct 2 ms 376 KB Output is correct
18 Correct 2 ms 376 KB Output is correct
19 Correct 3 ms 420 KB Output is correct
20 Correct 2 ms 376 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 2 ms 376 KB Output is correct
2 Correct 3 ms 376 KB Output is correct
3 Correct 3 ms 376 KB Output is correct
4 Correct 3 ms 376 KB Output is correct
5 Correct 2 ms 376 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 2 ms 376 KB Output is correct
9 Correct 2 ms 376 KB Output is correct
10 Correct 2 ms 376 KB Output is correct
11 Correct 2 ms 376 KB Output is correct
12 Correct 3 ms 376 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 380 KB Output is correct
15 Correct 3 ms 380 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 10 ms 376 KB Output is correct
19 Correct 3 ms 376 KB Output is correct
20 Correct 3 ms 376 KB Output is correct
21 Correct 3 ms 376 KB Output is correct
22 Correct 3 ms 376 KB Output is correct
23 Correct 3 ms 376 KB Output is correct
24 Correct 3 ms 376 KB Output is correct
25 Correct 3 ms 376 KB Output is correct
26 Correct 3 ms 376 KB Output is correct
27 Correct 3 ms 376 KB Output is correct
28 Correct 3 ms 376 KB Output is correct
29 Correct 3 ms 376 KB Output is correct
30 Correct 3 ms 376 KB Output is correct
31 Correct 3 ms 376 KB Output is correct
32 Correct 3 ms 376 KB Output is correct
33 Correct 3 ms 376 KB Output is correct
34 Correct 3 ms 376 KB Output is correct
35 Correct 3 ms 376 KB Output is correct
36 Correct 3 ms 376 KB Output is correct
37 Correct 3 ms 376 KB Output is correct
38 Correct 3 ms 376 KB Output is correct
39 Correct 3 ms 376 KB Output is correct
40 Correct 3 ms 376 KB Output is correct
41 Correct 3 ms 376 KB Output is correct
42 Correct 3 ms 376 KB Output is correct
43 Correct 3 ms 380 KB Output is correct
44 Correct 3 ms 376 KB Output is correct
45 Correct 4 ms 344 KB Output is correct