text
stringlengths
49
983k
#include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T--) { string s; long long n, ans = 0; cin >> n >> s; vector<int> psum(n + 1); map<int, int> prefs; prefs[0]++; for (int i = 0; i < n; ++i) { psum[i + 1] = psum[i] + s[i] - '0' - 1; ans += prefs[psum[i + 1]]; prefs[psum[i + 1]]++; } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using std::make_pair; template <typename T> inline T read() { T x = 0; char ch = std::getchar(); bool f = 0; while (!std::isdigit(ch)) { f = (ch == '-'); ch = std::getchar(); } while (std::isdigit(ch)) { x = x * 10 + (ch - '0'); ch = std::getchar(); } return f ? -x : x; } template <typename T> void print(T x) { if (x < 0) { x = -x; std::putchar('-'); } if (x < 10) { std::putchar(x + 48); return; } print<T>(x / 10); std::putchar(x % 10 + 48); return; } namespace Solution { using std::cin; using std::cout; using std::deque; using std::endl; using std::map; using std::pair; using std::queue; using std::set; using std::vector; const int N = 1004001, mod = 1e9 + 7; int a[N + N / 10]; char c[N]; void Fakemain() { int t = read<int>(); while (t--) { int len = read<int>(); long long ans = 0; memset(a, 0, sizeof a); scanf("%s", c + 1); a[0 + N / 10]++; int S = 0; for (int i = 1; i <= len; ++i) { S += c[i] - '0' - 1; ans += a[S + N / 10]++; } printf("%lld\n", ans); } return; } } // namespace Solution int main(int argc, char* argv[]) { Solution::Fakemain(); return 0; }
#include <bits/stdc++.h> using namespace std; long long C[100002]; void solv() { int n; cin >> n; int t[n + 1]; char cc; t[0] = 0; for (int i = 1; i <= n; i++) { cin >> cc; t[i] = (cc - '0') + t[i - 1]; } long long ans = 0, j; for (int i = 0; i <= n; i++) t[i] -= i; sort(t, t + n + 1); for (int i = 0; i <= n;) { j = i++; while (i <= n && t[j] == t[i]) i++; if (i - j > 1) ans += C[i - j]; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); C[2] = 1; for (int i = 3; i < 100002; i++) C[i] = C[i - 1] + i - 1; long long T = 1; cin >> T; while (T--) solv(); return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int t, i, l, n, sum, count, ans; cin >> t; for (l = 0; l < t; l++) { sum = 0, ans = 0; string s; cin >> n; cin >> s; map<long long int, long long int> m; m[0] = 1; for (i = 0; i < n; i++) { sum = sum + (s[i] - '0'); count = sum - i - 1; ans = ans + m[count]; if (m[count] == 0) m[count] = 1; else m[count]++; } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; const int N = 1e5 + 7, M = 1e6 + 7; int n, t, sum[N]; char s[N]; map<int, long long> mp; int main() { scanf("%d", &t); while (t--) { scanf("%d", &n); scanf("%s", s + 1); mp.clear(); mp[0]++; for (int i = 1; i <= n; i++) { int p = s[i] - '0'; sum[i] = sum[i - 1] + p; mp[sum[i] - i]++; } long long ans = 0; for (map<int, long long>::iterator it = mp.begin(); it != mp.end(); it++) { long long num = it->second; ans += (num * (num - 1)) / 2; } printf("%lld\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; string s; cin >> n; cin >> s; long long int ans = 0; unordered_map<long long int, long long int> m; m[0]++; long long int x = 0; for (int i = 0; i < n; i++) { x = x + s[i] - '1'; ans = ans + m[x]; m[x]++; } cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; const long long mod = 1e9 + 7; long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; long long sum = 0; unordered_map<long long, long long> mp; mp[0] = 1; long long ans = 0; int k = 0; for (int i = 0; i < n; i++) { sum += (s[i] - '0') - 1; if (mp[sum] > 0) ans += mp[sum]; mp[sum]++; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using ld = long double; using pii = pair<int, int>; void solve() { int n; cin >> n; string s; cin >> s; vector<int> a(n + 1); vector<int> pref(n + 1); for (int i = 0; i < n; i++) { int c = s[i] - '0'; a[i + 1] = c; pref[i + 1] = pref[i] + c; } map<int, int> opts; ll ans = 0; opts[0] = 1; for (int i = 1; i <= n; i++) { int curr = pref[i] - i; ans += opts[curr]; opts[curr]++; } cout << ans << '\n'; } int main() { cin.tie(0); ios_base::sync_with_stdio(0); int t; cin >> t; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; using ll = long long; int main() { int tests; cin >> tests; while (tests--) { int n; cin >> n; string str; cin >> str; vector<int> arr; arr.reserve(n); for (auto& ele : str) arr.push_back(ele - '0'); map<ll, ll> pref; ll run = 0; pref[0]++; for (int i = 0; i < n; i++) { run += arr[i]; pref[run - (i + 1)]++; } ll ans = 0; for (const auto& ele : pref) { ans += (ele.second * (ele.second - 1)) / 2; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long int n, answer = 0, sum = 0; cin >> n; string s; cin >> s; map<long long int, long long int> mp; mp[0] = 1; for (long long int i = 1; i <= n; i++) { sum += (s[i - 1] - '0'); answer += mp[sum - i]++; } cout << answer; cout << "\n"; } int main() { ios::sync_with_stdio(0); cin.tie(0); long long int tests = 1; cin >> tests; for (long long int test_case = 1; test_case <= tests; test_case++) { solve(); } cerr << "Execution Time : " << (float)clock() / CLOCKS_PER_SEC << " secs" << endl; return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; map<int, int> mp; long long pre[maxn], a[maxn]; int main() { int t; cin >> t; while (t--) { int n; mp.clear(); string s; cin >> n >> s; for (int i = 0; i < n; i++) a[i] = s[i] - '0' - 1; pre[0] = a[0]; mp[pre[0]]++; for (int i = 1; i < n; i++) pre[i] = pre[i - 1] + a[i], mp[pre[i]]++; long long res = 0; for (auto i : mp) { int tmp = i.second; if (i.first == 0) res += tmp + 1ll * tmp * (tmp - 1) / 2; else if (tmp > 1) res += 1ll * tmp * (tmp - 1) / 2; } cout << res << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 10; char shu[maxn]; long long sum; long long ge; int main() { int t; scanf("%d", &t); while (t--) { map<long long, long long> mp; ge = 0; sum = 0; long long n; scanf("%lld", &n); scanf("%s", shu); for (int i = 0; i < n; i++) { sum += (shu[i] - '0' - 1); if (sum == 0) { ge++; } ge += mp[sum]; mp[sum]++; } printf("%lld\n", ge); } return 0; }
#include <bits/stdc++.h> using namespace std; using lol = long long int; const lol mod1 = 1e9 + 7; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { int n; cin >> n; map<int, lol> m; int sum = 0; for (int i = 0; i < n; i++) { char c; cin >> c; sum += (c - '0') - 1; m[sum]++; } m[0]++; lol ans = 0; for (auto p : m) { ans += (p.second * (p.second - 1)) / 2; } cout << ans << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int arr[100000]; int prefix[100001]; int main(void) { int T; cin >> T; while (T--) { int n; cin >> n; string s; cin >> s; for (int i = 0; i < n; i++) { arr[i] = s[i] - '1'; } prefix[0] = 0; for (int i = 0; i < n; i++) { prefix[i + 1] = prefix[i]; prefix[i + 1] += arr[i]; } unordered_map<int, long long> cnt; for (int i = 0; i <= n; i++) { if (cnt.find(prefix[i]) == cnt.end()) { cnt.insert(make_pair(prefix[i], 1)); } else { cnt[prefix[i]]++; } } long long result = 0; for (auto p : cnt) { result += p.second * (p.second - 1) / 2; } cout << result << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int t, n; cin >> t; while (t--) { cin >> n; string str; long long s = 0, ans = 0; map<long long, long long> occur; occur[0] = 1; cin >> str; for (int i = 0; i < n; i++) { s += (int)str[i] - 48; long long x = s - i - 1; occur[x]++; ans += occur[x] - 1; } cout << ans << '\n'; } }
#include <bits/stdc++.h> using namespace std; pair<int, int> mp[2000000], mp2[2000000]; int x, n, t; int it; inline void add(int x) { if (mp[x].first < it) { mp[x].first = it; mp[x].second = 0; } ++mp[x].second; } inline int get(int x) { if (mp[x].first < it) return 0; return mp[x].second; } inline void add2(int x) { x = abs(x); if (mp2[x].first < it) { mp2[x].first = it; mp2[x].second = 0; } ++mp2[x].second; } inline int get2(int x) { x = abs(x); if (mp2[x].first < it) return 0; return mp2[x].second; } int main() { ios_base::sync_with_stdio(false); cin.tie(0); cin >> t; while (t-- > 0) { ++it; string s; cin >> n; cin >> s; int sum = 0; long long ans = 0; for (int i = 1; i <= n; ++i) { int x = s[i - 1] - '0'; if (sum - i + 1 >= 0) add(sum - i + 1); else add2(sum - i + 1); sum += x; if (sum - i >= 0) ans += get(sum - i); else ans += get2(sum - i); } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve(vector<int>& v, int n) { map<int, int> mp; mp[0]++; vector<int> prefixSum(n + 1, 0); long long int ans = 0; for (int i = 0; i < n; i++) { prefixSum[i + 1] = prefixSum[i] + v[i]; int x = prefixSum[i + 1] - i - 1; ans += mp[x]; mp[x]++; } cout << ans << endl; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; cin >> t; while (t--) { int n; cin >> n; vector<int> v(n); for (auto& x : v) { char ch; cin >> ch; x = ch - '0'; } solve(v, n); } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; while (t--) { long long int n; cin >> n; string s; cin >> s; vector<long long int> v; v.push_back(-1); for (auto x : s) v.push_back(x - '0'); v.resize(n + 1); long long int sum = 0, ans = 0; map<long long int, long long int> m; m[0] = 1; for (int i = 1; i <= n; i++) { sum += v[i]; ans += m[i - sum]; m[i - sum]++; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { long long int n; long long int sum = 0; map<long long int, long long int> mp; cin >> n; long long int arr1[n], i; string arr; cin >> arr; for (i = 0; i < n; i++) arr1[i] = (arr[i] - '0') - 1; long long int ans = 0; long long int brr[n + 4]; brr[0] = 0; for (i = 0; i < n; i++) { brr[i + 1] = brr[i] + arr1[i]; } for (i = 0; i <= n; i++) mp[brr[i]]++; for (i = 0; i <= n; i++) { if (mp[brr[i]] > 0) { ans = ans + (mp[brr[i]] * (mp[brr[i]] - 1)) / 2; mp[brr[i]] = 0; } } cout << ans << endl; } int main() { long long int t; cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; const long long int inf = (long long int)2e18; const int mod = 1e9 + 7; bool overflow(long long int a, long long int b) { if (a <= (inf + b - 1) / b) return false; return true; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; long long int a[n + 1]; for (int i = 0; i < n; i++) { a[i] = (s[i] - 48); } map<long long int, long long int> m; long long int sum = 0, ans = 0; m[0] = 1; for (int i = 0; i < n; i++) { sum += a[i]; ans += (m[(i + 1) - sum]); m[(i + 1) - sum]++; } cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; long long int findSubarraySum(long long int arr[], long long int n, long long int sum) { map<long long int, long long int> prevSum; long long int res = 0; long long int currsum = 0; for (long long int i = 0; i < n; i++) { currsum += arr[i]; if (currsum == sum) res++; if (prevSum.find(currsum - sum) != prevSum.end()) res += (prevSum[currsum - sum]); prevSum[currsum]++; } return res; } void ETA() { long long int n; cin >> n; string str; cin >> str; long long int arr[n]; for (long long int i = 0; i < n; i++) arr[i] = str[i] - '1'; cout << findSubarraySum(arr, n, 0) << '\n'; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); long long int t = 1; cin >> t; while (t--) { ETA(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t; cin >> t; while (t--) { long long n; cin >> n; string s; cin >> s; map<long long, long long> m; m[0] += 1; long long sum = 0, ans = 0; for (long long i = 0; i < n; i++) { sum += s[i] - '0'; ans += m[sum - (i + 1)]; m[sum - (i + 1)] += 1; } cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int tc = 1; cin >> tc; while (tc--) { int n; cin >> n; string s; cin >> s; int pre[n + 1], suff[n + 1]; pre[0] = 0; for (int i = 1; i <= n; i++) { pre[i] = pre[i - 1] + (s[i - 1] - '0'); } suff[n] = 0; for (int i = n - 1; i >= 0; i--) { suff[i] = suff[i + 1] + (s[i] - '0'); } for (int i = 0; i <= n; i++) { pre[i] = pre[i] - (i + 1); } for (int i = n, j = 1; i >= 0; i--) { suff[i] = suff[i] - j; j++; } map<int, int> cnt1, cnt2; long long ans = 0; for (int i = 0; i <= n; i++) { if (cnt1.count(pre[i]) > 0) { ans += cnt1[pre[i]]; } cnt1[pre[i]]++; } for (int i = n; i >= 0; i--) { if (cnt2.count(suff[i]) > 0) { ans += cnt2[suff[i]]; } cnt2[suff[i]]++; } cout << ans / 2 << endl; } }
#include <bits/stdc++.h> using namespace std; const int N = 1e6; int p[N + 13]; map<int, int> cow; int n; void sol() { cin >> n; memset(p, 0, sizeof p); cow.clear(); cow[p[0]] = 1; int64_t res = 0; for (int i = 1; i <= n; ++i) { char c; cin >> c; p[i] = p[i - 1] + (c - '0'); res += cow[p[i] - i]; cow[p[i] - i]++; } cout << res << '\n'; } int main() { ios::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) sol(); }
#include <bits/stdc++.h> using namespace std; int main() { int tc; cin >> tc; ; while (tc--) { int n; cin >> n; ; string s; cin >> s; vector<int> v(n); v[0] = ((int)s[0] - 49); unordered_map<int, int> mp; long long sm = 0; for (int i = (1); i <= (n - 1); ++i) { v[i] = v[i - 1] + ((int)s[i] - 49); } for (int i = (0); i <= (n - 1); ++i) mp[v[i]]++; sm = sm + mp[0]; for (auto d : mp) { int h = d.second; long long k; if (h % 2) k = (h - 1) / 2, k = k * h; else k = h / 2, k *= h - 1; sm += k; } cout << sm << "\n"; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; unordered_map<long long, long long> umap; umap.clear(); umap[0] = 1; long long ans = 0; long long sum = 0; for (char x : s) { sum += x - '0' - 1; ans += umap[sum]; umap[sum]++; } cout << ans << endl; } }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") #pragma GCC optimization("unroll-loops") using namespace std; void func() { long long n, i, j; cin >> n; long long tab[n], x, ans = 0, pre[n + 1]; string s; cin >> s; for (i = 0; i < n; i++) { tab[i] = s[i] - '0'; } for (i = 1; i <= n; i++) { pre[i] = pre[i - 1] + tab[i - 1]; } for (i = 1; i <= n; i++) { for (j = i; j <= n; j++) { ans += (pre[j] - pre[i - 1] == j - i + 1); } } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); int t; cin >> t; for (long long i = 1; i <= t; i++) { func(); } return 0; }
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long x; cin >> x; while (x--) { long long n; cin >> n; string s; cin >> s; vector<long long> v; for (char ch : s) v.push_back((ch - '0' - 1)); unordered_map<long long, long long> um; um[0] += 1; um[v[0]] += 1; for (long long i = 1; i < n; i++) { v[i] += v[i - 1]; um[v[i]] += 1; } long long ans = 0; for (auto u : um) { ans += u.second * (u.second - 1) / 2; } cout << ans << "\n"; } }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") #pragma GCC target("avx,avx2,fma") using namespace std; long long cnt[10]; string s, s1, s2, s3, s4; map<long long, long long> mp; int main() { long long t; cin >> t; for (long long cs = 1; cs <= t; cs++) { long long m, n, b, c, d, i, j, k, x, y, z, l, r, p, q; long long cn = 0, ans = 0, sum = 0; cin >> n >> s; mp[0] = 1; for (long long i = 0; i < n; i++) { long long val = s[i] - '0'; val--; sum += val; ans += mp[sum]; mp[sum]++; } printf("%lld\n", ans); mp.clear(); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t, n; string s; cin >> t; while (t--) { cin >> n >> s; string temp = s; unordered_map<int, int> dict; long long ans = 0; int prev = 0; dict[0] = 1; for (int k = 0; k < n; k++) { prev += s[k] - '0'; int temp = prev - k - 1; if (dict.find(temp) != dict.end()) ans += dict[temp]; dict[temp] += 1; } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long t, n, a[100011], sum[100011], ans; map<long long, long long> mp; signed main() { scanf("%lld", &t); while (t--) { mp.clear(); scanf("%lld", &n); memset(a, 0, sizeof a); memset(sum, 0, sizeof sum); ans = 0; for (register long long i = 1; i <= n; ++i) { a[i] = getchar() - '0'; while (a[i] < 0 || a[i] > 9) a[i] = getchar() - '0'; a[i] = (a[i]) ? a[i] - 1 : -1; sum[i] = sum[i - 1] + a[i]; } mp[0] = 1; for (register long long i = 1; i <= n; ++i) { ans += mp[sum[i]]; ++mp[sum[i]]; } printf("%lld\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long maxn = 1e5 + 10; long long sum[maxn]; char str[maxn]; void solve() { long long n; scanf("%lld", &n); scanf("%s", str + 1); for (long long i = 1; i <= n; ++i) { sum[i] = (long long)(str[i] - '0'); } map<long long, long long> mt; mt[0] = 1; long long ans = 0; for (long long i = 1; i <= n; ++i) { sum[i] += sum[i - 1]; ans += mt[sum[i] - i]; ++mt[sum[i] - i]; } printf("%lld\n", ans); } signed main() { long long t; scanf("%lld", &t); while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n, sum = 0; long long ans = 0; cin >> n; char s[n]; cin >> (s + 1); map<int, int> f; f[0] = 1; for (int i = 1; i <= n; i++) { int tmp = s[i] - '0'; sum += tmp; ans += f[sum - i]; f[sum - i]++; } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { int n2; cin >> n2; string s; cin >> s; int n = s.length(); map<int, int> mp; mp[0] = 1; long long ans = 0, sum = 0; for (int i = 0; i < n; i++) { sum += s[i] - '1'; ans += mp[sum]++; } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; long long int mod = 1000000007; long long int z = 1000000000; int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; string s; cin >> s; map<long long int, long long int> mp; mp[0]++; long long int sum = 0; long long int res = 0; for (int i = 0; i < n; i++) { sum += (s[i] - '0'); res += mp[sum - (i + 1)]; mp[sum - (i + 1)]++; } cout << res << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long int t; cin >> t; while (t--) { long long int n; cin >> n; string s; cin >> s; vector<long long int> pre(n); pre[0] = s[0] - '0' - 1; for (long long int i = 1; i < n; i++) pre[i] = pre[i - 1] + s[i] - '0' - 1; map<long long int, long long int> m; for (long long int i = 0; i < n; i++) m[pre[i]]++; map<long long int, long long int>::iterator it = m.begin(); long long int count = 0; while (it != m.end()) { long long int k = it->second; if (it->first == 0) count += (k * (k + 1)) / 2; else count += (k * (k - 1)) / 2; it++; } cout << count << endl; } }
#include <bits/stdc++.h> using namespace std; const signed inf = 1000000100; const long long inf64 = 1ll * inf * inf; const long long mod = 1e9 + 7; mt19937 rng(0); void run() { long long n; cin >> n; vector<char> a(n); for (auto &i : a) cin >> i; vector<long long> pref(n); pref[0] = a[0] - 1 - '0'; map<long long, long long> cnt; cnt[0]++; long long ans = cnt[a[0] - 1 - '0']; cnt[a[0] - 1 - '0']++; for (long long i = 1; i < n; ++i) { pref[i] = pref[i - 1] + a[i] - 1 - '0'; ans += cnt[pref[i]]; cnt[pref[i]]++; } cout << ans << '\n'; } signed main() { iostream::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) { run(); cout.flush(); } }
#include <bits/stdc++.h> using namespace std; mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); vector<long long> dir_x{0ll, 0ll, -1ll, 1ll}; vector<long long> dir_y{1ll, -1ll, 0ll, 0ll}; long long power_mod(long long x, long long y, long long m) { long long arr = 1; x %= m; while (y) { if (y & 1) { arr = (arr * x) % m; } y >>= 1; x = (x * x) % m; } return arr; } long long power(long long x, long long y) { long long arr = 1; while (y) { if (y & 1) { arr *= x; } y >>= 1; x = x * x; } return arr; } long long mod_inv(long long x, long long m) { return power_mod(x, m - 2, m); } int randomGenerator(int limit) { random_device rd; mt19937 gen(rd()); uniform_int_distribution<> dis(1, limit); return dis(gen); } struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; bool comp(const pair<long long, pair<long long, long long>> &a, const pair<long long, pair<long long, long long>> &b) { if (a.first == b.first) { return (a.second < b.second); } return (a.first < b.first); } struct hash_pair { template <class T1, class T2> size_t operator()(const pair<T1, T2> &p) const { auto hash1 = hash<T1>{}(p.first); auto hash2 = hash<T2>{}(p.second); return hash1 ^ hash2; } }; bool isPalindrome(string s) { bool flag = true; for (int i = 0; i < s.length() / 2; i++) { if (s[i] != s[s.length() - i - 1]) { flag = false; break; } } return flag; } bool isPerfectSquare(long long n) { long double val = sqrt(n); return (val - floor(val) == 0); } bool isPrime(long long n) { if (n < 2) { return false; } for (long long i = 2; i * i <= n; i++) { if (n % i == 0) { return false; } } return true; } long long NCR(long long n, long long k) { long long res = 1; if (k > n - k) { k = n - k; } for (long long i = 0; i < (k); i++) { res *= (n - i); res /= (i + 1); } return res; } long long cntBits(long long n) { long long cnt = 0; for (long long i = 0; i < 64; i++) { if ((1ll << i) & n) { cnt++; } } return cnt; } void solve() { long long n; cin >> n; string s; cin >> s; map<long long, long long> mp; long long sum = 0; mp[0]++; for (long long i = 0; i < (n); i++) { sum += (s[i] - '0'); mp[(sum - (i + 1))]++; } long long ans = 0; for (auto it : mp) { ans += ((it.second * (it.second - 1)) / 2); } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long t; cin >> t; for (long long i = (1); i <= (t); i++) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; long long int nc2(long long int n) { long long int d = (n * (n - 1)) / 2; if (n == 0 || n == 1) return 0; else return d; } int32_t main() { ios::sync_with_stdio(false); cin.tie(0); long long int t = 1; cin >> t; while (t--) { long long int n; cin >> n; string s; cin >> s; vector<long long int> a; a.push_back(0); for (long long int i = 0; i < n; i++) { a.push_back((char)s[i] - 48 - 1); } vector<long long int> c; map<long long int, long long int> m; c.push_back(a[0]); m[c[0]]++; for (long long int i = 1; i <= n; i++) { c.push_back(c[i - 1] + a[i]); m[c[i]]++; } long long int ans = 0; for (auto x : m) { ans += nc2(x.second); } cout << ans << endl; } }
#include <bits/stdc++.h> #pragma GCC optimize("Ofast") using namespace std; template <class T> using V = vector<T>; template <class T> using VV = V<V<T>>; template <class T> using priority_queue_rev = priority_queue<T, vector<T>, greater<T>>; template <class T, class U> inline bool chmax(T &a, const U &b) { if (a < b) { a = b; return 1; } return 0; } template <class T, class U> inline bool chmin(T &a, const U &b) { if (a > b) { a = b; return 1; } return 0; } template <class T1, class T2> istream &operator>>(istream &is, pair<T1, T2> &p) { is >> p.first >> p.second; return is; } template <class T1, class T2> ostream &operator<<(ostream &os, const pair<T1, T2> &p) { os << '(' << p.first << ", " << p.second << ')'; return os; } template <class T> istream &operator>>(istream &is, vector<T> &v) { for (auto &e : v) is >> e; return is; } template <class T> ostream &operator<<(ostream &os, const vector<T> &v) { for (auto &e : v) os << e << ' '; return os; } template <typename T> ostream &operator<<(ostream &os, set<T> &set_var) { os << "{"; for (auto itr = set_var.begin(); itr != set_var.end(); itr++) { os << *itr; ++itr; if (itr != set_var.end()) os << ", "; itr--; } os << "}"; return os; } template <typename T, typename U> ostream &operator<<(ostream &os, map<T, U> &map_var) { os << "{"; for (auto itr = map_var.begin(); itr != map_var.end(); itr++) { os << *itr; itr++; if (itr != map_var.end()) os << ", "; itr--; } os << "}"; return os; } template <class T> inline int count_between(vector<T> &a, T l, T r) { return lower_bound(a.begin(), a.end(), r) - lower_bound(a.begin(), a.end(), l); } template <typename T> T gcd(T x, T y) { if (x % y == 0) return y; return gcd(y, x % y); } template <typename T> T gcd(vector<T> a) { T res = a[0]; for (auto &x : a) res = gcd(res, x); return res; } template <typename T> T mypow(T x, long long n) { T ret = 1; while (n > 0) { if (n & 1) (ret *= x); (x *= x); n >>= 1; } return ret; } int dx[8] = {1, 0, -1, 0, 1, 1, -1, -1}; int dy[8] = {0, 1, 0, -1, 1, -1, -1, 1}; class Prime { public: const int N; vector<int> min_prime; vector<int> primes; Prime(int size) : N(size), min_prime(N + 1) { for (int i = (0); i < (N + 1); ++i) min_prime[i] = i; min_prime[0] = -1; min_prime[1] = -1; for (int i = 2; i <= N; i++) { if (min_prime[i] != i) continue; primes.push_back(i); int tmp = 2 * i; while (tmp <= N) { if (min_prime[tmp] == tmp) min_prime[tmp] = i; tmp += i; } } } bool check(int x) { return min_prime[x] == x; } }; void solve() { int n; cin >> n; string s; cin >> s; V<int> a(n); for (int i = (0); i < (n); ++i) a[i] = s[i] - '1'; map<long long, long long> q; q[0] = 1; long long now = 0; for (int i = (0); i < (n); ++i) { now += a[i]; q[now]++; } long long ans = 0; for (auto itr : q) { ans += itr.second * (itr.second - 1) / 2; } cout << ans << '\n'; } int main() { cin.tie(0); ios_base::sync_with_stdio(false); cout << setprecision(20); int codeforces = 1; cin >> codeforces; while (codeforces--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; void solve(); int main() { ios_base::sync_with_stdio(false); int t; cin >> t; while (t--) solve(); } void solve() { int n; string s; cin >> n >> s; vector<int> pref(n); for (int i = 0; i < n; i++) { if (!i) { pref[i] = (s[i] - '0'); } else { pref[i] = pref[i - 1] + (s[i] - '0'); } } for (int i = 0; i < n; i++) pref[i] -= (i + 1); map<int, int> cnt; cnt[0] = 1; long long ans = 0; for (auto it : pref) { ans += cnt[it]; cnt[it]++; } cout << ans << '\n'; }
#include <bits/stdc++.h> using namespace std; long long int findSubarraySum(long long int arr[], long long int n, long long int sum = 0) { unordered_map<long long int, long long int> prevSum; long long int res = 0; long long int currsum = 0; for (long long int i = 0; i < n; i++) { currsum += arr[i]; if (currsum == sum) res++; if (prevSum.find(currsum - sum) != prevSum.end()) res += (prevSum[currsum - sum]); prevSum[currsum]++; } return res; } void olve() { long long int n; cin >> n; string s; cin >> s; long long int ar[n]; for (long long int i = 0; i < n; i++) { ar[i] = s[i] - '0'; ar[i] -= 1; } cout << findSubarraySum(ar, n) << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; long long int t; cin >> t; while (t--) olve(); return 0; }
#include <bits/stdc++.h> #pragma GCC optimize(3) #pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer", "inline") #pragma GCC option("arch=native", "tune=native", "no-zero-upper") #pragma GCC target("avx2") using namespace std; const long long INF = 1e17; const int maxn = 1e6 + 700; const int mod = 1e9 + 9; inline bool read(long long &num) { char in; bool IsN = false; in = getchar(); if (in == EOF) return false; while (in != '-' && (in < '0' || in > '9')) in = getchar(); if (in == '-') { IsN = true; num = 0; } else num = in - '0'; while (in = getchar(), in >= '0' && in <= '9') { num *= 10, num += in - '0'; } if (IsN) num = -num; return true; } long long n, m, p; char s[maxn]; long long f[maxn]; int vis[maxn]; int main() { int T; scanf("%d", &T); while (T--) { read(n); memset(vis, 0, sizeof(vis)); scanf("%s", s + 1); int len = strlen(s + 1); for (int i = 1; i <= len; i++) f[i] = f[i - 1] + s[i] - '0'; vis[100000] = 1; long long ans = 0; for (int i = 1; i <= len; i++) { ans += vis[f[i] - i + 100000]; vis[f[i] - i + 100000]++; } printf("%lld\n", ans); } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int n; cin >> n; string s; cin >> s; map<long long int, long long int> mp; set<long long int> q; long long int sum = 0; long long int ans = 0; for (int i = 0; i < n; i++) { sum += s[i] - '0'; if (sum == i + 1) ans++; mp[sum - i]++; q.insert(sum - i); } for (auto i : q) ans += ((mp[i] - 1) * (mp[i])) / 2; cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while (t--) { long long int n, i, c = 0; cin >> n; string s; cin >> s; long long int curr_sum = 0; unordered_map<long long int, long long int> mp; for (i = 0; i < n; i++) { long long int x = s[i] - '0'; x--; curr_sum += x; if (curr_sum == 0) c++; if (mp.find(curr_sum) != mp.end()) { c += mp[curr_sum]; } mp[curr_sum]++; } cout << c << '\n'; } }
#include <bits/stdc++.h> using namespace std; long long prck(long long n) { long long f = 1; if (n == 1) f = 0; else if (n == 2) f = 1; else if (n % 2 == 0) f = 0; else { long long i; for (i = 3; (i * i <= n) && (f == 1); i += 2) { if (n % i == 0) f = 0; } } return f; } long long nCr(long long n, long long r) { long long i, res = 1; if (r > n - r) r = n - r; for (i = 0; i < r; ++i) { res *= (n - i); res /= (i + 1); } return res; } long long gcd(long long a, long long b) { if (a == 0) return b; return gcd(b % a, a); } long long btod(string n) { long long lg = n.length(); long long i, val = 0, bs = 1; for (i = lg - 1; i >= 0; i--) { if (n[i] == '1') { val += bs; } bs *= 2; } return val; } long long ston(string s) { long long val = 0, bs = 1; long long sl = s.length(); sl--; while (sl >= 0) { val += ((s[sl] - 48) * bs); bs *= 10; sl--; } return val; } long long nCrModpDP(long long n, long long r, long long p) { long long i, j, C[r + 1]; memset(C, 0, sizeof(C)); C[0] = 1; for (i = 1; i <= n; i++) { for (j = min(i, r); j > 0; j--) { C[j] = (C[j] + C[j - 1]) % p; } } return C[r]; } long long nCrModpLucas(long long n, long long r, long long p) { if (r == 0) return 1; long long ni = n % p, ri = r % p; return (nCrModpLucas(n / p, r / p, p) * nCrModpDP(ni, ri, p)) % p; } long long pwr(long long x, long long y, long long p) { long long res = 1; x = x % p; while (y > 0) { if (y & 1) res = (res * x) % p; y = y >> 1; x = (x * x) % p; } return res; } long long modInverse(long long n, long long p) { return pwr(n, p - 2, p); } long long nCrModPFermat(long long n, long long r, long long p, long long fac[]) { if (r == 0) return 1; return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) % p; } int calcDate(int date, int month, int year) { if (month == 1) { month = 13; year--; } if (month == 2) { month = 14; year--; } int q = date, m = month, k = year % 100, j = year / 100; int day = q + (13 * (m + 1)) / 5 + k + (k / 4) + (j / 4) + 5 * j; day = day % 7; return day; } vector<vector<long long>> matmul(vector<vector<long long>> a, vector<vector<long long>> b) { long long k = a.size(); vector<vector<long long>> c(k, vector<long long>(k)); long long i, j, l; for (i = 0; i < k; i++) { for (j = 0; j < k; j++) { for (l = 0; l < k; l++) { c[i][j] = (c[i][j] + a[i][l] * b[l][j]) % 1000000007; } } } return c; } vector<vector<long long>> matexp(vector<vector<long long>> a, long long p) { if (p == 1) return a; if (p & 1) return matmul(a, matexp(a, p - 1)); vector<vector<long long>> x = matexp(a, p / 2); return matmul(x, x); } long long linrec(long long n, long long k, vector<long long> a, vector<long long> b) { if (n == 0) return 0; if (n <= k) return a[n - 1]; long long i, j; vector<long long> F1(k + 1); for (i = 1; i <= k; i++) F1[i] = a[i - 1]; vector<vector<long long>> T(k + 1, vector<long long>(k + 1)); for (i = 1; i <= k; i++) { for (j = 1; j <= k; j++) { if (i < k) { if (j == i + 1) T[i][j] = 1; else T[i][j] = 0; continue; } T[i][j] = b[k - j]; } } T = matexp(T, n - 1); long long res = 0; for (i = 1; i <= k; i++) { res += (T[1][i] * F1[i]) % 1000000007; } return res % 1000000007; } long long modexp(long long x, long long n) { if (n == 0) return 1; else if (n % 2 == 0) { return modexp((x * x) % 1000000007, n / 2); } else { return (x * modexp((x * x) % 1000000007, (n - 1) / 2) % 1000000007); } } long long getFractionModulo(long long a, long long b) { long long c = gcd(a, b); a = a / c; b = b / c; long long d = modexp(b, 1000000007 - 2); long long ans = ((a % 1000000007) * (d % 1000000007)) % 1000000007; return ans; } long long INF = (1LL << 60) - 1; void dfs(long long node, long long root, long long vis[], vector<long long> adj[], long long a[], long long b[], long long ans[], long long del[], long long p[], long long i) { vis[node] = 1; b[node] -= min(b[node], a[root]); if (b[node] <= 0) { ans[node] = i + 1; } for (long long child : adj[node]) { if (vis[child] == 0 && del[child] == 0) { dfs(child, root, vis, adj, a, b, ans, del, p, i); } } } void dfs2(long long x, long long par, long long val, vector<pair<long long, long long>> adj[], long long dist[], long long l[], long long r[]) { dist[x] = max(dist[x], val); long long sz = adj[x].size(), child, i; long long flg = 0; for (i = 0; i < sz; i++) { if (adj[x][i].first == par) { adj[x].erase(adj[x].begin() + i); flg = 1; break; } } if (flg) sz--; for (i = 0; i < sz; i++) { child = adj[x][i].first; if (i == 0) l[child] = dist[child] + adj[x][i].second; else { l[child] = max(l[adj[x][i - 1].first], dist[child] + adj[x][i].second); } } for (i = sz - 1; i >= 0; i--) { child = adj[x][i].first; if (i == sz - 1) r[child] = dist[child] + adj[x][i].second; else { r[child] = max(r[adj[x][i + 1].first], dist[child] + adj[x][i].second); } } long long mx; for (i = 0; i < sz; i++) { child = adj[x][i].first; mx = val; if (i > 0) mx = max(mx, l[adj[x][i - 1].first]); if (i < sz - 1) mx = max(mx, r[adj[x][i + 1].first]); dfs2(child, x, mx + adj[x][i].second, adj, dist, l, r); } } void bfs(long long node, long long vis[], vector<long long> adj[], long long prev[], long long col[], long long n) { queue<pair<long long, long long>> q; q.push({node, -1}); long long ptr, cnt = 0; while (!q.empty()) { pair<long long, long long> cur = q.front(); q.pop(); vis[cur.first] = 1; cnt++; for (long long child : adj[cur.first]) { if (child == cur.second) continue; col[child] = 1 - col[cur.first]; if (vis[child] == 1) { break; } else { q.push({child, cur.first}); prev[child] = cur.first; } } } } void addSelf(long long &a, long long b) { a += b; while (a >= 1000000007) a -= 1000000007; } void subSelf(long long &a, long long b) { a -= b; while (a < 0) a += 1000000007; } struct sr { long long type, val, ind; }; bool cmp(struct sr aa, struct sr ab) { if (aa.val == ab.val) return aa.type < ab.type; return aa.val > ab.val; } long long f[200002]; long long find(long long x) { if (f[x] == x) return x; else return f[x] = find(f[x]); } bool connected(long long a, long long b) { a = find(a); b = find(b); if (a == b) return 1; f[a] = b; return 0; } struct segtree { long long size; vector<long long> values; long long single(long long v) { return v; } long long NEUTRAL_ELEMENT = 0; long long merge(long long a, long long b) { return a + b; } void init(long long n) { size = 1; while (size < n) { size *= 2; } values.resize(2 * size); } void build(vector<long long> &a, long long x, long long lx, long long rx) { if (rx - lx == 1) { if (lx < (long long)a.size()) { values[x] = single(a[lx]); } return; } long long mid = (lx + rx) / 2; build(a, 2 * x + 1, lx, mid); build(a, 2 * x + 2, mid, rx); values[x] = merge(values[2 * x + 1], values[2 * x + 2]); } void build(vector<long long> &a) { build(a, 0, 0, size); } void set(long long i, long long v, long long x, long long lx, long long rx) { if (rx - lx == 1) { values[x] = single(v); return; } long long mid = (lx + rx) / 2; if (i < mid) { set(i, v, 2 * x + 1, lx, mid); } else { set(i, v, 2 * x + 2, mid, rx); } values[x] = merge(values[2 * x + 1], values[2 * x + 2]); } void set(long long i, long long v) { set(i, v, 0, 0, size); } long long calc(long long l, long long r, long long x, long long lx, long long rx) { if (lx >= r || rx <= l) { return NEUTRAL_ELEMENT; } if (lx >= l && rx <= r) return values[x]; long long mid = (lx + rx) / 2; long long s1 = calc(l, r, 2 * x + 1, lx, mid); long long s2 = calc(l, r, 2 * x + 2, mid, rx); return merge(s1, s2); } long long calc(long long l, long long r) { return calc(l, r, 0, 0, size); } long long find(long long i, long long k, long long x, long long lx, long long rx) { if (values[x] < k) return -1; if (rx <= i) return -1; if (rx - lx == 1) { return lx; } long long mid = (lx + rx) / 2; long long res = find(i, k, 2 * x + 1, lx, mid); if (res == -1) res = find(i, k, 2 * x + 2, mid, rx); return res; } long long find(long long i, long long k) { return find(i, k, 0, 0, size); } }; int main() { std::ios::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t, w; cin >> t; w = 0; while (w < t) { long long n, i, j, k; cin >> n; string s; cin >> s; map<long long, long long> mp; long long sm = 0, ans = 0; mp[0] = 1; for (i = 0; i < n; i++) { sm += (s[i] - '0'); ans += mp[sm - (i + 1)]; mp[sm - (i + 1)]++; } cout << ans << "\n"; w++; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long maxn = 105; const long long mod = 1e9 + 7; long long nc2(long long n) { return n * (n - 1) / 2; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; int t, n, i, j, k; cin >> t; while (t--) { cin >> n; string s; cin >> s; vector<int> a(n), p(n); map<int, int> f; long long ans = 0; for (i = 0; i < n; i++) { a[i] = s[i] - '0'; } for (i = 0; i < n; i++) { if (i > 0) p[i] = p[i - 1] + a[i]; else p[i] = a[i]; } for (i = 0; i < n; i++) { if (p[i] == i + 1) ++ans; ++f[p[i] - i]; } for (auto [el, fr] : f) { ans += nc2(fr); } cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; using ll = long long int; int main() { int t; cin >> t; while (t--) { ll n, out = 0, t = 0; cin >> n; string s; cin >> s; map<ll, ll> mp; mp[0] = 1; for (int i = 0; i < s.size(); i++) { t += s[i] - '1'; out += mp[t]; mp[t]++; } cout << out << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e5 + 5; inline void io() { std::ios::sync_with_stdio(false); std::cin.tie(0); std::cout.tie(0); } long long a[MAXN], sum[MAXN], t[MAXN]; int main() { io(); int T; cin >> T; while (T--) { int n; string str; cin >> n >> str; for (int i = 0; i < n; i++) { a[i + 1] = str[i] - '0'; sum[i + 1] = sum[i] + a[i + 1]; } map<long long, int> ccc; for (int i = 0; i <= n; i++) { t[i] = sum[i] - i; ccc[t[i]]++; } long long allans = 0; for (auto v : ccc) { allans += (1ll) * (1 + v.second - 1) * (v.second - 1) / 2; } cout << allans << '\n'; } }
#include <bits/stdc++.h> using namespace std; const int MAX_N = 1e5 + 1; const long long MOD = 1e9 + 7; const long long INF = 1e9; struct custom_hash { static uint64_t splitmix64(uint64_t x) { x += 0x9e3779b97f4a7c15; x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9; x = (x ^ (x >> 27)) * 0x94d049bb133111eb; return x ^ (x >> 31); } size_t operator()(uint64_t x) const { static const uint64_t FIXED_RANDOM = chrono::steady_clock::now().time_since_epoch().count(); return splitmix64(x + FIXED_RANDOM); } }; void output_vector(vector<long long>& v) { for (long long i = 0; i < v.size() - 1; i++) { cout << v[i] << " "; } cout << v.back() << endl; } void solve() { long long n; cin >> n; string s; cin >> s; vector<long long> v(n + 1, 0); for (long long i = 1; i <= n; i++) { v[i] = (s[i - 1] - '0') + v[i - 1]; } for (long long i = 0; i <= n; i++) { v[i] = v[i] - i; } unordered_map<long long, long long, custom_hash> umap; for (long long i = 0; i <= n; i++) { umap[v[i]]++; } long long ans = 0; for (auto i : umap) { if (i.second != 1) { ans += (i.second * (i.second - 1)) / 2; } } cout << ans << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int tc = 1; cin >> tc; for (int t = 1; t <= tc; t++) { solve(); } }
#include <bits/stdc++.h> using namespace std; const long long MAX = 1e5 + 1; long long test, n, arr[MAX]; string s; signed main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); ; cin >> test; while (test--) { map<long long, long long> cnt; cin >> n >> s; long long sum = 0, ans = 0; cnt[0]++; for (long long i = 0; i < n; i++) { sum += (s[i] - '0') - 1; ans += cnt[sum]; cnt[sum]++; } cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; int t = 4550000, T_d[5200100]; vector<int> d_cl; int z_int(char a) { int res = a - 48; return res; } int main() { int q; cin >> q; for (int w = 0; w < q; w++) { long long n, ans = 0; cin >> n; string s; cin >> s; for (int i = 0; i < n; i++) { if (s[i] == '0') { if (T_d[t] == 0) d_cl.push_back(t); T_d[t]++; t++; ans += T_d[t]; } else { if (T_d[t] == 0) d_cl.push_back(t); T_d[t]++; t += (1 - z_int(s[i])); ans += T_d[t]; } } cout << ans << endl; for (int i = 0; i < d_cl.size(); i++) T_d[d_cl[i]] = 0; d_cl.clear(); t = 4550000; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); int t; cin >> t; while (t--) { int n; cin >> n; string st; cin >> st; vector<long long> v(n); map<long long, long long> m; for (int i = 0; i < n; i++) { v[i] = (st[i] - '0') - 1; } long long cu = 0, ans = 0; m[0] = 1; for (int i = 0; i < n; i++) { cu += v[i]; ans += m[cu]; m[cu]++; } cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; void solve() { int n; string s; long long ans = 0; cin >> n >> s; map<int, int> m; m[0] = 1; int l = 0; int sum = 0; for (char c : s) { int v = c - '0'; l++; sum += v; ans += m[sum - l]++; } cout << ans << endl; } int main() { int t; ios::sync_with_stdio(0); cin.tie(0); cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; void solve() { int n, r = 0; char x; map<int, int> s; cin >> n; long long int ans = 0; s[0] = 1; for (int i = 0; i < n; ++i) { cin >> x; r += (x - '0' - 1); ans += (1ll * s[r]); ++s[r]; } cout << ans << "\n"; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> long long mini(long long a, long long b) { if (a < b) return a; return b; } long long maxi(long long a, long long b) { if (a > b) return a; return b; } using namespace std; void solve() { int temp; long long n; cin >> n; string s; cin >> s; vector<long long> a; for (long long i = 0; i < s.size(); i++) { string t; t += s[i]; int k = stoi(t); a.push_back(k); } vector<long long> prefix(n + 1); for (long long i = 0; i < n; i++) { prefix[i + 1] = prefix[i] + a[i]; } map<long long, long long> counter; for (long long i = 0; i <= n; i++) { prefix[i] -= i; counter[prefix[i]]++; } long long ans = 0; for (auto it = counter.begin(); it != counter.end(); it++) { ans += (it->second) * (it->second - 1) / 2; } cout << ans; cout << "\n"; ; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(0); long long int t = 1; cin >> t; while (t--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; const int N = 100010, M = 200010; const int mod = 1e9 + 7; bool cmp(int x, int y) { return x > y; } long long a[200010]; long long ans[N]; unordered_map<long long, long long> mp; int main() { int t; scanf("%d", &t); while (t--) { int n; scanf("%d", &n); string st; cin >> st; for (int i = 0; i < n; i++) { a[i] = st[i] - '0' - 1; } for (int i = 1; i <= n; i++) { ans[i] = ans[i - 1] + a[i - 1]; } for (int i = 1; i <= n; i++) { mp[ans[i]]++; } long long res = 0; for (auto x : mp) { long long y = x.second; if (!x.first) { res += y + (y - 1) * y / 2; } else { res += y * (y - 1) / 2; } } printf("%lld\n", res); mp.clear(); } return 0; }
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932384626433; const long long mod = 1e9 + 7; const long long inf = 1e18; long long god(vector<long long> arr, long long n) { unordered_map<int, int> mp; long long res = 0; long long currsum = 0; for (long long i = 0; i < n; i++) { currsum += arr[i]; if (currsum == 0) res++; if (mp.find(currsum) != mp.end()) res += (mp[currsum]); mp[currsum]++; } return res; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; cin >> t; while (t--) { long long n; cin >> n; string s; cin >> s; vector<long long> a(n); for (long long i = 0; i < n; i++) { a[i] = s[i] - '0'; a[i] = a[i] - 1; } cout << god(a, n) << "\n"; } }
#include <bits/stdc++.h> using namespace std; long long c[9]; long long solve(long long x, long long y) { if (x == 0 && y == 0) return 0; long long r = abs(x); x /= r; y /= r; long long ans; if (x == 1 && y == 1) ans = c[1]; else if (x == 1 && y == -1) ans = c[8]; else if (x == -1 && y == 1) ans = c[7]; else ans = c[4]; return r * ans; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); long long t; cin >> t; while (t--) { long long x, y; cin >> x >> y; for (long long i = 1; i < 7; i++) cin >> c[i]; c[1] = min(c[1], c[2] + c[6]); c[2] = min(c[2], c[1] + c[3]); c[3] = min(c[3], c[2] + c[4]); c[4] = min(c[4], c[3] + c[5]); c[5] = min(c[5], c[4] + c[6]); c[6] = min(c[6], c[1] + c[5]); c[7] = c[2] + c[3]; c[8] = c[5] + c[6]; long long ans = 0; if (abs(x) == abs(y)) ans = solve(x, y); else if (abs(x) < abs(y)) { if (y > 0) { ans += c[2] * (abs(y) - abs(x)); y -= abs(y) - abs(x); } else { ans += c[5] * (abs(y) - abs(x)); y += abs(y) - abs(x); } ans += solve(x, y); } else { if (x > 0) { ans += c[6] * (abs(x) - abs(y)); x -= abs(x) - abs(y); } else { ans += c[3] * (abs(x) - abs(y)); x += abs(x) - abs(y); } ans += solve(x, y); } cout << ans << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int t; long long x, y; long long c[10], best[10]; long long ans; int main() { ios_base::sync_with_stdio(0); cin.tie(NULL); cin >> t; while (t--) { cin >> x >> y; for (int i = 1; i <= 6; i++) { cin >> c[i]; } for (int i = 2; i <= 5; i++) { best[i] = min(c[i], c[i - 1] + c[i + 1]); } best[1] = min(c[1], c[2] + c[6]); best[6] = min(c[6], c[1] + c[5]); if (x == 0 && y == 0) { cout << 0 << "\n"; continue; } if (x * y >= 0) { if (x < 0 && y < 0) { ans += max(x, y) * (-1) * best[4]; long long m = (-1) * max(x, y); x += m; y += m; } if (x > 0 && y > 0) { ans += min(x, y) * best[1]; long long m = min(x, y); x -= m; y -= m; } if (x == 0 && y > 0) { ans += y * best[2]; y = 0; } if (x == 0 && y < 0) { ans += (-y) * best[5]; y = 0; } if (x < 0 && y == 0) { ans += (-x) * best[3]; x = 0; } if (x > 0 && y == 0) { ans += x * best[6]; x = 0; } } else { if (x > 0 && y < 0) { ans += best[6] * x; ans += best[5] * (-y); x = 0; y = 0; } if (x < 0 && y > 0) { ans += (-x) * best[3]; ans += y * best[2]; x = 0; y = 0; } } cout << ans << "\n"; ans = 0; } }
#include <bits/stdc++.h> using namespace std; template <class T> inline void read(T &res) { char c; T flag = 1; while ((c = getchar()) < '0' || c > '9') if (c == '-') flag = -1; res = c - '0'; while ((c = getchar()) >= '0' && c <= '9') res = res * 10 + c - '0'; res *= flag; } const int maxn = 3e5 + 10; const int mod = 1e9 + 7; int t, n; long long x, y; long long c[15]; void init() { c[1] = min(c[1], c[6] + c[2]); c[2] = min(c[2], c[1] + c[3]); c[3] = min(c[3], c[2] + c[4]); c[4] = min(c[4], c[3] + c[5]); c[5] = min(c[5], c[4] + c[6]); c[6] = min(c[6], c[5] + c[1]); } int main() { scanf("%d", &t); while (t--) { scanf("%lld%lld", &x, &y); for (int i = 1; i <= 6; i++) { scanf("%lld", &c[i]); } init(); long long res = 1e19; if (x >= 0 && y <= 0) { y = abs(y); res = min(res, c[6] * x + y * c[5]); } else if (x <= 0 && y >= 0) { x = abs(x); res = min(res, c[3] * x + y * c[2]); } else if (x >= 0 && y >= 0) { res = min(res, min(x, y) * c[1] + (y - min(x, y)) * c[2] + (x - min(x, y)) * c[6]); } else { x = abs(x); y = abs(y); res = min(res, min(x, y) * c[4] + (y - min(x, y)) * c[5] + (x - min(x, y)) * c[3]); } printf("%lld\n", res); } return 0; }
#include <bits/stdc++.h> using namespace std; int read() { int x, f = 1; char ch; while (ch = getchar(), ch < '0' || ch > '9') if (ch == '-') f = -1; x = ch - '0'; while (ch = getchar(), ch >= '0' && ch <= '9') x = x * 10 + ch - 48; return x * f; } unsigned long long a, b, c, d, e, f, s; int main() { int t, p, q; t = read(); while (t--) { s = 0; p = read(); q = read(); scanf("%lld %lld %lld %lld %lld %lld", &a, &b, &c, &d, &e, &f); a = min(a, b + f); b = min(b, a + c); c = min(c, b + d); d = min(d, e + c); e = min(e, d + f); f = min(f, a + e); if (p >= 0 && q >= 0) { if (p > q) s = q * a + (p - q) * f; else s = p * a + (q - p) * b; } else if (p <= 0 && q <= 0) { if (p > q) s = (-p) * d + (p - q) * e; else s = (-q) * d + (q - p) * c; } else if (p >= 0 && q <= 0) s = (-q) * e + p * f; else s = q * b + c * (-p); printf("%lld\n", s); } return 0; }
#include <bits/stdc++.h> using namespace std; const int maxn = 1e5 + 50; long long T, x, y, c[10]; int main() { cin >> T; while (T--) { cin >> x >> y; for (long long i = 1; i <= 6; i++) cin >> c[i]; if (x < 0 && y < 0) { long long tot = min(abs(x), abs(y)); long long tot2 = max(abs(x), abs(y)); long long res1 = 0, res2 = 0, res3 = 0; res1 = abs(x) * c[3] + abs(y) * c[5]; x += tot; y += tot; res2 = tot * c[4] + abs(x) * c[3] + abs(y) * c[5]; x -= tot; y -= tot; x += tot2; y += tot2; res3 = tot2 * c[4] + abs(x) * c[6] + abs(y) * c[2]; cout << min(res1, min(res2, res3)) << endl; } else if (x >= 0 && y < 0) { long long tot = min(abs(x), abs(y)); long long res1 = 0, res2 = 0, res3 = 0; res1 = c[6] * x + c[5] * abs(y); res2 = c[1] * x + (abs(y) + x) * c[5]; res3 = c[4] * abs(y) + (abs(y) + x) * c[6]; cout << min(res1, min(res2, res3)) << endl; } else if (x < 0 && y >= 0) { long long tot = min(abs(x), abs(y)); long long res1 = 0, res2 = 0, res3 = 0; res1 = c[3] * abs(x) + c[2] * y; res2 = c[4] * abs(x) + (abs(x) + y) * c[2]; res3 = c[1] * y + (abs(x) + y) * c[3]; cout << min(res1, min(res2, res3)) << endl; } else if (x >= 0 && y >= 0) { long long tot = min(abs(x), abs(y)); long long tot2 = max(abs(x), abs(y)); long long res1 = 0, res2 = 0, res3 = 0; res1 = x * c[6] + y * c[2]; x -= tot; y -= tot; res2 = tot * c[1] + x * c[6] + y * c[2]; x += tot; y += tot; x -= tot2; y -= tot2; res3 = tot2 * c[1] + abs(x) * c[3] + abs(y) * c[5]; cout << min(res1, min(res2, res3)) << endl; } } return 0; }
#include <bits/stdc++.h> using namespace std; const long double pi = 3.1415926535897932384626433; const long long inf = 1e18; const long long mod = 1e9 + 7; void solve() { long long x, y; cin >> x >> y; vector<long long> a(6); for (long long i = 0; i < 6; i++) { cin >> a[i]; } for (long long i = 0; i < 12; i++) { long long p = (i - 1) % 6; long long n = (i + 1) % 6; a[i % 6] = min(a[i % 6], a[p] + a[n]); } long long ans = 0; if (x <= 0 and y >= 0) { ans += (abs(x) * a[2]); ans += (y * a[1]); cout << ans << "\n"; return; } if (x >= 0 and y <= 0) { ans += (x * a[5]); ans += (abs(y) * a[4]); cout << ans << "\n"; return; } if (x < 0 and y < 0) { long long k = min(x, y); long long j = max(x, y); ans += abs(j) * a[3]; if (x < y) ans += abs(k - j) * a[2]; else { ans += abs(k - j) * a[4]; } cout << ans << "\n"; return; } long long k = min(x, y); long long j = max(x, y); ans += (k * a[0]); if (x > y) { ans += (j - k) * a[5]; } else { ans += (j - k) * a[1]; } cout << ans << "\n"; return; } int main() { ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); ; long long t; t = 1; cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) { long long first, second, c[7]; cin >> first >> second; for (long long i = 1; i <= 6; i++) cin >> c[i]; for (long long i = 1; i <= 6; i++) for (long long j = 1; j <= 6; j++) c[j] = min(c[j], c[j % 6 + 1] + c[(j + 4) % 6 + 1]); long long ans = 0; if (first > 0) { long long a = min(first, max(0ll, second)), b = first - a; assert(b >= 0); ans += c[1] * a + c[6] * b; first -= a + b; second -= a; } if (first < 0) { long long a = min(-first, max(0ll, -second)), b = -first - a; assert(b >= 0); ans += c[4] * a + c[3] * b; first += a + b; second += a; } assert(first == 0); if (second > 0) ans += c[2] * second; else ans += c[5] * (-second); cout << ans << "\n"; } }
#include <bits/stdc++.h> using namespace std; using ll = long long; using vi = vector<int>; using vll = vector<ll>; using vc = vector<char>; using pii = pair<int, int>; using vpii = vector<pii>; int t; int main() { cin >> t; while (t--) { ll x, y; ll c1, c2, c3, c4, c5, c6; cin >> x >> y >> c1 >> c2 >> c3 >> c4 >> c5 >> c6; ll r = min(c6, c5 + c1); ll b = min(c5, c4 + c6); ll bl = min(c4, c5 + c3); ll l = min(c3, c4 + c2); ll t = min(c2, c1 + c3); ll tr = min(c1, c6 + c2); if (x > 0) { if (y > 0) { if (x > y) { cout << y * tr + (x - y) * r; } else { cout << x * tr + (y - x) * t; } } else if (y < 0) { cout << x * r + -y * b; } else { cout << x * r; } } else if (x < 0) { if (y < 0) { if (x < y) { cout << -y * bl + -(x - y) * l; } else { cout << -x * bl + -(y - x) * b; } } else if (y > 0) { cout << -x * l + y * t; } else { cout << -x * l; } } else { if (y > 0) { cout << y * t; } else { cout << -y * b; } } cout << '\n'; } }
#include <bits/stdc++.h> using namespace std; void solve() { long long x, y; cin >> x >> y; long long c1, c2, c3, c4, c5, c6; cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6; long long n1, n2, n3, n4, n5, n6; n1 = min(c1, c2 + c6); n2 = min(c2, c1 + c3); n3 = min(c3, c2 + c4); n4 = min(c4, c3 + c5); n5 = min(c5, c4 + c6); n6 = min(c6, c1 + c5); long long ans = 0; if (x >= 0 && y >= 0) { long long d = min(x, y); ans += d * n1; x -= d; y -= d; if (x > 0) ans += n6 * x; if (y > 0) ans += n2 * y; } else if (x <= 0 && y >= 0) { x = -x; ans += y * n2; ans += x * n3; } else if (x <= 0 && y <= 0) { x = -x; y = -y; long long d = min(x, y); ans += d * n4; x -= d; y -= d; if (x > 0) ans += x * n3; if (y > 0) ans += y * n5; } else if (x >= 0 && y <= 0) { y = -y; ans += x * n6; ans += y * n5; } cout << ans << "\n"; } int main() { int t; cin >> t; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; long long mx[] = {+1, 0, -1, -1, 0, +1}; long long my[] = {+1, +1, 0, -1, -1, 0}; long long c[6]; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int t; cin >> t; while (t--) { long long x, y; cin >> x >> y; for (int i = 0; i < 6; ++i) cin >> c[i]; long long ans = LLONG_MAX; for (int i = 0; i < 6; ++i) { for (int j = i + 1; j < 6; ++j) { if (j - i == 3) continue; long long det = mx[i] * my[j] - my[i] * mx[j]; if (det == 0) continue; long long detA = x * my[j] - y * mx[j], detB = mx[i] * y - my[i] * x; long long A = detA / det, B = detB / det; if (A >= 0 && B >= 0 && (mx[i] * A + mx[j] * B == x) && (my[i] * A + my[j] * B == y)) ans = min(ans, A * c[i] + B * c[j]); } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; class dsu { long long *parent; long long *size; public: long long distinct; dsu() {} dsu(int n) : distinct(n) { parent = new long long[n + 1]; size = new long long[n + 1]; for (int i = 1; i <= n; i++) { parent[i] = i; size[i] = 1; } } long long find_parent(int i) { if (parent[parent[i]] != parent[i]) parent[i] = find_parent(parent[i]); return parent[i]; } bool find(int a, int b) { if (find_parent(a) == find_parent(b)) { return true; } return false; } void uni(int a, int b) { long long parent_a = find_parent(a), parent_b = find_parent(b); if (parent_a == parent_b) return; if (size[parent_a] >= size[parent_b]) { distinct--; size[parent_a] += size[parent_b]; parent[parent_b] = parent_a; } else { distinct--; size[parent_b] += size[parent_a]; parent[parent_a] = parent_b; } } long long siz(int a) { return size[find_parent(a)]; } ~dsu() { delete[] size; delete[] parent; } }; long long binpow(long long a, long long b) { if (b == 0) return 1; long long res = binpow(a, b / 2) % 1000000007; if (b % 2) return (((res * res) % 1000000007) * a) % 1000000007; else return (res * res) % 1000000007; } struct prep { vector<long long> fact; vector<long long> inv; long long m; prep() {} explicit prep(int n) : m(1000000007) { long long mod = 1000000007; int num = n; fact.resize(n + 31); inv.resize(n + 31); fact[0] = 1; inv[0] = 1; for (int i = 1; i <= num + 30; i++) { fact[i] = (fact[i - 1] * i) % mod; inv[i] = binpow(fact[i], mod - 2) % 1000000007; } } long long ncr(long long n, long long r) { if (r > n || r < 0) { return 0; } long long p = ((fact[n] * inv[r]) % m); p = (p * (inv[n - r]) % m); return p; } long long fac(long long num) { return fact[num]; } }; bool isPrime(long long n) { if (n == 1) return false; if (n == 2) return true; for (long long i = 2; i * i <= n; i++) if (n % i == 0) return false; return true; } inline long long min(long long a, long long b) { if (a > b) return b; return a; } inline long long max(long long a, long long b) { if (a > b) return a; return b; } inline long long dif(long long a, long long b) { if (a > b) return a - b; return b - a; } long long gcd(long long a, long long b) { if (b == 0) return a; return gcd(b, a % b); } long long lcm(long long a, long long b) { long long k = gcd(a, b); return (a * b) / k; } void read(vector<long long> &arr, int n) { if (arr.size() != n) { arr.resize(n); } for (int i = 0; i < n; i++) cin >> arr[i]; } void sieveOferatosthenes(long long MAX) { vector<long long> prime; vector<long long> lps(MAX + 1); vector<bool> pri(MAX + 1, true); pri[1] = false; lps[1] = 1; lps[2] = 2; for (long long p = 2; p * p <= MAX; p++) { if (pri[p]) { lps[p] = p; for (long long i = p * p; i <= MAX; i += p) { pri[i] = false; if (lps[i] == 0) { lps[i] = p; } } } } for (long long i = 1; i <= MAX; i++) { if (pri[i]) { prime.push_back(i); lps[i] = i; } } } bool valid(pair<long long, long long> &curr) { return curr.first >= 0 && curr.second >= 0 && curr.first < 3 && curr.second < 3; } void solve() { long long x, y; cin >> x >> y; vector<long long> cost; for (long long i = 0; i < 6; i++) { long long k; cin >> k; cost.push_back(k); } vector<vector<long long>> moves; moves.push_back({1, 1}); moves.push_back({0, 1}); moves.push_back({-1, 0}); moves.push_back({-1, -1}); moves.push_back({0, -1}); moves.push_back({1, 0}); long long dp[3][3]; for (long long i = 0; i < 3; i++) { for (long long j = 0; j < 3; j++) { dp[i][j] = 1000000000000000000; } } dp[1][1] = 0; set<pair<long long, pair<long long, long long>>> s; for (long long i = 0; i < 3; i++) { for (long long j = 0; j < 3; j++) { s.insert({dp[i][j], {i, j}}); } } while (not s.empty()) { auto it = *s.begin(); s.erase(s.begin()); for (long long i = 0; i < 6; i++) { pair<long long, long long> curr = {it.second.first + moves[i][0], it.second.second + moves[i][1]}; if (valid(curr)) { if (dp[curr.first][curr.second] > dp[it.second.first][it.second.second] + cost[i]) { s.erase(make_pair(dp[curr.first][curr.second], curr)); s.insert( make_pair(dp[it.second.first][it.second.second] + cost[i], curr)); dp[curr.first][curr.second] = dp[it.second.first][it.second.second] + cost[i]; } } } } if (x == 0 && y == 0) { cout << 0 << endl; return; } long long mi = min(abs(x), abs(y)); long long ma = max(abs(x), abs(y)); long long dif = ma - mi; if (x >= 0 && y >= 0) { long long res = mi * dp[2][2]; if (ma == x) { res += (x - y) * dp[2][1]; } else { res += (y - x) * dp[1][2]; } cout << res << endl; } else if (x >= 0 && y <= 0) { long long res = mi * dp[2][0]; if (abs(x) == ma) { res += dif * dp[2][1]; } else { res += dif * dp[1][0]; } cout << res << endl; } else if (x <= 0 && y >= 0) { long long res = mi * dp[0][2]; if (abs(x) == ma) { res += dif * dp[0][1]; } else { res += dp[1][2] * dif; } cout << res << endl; } else { long long res = mi * dp[0][0]; if (abs(x) == ma) { res += dif * dp[0][1]; } else { res += dif * dp[1][0]; } cout << res << endl; } } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout << fixed << showpoint; cout << setprecision(6); int t; t = 1; cin >> t; while (t--) { solve(); } }
#include <bits/stdc++.h> using namespace std; int main() { int t; long long x, y, ans, c[6]; scanf("%d", &t); for (int i = 0; i < t; i++) { scanf("%lld %lld", &x, &y); for (int j = 0; j < 6; j++) scanf("%lld", &c[j]); if (x == 0 && y == 0) printf("0\n"); else { ans = LLONG_MAX; if (x >= 0 && x <= y) ans = min(ans, c[0] * x + c[1] * (y - x)); if (x <= 0 && x >= y) ans = min(ans, c[3] * (-x) + c[4] * (x - y)); if (x <= 0 && y >= 0) ans = min(ans, c[1] * y + c[2] * (-x)); if (x >= 0 && y <= 0) ans = min(ans, c[4] * (-y) + c[5] * x); if (y <= 0 && x <= y) ans = min(ans, c[3] * (-y) + c[2] * (y - x)); if (y >= 0 && x >= y) ans = min(ans, c[0] * y + c[5] * (x - y)); if (y >= 0 && x <= y) ans = min(ans, c[0] * y + c[2] * (y - x)); if (y <= 0 && x >= y) ans = min(ans, c[3] * (-y) + c[5] * (x - y)); if (x <= 0 && x <= y) ans = min(ans, c[3] * (-x) + c[1] * (y - x)); if (x >= 0 && x >= y) ans = min(ans, c[0] * x + c[4] * (x - y)); if (y <= 0 && x <= 0) ans = min(ans, c[2] * (-x) + c[4] * (-y)); if (y >= 0 && x >= 0) ans = min(ans, c[5] * x + c[1] * y); printf("%lld\n", ans); } } return 0; }
#include <bits/stdc++.h> #pragma GCC target( \ "sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native") #pragma GCC optimize("O3,no-stack-protector") using namespace std; int64_t GetCoeff(int i, int diff, const vector<int64_t>& c) { if (i == 0) { return (diff > 0 ? c[6] : c[3]); } else if (i == 1) { return (diff > 0 ? c[2] : c[5]); } else if (i == 2) { return (diff > 0 ? c[1] : c[4]); } throw runtime_error("invalid i in GetCoeff"); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); int tc; cin >> tc; while (tc--) { int64_t x0, y0; cin >> x0 >> y0; vector<int64_t> c(7); for (int i = 1; i <= 6; ++i) { cin >> c[i]; } vector<int64_t> pts = {0, x0, y0}; int64_t best = numeric_limits<int64_t>::max(); for (int64_t z : pts) { vector<int64_t> pt = {x0 - z, y0 - z, z}; int64_t cur = 0; for (int i = 0; i < 3; ++i) { cur += GetCoeff(i, pt[i], c) * abs(pt[i]); } best = min(best, cur); } cout << best << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t, n, x, y, a1, a2, x1, x2, ans, y1, y2, i, val, j; cin >> t; vector<pair<long long, long long>> v = {{1, 1}, {0, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, 0}, {0, 0}}; while (t--) { cin >> x >> y; vector<long long> c; for (i = 1; i <= 6; ++i) { cin >> val; c.push_back(val); } c.push_back(0); ans = 9223372036854775807; for (i = 0; i < 7; ++i) { for (j = 0; j < 7; ++j) { x1 = v[i].first, y1 = v[i].second; x2 = v[j].first, y2 = v[j].second; long long num1 = x * y2 - y * x2; long long val1 = x1 * y2 - y1 * x2; long long num2 = x * y1 - y * x1; long long val2 = x2 * y1 - y2 * x1; if (val1 == 0 || val2 == 0 || (num1 % val1) || (num2 % val2) || (num1 / val1) < 0 || (num2 / val2) < 0) continue; else { a1 = num1 / val1; a2 = num2 / val2; ans = min(ans, a1 * c[i] + a2 * c[j]); } } } cout << ans << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; namespace { long long x, y, c[7]; unsigned long long u, d, l, r, ru, ld; void Solve() { cin >> x >> y; for (int i = 1; i <= 6; i++) cin >> c[i]; r = min(c[6], c[1] + c[5]); l = min(c[3], c[2] + c[4]); u = min(c[2], c[1] + c[3]); d = min(c[5], c[4] + c[6]); ru = min(c[1], c[6] + c[2]); ld = min(c[4], c[5] + c[3]); if (x <= 0 && y <= 0) { x = -x, y = -y; if (x > y) cout << y * ld + (x - y) * l << endl; else cout << x * ld + (y - x) * d << endl; } else if (x >= 0 && y >= 0) { if (x > y) cout << y * ru + (x - y) * r << endl; else cout << x * ru + (y - x) * u << endl; } else if (x <= 0 && y >= 0) cout << -x * l + y * u << endl; else if (x >= 0 && y <= 0) cout << x * r + -y * d << endl; } } // namespace int main() { ios::sync_with_stdio(false), cin.tie(nullptr); int t(1); cin >> t; while (t--) Solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long t; int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cin >> t; while (t--) { long long x, y; cin >> x >> y; long long cc1, cc2, cc3, cc4, cc5, cc6; cin >> cc1 >> cc2 >> cc3 >> cc4 >> cc5 >> cc6; long long pr, dr; if (0 > x) pr = cc3; else pr = cc6; if (0 > y) dr = cc5; else dr = cc2; long long s1 = abs(x) * pr + abs(y) * dr; if (0 > x) pr = cc4; else pr = cc1; if (y > x) dr = cc2; else dr = cc5; long long s2 = abs(x) * pr + abs(y - x) * dr; if (x > y) pr = cc6; else pr = cc3; if (0 > y) dr = cc4; else dr = cc1; long long s3 = abs(x - y) * pr + abs(y) * dr; cout << min(min(s1, s2), s3) << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; int main() { long long t; scanf("%lld", &t); while (t--) { long long x, y; scanf("%lld %lld", &x, &y); long long adjx[] = {1, 0, -1, -1, 0, 1}; long long adjy[] = {1, 1, 0, -1, -1, 0}; long long c[6]; for (long long i = 0; i < 6; i++) { scanf("%lld", &c[i]); } long long ans = 9e18; for (long long i = 0; i < 6; i++) { for (long long j = i + 1; j < 6; j++) { if (i + 3 == j) { continue; } bool swapped = false; if (adjx[i] == adjy[i]) { swap(i, j); swapped = true; } if (adjx[i] == 0) { long long costl = x / adjx[j]; long long ly = costl * adjy[j]; long long costr = (y - ly) / adjy[i]; if (costl < 0 || costr < 0) { if (swapped) { swap(i, j); } continue; } ans = min(ans, c[j] * costl + c[i] * costr); } else { long long costl = y / adjy[j]; long long lx = costl * adjx[j]; long long costr = (x - lx) / adjx[i]; if (costl < 0 || costr < 0) { if (swapped) { swap(i, j); } continue; } ans = min(ans, c[j] * costl + c[i] * costr); } if (swapped) { swap(i, j); } } } for (long long i = 0; i < 6; i++) { if (adjx[i] == 0) { if (x == 0 && y / adjy[i] >= 0) { ans = min(ans, y / adjy[i] * 1ll * c[i]); } } else if (adjy[i] == 0) { if (y == 0 && x / adjx[i] >= 0) { ans = min(ans, x / adjx[i] * 1ll * c[i]); } } else { if (x / adjx[i] == y / adjy[i] && x / adjx[i] >= 0) { ans = min(ans, x / adjx[i] * 1ll * c[i]); } } } printf("%lld\n", ans); } }
#include <bits/stdc++.h> using namespace std; void solve() { long long x, y; cin >> x >> y; long long c[6]; for (long long& cost : c) cin >> cost; long long cost1 = 0, cost2 = 0, cost3 = 0; cost1 = x * (x > 0 ? c[5] : -c[2]) + y * (y > 0 ? c[1] : -c[4]); cost2 = x * (x > 0 ? c[0] : -c[3]) + (x - y) * (x - y > 0 ? c[4] : -c[1]); cost3 = (y - x) * (y - x > 0 ? c[2] : -c[5]) + y * (y > 0 ? c[0] : -c[3]); cout << min({cost1, cost2, cost3}); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); int t = 1; cin >> t; while (t--) { solve(); cout << '\n'; } return 0; }
#include <bits/stdc++.h> using namespace std; using namespace std::chrono; template <typename A, typename B> string to_string(pair<A, B> p); template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p); template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p); string to_string(const string& s) { return '"' + s + '"'; } string to_string(const char* s) { return to_string((string)s); } string to_string(bool b) { return (b ? "true" : "false"); } string to_string(vector<bool> v) { bool first = true; string res = "{"; for (int i = 0; i < static_cast<int>(v.size()); i++) { if (!first) { res += ", "; } first = false; res += to_string(v[i]); } res += "}"; return res; } template <size_t N> string to_string(bitset<N> v) { string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; } template <typename A> string to_string(A v) { bool first = true; string res = "{"; for (const auto& x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res; } template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; } template <typename A, typename B, typename C> string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; } template <typename A, typename B, typename C, typename D> string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; } void debug_out() { cout << endl; } template <typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cout << " " << to_string(H); debug_out(T...); } template <typename T> using mxpq = priority_queue<T>; template <typename T> using mnpq = priority_queue<T, vector<T>, greater<T>>; const long long mod = 1e9 + 7; const long long N = 1e6 + 6; const long long maxN = 3e5 + 15; const long long MAX_SIZE = 2e6 + 6; const long long INF = 0x3f3f3f3f3f3f3f3fll; const double PI = 3.14159265359; int dx[4] = {-1, 0, 1, 0}; int dy[4] = {0, 1, 0, -1}; long long powerM(long long x, long long y, long long M = mod) { long long v = 1; x = x % M; while (y > 0) { if (y & 1) v = (v * x) % M; y = y >> 1; x = (x * x) % M; } return v; } long long power(long long x, long long y) { long long v = 1; while (y > 0) { if (y & 1) v = v * x; y = y >> 1; x = x * x; } return v; } int main() { ios_base::sync_with_stdio(false); cin.tie(NULL); int testcases; cin >> testcases; for (long long caseno = 1; caseno <= testcases; caseno++) { long long x, y, c1, c2, c3, c4, c5, c6; cin >> x >> y >> c1 >> c2 >> c3 >> c4 >> c5 >> c6; long long a1 = min(c1, c2 + c6); long long a2 = min(c2, c1 + c3); long long a3 = min(c3, c2 + c4); long long a4 = min(c4, c3 + c5); long long a5 = min(c5, c4 + c6); long long a6 = min(c6, c1 + c5); long long cost = 0; long long al = abs(x - y); if (x == 0) { if (y > 0) { cost = a2 * y; } else { cost = -a5 * y; } } else if (y == 0) { if (x > 0) { cost = x * a6; } else { cost = -x * a3; } } else if (x >= 0 && y >= x) { cost = x * a1 + (y - x) * a2; } else if (x > 0 && y < x && y >= 0) { cost = y * a1 + (x - y) * a6; } else if (x >= 0 && y < 0) { cost = abs(y) * a5 + x * a6; } else if (x < 0 && y <= x) { cost = abs(x) * a4 + abs(x - y) * a5; } else if (x < 0 && y > x && y < 0) { cost = abs(y) * a4 + abs(x - y) * a3; } else if (x < 0 && y >= 0) { cost = abs(x) * a3 + y * a2; } cout << cost << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 2e3 + 1; long long x[6] = {1, 0, -1, -1, 0, 1}; long long y[6] = {0, -1, -1, 0, 1, 1}; long long c[6]; long long cc[6]; long long tx, ty; signed main() { ios::sync_with_stdio(false); long long T; cin >> T; while (T--) { cin >> tx >> ty; cin >> c[5] >> c[4] >> c[3] >> c[2] >> c[1] >> c[0]; for (long long cs = 0; cs < 1000; cs++) { for (long long i = 0; i < 6; i++) { cc[(i + 1) % 6] = min(c[(i + 1) % 6], c[i] + c[(i + 2) % 6]); } for (long long i = 0; i < 6; i++) { c[i] = cc[i]; } } if (tx == 0) { if (ty > 0) { cout << c[4] * ty << endl; } else { cout << c[1] * abs(ty) << endl; } } else if (ty == 0) { if (tx > 0) { cout << c[0] * tx << endl; } else { cout << c[3] * abs(tx) << endl; } } else { if (tx > 0 && ty > 0) { if (tx > ty) { cout << ty * c[5] + c[0] * (tx - ty) << endl; } else { cout << tx * c[5] + c[4] * (ty - tx) << endl; } } if (tx > 0 && ty < 0) { cout << tx * c[0] + abs(ty) * c[1] << endl; } if (tx < 0 && ty < 0) { if (tx < ty) { cout << abs(ty) * c[2] + c[3] * (abs(tx) - abs(ty)) << endl; } else { cout << abs(tx) * c[2] + c[1] * (abs(ty) - abs(tx)) << endl; } } if (tx < 0 && ty > 0) { cout << abs(tx) * c[3] + ty * c[4] << endl; } } } return 0; }
#include <bits/stdc++.h> using namespace std; const long long inf = 1e16; long long T, a1, a2, a3, a[101], ans, x, y; signed main() { cin >> T; for (long long i = 1; i <= T; i++) { cin >> x >> y; if (x > 0) { if (y > 0) { a1 = 6, a2 = 2, a3 = 1; } else a1 = 6, a2 = 5, a3 = 7; } else { if (y > 0) { a1 = 3, a2 = 2, a3 = 8; } else a1 = 3, a2 = 5, a3 = 4; } x = abs(x); y = abs(y); for (long long j = 1; j <= 6; j++) { cin >> a[j]; } a[7] = inf; a[8] = inf; a[2] = min(a[1] + a[3], a[2]); a[5] = min(a[4] + a[6], a[5]); a[6] = min(a[1] + a[5], a[6]); a[3] = min(a[2] + a[4], a[3]); a[1] = min(a[6] + a[2], a[1]); a[8] = min(a[2] + a[3], a[8]); a[4] = min(a[3] + a[5], a[4]); a[7] = min(a[6] + a[5], a[7]); ans = min(x, y) * a[a3]; if (x > y) { ans += (x - y) * a[a1]; } else ans += (y - x) * a[a2]; cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; const int MX = 1e5 + 9; long long val[MX], va[MX]; int main() { int T; scanf("%d", &T); while (T--) { long long x, y; scanf("%lld %lld", &x, &y); for (int i = 0; i < 6; i++) scanf("%lld", &val[i]), va[i] = val[i]; for (int i = 0; i < 6; i++) { long long v = (val[(i - 1 + 6) % 6] + val[(i + 1) % 6]); va[i] = min(va[i], v); } long long xx, numx, yy, numy; if (x >= 0) { if (y <= 0) xx = 5, numx = x, yy = 4, numy = -y; else { if (x >= y) xx = 0, numx = y, yy = 5, numy = x - y; else xx = 0, numx = x, yy = 1, numy = y - x; } } else { if (y <= 0) { y = abs(y), x = abs(x); if (x >= y) xx = 2, numx = x - y, yy = 3, numy = y; else xx = 3, numx = x, yy = 4, numy = y - x; } else xx = 2, numx = -x, yy = 1, numy = y; } printf("%lld\n", va[xx] * numx + va[yy] * numy); } return 0; }
#include <bits/stdc++.h> using namespace std; long long v[7]; int wx[7] = {0, 1, 0, -1, -1, 0, 1}, wy[7] = {0, 1, 1, 0, -1, -1, 0}; int main() { int t; long long x, y, t1, t2, t3; cin >> t; while (t--) { cin >> x >> y; for (int i = 1; i <= 6; i++) { cin >> v[i]; } long long ans = 1e19; for (int i = 1; i <= 6; i++) { for (int j = i + 1; j <= 6; j++) { if (j - i == 3) continue; t1 = (wy[j] * x - wx[j] * y) / (wx[i] * wy[j] - wx[j] * wy[i]); t2 = (wy[i] * x - wx[i] * y) / (wx[j] * wy[i] - wx[i] * wy[j]); if (t1 < 0 || t2 < 0) continue; ans = min(ans, t1 * v[i] + t2 * v[j]); } } cout << ans << endl; } return 0; }
#include <bits/stdc++.h> using namespace std; long long powr(long long m, long long n) { if (m == 1) return 1; if (n == 0) return 1; if (n == 1) return m % 1000000007; if (n % 2) { long long te = powr(m, n / 2); return (((te * te) % 1000000007) * (m % 1000000007)) % 1000000007; } else { long long te = powr(m, n / 2); return (te * te) % 1000000007; } } long long c[7]; long long x, y; long long ok() { if (x < 0 && y > 0) { long long mn; long long sum = 0LL; if (c[1] + c[3] > c[2]) sum += (y * c[2]); else sum += (y * (c[1] + c[3])); sum += (((x) > 0 ? (x) : -(x)) * c[3]); mn = sum; sum = 0; if (c[2] + c[4] > c[3]) sum += (((x) > 0 ? (x) : -(x)) * c[3]); else sum += (((x) > 0 ? (x) : -(x)) * (c[2] + c[4])); sum += (y * c[2]); mn = min(mn, sum); return mn; } if (x > 0 && y < 0) { long long mn; long long sum = 0LL; if (c[1] + c[5] > c[6]) sum += (x * c[6]); else sum += (x * (c[1] + c[5])); sum += (((y) > 0 ? (y) : -(y)) * c[5]); mn = sum; sum = 0; if (c[4] + c[6] > c[5]) sum += (((y) > 0 ? (y) : -(y)) * c[5]); else sum += (((y) > 0 ? (y) : -(y)) * (c[4] + c[6])); sum += (x * c[6]); mn = min(mn, sum); return mn; } if (x >= 0 && y >= 0) { long long mn; long long tmp1 = (x * c[6]) + (y * c[2]); mn = min(x, y); long long tmp2 = (mn * c[1]) + ((x - mn) * c[6]) + ((y - mn) * c[2]); long long mx = max(x, y); long long tmp3 = (mx * c[1]) + ((mx - x) * c[3]) + ((mx - y) * c[5]); return min(min(tmp1, tmp2), tmp3); } if (y <= 0 && x <= 0) { long long mn; x = ((x) > 0 ? (x) : -(x)); y = ((y) > 0 ? (y) : -(y)); long long tmp1 = (((x) > 0 ? (x) : -(x)) * c[3]) + (((y) > 0 ? (y) : -(y)) * c[5]); mn = min(((x) > 0 ? (x) : -(x)), ((y) > 0 ? (y) : -(y))); long long tmp2 = (mn * c[4]) + ((((x) > 0 ? (x) : -(x)) - mn) * c[3]) + ((((y) > 0 ? (y) : -(y)) - mn) * c[5]); long long mx = max(x, y); long long tmp3 = (mx * c[4]) + ((mx - x) * c[6]) + ((mx - y) * c[2]); return min(min(tmp1, tmp2), tmp3); } return 0; } int main() { ios::sync_with_stdio(0); cin.tie(0); int tc = 1; cin >> tc; int cs = 0; while (tc--) { cin >> x >> y; for (int i = 1; i <= 6; i++) cin >> c[i]; cout << ok() << endl; } }
#include <bits/stdc++.h> using namespace std; long long best = 1999990000990009999; void solve(long long x, long long y, long long calls, vector<long long>& c) { if (x >= 0 && y >= 0) { long long ans = x * c[6]; ans += (y * c[2]); best = min(best, ans); long long a = x; long long b = y; long long mn = min(x, y); x -= mn; y -= mn; ans = mn * c[1]; if (x > 0) { ans += (x * c[6]); } if (y > 0) { ans += (y * c[2]); } best = min(best, ans); long long mx = max(a, b); ans = mx * c[1]; a -= mx; b -= mx; x = a; y = b; if (x < 0) { ans += (abs(x) * c[3]); } if (y < 0) { ans += (abs(y) * c[5]); } if (x > 0) { ans += (x * c[6]); } if (y > 0) { ans += (y * c[2]); } best = min(best, ans); } else if (x <= 0 && y <= 0) { long long ans = abs(x) * c[3]; ans += (abs(y) * c[5]); best = min(best, ans); long long a = x; long long b = y; long long mx = max(x, y); ans = abs(mx) * c[4]; x -= mx; y -= mx; if (x < 0) { ans += (abs(x) * c[3]); } if (y < 0) { ans += (abs(y) * c[5]); } best = min(ans, best); long long mn = min(a, b); ans = abs(mn) * c[4]; a -= mn; b -= mn; x = a; y = b; if (x < 0) { ans += (abs(x) * c[3]); } if (y < 0) { ans += (abs(y) * c[5]); } if (x > 0) { ans += (x * c[6]); } if (y > 0) { ans += (y * c[2]); } best = min(best, ans); } else if (x >= 0 && y <= 0) { long long ans = x * c[6]; ans += (abs(y) * c[5]); best = min(best, ans); ans = abs(y) * c[4]; ans += ((x + abs(y)) * c[6]); best = min(best, ans); ans = x * c[1]; ans += ((x + abs(y)) * c[5]); best = min(ans, best); } else if (x <= 0 && y >= 0) { long long ans = abs(x) * c[3]; ans += (y * c[2]); best = min(best, ans); ans = abs(x) * c[4]; ans += ((y + abs(x)) * c[2]); best = min(best, ans); ans = y * c[1]; ans += ((y + abs(x)) * c[3]); best = min(best, ans); } } int32_t main() { long long t; cin >> t; while (t--) { best = 1999990000990009999; long long x, y; cin >> x >> y; vector<long long> v(7); for (long long i = 1; i <= 6; i++) { cin >> v[i]; } solve(x, y, 0, v); cout << best << "\n"; } }
#include <bits/stdc++.h> using namespace std; template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; } template <typename T, size_t size> ostream &operator<<(ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; } template <typename A, typename B> ostream &operator<<(ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; } void dbg_out() { cerr << endl; } template <typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << ' ' << H; dbg_out(T...); } void run_case() { int64_t X, Y; array<int64_t, 6> C; cin >> X >> Y; for (auto &c : C) cin >> c; for (int i = 0; i < 6; i++) C[i] = min(C[i], C[(i + 5) % 6] + C[(i + 1) % 6]); auto rotate60 = [&] { int64_t new_X = Y; int64_t new_Y = Y - X; X = new_X; Y = new_Y; rotate(C.begin(), C.begin() + 1, C.end()); }; while (!(X >= 0 && Y >= X)) rotate60(); int64_t right = Y - X; int64_t up_right = X; cout << C[0] * up_right + C[1] * right << '\n'; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); int tests; cin >> tests; while (tests-- > 0) run_case(); }
#include <bits/stdc++.h> using namespace std; void solve() { long long int x, y; cin >> x >> y; vector<int> c(6); for (int i = 0; i < 6; i++) cin >> c[i]; map<pair<int, int>, int> p; p[{1, 1}] = c[0]; p[{0, 1}] = c[1]; p[{-1, 0}] = c[2]; p[{-1, -1}] = c[3]; p[{0, -1}] = c[4]; p[{1, 0}] = c[5]; p[{1, 1}] = min(p[{1, 1}], p[{0, 1}] + p[{1, 0}]); p[{0, 1}] = min(p[{0, 1}], p[{1, 1}] + p[{-1, 0}]); p[{-1, 0}] = min(p[{-1, 0}], p[{-1, -1}] + p[{0, 1}]); p[{-1, -1}] = min(p[{-1, -1}], p[{0, -1}] + p[{-1, 0}]); p[{0, -1}] = min(p[{0, -1}], p[{-1, -1}] + p[{1, 0}]); p[{1, 0}] = min(p[{1, 0}], p[{1, 1}] + p[{0, -1}]); if (x == 0) { cout << abs(y) * p[{0, y > 0 ? 1 : -1}] << endl; } else if (y == 0) { cout << abs(x) * p[{x > 0 ? 1 : -1, 0}] << endl; } else if (x / abs(x) != y / abs(y)) { long long int xx = abs(x) * p[{x / abs(x), 0}]; long long int yy = abs(y) * p[{0, y / abs(y)}]; cout << xx + yy << endl; } else { long long int both = min(abs(x), abs(y)) * p[{x / abs(x), y / abs(y)}]; long long int one; if (abs(x) >= abs(y)) { one = (abs(x) - abs(y)) * p[{x / abs(x), 0}]; } else { one = (abs(y) - abs(x)) * p[{0, y / abs(y)}]; } cout << both + one << endl; } } int main() { int tt; cin >> tt; while (tt--) { solve(); } return 0; }
#include <bits/stdc++.h> using namespace std; long long int n, m; long long int arr[300000]; int main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); ; long long int TESTS, q, a, b, l, r, c, k, p, h, w, x, y, z, xs, ys, t, f; long double d; TESTS = 1; cin >> TESTS; while (TESTS--) { cin >> y >> x; for (long long int i = 0; i <= 5; i++) cin >> arr[i]; long long int ans1, ans2, ans3; a = x - y; b = 0; if (a < 0) ans1 = abs(a) * arr[4]; else ans1 = a * arr[1]; if (y > 0) ans1 += y * arr[0]; else ans1 += abs(y) * arr[3]; if (x < 0) ans2 = abs(x) * arr[4]; else ans2 = x * arr[1]; if (y > 0) ans2 += y * arr[5]; else ans2 += abs(y) * arr[2]; b = y - x; if (b < 0) ans3 = abs(b) * arr[2]; else ans3 = b * arr[5]; if (x > 0) ans3 += x * arr[0]; else ans3 += abs(x) * arr[3]; cout << min(ans1, min(ans2, ans3)) << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; long long infy = 1e30; void solve() { long long x, y, c[8], ax, ay; cin >> x >> y; ax = abs(x); ay = abs(y); for (long long i = 1; i <= 6; i++) cin >> c[i]; c[7] = c[1]; c[0] = c[6]; for (long long i = 1; i <= 6; i++) c[i] = min(c[i], c[i - 1] + c[i + 1]); c[7] = c[1]; c[0] = c[6]; long long xy[7][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, 0}}, ans = infy; for (long long i = 1; i <= 6; i++) { long long x1, x2, y1, y2, temp; x1 = xy[i - 1][0]; y1 = xy[i - 1][1]; x2 = xy[i][0]; y2 = xy[i][1]; long long z1 = ((x1 * y2) - (x2 * y1)); long long z2 = ((x * y2) - (y * x2)); long long z3 = ((x2 * y1) - (y2 * x1)); long long z4 = ((x * y1) - (y * x1)); if (z1 == 0 || z3 == 0) continue; long long n1 = (z2 / z1); long long n2 = (z4 / z3); temp = ((n1 * c[i - 1]) + (n2 * c[i])); if (n1 >= 0 && n2 >= 0) { ans = min(ans, temp); } } cout << ans << endl; } int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long t; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; using ll = long long; using db = double; using ui = unsigned int; const ll amn = 1e6 + 5, mod = 1e9 + 7, an = 1e3 + 5; ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; } ll n, m; ll c[10]; ll cal(ll x, ll a, ll b) { if (x >= 0) return x * a; else return -x * b; } int main() { int T = 1; cin >> T; while (T--) { ll x, y; cin >> x >> y; for (ll i = 1; i <= 6; i++) cin >> c[i]; ll ans = 9e18; ans = min(ans, cal(x, c[6], c[3]) + cal(y, c[2], c[5])); ans = min(ans, cal(x - y, c[6], c[3]) + cal(y, c[1], c[4])); ans = min(ans, cal(x, c[1], c[4]) + cal(y - x, c[2], c[5])); cout << ans << endl; } }
#include <bits/stdc++.h> using namespace std; const int N = 10; const int dx[] = {1, 1, 0, -1, -1, 0}; const int dy[] = {0, 1, 1, 0, -1, -1}; long long cost[6], dist[25][25]; bool vis[25][25]; void dkst() { memset(dist, 0x3f, sizeof(dist)); memset(vis, false, sizeof(vis)); set<pair<long long, array<int, 2> > > st; st.insert({0, {0, 0}}); dist[N][N] = 0; while (!st.empty()) { int x = (st.begin()->second)[0]; int y = (st.begin()->second)[1]; st.erase(*st.begin()); if (vis[x + N][y + N]) continue; vis[x + N][y + N] = true; for (int i = 0; i < 6; i++) { int nx = x + dx[i]; int ny = y + dy[i]; if (abs(nx) > N || abs(ny) > N) continue; if (dist[nx + N][ny + N] > dist[x + N][y + N] + cost[i]) { dist[nx + N][ny + N] = dist[x + N][y + N] + cost[i]; st.insert({dist[nx + N][ny + N], {nx, ny}}); } } } for (int i = 0; i < 6; i++) { cost[i] = dist[dx[i] + N][dy[i] + N]; } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); cout << setprecision(32); int t; cin >> t; while (t--) { long long x, y; cin >> y >> x; for (int i : {1, 0, 5, 4, 3, 2}) { cin >> cost[i]; } dkst(); if (x >= 0 && y >= 0) { long long d = min(x, y); long long ans = d * cost[1] + max(x - d, 0LL) * cost[0] + max(y - d, 0LL) * cost[2]; cout << ans << '\n'; } else if (x <= 0 && y <= 0) { x = -x; y = -y; long long d = min(x, y); long long ans = d * cost[4] + max(x - d, 0LL) * cost[3] + max(y - d, 0LL) * cost[5]; cout << ans << '\n'; } else { long long ans = 0; if (x > 0) ans += x * cost[0]; else ans += (-x) * cost[3]; if (y > 0) ans += y * cost[2]; else ans += (-y) * cost[5]; cout << ans << '\n'; } } return 0; }
#include <bits/stdc++.h> #pragma GCC optimize("-Ofast") std::ifstream ___in("input.in"); using namespace std; using ll = long long; static const bool tc = 1; static const ll mod = LLONG_MAX - 24; void solve() { ll x, y, c[7]; cin >> x >> y; for (int i = 1; i <= 6; ++i) cin >> c[i]; vector<ll> plusX, plusY, minX, minY, minXY, plusXY; plusX.push_back(c[6]); plusX.push_back(c[1] + c[5]); plusY.push_back(c[2]); plusY.push_back(c[1] + c[3]); minX.push_back(c[3]); minX.push_back(c[4] + c[2]); minY.push_back(c[5]); minY.push_back(c[4] + c[6]); plusXY.push_back(c[1]); plusXY.push_back(c[6] + c[2]); minXY.push_back(c[4]); minXY.push_back(c[3] + c[5]); sort(plusX.begin(), plusX.end()); sort(plusY.begin(), plusY.end()); sort(minX.begin(), minX.end()); sort(minY.begin(), minY.end()); sort(minXY.begin(), minXY.end()); sort(plusXY.begin(), plusXY.end()); if (x == 0 && y == 0) { cout << 0 << "\n"; return; } if (x == 0) { if (y > 0) cout << plusY[0] * y << "\n"; else cout << minY[0] * (-y) << "\n"; return; } if (y == 0) { if (x > 0) cout << plusX[0] * x << "\n"; else cout << minX[0] * (-x) << "\n"; return; } ll ans = mod; if (x > 0 && y > 0) { ans = min(ans, min(min(x, y) * plusXY[0] + (x - min(x, y)) * plusX[0] + (y - min(x, y)) * plusY[0], x * plusX[0] + y * plusY[0])); } if (x < 0 && y < 0) { ans = min(ans, min(-max(x, y) * minXY[0] - (x - max(x, y)) * minX[0] - (y - max(x, y)) * minY[0], -x * minX[0] - y * minY[0])); } if (x < 0 && y > 0) { ans = min(ans, -x * minX[0] + y * plusY[0]); } if (x > 0 && y < 0) { ans = min(ans, x * plusX[0] - y * minY[0]); } cout << ans << "\n"; } int main() { srand(time(0)); int T = 1; if (tc) cin >> T; for (int i = 1; i <= T; ++i) { solve(); } return 0; }
#include <bits/stdc++.h> const int bufSize = 1e6; inline char nc() { return getchar(); static char buf[bufSize], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, bufSize, stdin), p1 == p2) ? EOF : *p1++; } template <typename T> inline T read(T &r) { static char c; static long long flag; flag = 1, r = 0; for (c = nc(); !isdigit(c); c = nc()) if (c == '-') flag = -1; for (; isdigit(c); c = nc()) r = r * 10 + c - 48; return r *= flag; } long long T, x, y, c1, c2, c3, c4, c5, c6; long long d[10][10], dis[10][10], id[10][10], xx[10] = {0, 1, 0, -1, -1, 0, 1, 1, -1}, yy[10] = {0, 1, 1, 0, -1, -1, 0, -1, 1}; signed main() { read(T); id[3][3] = 1, id[2][3] = 2, id[1][2] = 3, id[1][1] = 4, id[2][1] = 5, id[3][2] = 6, id[3][1] = 7, id[1][3] = 8; while (T--) { read(x), read(y), read(c1), read(c2), read(c3), read(c4), read(c5), read(c6); std::memset(dis, 0x3f, sizeof(dis)); std::memset(d, 0x3f, sizeof(d)); d[3][3] = c1, d[2][3] = c2, d[1][2] = c3, d[1][1] = c4, d[2][1] = c5, d[3][2] = c6; for (long long i = 0; i <= 8; ++i) dis[i][i] = 0; for (long long i = 0; i <= 8; ++i) { for (long long j = 1; j <= 3; ++j) for (long long k = 1; k <= 3; ++k) { long long nx = xx[i] + j, ny = yy[i] + k; if (nx < 0 || ny < 0 || nx > 3 || ny > 3) continue; dis[i][id[nx][ny]] = d[j][k]; } } for (long long k = 0; k <= 8; ++k) for (long long i = 0; i <= 8; ++i) for (long long j = 0; j <= 8; ++j) dis[i][j] = std::min(dis[i][j], dis[i][k] + dis[k][j]); long long res = 0; if (x < 0 && y < 0) { long long t = std::min(-x, -y); res += t * dis[0][id[1][1]]; x += t, y += t; } else if (x < 0 && y > 0) { long long t = std::min(-x, y); res += t * dis[0][id[1][3]]; x += t, y -= t; } else if (x > 0 && y < 0) { long long t = std::min(x, -y); res += t * dis[0][id[3][1]]; x -= t, y += t; } else if (x > 0 && y > 0) { long long t = std::min(x, y); res += t * dis[0][id[3][3]]; x -= t, y -= t; } if (x > 0) res += dis[0][id[3][2]] * x; else res += dis[0][id[1][2]] * -x; if (y > 0) res += dis[0][id[2][3]] * y; else res += dis[0][id[2][1]] * -y; printf("%lld\n", res); } return 0; }
#include <bits/stdc++.h> using namespace std; const long long N = 3e5 + 5, N2 = 1e5 + 5, N3 = 1e6 + 5, N4 = 1050, mod1 = 998244353, mod2 = 1e9 + 7; inline long long read() { long long x = 0, f = 1; char ch = getchar(); while (!isdigit(ch)) { if (ch == '-') f = -1; ch = getchar(); } while (isdigit(ch)) { x = (x << 1) + (x << 3) + (ch & 15); ch = getchar(); } return x * f; } void work(); signed main() { long long g = 1; cin >> g; while (g--) work(); } void work() { long long x, y; cin >> y >> x; long long ans = 4e18; long long c[6]; for (long long i = 0; i < 6; ++i) cin >> c[i]; if (x >= 0 && y >= 0) { ans = min(ans, c[0] * min(abs(x), abs(y)) + max(abs(x) - abs(y), 0LL) * c[1] + max(abs(y) - abs(x), 0LL) * c[5]); ans = min(ans, c[0] * max(abs(x), abs(y)) + max(abs(x) - abs(y), 0LL) * c[2] + max(abs(y) - abs(x), 0LL) * c[4]); ans = min(ans, c[5] * abs(y) + c[1] * abs(x)); } else if (x >= 0 && y <= 0) { ans = min(ans, c[3] * abs(y) + c[1] * abs(y - x)); ans = min(ans, c[0] * abs(x) + c[2] * abs(y - x)); ans = min(ans, c[1] * abs(x) + c[2] * abs(y)); } else if (x <= 0 && y >= 0) { ans = min(ans, c[0] * abs(y) + c[4] * abs(y - x)); ans = min(ans, c[3] * abs(x) + c[5] * abs(y - x)); ans = min(ans, c[4] * abs(x) + c[5] * abs(y)); } else if (x <= 0 && y <= 0) { ans = min(ans, c[3] * min(abs(x), abs(y)) + max(abs(x) - abs(y), 0LL) * c[4] + max(abs(y) - abs(x), 0LL) * c[2]); ans = min(ans, c[3] * max(abs(x), abs(y)) + max(abs(x) - abs(y), 0LL) * c[5] + max(abs(y) - abs(x), 0LL) * c[1]); ans = min(ans, c[2] * abs(y) + c[4] * abs(x)); } cout << ans << endl; }
#include <bits/stdc++.h> using namespace std; long long dx[] = {1, 0, -1, -1, 0, +1}; long long dy[] = {1, +1, 0, -1, -1, 0}; void solve() { long long x, y, c[6]; cin >> x >> y; for (long long i = 0; i < 6; i++) cin >> c[i]; map<pair<long long, long long>, long long> mp; mp[make_pair(0, 0)] = 0; priority_queue<pair<long long, pair<long long, long long> > > pq; pq.push({-0, {0, 0}}); while (pq.size()) { long long xx, yy; tie(xx, yy) = pq.top().second; pq.pop(); for (long long i = 0; i < 6; i++) { long long ax = xx + dx[i], ay = yy + dy[i]; if (abs(ax) > 3 || abs(ay) > 3) continue; if (!mp.count(make_pair(ax, ay)) || mp[make_pair(xx, yy)] + c[i] < mp[make_pair(ax, ay)]) { mp[make_pair(ax, ay)] = mp[make_pair(xx, yy)] + c[i]; pq.push({-mp[make_pair(ax, ay)], {ax, ay}}); } } } long long ans = mp[make_pair(x / max(abs(x), 1LL), 0)] * abs(x) + mp[make_pair(0, y / max(abs(y), 1LL))] * abs(y); if (x >= 0) { if (y >= 0) { if (x >= y) ans = min(y * mp[make_pair(1, 1)] + mp[make_pair(1, 0)] * (x - y), ans); else ans = min(x * mp[make_pair(1, 1)] + mp[make_pair(0, 1)] * (y - x), ans); } else { if (x >= -y) ans = min(-y * mp[make_pair(1, -1)] + mp[make_pair(1, 0)] * (x + y), ans); else ans = min(+x * mp[make_pair(1, -1)] + mp[make_pair(0, -1)] * (-x - y), ans); } } else { if (y >= 0) { if (-x >= y) ans = min(+y * mp[make_pair(-1, 1)] + mp[make_pair(-1, 0)] * abs(x - y), ans); else ans = min(-x * mp[make_pair(-1, 1)] + mp[make_pair(0, 1)] * abs(x - y), ans); } else { if (-x >= -y) ans = min(-y * mp[make_pair(-1, -1)] + mp[make_pair(-1, 0)] * abs(x - y), ans); else ans = min(-x * mp[make_pair(-1, -1)] + mp[make_pair(0, -1)] * abs(x - y), ans); } } cout << ans << endl; } int32_t main() { ios_base ::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long test, tc = 0; cin >> test; while (test--) { solve(); } }
#include <bits/stdc++.h> using namespace std; void solve() { long long x, y; cin >> x >> y; vector<int> c(7); for (int i = 1; i <= 6; i++) cin >> c[i]; long long ans = (x >= 0 ? c[6] * x : c[3] * (-x)) + (y >= 0 ? c[2] * y : c[5] * (-y)); ans = min(ans, (x >= 0 ? c[1] * x : c[4] * (-x)) + ((y - x) >= 0 ? c[2] * (y - x) : c[5] * (x - y))); ans = min(ans, (y >= 0 ? c[1] * y : c[4] * (-y)) + ((x - y) >= 0 ? c[6] * (x - y) : c[3] * (y - x))); cout << ans << '\n'; } int main() { int t; cin >> t; while (t--) solve(); }
#include <bits/stdc++.h> using namespace std; long long add(long long a, long long b) { return (a % 1000000007 + b % 1000000007) % 1000000007; } long long mul(long long a, long long b) { return (a % 1000000007 * b % 1000000007) % 1000000007; } long long power(long long a, long long b) { long long ans = 1; if (b < 0) return 0; while (b) { if (b & 1) ans = mul(ans, a); b >>= 1; a = mul(a, a); } return ans; } long long c[7], x, y; long long min(long long a, long long b) { return (a > b ? b : a); } void solve() { long long ans = (x >= 0 ? c[6] : c[3]) * abs(x) + (y >= 0 ? c[2] : c[5]) * abs(y); ans = min(ans, abs(x) * (x >= 0 ? c[1] : c[4]) + abs(y - x) * (y - x >= 0 ? c[2] : c[5])); ans = min(ans, abs(y) * (y >= 0 ? c[1] : c[4]) + abs(y - x) * (y - x >= 0 ? c[3] : c[6])); cout << ans; } int main() { std::ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0); long long test = 1; cin >> test; while (test--) { cin >> x >> y; for (long long i = 1; i < (7); ++i) cin >> c[i]; solve(); cout << "\n"; } return 0; }
#include <bits/stdc++.h> using namespace std; inline long long read() { long long f = 1, ans = 0; char c = getchar(); while (c < '0' || c > '9') { if (c == '-') f = -1; c = getchar(); } while (c >= '0' && c <= '9') { ans = ans * 10 + c - '0'; c = getchar(); } return f * ans; } long long T, X, Y, C[7], Minn; signed main() { T = read(); while (T--) { X = read(), Y = read(); for (long long i = 1; i <= 6; i++) C[i] = read(); Minn = LLONG_MAX; for (long long i = 0; i <= 1; i++) { for (long long j = 0; j <= 1; j++) { for (long long k = 0; k <= 1; k++) { long long curx1 = 1, curx2 = 0, curx3 = -1, cury1 = 1, cury2 = 1, cury3 = 0, cos1 = C[1], cos2 = C[2], cos3 = C[3]; if (i == 1) curx1 = -1, cury1 = -1, cos1 = C[4]; if (j == 1) curx2 = 0, cury2 = -1, cos2 = C[5]; if (k == 1) curx3 = 1, cury3 = 0, cos3 = C[6]; long long x, y, z; x = 0, y = (Y - cury1 * x) / cury2, z = (X - curx1 * x) / curx3; if (x >= 0 && y >= 0 && z >= 0) Minn = min(Minn, x * cos1 + y * cos2 + z * cos3); x = X, y = (Y - cury1 * x) / cury2, z = (X - curx1 * x) / curx3; if (x >= 0 && y >= 0 && z >= 0) Minn = min(Minn, x * cos1 + y * cos2 + z * cos3); x = Y, y = (Y - cury1 * x) / cury2, z = (X - curx1 * x) / curx3; if (x >= 0 && y >= 0 && z >= 0) Minn = min(Minn, x * cos1 + y * cos2 + z * cos3); x = -X, y = (Y - cury1 * x) / cury2, z = (X - curx1 * x) / curx3; if (x >= 0 && y >= 0 && z >= 0) Minn = min(Minn, x * cos1 + y * cos2 + z * cos3); x = -Y, y = (Y - cury1 * x) / cury2, z = (X - curx1 * x) / curx3; if (x >= 0 && y >= 0 && z >= 0) Minn = min(Minn, x * cos1 + y * cos2 + z * cos3); } } } printf("%lld\n", Minn); } return 0; }
#include <bits/stdc++.h> using namespace std; template <class Num> Num mabs(Num A) { if (A < 0) return -A; return A; } void solve() { int x, y; scanf("%d %d", &x, &y); vector<int> cost(6); for (int i = (0); i < (6); i++) scanf("%d", &cost[i]); long long ans = (long long)2e18; vector<pair<int, int> > delta = {{1, 1}, {0, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, 0}}; for (int i = (0); i < (6); i++) { int dx = delta[i].first; int dy = delta[i].second; if (x > 0 && dx <= 0) continue; if (x < 0 && dx >= 0) continue; if (y > 0 && dy <= 0) continue; if (y < 0 && dy >= 0) continue; if (x > 0) { int cnt1 = mabs(x), cnt2 = mabs(y); if (cnt1 != cnt2) continue; long long tmp = (long long)cost[i] * (cnt1); ans = min(ans, tmp); } } for (int dir1 = (0); dir1 < (6); dir1++) { for (int dir2 = (0); dir2 < (6); dir2++) { if (dir1 == dir2) continue; pair<int, int> d1 = delta[dir1]; pair<int, int> d2 = delta[dir2]; int dx1 = d1.first, dy1 = d1.second; int dx2 = d2.first, dy2 = d2.second; int a1 = 0, a2 = 0; if (dy1 == 0) { if (dy2 == 0) continue; a2 = y / dy2; } else { if ((dx2 - (dy2 * dx1) / dy1) != 0) a2 = (x - (y * dx1) / dy1) / (dx2 - (dy2 * dx1) / dy1); } if (dx1 != 0) { a1 = (x - a2 * dx2) / dx1; } if (a1 < 0 || a2 < 0) continue; if (a1 * dx1 + a2 * dx2 != x) continue; if (a1 * dy1 + a2 * dy2 != y) continue; long long tmp = (long long)cost[dir1] * a1 + (long long)cost[dir2] * a2; ans = min(ans, tmp); } } printf("%lld\n", ans); } int main() { int T; scanf("%d", &T); while (T--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; const int MAXN = 1e3 + 20, MAXM = 20, MOD = 1000 * 1000 * 1000 + 7; const long long INF = 1e18; char c[MAXN][MAXN]; void solve() { long long x, y; cin >> x >> y; int n = 7; vector<int> cost(n); for (int i = 1; i < n; i++) cin >> cost[i]; vector<pair<long long, int> > vec[n + 1]; for (int i = 1; i < n; i++) { vec[0].push_back(make_pair(i, cost[i])); vec[i].push_back(make_pair(0, cost[i])); } vec[1].push_back(make_pair(2, cost[3])); vec[2].push_back(make_pair(3, cost[4])); vec[3].push_back(make_pair(4, cost[5])); vec[4].push_back(make_pair(5, cost[6])); vec[5].push_back(make_pair(6, cost[1])); vec[6].push_back(make_pair(1, cost[2])); vec[2].push_back(make_pair(1, cost[6])); vec[3].push_back(make_pair(2, cost[1])); vec[4].push_back(make_pair(3, cost[2])); vec[5].push_back(make_pair(4, cost[3])); vec[6].push_back(make_pair(5, cost[4])); vec[1].push_back(make_pair(6, cost[5])); set<pair<long long, int> > s; vector<long long> dis(n); s.insert(make_pair(0, 0)); for (int i = 1; i < n; i++) { dis[i] = INF; s.insert(make_pair(dis[i], i)); } vector<bool> vis(n); while (s.size()) { pair<int, int> d = *s.begin(); s.erase(d); int w = d.first; int u = d.second; vis[u] = 1; for (int i = 0; i < vec[u].size(); i++) if (!vis[vec[u][i].first]) { s.erase(make_pair(dis[vec[u][i].first], vec[u][i].first)); dis[vec[u][i].first] = min(dis[vec[u][i].first], dis[u] + vec[u][i].second); s.insert(make_pair(dis[vec[u][i].first], vec[u][i].first)); } } long long ans = 0; if (x <= 0 && y <= 0) { ans += min(-x, -y) * dis[4]; int mn = min(-x, -y); x += mn; y += mn; } else if (x >= 0 && y >= 0) { ans += min(x, y) * dis[1]; int mn = min(x, y); x -= mn; y -= mn; } if (x >= 0) ans += x * dis[6]; else ans += -x * dis[3]; if (y >= 0) ans += y * dis[2]; else ans += -y * dis[5]; cout << ans << endl; } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int q; cin >> q; while (q--) solve(); }
#include <bits/stdc++.h> using namespace std; void solve() { long long x, y, c1, c2, c3, c4, c5, c6; cin >> x >> y; cin >> c1 >> c2 >> c3 >> c4 >> c5 >> c6; swap(x, y); if (x >= 0 && y >= 0) { long long res = x * c2 + y * c6; long long temp = min(x, y); res = min(res, res - temp * (c2 + c6) + temp * c1); res = min(res, max(x, y) * c1 + (max(x, y) - y) * c3 + (max(x, y) - x) * c5); cout << res << "\n"; return; } if (x <= 0 && y >= 0) { x = -x; long long res = x * c5 + y * c6; res = min(res, res - y * c6 + y * (c1 + c5)); res = min(res, res - x * c5 + x * (c4 + c6)); cout << res << "\n"; return; } if (x <= 0 && y <= 0) { x = -x; y = -y; long long res = x * c5 + y * c3; long long temp = min(x, y); res = min(res, res - temp * (c3 + c5) + temp * c4); res = min(res, max(x, y) * c4 + (max(x, y) - y) * c6 + (max(x, y) - x) * c2); cout << res << "\n"; return; } if (x >= 0 && y <= 0) { y = -y; long long res = x * c2 + y * c3; res = min(res, res - y * c3 + y * (c4 + c2)); res = min(res, res - x * c2 + x * (c1 + c3)); cout << res << "\n"; return; } } signed main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); long long t = 1; cin >> t; while (t--) solve(); return 0; }
#include <bits/stdc++.h> using namespace std; long long c[7]; int main(void) { int T; cin >> T; while (T--) { long long x, y; cin >> x >> y; for (int i = 1; i <= 6; i++) { cin >> c[i]; } c[1] = min(c[1], c[6] + c[2]); c[2] = min(c[2], c[1] + c[3]); c[3] = min(c[3], c[2] + c[4]); c[4] = min(c[4], c[3] + c[5]); c[5] = min(c[5], c[4] + c[6]); c[6] = min(c[6], c[5] + c[1]); long long result = 0; if (x > 0) { if (y > 0) { result += c[1] * min(x, y); if (x > y) result += c[6] * (x - y); else result += c[2] * (y - x); } else { result += c[6] * x; result += c[5] * (-y); } } else { if (y > 0) { result += c[3] * (-x); result += c[2] * y; } else { result += c[4] * min(-x, -y); if ((-x) > (-y)) result += c[3] * ((-x) - (-y)); else result += c[5] * ((-y) - (-x)); } } cout << result << endl; } }