Submission #91632

#TimeUsernameProblemLanguageResultExecution timeMemory
91632MercenaryPalindrome-Free Numbers (BOI13_numbers)C++14
100 / 100
11 ms508 KiB
#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 (stderr)

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 timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...