Palindromic numbers
Topic Link
Ideas: DFS try every
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; #define ll long long #define ios std::ios::sync_with_stdio(false) #define int long long typedef unsigned long long ULL; vector<int>ans; int a[10]; void dfs(int step , int s) { if(step == 6 || step == 7) { if(s == 0){ bool ok = true; if(a[1] == 0)ok = false; for(int i = 1 ; i <= step / 2 ; i ++){ if(a[i] != a[step - i]){ ok = false; break; } } if(ok){ int aa = 0; for(int i = 1 ; i <= step - 1 ; i ++){ aa = aa * 10 + a[i]; } ans.push_back(aa); } } if(step == 7)return ; } for(int i = 0 ; i <= 9 ; i ++){ if(i > s)break; a[step] = i; dfs(step + 1 , s - i); } } signed main() { ios; cin.tie(0);/// int n; cin >> n; dfs(1 , n); if(ans.size() == 0){ cout << -1 << '\n'; } else{ sort(ans.begin() , ans.end()); for(int i = 0 ; i < ans.size() ; i ++){ cout << ans[i] << '\n'; } } return 0; }
Flip a coin
#include <bits/stdc++.h> using namespace std; const int maxn = 2e5 + 10; #define ll long long #define ios std::ios::sync_with_stdio(false) #define int long long typedef unsigned long long ULL; #define debug(x) cout << #x << " : " << x << '\n'; vector<int>vec; char a[maxn] , b[maxn]; signed main() { ios; cin.tie(0);/// cin >> a + 1 >> b + 1; int n = strlen(a + 1); for(int i = 1 ; i <= n ; i ++){ if(a[i] != b[i])vec.push_back(i); } int ans = 0; for(int i = 0 ; i < vec.size() ; i += 2){ ans += vec[i + 1] - vec[i]; } cout << ans << '\n'; return 0; }