Submission #91632

# Submission time Handle Problem Language Result Execution time Memory
91632 2018-12-28T18:27:14 Z Mercenary Palindrome-Free Numbers (BOI13_numbers) C++14
100 / 100
11 ms 508 KB
#include<bits/stdc++.h>

using namespace std;
#define taskname "BOI13_numbers"
#define pb	push_back
typedef long double ld;
typedef long long ll;
const int maxn = 19;

ll a , b;

int brute()
{
    int res = 0;
    for(ll i = a ; i <= b ; ++i)
    {
        string s = to_string(i);
//        cout << s << endl;
        bool ok = 1;
        for(int j = 1 ; j < s.size() ; ++j){
            if(j > 1 && s[j] == s[j - 2])ok = 0;
            if(j > 0 && s[j] == s[j - 1])ok = 0;
        }
        res += ok;
    }
    return res;
}

ll f[maxn][11][11][2];
string s;
int cur = 0;
ll cal(int pos , int pre2 , int pre1 , bool pre)
{
    if(pos == (int)s.size())return 1;
    ll & res = f[pos][pre2][pre1][pre];
    if(res != -1)return res;
    int lim = (pre ? 9 : s[pos] - '0');
    res = 0;
    for(int i = (pos == cur ? 1 : 0) ; i <= lim ; ++i){
        if(i == pre1 || i == pre2)continue;
        res += cal(pos + 1 , pre1 , i , pre || (i < lim));
//        if(cur == 1)cout << pos;
    }
    return res;
}

ll solve()
{
    a--;
    ll ares = 0;
    if(a > 0)
    {
        s = to_string(a);
        for(int i = 1 ; i <= s.size() ; ++i)
        {
//            memset(f , -1 , sizeof(f) / sizeof(f[0][0][0][0]));
            fill_n(&f[0][0][0][0],maxn*11*11*2,-1);
            cur = s.size() - i;
            ares += cal(s.size() - i , 10 , 10 , (i != s.size()));
        }
    }
    ll bres = 0;
    s = to_string(b);
    for(int i = 1 ; i <= s.size() ; ++i)
    {
//        memset(f , -1 , sizeof(f) / sizeof(f[0][0][0][0]));
        fill_n(&f[0][0][0][0],maxn*11*11*2,-1);
        cur = s.size() - i;
        bres += cal(s.size() - i , 10 , 10 , (i != s.size() ? 1 : 0));
    }
    return bres - ares + (a == -1 ? 1 : 0);
}

int main()
{
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	if(fopen(taskname".INP","r"))
        freopen(taskname".INP", "r",stdin) ,
        freopen(taskname".OUT", "w",stdout);
//    srand(time(NULL));
//    int c = 100;
//    while(c--){
////        a = rand();b = a + rand() % (int)1e5;
////        cout << a << " " << b << endl;
//        assert(brute() == solve());
//    }
    cin >> a >> b;
    cout << solve();
}

Compilation message

numbers.cpp: In function 'int brute()':
numbers.cpp:20:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int j = 1 ; j < s.size() ; ++j){
                         ~~^~~~~~~~~~
numbers.cpp: In function 'll solve()':
numbers.cpp:54:27: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         for(int i = 1 ; i <= s.size() ; ++i)
                         ~~^~~~~~~~~~~
numbers.cpp:59:53: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             ares += cal(s.size() - i , 10 , 10 , (i != s.size()));
                                                   ~~^~~~~~~~~~~
numbers.cpp:64:23: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(int i = 1 ; i <= s.size() ; ++i)
                     ~~^~~~~~~~~~~
numbers.cpp:69:49: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
         bres += cal(s.size() - i , 10 , 10 , (i != s.size() ? 1 : 0));
                                               ~~^~~~~~~~~~~
numbers.cpp: In function 'int main()':
numbers.cpp:79:44: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
         freopen(taskname".INP", "r",stdin) ,
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
         freopen(taskname".OUT", "w",stdout);
         ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 
numbers.cpp:79:44: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
# Verdict Execution time Memory Grader output
1 Correct 2 ms 508 KB Output is correct
2 Correct 2 ms 376 KB Output is correct
3 Correct 11 ms 376 KB Output is correct
4 Correct 2 ms 376 KB Output is correct
5 Correct 2 ms 420 KB Output is correct
6 Correct 2 ms 376 KB Output is correct
7 Correct 2 ms 376 KB Output is correct
8 Correct 1 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 504 KB Output is correct
13 Correct 2 ms 376 KB Output is correct
14 Correct 2 ms 376 KB Output is correct
15 Correct 2 ms 360 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 376 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 340 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 376 KB Output is correct
15 Correct 3 ms 424 KB Output is correct
16 Correct 3 ms 376 KB Output is correct
17 Correct 3 ms 376 KB Output is correct
18 Correct 3 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 380 KB Output is correct
26 Correct 3 ms 368 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 380 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 424 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 376 KB Output is correct
44 Correct 3 ms 376 KB Output is correct
45 Correct 3 ms 376 KB Output is correct