제출 #1124331

#제출 시각아이디문제언어결과실행 시간메모리
1124331stefanneaguPalindrome-Free Numbers (BOI13_numbers)C++20
88.33 / 100
1 ms584 KiB
#include <bits/stdc++.h> #define int long long using namespace std; int mai_mare(string s) { if(s.size() == 1) { int a = stoll(s); return 10 - a; } s = "$" + s; int dp[s.size() + 1][s.size() + 1][10][10]; memset(dp, 0, sizeof(dp)); if(s[1] != s[2]) { dp[2][2][s[1] - '0'][s[2] - '0']++; } for(int j = s[2] - '0' + 1; j < 10; j++) { if(s[1] - '0' != j) { dp[2][1][s[1] - '0'][j]++; } } for(int i = s[1] - '0' + 1; i < 10; i++) { for(int j = 0; j <= 9; j++) { if(i != j) { dp[2][0][i][j]++; } } } bool ok = 0; for(int i = 2; i <= (int) s.size() - 2; i++) { // nu continua for(int j = 0; j < i; j++) { for(int nr1 = 0; nr1 < 10; nr1++) { for(int nr2 = 0; nr2 < 10; nr2++) { if(nr1 == nr2 || dp[i][j][nr1][nr2] == 0) { continue; } for(int nr3 = 0; nr3 < 10; nr3++) { if(nr3 == nr1 || nr3 == nr2) { continue; } dp[i + 1][j][nr2][nr3] += dp[i][j][nr1][nr2]; } } } } if(s[i] == s[i - 1] || ok) { continue; } for(int nr = s[i + 1] - '0' + 1; nr < 10; nr++) { if(nr == s[i] - '0' || nr == s[i - 1] - '0') { continue; } dp[i + 1][i][s[i] - '0'][nr]++; } if(s[i + 1] != s[i] && s[i + 1] != s[i - 1]) { dp[i + 1][i + 1][s[i] - '0'][s[i + 1] - '0']++; } else { ok = 1; } } int ans = 0; for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { for(int lot = 0; lot < (int) s.size(); lot++) { ans += dp[s.size() - 1][lot][i][j]; } } } return ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); string a, b; cin >> a >> b; // assert(stoll(b) < (int) 1e9); int bb = stoll(b) + 1; int ans = mai_mare(a) - mai_mare(to_string(bb)); string s = "1"; int numar = 1; while(s.size() < a.size()) { s += "0"; numar *= 10; } for(int i = a.size() + 1; i <= (int) b.size(); i++) { s += "0"; numar *= 10; ans += mai_mare(s); } s += "0"; if(stoll(s) == bb) { ans += mai_mare(to_string(bb)); } cout << ans; return 0; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...