Submission #1120449

#TimeUsernameProblemLanguageResultExecution timeMemory
1120449HasanV11010238Palindrome-Free Numbers (BOI13_numbers)C++17
77.50 / 100
2 ms580 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long long #define INF 1000000000 ll calc(string a, string b){ while (a.size() < b.size()){ a = "0" + a; } int n = b.size(); vector<vector<vector<vector<ll>>>> dp(n + 1, vector<vector<vector<ll>>>(10, vector<vector<ll>>(10, vector<ll>(4, 0)))); if (a[0] == b[0]){ dp[0][a[0] - '0'][a[0] - '0'][3] = 1; } else{ int st = a[0] - '0', en = b[0] - '0'; for (int i = st + 1; i < en; i++){ dp[0][i][i][0] = 1; } dp[0][st][st][1] = dp[0][en][en][2] = 1; } for (int i = 1; i < n; i++){ int st = a[i] - '0', en = b[i] - '0'; for (int j = 0; j < 10; j++){ for (int k = 0; k < 10; k++){ for (int l = 0; l < 4; l++){ for (int no = 0; no < 10; no++){ if (no == j || no == k) continue; if (no < st && (l == 1 || l == 3)) continue; if (no > en && (l == 2 || l == 3)) continue; int ne = 0; if (no == st && (l == 1 || l == 3)){ ne += 1; } if (no == en && (l == 2 || l == 3)){ ne += 2; } dp[i][k][no][ne] += dp[i - 1][j][k][l]; } } } } } ll ans = 0; for (int i = 0; i < 10; i++){ for (int j = 0; j < 10; j++){ for (int k = 0; k < 4; k++){ ans += dp[n - 1][i][j][k]; } } } return ans; } int main(){ unsigned ll a, b, ans = 0; cin>>a>>b; string str1 = "0", str2 = "9"; for (int i = 0; i < 20; i++){ unsigned ll nu1 = stoull(str1), nu2 = stoull(str2); if (nu1 > b){ break; } if (nu2 < a){ str1 = "1" + str1, str2 = str2 + "9"; continue; } ans += calc(to_string(max(nu1, a)), to_string(min(nu2, b))); str1 = "1" + str1, str2 = str2 + "9"; } cout<<ans; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...