malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const long double e = 1e-2;   int k;   long double R, r, l;   long double x, y, xl, yl, xr, yr;   long double a, b, c, S;   long double COS[12], SIN[12];   bool check(long double x, long double y) {     long double s = 0;     for (int i = 0; i < 12; i += 2)       s = max(s, ((x + COS[i]) * (x + COS[i])) + ((y + SIN[i]) * (y + SIN[i])));     return s <= l + r;   }   bool end(long double x, long double y) {     long double s = 1e+60;     for (int i = 0; i < 12; i += 2)       s = min(s, ((x + COS[i]) * (x + COS[i])) + ((y + SIN[i]) * (y + SIN[i])));     return s > S;   }   int main() {     R = 1;     r = sqrt(((R) * (R)) - ((0.5) * (0.5)));     for (int i = 0; i < 12; i++) {       COS[i] = cos(((i * 30) * M_PI / 180));       SIN[i] = sin(((i * 30) * M_PI / 180));     }     scanf( %d , &k);     l = k;     l *= l;     S = k + 3;     S *= S;     x = (k + 2) / 3 * 3 * COS[2];     y = (k + 2) / 3 * 3 * SIN[2];     xl = xr = x;     yl = yr = y;     long long ans = 0;     do {       long double s;       s = y * xr - x * yr;       while (s > 0 && abs(s) > e && !check(xr, yr)) {         xr += 2 * r * COS[5];         yr += 2 * r * SIN[5];         s = y * xr - x * yr;       }       while (check(xr, yr)) {         xr -= 2 * r * COS[5];         yr -= 2 * r * SIN[5];       }       s = y * xl - x * yl;       while (s < 0 && abs(s) > e && !check(xl, yl)) {         xl += 2 * r * COS[11];         yl += 2 * r * SIN[11];         s = y * xl - x * yl;       }       while (check(xl, yl)) {         xl -= 2 * r * COS[11];         yl -= 2 * r * SIN[11];       }       long double L = sqrt(((xl - xr) * (xl - xr)) + ((yl - yr) * (yl - yr)));       ans += max((long long)((L + r) / (2 * r)) - 1, 0LL);       xl += 2 * r * COS[7];       yl += 2 * r * SIN[7];       xr += 2 * r * COS[9];       yr += 2 * r * SIN[9];     } while (!end(xr, yr) || !end(xl, yl));     cout << ans << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     long long n;     cin >> n;     string s[n];     long long flg = 1;     long long ar[n];     for (long long i = 0; i < n; i++) {       cin >> s[i];       long long flag = 0;       long long pos = -1;       for (long long j = 0; j < n; j++) {         if (s[i][j] ==  . ) {           flag = 1;           pos = j;           break;         }       }       if (flag == 0) {         flg = 0;       }       ar[i] = pos;     }     if (!flg) {       flg = 1;       long long ar[n];       for (long long i = 0; i < n; i++) {         long long flag = 0;         long long pos = -1;         for (long long j = 0; j < n; j++) {           if (s[j][i] ==  . ) {             flag = 1;             pos = j;             break;           }         }         if (flag == 0) {           flg = 0;           break;         }         ar[i] = pos;       }       if (!flg)         cout << -1;       else {         for (long long i = 0; i < n; i++) {           cout << ar[i] + 1 <<     << i + 1 <<   n ;         }       }       return 0;     } else {       for (long long i = 0; i < n; i++) {         cout << i + 1 <<     << ar[i] + 1 <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s;     getline(cin, s);     long long int a[s.size()];     vector<long long int> v;     for (long long int i = 0; i < s.size(); i++) {       if (i == 0) {         vector<long long int> tp(8, 0);         long long int j = 0;         long long int k = s[i];         while (k) {           tp[j] = (k >> 0) & 1;           k = k >> 1;           j++;         }         long long int ans = 0;         reverse(tp.begin(), tp.end());         for (long long int l = 0; l < 8; l++) {           ans = ans + tp[l] * pow(2, l);         }         v.push_back(ans);         a[i] = 256 - ans;         continue;       }       vector<long long int> tp(8, 0);       long long int j = 0;       long long int k = s[i];       while (k) {         tp[j] = (k >> 0) & 1;         k = k >> 1;         j++;       }       long long int ans = 0;       reverse(tp.begin(), tp.end());       for (long long int l = 0; l < 8; l++) {         ans = ans + tp[l] * pow(2, l);       }       a[i] = (v[i - 1] - ans + 256) % 256;       v.push_back(ans);     }     for (long long int i = 0; i < s.size(); i++) cout << a[i] <<   n  ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     int n;     cin >> n;     long long m_min = 1ll << 62;     long long ans = 0;     for (int i = 0; i < n; i++) {       long long x;       cin >> x;       ans += x;       if (x % 2 == 1) m_min = min(m_min, x);     }     if (ans % 2 == 1) ans -= m_min;     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int mod = 1e9 + 7;   const int maxn = 1e6 + 5;   long long int power(long long int base, long long int exp) {     long long int res = 1;     while (exp) {       if (exp % 2) res *= base;       base *= base;       res %= mod;       base %= mod;       exp /= 2;     }     return res;   }   long long int n, m, k, l[maxn], r[maxn], dp[350][350], adj[350][350], c[maxn];   long long int go(long long int idx, long long int k) {     if (k <= 0) return 0;     if (idx >= n + 1) return 1e16;     long long int &ret = dp[idx][k];     if (ret != -1) return ret;     ret = go(idx + 1, k);     for (long long int i = idx; i <= min(idx + k, n); i++) {       ret = min(ret, go(i + 1, k - (i - idx + 1)) + adj[idx][i]);     }     return ret;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     cout.tie(NULL);     cin >> n >> m >> k;     for (long long int i = 1; i <= n; i++)       for (long long int j = i; j <= n; j++) adj[i][j] = 1e15;     for (long long int i = 1; i <= m; i++) {       cin >> l[i] >> r[i] >> c[i];       adj[l[i]][r[i]] = min(adj[l[i]][r[i]], c[i]);     }     for (long long int i = 1; i <= n; i++) {       for (long long int j = 1; j <= n; j++) {         if (i < j) break;         for (long long int k = j; k <= n; k++) {           adj[j][k] = min(adj[j][k], adj[j][i] + adj[i][k]);         }       }     }     memset(dp, -1, sizeof(dp));     long long int ans = go(1, k);     if (ans > 1e12) ans = -1;     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, A;     cin >> n;     for (int i = 1; i <= n; i++) {       cin >> A;       if (i == n) {         while (A--) cout <<  PLR ;         return 0;       }       while (A--) {         cout <<  PRL ;       }       cout <<  R ;     }   }
#include <bits/stdc++.h>   const int inf = int(1e9);   const int mod = inf + 7;   const double PI = acos(-1.0);   using namespace std;   const int N = 1e5 + 155;   void solve() {     int n, k, s = 0;     cin >> n >> k;     for (int i = 0; i < n; ++i) {       int x;       cin >> x;       s += x;     }     for (int ans = 0;; ans++) {       int a = 2 * (s + ans * k);       int b = (2 * k - 1) * (ans + n);       if (a >= b) {         cout << ans;         return;       }     }   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t = 1;     while (t--) solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int a, b;     cin >> a >> b;     int ans = 0;     while ((a >= 1 && b > 1) || (a > 1 && b >= 1)) {       if (a < b) {         ans++;         a--;         b -= 2;       } else {         ans++;         a -= 2;         b--;       }     }     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int L[7], R[7];   int main() {     int n;     cin >> n;     for (int i = 1; i <= n; ++i) {       cin >> L[i] >> R[i];     }     long double cases = 0;     long double sum = 0;     for (int p = 1; p <= 10000; ++p) {       for (int mask = 3; mask < 1 << n; ++mask) {         bool ok = true;         long double cnt = 1;         for (int j = 1; j <= n; ++j)           if (!((mask >> (j - 1)) & 1)) {             if (L[j] < p) {               cnt *= min(p - 1, R[j]) - L[j] + 1;             } else {               ok = false;             }           }         if (!ok) continue;         vector<pair<bool, int>> ranges;         for (int j = 1; j <= n; ++j)           if ((mask >> (j - 1)) & 1) {             if (p <= R[j]) {               ranges.push_back(make_pair(L[j] <= p, R[j] - max(p + 1, L[j]) + 1));             } else {               ok = false;             }           }         if (!ok) continue;         long double cnt2 = 0;         for (int j = 0; j < int(ranges.size()); ++j) {           bool ok = true;           for (int k = 0; k < int(ranges.size()); ++k)             if (k != j)               if (!ranges[k].first) ok = false;           if (ok) {             cnt2 += ranges[j].second;           }         }         int b = 0;         for (int tmp = mask; tmp; tmp &= tmp - 1) ++b;         if (b < 2) continue;         bool add = true;         for (int j = 0; j < int(ranges.size()); ++j) {           add &= ranges[j].first;         }         cnt *= (cnt2 + add);         cases += cnt;         sum += cnt * p;       }     }     cout << fixed << setprecision(15) << 1.0 * sum / cases;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, sub;     int grp;     cin >> n >> sub;     if (n == sub)       grp = 0;     else if (sub == 0 || sub == 1)       grp = 1;     else if (sub >= n - n / 2)       grp = n - sub;     else       grp = sub;     cout << grp << endl;   }
#include <bits/stdc++.h>   using namespace std;   template <typename Bp, typename Ep, typename Mp>   inline Mp __powMod(Bp a, Ep b, const Mp &m) {     a %= m;     Mp res = 1;     while (b > 0) {       if (b & 1) {         res = ((long long)res * a) % m;       }       a = ((long long)a * a) % m;       b >>= 1;     }     return res % m;   }   const int MOD = 1e9 + 7;   template <typename Tp>   inline Tp inv(const Tp &x) {     return __powMod(x, MOD - 2, MOD);   }   const int N = 100000 + 5;   int m;   int dp[N];   vector<int> prime;   int mu[N];   bool notPrime[N];   inline void init() {     mu[1] = 1;     for (int i = 2; i < N; ++i) {       if (!notPrime[i]) {         mu[i] = -1;         prime.emplace_back(i);       }       for (const auto &p : prime) {         if (i * p >= N) {           break;         }         notPrime[i * p] = true;         if (i % p == 0) {           mu[i * p] = 0;           break;         } else {           mu[i * p] = -mu[i];         }       }     }   }   int main() {     init();     scanf( %d , &m);     int ans = 1;     for (int i = 2, t; i <= m; ++i) {       if (mu[i] != 0) {         t = m / i;         ans = (ans - (long long)mu[i] * t * inv(m - t) % MOD + MOD) % MOD;       }     }     printf( %d n , ans);   }
#include <bits/stdc++.h>   using namespace std;   double pd[40][40][300];   int n, K;   int r[40];   int go(int a, int i, int j) {     if (a < i || a > j) return a;     return j - (a - i);   }   double solve(int a, int b, int k) {     if (k == 0) return b < a;     double& ret = pd[a][b][k];     if (ret > -0.5) return ret;     ret = 0;     double mul = 1. / ((double)((n * (n + 1)) / 2));     for (int i = 0; i < n; i++)       for (int j = i; j < n; j++) {         int aa = go(a, i, j);         int bb = go(b, i, j);         ret += mul * solve(aa, bb, k - 1);       }     return ret;   }   int main() {     cin >> n >> K;     memset(pd, -1, sizeof pd);     for (int i = 0; i < n; i++) {       int x;       cin >> x;       r[x] = i;     }     double ans = 0;     for (int i = 1; i <= n; i++)       for (int j = i + 1; j <= n; j++) {         ans += solve(r[i], r[j], K);       }     printf( %.15f n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long MOD = 998244353;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie();     cout.tie();     long long n, k;     cin >> n >> k;     long long m = 0;     vector<long long> v(n);     for (long long i = 0; i < n; i++) cin >> v[i];     for (long long i = n; i > n - k; i--) {       m += i;     }     vector<long long> ind;     for (long long i = 0; i < n; i++) {       if (v[i] > n - k) ind.push_back(i);     }     long long x = 1;     for (long long i = 1; i < ind.size(); i++) {       x *= ind[i] - ind[i - 1];       x %= MOD;     }     cout << m <<     << x;   }
#include <bits/stdc++.h>   using namespace std;   int n, m, q;   int nxt[100001];   char s[100001], t[100001];   int temp[100001];   int l, r;   bool check(int u) {     for (int i = 0; i < m; ++i) {       if (s[u + i] != t[i]) return 0;     }     return 1;   }   void pre_do() {     for (int i = 1; i <= n - m + 1; i++) {       temp[i] += check(i);     }   }   void putin() {     cin >> n >> m >> q;     scanf( %s%s , s + 1, t);   }   int main() {     putin();     pre_do();     for (int i = 1; i <= n; i++) {       temp[i] += temp[i - 1];     }     for (int i = 1; i <= q; i++) {       cin >> l >> r;       if (r - l + 1 < m)         cout << 0 << endl;       else         cout << temp[r - m + 1] - temp[l - 1] << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 5e4 + 5;   const int SQRN = 210;   const int BSZ = 256;   vector<int> g[MAXN];   int v[MAXN], lvl[MAXN], pai[MAXN];   int jump[MAXN], best[MAXN][SQRN];   int trie[BSZ * 20 + 5][2];   int n, q, cnt_trie = 1;   void predfs(int cur, int p) {     for (auto viz : g[cur]) {       if (viz == p) continue;       lvl[viz] = lvl[cur] + 1;       pai[viz] = cur;       predfs(viz, cur);     }   }   void build(int cur, int k, int root) {     if (k >= BSZ) return;     jump[root] = cur;     int val = v[cur] ^ k;     int tcur = 1;     for (int i = 16; i >= 0; i--) {       bool viz = val & (1 << i);       if (!trie[tcur][viz]) trie[tcur][viz] = ++cnt_trie;       tcur = trie[tcur][viz];     }     if (pai[cur] == cur) return;     build(pai[cur], k + 1, root);   }   int find_best(int mask) {     int cur = 1;     int resp = 0;     for (int i = 16; i >= 0; i--) {       bool want = 1 ^ ((bool)(mask & (1 << i)));       if (trie[cur][want])         cur = trie[cur][want], resp += (1 << i);       else         cur = trie[cur][!want];     }     return resp;   }   int main() {     scanf( %d %d , &n, &q);     for (int i = 1; i <= n; i++) scanf( %d , &v[i]);     for (int i = 1; i <= n - 1; i++) {       int a, b;       scanf( %d %d , &a, &b);       g[a].push_back(b);       g[b].push_back(a);     }     pai[1] = 1;     predfs(1, 1);     for (int i = 1; i <= n; i++) {       build(i, 0, i);       for (int k = 0; k * BSZ <= n; k++) best[i][k] = find_best(k * BSZ);       memset(trie, 0, sizeof(trie));       cnt_trie = 1;     }     while (q--) {       int a, b;       scanf( %d %d , &a, &b);       int w = 0;       int respf = 0;       while (lvl[jump[b]] > lvl[a]) {         respf = max(respf, best[b][w]);         b = pai[jump[b]];         w++;       }       int dist = w * BSZ;       while (b != a) {         respf = max(respf, v[b] ^ dist);         b = pai[b];         dist++;       }       respf = max(respf, v[a] ^ dist);       printf( %d n , respf);     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, t = 0, c = 0;     char ch[105][105];     cin >> n;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= n; j++) cin >> ch[i][j];     for (int i = 1; i <= n; i++) {       c = 0;       for (int j = 1; j <= n; j++) {         if (ch[i][j] ==  C ) c++;       }       t += (c * (c - 1)) / 2;     }     for (int j = 1; j <= n; j++) {       c = 0;       for (int i = 1; i <= n; i++) {         if (ch[i][j] ==  C ) c++;       }       t += (c * (c - 1)) / 2;     }     cout << t;   }
#include <bits/stdc++.h>   using namespace std;   namespace std {   bool operator<(const complex<double> &lhs, const complex<double> &rhs) {     if (real(lhs) == real(rhs)) {       return imag(lhs) < imag(rhs);     } else {       return real(lhs) < real(rhs);     }   }   }  // namespace std   ostream &operator<<(ostream &os, const complex<double> &p) {     return os <<  (  << p.real() <<  ,  << p.imag() <<  ) ;   }   template <typename T>   ostream &operator<<(ostream &os, const vector<T> &vec) {     os <<  [ ;     for (const auto &v : vec) {       os << v <<  , ;     }     os <<  ] ;     return os;   }   complex<double> counter_clock_wise(complex<double> p, double rad) {     double a = abs(p);     return polar(a, arg(p) + rad);   }   const double EPS = 1e-7;   const double PI = 3.141592653589793;   double dot(complex<double> a, complex<double> b) { return real(conj(a) * b); }   double point_distance(complex<double> a, complex<double> b) {     return abs(a - b);   }   bool is_square(vector<complex<double> > ps) {     sort(ps.begin(), ps.end());     vector<double> dist;     for (long long i = 0; i < 4; i++) {       for (long long j = i + 1; j < 4; j++) {         if (abs(ps[i] - ps[j]) < EPS) return false;         dist.push_back(abs(ps[i] - ps[j]));       }     }     sort(dist.begin(), dist.end());     for (long long i = 0; i < 4; i++) {       if (abs(dist[0] - dist[i]) > EPS) return false;     }     for (long long i = 4; i < 6; i++) {       if (abs(sqrt(2) * dist[1] - dist[i]) > EPS) return false;     }     return true;   }   const long long INF = 100000000;   long long solve(vector<long long> y, vector<long long> x, vector<long long> b,                   vector<long long> a) {     long long ret = INF;     ;     ;     for (long long i = 0; i < static_cast<long long>(4); i++) {       for (long long j = 0; j < static_cast<long long>(4); j++) {         for (long long k = 0; k < static_cast<long long>(4); k++) {           for (long long l = 0; l < static_cast<long long>(4); l++) {             vector<long long> cur = {i, j, k, l};             vector<complex<double> > ps;             for (long long m = 0; m < static_cast<long long>(4); m++) {               complex<double> p(x[m], y[m]);               p -= complex<double>(a[m], b[m]);               p = counter_clock_wise(p, (PI / 2) * cur[m]);               p += complex<double>(a[m], b[m]);               ps.push_back(p);             }             sort(ps.begin(), ps.end());             if (is_square(ps)) {               ;               ;               ret = min(ret, i + j + k + l);             }           }         }       }     }     if (ret == INF) return -1;     return ret;   }   signed main() {     ios::sync_with_stdio(false);     cin.tie(0);     long long n;     cin >> n;     for (long long i = 0; i < n; i++) {       vector<long long> y(4), x(4), b(4), a(4);       for (long long j = 0; j < 4; j++) {         cin >> x[j] >> y[j] >> a[j] >> b[j];       }       long long ret = solve(y, x, b, a);       cout << ret << endl;     }     return 0;   }
#include <bits/stdc++.h>   namespace IO {   char gc() { return getchar(); }   template <typename Tp>   bool get1(Tp &x) {     bool neg = 0;     char c = gc();     while (c != EOF && (c <  0  || c >  9 ) && c !=  - ) c = gc();     if (c ==  - ) c = gc(), neg = 1;     if (c == EOF) return false;     x = 0;     for (; c >=  0  && c <=  9 ; c = gc()) x = x * 10 + c -  0 ;     if (neg) x = -x;     return true;   }   template <typename Tp>   void printendl(Tp x) {     if (x < 0) putchar( - ), x = -x;     static short a[40], sz;     sz = 0;     while (x > 0) a[sz++] = x % 10, x /= 10;     if (sz == 0) putchar( 0 );     for (int i = sz - 1; i >= 0; i--) putchar( 0  + a[i]);     puts(  );   }   }  // namespace IO   using IO::get1;   using IO::printendl;   const int inf = 0x3f3f3f3f;   const long long Linf = 1ll << 61;   const int maxn = 111;   int n, q1, q2, a[maxn], b[maxn], c[maxn], ans[maxn];   int d[maxn];   void getv(int id) {     for (int i = 1; i <= n; i++) a[id] += d[i] * (d[i] - 1) * (d[i] - 2) / 6;     for (int i = 1; i <= n - 2; i++) b[id] += d[i] * d[i + 1] * d[i + 2];   }   int main() {     get1(n);     get1(a[0]) && get1(b[0]);     for (int i = 1; i <= n; i++) {       c[i] = (i < n ? i : 1);       printf( + %d n , c[i]);       fflush(stdout);       get1(a[i]) && get1(b[i]);     }     for (int i = n; i >= 1; i--) {       a[i] -= a[i - 1];       b[i] -= b[i - 1];     }     memset(ans, -1, sizeof(ans));     ans[1] = (a[n] - a[1]);     for (int i = 2; i < n; i++)       if (a[i]) {         ans[i] = 0;         while (ans[i] * (ans[i] - 1) / 2 < a[i]) ans[i]++;       }     if (ans[3] == -1) ans[3] = (b[2] > 0);     if (ans[2] == -1) ans[2] = b[n] / (ans[3] + 1) - 1;     for (int i = 3; i < n - 1; i++) {       b[i] -= (ans[i - 1] + 1) * (ans[i - 2] + 1);       if (ans[i + 1] == -1) ans[i + 1] = (b[i] > 0);     }     ans[n] = (b[n - 1] - (ans[n - 2] + 1) * (ans[n - 3] + 1)) / (ans[n - 2] + 1);     printf( ! );     for (int i = 1; i <= n; i++) printf(  %d , ans[i]);     puts(  );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <typename T>   T gcd(T x, T y) {     if (x < y) swap(x, y);     while (y > 0) {       T f = x % y;       x = y;       y = f;     }     return x;   }   const int maxn = 200005;   int n;   int a[maxn];   int dp[maxn];   vector<int> adj[maxn];   void dfs(int u, int p, set<int> gcd0, set<int> gcd1) {     dp[u] = *gcd0.rbegin();     if (!gcd1.empty()) {       dp[u] = max(dp[u], *gcd1.rbegin());     }     for (int v : adj[u]) {       if (v != p) {         set<int> g0;         set<int> g1 = gcd0;         for (auto x : gcd0) g0.insert(gcd(x, a[v]));         for (auto x : gcd1) g1.insert(gcd(x, a[v]));         dfs(v, u, g0, g1);       }     }   }   int main(int argc, char *argv[]) {     std::cin.sync_with_stdio(false);     std::cin.tie(nullptr);     cin >> n;     {       for (int i = 1; i <= n; i++) cin >> a[i], dp[i] = i;       for (int i = 1; i < n; i++) {         int x, y;         cin >> x >> y;         adj[x].push_back(y);         adj[y].push_back(x);       }       set<int> gcd0;       set<int> gcd1;       gcd0.insert(a[1]);       gcd1.insert(0);       dfs(1, 0, gcd0, gcd1);       for (int i = 1; i <= n; i++) {         cout << dp[i] << (i == n ?   n  :    );       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e6 + 1;   int v[N], md[N];   vector<int> freq[N];   int lm[N], rm[N];   int cs[N];   int val(int x, int l, int r) {     if (l > r || l < 0) return 0;     return upper_bound(freq[x].begin(), freq[x].end(), r) -            lower_bound(freq[x].begin(), freq[x].end(), l);   }   int main() {     int n, k;     scanf( %d%d , &n, &k);     freq[0].push_back(0);     for (int i = 1; i <= n; i++) {       scanf( %d , &v[i]);       md[i] = v[i] % k;       cs[i] += v[i] % k;       cs[i] += cs[i - 1];       cs[i] %= k;       freq[cs[i]].push_back(i);     }     stack<pair<int, int> > s;     for (int i = n; i > 0; i--) {       while (s.size() && s.top().first < v[i]) {         s.pop();       }       if (s.size()) {         rm[i] = s.top().second - 1;       } else         rm[i] = n;       s.push(make_pair(v[i], i));     }     while (!s.empty()) {       s.pop();     }     for (int i = 1; i <= n; i++) {       while (s.size() && s.top().first <= v[i]) {         s.pop();       }       if (s.size()) {         lm[i] = s.top().second + 1;       } else         lm[i] = 1;       s.push(make_pair(v[i], i));     }     long long ans = 0;     int l, r, al, ar;     int x, d;     for (int i = 1; i <= n; i++) {       if (lm[i] == rm[i]) continue;       if (abs(lm[i] - i) + 1 < abs(i - rm[i]) + 1) {         l = lm[i] - 1, r = i - 2;         al = i + 1, ar = rm[i];         d = md[i];       } else {         d = -md[i];         l = i + 1, r = rm[i];         al = lm[i] - 1, ar = i - 2;       }       for (int j = l; j <= r; j++) {         x = cs[j] + d + k;         x %= k;         ans += val(x, al, ar);       }       ans += val(cs[i - 1], lm[i] - 1, i - 2);       ans += val(cs[i], i + 1, rm[i]);     }     cout << ans << endl;     return 0;   }