Submission #1120805

#TimeUsernameProblemLanguageResultExecution timeMemory
1120805vjudge1Palindrome-Free Numbers (BOI13_numbers)C++17
85 / 100
5 ms576 KiB
#include<bits/stdc++.h> using namespace std; #define ll long long #define ld long long #define INF 1000000000 bool check(string a){ bool can = 1; int n = a.size(); for (int i = 0; i < n - 1; i++){ if (a[i] == a[i + 1]) can = 0; } for (int i = 1; i < n - 1; i++){ if (a[i - 1] == a[i + 1]) can = 0; } return can; } 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; if (b - a <= 100000){ for (ll i = a; i <= b; i++){ if (check(to_string(i))) ans++; } } else{ 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; }

Compilation message (stderr)

numbers.cpp: In function 'int main()':
numbers.cpp:69:26: warning: comparison of integer expressions of different signedness: 'long long int' and 'long long unsigned int' [-Wsign-compare]
   69 |         for (ll i = a; i <= b; i++){
      |                        ~~^~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...