제출 #779901

#제출 시각아이디문제언어결과실행 시간메모리
779901vjudge1Palindrome-Free Numbers (BOI13_numbers)C++17
100 / 100
1 ms340 KiB
#include <bits/stdc++.h> #define ll long long using namespace std; const ll maxn = 1e5 + 5, mod = 1e9 + 7, inf = 1e18; ll dp[20][12][12][2]; ll bachtrach(string &lim, int ith, int last1, int last2, bool smaller) { if(ith == lim.size()) return 1; if(dp[ith][last1+1][last2+1][smaller] != -1) { return dp[ith][last1+1][last2+1][smaller]; } int curdig = lim[ith]-'0'; ll ans = 0; if(last1 == -1) { if(smaller) { for(int i = 1; i <= 9; ++i) { if(i != last1 && i != last2) ans += bachtrach(lim, ith + 1, i, last1, 1); } ans += bachtrach(lim, ith + 1, -1, last1, 1); } else { for(int i = 1; i < curdig; ++i) { if(i != last1 && i != last2) ans += bachtrach(lim, ith + 1, i, last1, 1); } if(curdig != 0) { // ans += bachtrach(lim, ith + 1, -1, last1, 1); ans += bachtrach(lim, ith + 1, curdig, last1,0); ans += bachtrach(lim, ith + 1, -1, last1, 1); } else ans += bachtrach(lim, ith + 1, -1, last1, 0); } } else if(smaller) { for(int i = 0; i <= 9; ++i) { if(i != last1 && i != last2) ans += bachtrach(lim, ith + 1, i, last1, 1); } } else { for(int i = 0; i < curdig; ++i) { if(i != last1 && i != last2) ans += bachtrach(lim, ith + 1, i, last1, 1); } if(curdig != last1 && curdig != last2) ans += bachtrach(lim, ith + 1, curdig, last1, 0); } return dp[ith][last1+1][last2+1][smaller] = ans; } ll slove(ll x) { if(x < 0) return 0; string lim = to_string(x); memset(dp, -1, sizeof(dp)); return bachtrach(lim, 0, -1, -1, 0); } int main() { // freopen(".INP", "r", stdin); // freopen(".OUT", "w", stdout); cin.tie(0)->sync_with_stdio(0); ll a, b; cin >> a >> b; cout << slove(b) - slove(a - 1); }

컴파일 시 표준 에러 (stderr) 메시지

numbers.cpp: In function 'long long int bachtrach(std::string&, int, int, int, bool)':
numbers.cpp:8:9: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
    8 |  if(ith == lim.size()) return 1;
      |     ~~~~^~~~~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...