malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2007;   int n, m, a[maxn][maxn];   long long sr[maxn][maxn], sc[maxn][maxn], sumr[maxn], sumc[maxn], allsumr[maxn],       allsumc[maxn];   int main() {     scanf( %d %d , &n, &m);     for (int i = 1; i <= n; i++) {       for (int j = 1; j <= m; j++) {         scanf( %d , &a[i][j]);         sumr[i] += a[i][j];         sumc[j] += a[i][j];       }     }     for (int r = 1; r <= n + 1; r++) {       for (int nr = 1; nr <= n; nr++) {         int dis;         if (nr < r)           dis = abs(r - nr - 1) * 4 + 2;         else           dis = abs(r - nr) * 4 + 2;         sr[r][nr] = dis * dis * sumr[nr];         allsumr[r] += sr[r][nr];       }     }     for (int c = 1; c <= m + 1; c++) {       for (int nc = 1; nc <= m; nc++) {         int dis;         if (nc < c)           dis = abs(c - nc - 1) * 4 + 2;         else           dis = abs(c - nc) * 4 + 2;         sc[c][nc] = dis * dis * sumc[nc];         allsumc[c] += sc[c][nc];       }     }     long long ans = 1e18 + 10;     int x, y;     for (int r = 1; r <= n + 1; r++) {       for (int c = 1; c <= m + 1; c++) {         if (allsumr[r] + allsumc[c] < ans) {           ans = allsumr[r] + allsumc[c];           x = r, y = c;         }       }     }     printf( %lld n%d %d n , ans, x - 1, y - 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int v[100001];   int acum[100001];   long long cnt = 0, ans[100001];   struct Q {     int i, j, p;     Q(int a, int b, int c) : i(a), j(b), p(c){};     Q() {}     bool operator<(const Q &other) const {       if (i / 300 == other.i / 300) return j < other.j;       return i / 300 < other.i / 300;     }   };   vector<Q> queries;   int freq[2097151];   int main() {     int n, m, k;     scanf( %d %d %d , &n, &m, &k);     for (int i = 1; i <= n; i++) {       scanf( %d , v + i);       acum[i] = v[i] ^ acum[i - 1];     }     for (int k = 0; k < m; k++) {       int i, j;       scanf( %d %d , &i, &j);       queries.push_back(Q(i - 1, j, k));     }     sort(queries.begin(), queries.end());     int l = 0, r = 0;     freq[0] = 1;     for (int q = 0; q < m; q++) {       int i = queries[q].i, j = queries[q].j, p = queries[q].p;       while (l > i) {         --l;         cnt += freq[acum[l] ^ k];         freq[acum[l]]++;       }       while (r < j) {         r++;         cnt += freq[acum[r] ^ k];         freq[acum[r]]++;       }       while (l < i) {         freq[acum[l]]--;         cnt -= freq[acum[l] ^ k];         l++;       }       while (r > j) {         freq[acum[r]]--;         cnt -= freq[acum[r] ^ k];         r--;       }       ans[p] = cnt;     }     for (int i = 0; i < m; i++) printf( %lld n , ans[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int people[105];   int main() {     int a, b, c;     memset(people, 0, sizeof(people));     int n, m;     scanf( %d%d , &n, &m);     for (int i = 0; i < m; i++) {       scanf( %d%d%d , &a, &b, &c);       people[a] -= c;       people[b] += c;     }     long long sum = 0;     for (int i = 1; i <= n; i++)       if (people[i] > 0) sum += people[i];     printf( %I64d n , sum);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   bool check(vector<int>& v, int k, int val, int start) {     vector<int> dp(v.size());     if (v[0] <= val && start == 0) {       dp[0] = dp[1] = 1;     }     if (v[1] <= val) {       dp[1] = 1;     }     for (int i = 2; i < v.size(); ++i) {       dp[i] = dp[i - 1];       if (v[i] <= val) {         dp[i] = max(dp[i], 1 + dp[i - 2]);       }     }     int endpos = v.size() - 1;     if ((start == 1 && k % 2) || (start == 0 && k % 2 == 0)) {       endpos--;     }     if (dp[endpos] >= (k + 1 - start) / 2) {       return true;     }     return false;   }   bool checkall(vector<int>& v, int k, int val) {     return check(v, k, val, 0) || check(v, k, val, 1);   }   void solve(std::istream& in, std::ostream& out) {     int n, k;     in >> n >> k;     vector<int> v(n);     for (auto& x : v) {       in >> x;     }     int l = 1, r = 1e9 + 1;     while (r - l > 1) {       int m = (r + l) / 2;       if (checkall(v, k, m)) {         r = m;       } else {         l = m;       }     }     if (checkall(v, k, l)) {       r = l;     }     out << r << endl;   }   int main() { solve(cin, cout); }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 1e6 + 5;   const int MOD = 1e9 + 7;   inline int read() {     int s = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) {       if (ch ==  - ) f = -1;       ch = getchar();     }     while (ch >=  0  && ch <=  9 ) {       s = (s << 1) + (s << 3) + ch -  0 ;       ch = getchar();     }     return s * f;   }   inline void write(long long x) {     if (x < 0) x = -x, putchar( - );     if (x > 9) write(x / 10);     putchar(x % 10 +  0 );   }   int a[maxn];   int main() {     int T = read(), n, m, k;     while (T--) {       n = read(), k = read();       for (int i = 1; i <= n; i++) a[i] = read();       sort(a + 1, a + n + 1);       int ans = 0;       for (int i = 2; i <= n; i++) {         if (a[1] + a[i] > k) continue;         ans += (k - a[i]) / a[1];       }       write(ans);       putchar(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long MOD = (long long)1e9 + 7;   const int N = 1e5 + 5, inf = 1e9 + 5;   long long add(long long x, long long y) {     x += y;     if (x >= MOD) return x - MOD;     return x;   }   long long sub(long long x, long long y) {     x -= y;     if (x < 0) return x + MOD;     return x;   }   long long mult(long long x, long long y) { return (x * y) % MOD; }   mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int T;     cin >> T;     while (T--) {       int n, m;       cin >> n >> m;       vector<int> row_sum(n), col_sum(m);       vector<vector<bool>> a(n, vector<bool>(m));       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           char c;           cin >> c;           row_sum[i] += c ==  * ;           col_sum[j] += c ==  * ;           a[i][j] = c ==  * ;         }       }       int ans = inf;       for (int i = 0; i < n; i++) {         for (int j = 0; j < m; j++) {           ans = min(ans, n - row_sum[i] + m - col_sum[j] - (!a[i][j]));         }       }       cout << ans << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   const int IINF = 1e9;   const long long LINF = 1e18;   const int K = 1e5 + 5;   const int KL = 1e6 + 5;   int n;   int a[505];   int mem[505][505];   bool tok[505];   int dp(int l, int r) {     if (l == r) return 1;     if (l > r) return 0;     if (mem[l][r] != -1) return mem[l][r];     int k;     int ans = 505, ans2 = 505, ans3 = 505;     if (a[l] == a[l + 1]) ans2 = min(ans2, 1 + dp(l + 2, r));     for (int k = l + 2; k <= r; k++)       if (a[l] == a[k]) {         ans = min(ans, dp(l + 1, k - 1) + dp(k + 1, r));       }     ans3 = 1 + dp(l + 1, r);     return mem[l][r] = min({ans, ans2, ans3});   }   int main() {     memset(mem, -1, sizeof mem);     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %d , &a[i]);     printf( %d n , dp(1, n));     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     for (int i = 0; i < n * n / 2; i++) {       cout << i + 1 <<     << n * n - i;       if (2 * (i + 1) % n == 0)         cout << endl;       else         cout <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 10, mod = 1e9 + 7;   int n, m, q, top, col, cnt, st[N], len[N], size[N], dfn[N], bl[N], son[N], S[N],       w[N], T[N], low[N], bin[N], dep[N], f[N];   char sc[N][10];   vector<int> e[N], v[N];   int read() {     int x = 0, f = 1;     char c = getchar();     while (c <  0  || c >  9 ) {       if (c ==  - ) f = -1;       c = getchar();     }     while (c >=  0  && c <=  9 )       x = (x << 3) + (x << 1) + (c ^ 48), c = getchar();     return f * x;   }   void Tarjan(int x) {     dfn[x] = low[x] = ++cnt;     st[++top] = x;     for (int i = 0; i < e[x].size(); i++) {       int y = e[x][i];       if (!dfn[y]) {         Tarjan(y);         low[x] = min(low[x], low[y]);         if (low[y] == dfn[x]) {           if (st[top] == y) {             top--;             v[x].push_back(y);             v[y].push_back(x);             continue;           }           size[++col] = 1;           while (1) {             int z = st[top--];             size[col]++;             bl[z] = col;             v[z].push_back(col);             v[col].push_back(z);             if (z == y) break;           }           bl[x] = col;           v[x].push_back(col);           v[col].push_back(x);         }       } else         low[x] = min(low[x], dfn[y]);     }   }   void dfs(int x, int fa) {     S[x] = 1;     f[x] = fa;     dep[x] = dep[fa] + 1;     w[x] = w[fa];     if (size[x] > 2)       w[x]--;     else if (size[bl[x]] > 2)       w[x]++;     for (int i = 0; i < v[x].size(); i++) {       int y = v[x][i];       if (y == fa) continue;       dfs(y, x);       S[x] += S[y];       if (S[y] > S[son[x]]) son[x] = y;     }   }   void dfs2(int x, int fa) {     T[x] = fa;     if (!son[x]) return;     dfs2(son[x], fa);     for (int i = 0; i < v[x].size(); i++) {       int y = v[x][i];       if (T[y]) continue;       dfs2(y, y);     }   }   int lca(int x, int y) {     while (T[x] ^ T[y]) {       if (dep[T[x]] > dep[T[y]])         x = f[T[x]];       else         y = f[T[y]];     }     return dep[x] > dep[y] ? y : x;   }   int main() {     n = read(), m = read();     col = n;     for (int i = 1, x, y; i <= m; i++) {       x = read(), y = read();       e[x].push_back(y);       e[y].push_back(x);     }     Tarjan(1);     bin[0] = 1;     sc[0][len[0]++] =  1 ;     for (int i = 1; i <= col; i++) {       bin[i] = bin[i - 1] * 2 % mod;       int x = bin[i];       while (x) {         sc[i][len[i]++] =  0  + x % 10;         x /= 10;       }     }     dfs(1, 0);     dfs2(1, 1);     q = read();     for (int i = 1, x, y, z; i <= q; i++) {       x = read(), y = read();       z = lca(x, y);       z = w[x] + w[y] - w[z] - w[f[z]];       for (int j = len[z] - 1; j > -1; j--) putchar(sc[z][j]);       putchar(  n );     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     long long n, k;     cin >> n >> k;     vector<long long> div;     for (long long i = 1; i <= sqrt(n); i++) {       if (n % i == 0) {         if (n / i != i) {           div.push_back(n / i);         }         div.push_back(i);       }     }     sort(div.begin(), div.end());     if (k > div.size()) {       cout << -1 << endl;     } else {       cout << div[k - 1] << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 107;   const int M = 107 * 26;   const int inf = (int)1e9 + 7;   const long long base = (long long)1e18;   const double pi = acos(-1);   const double ep = 1e-18;   int dp[107][M];   void solve() {     dp[0][0] = 1;     for (int i = 1; i < N; i++) {       for (int j = 0; j < M; j++) {         for (int t = 0; t <= min(25, j); t++)           dp[i][j] = (dp[i][j] + dp[i - 1][j - t]) % inf;       }     }     int test;     cin >> test;     while (test-- > 0) {       string s;       cin >> s;       int sum = 0;       for (int i = 0; i < s.size(); i++) sum += s[i] -  a ;       cout << dp[s.size()][sum] - 1 << endl;     }   }   int main() {     solve();     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, k;     while (cin >> n >> k) {       set<int> s;       int l = 0, r = 0;       int ans[200005];       int a;       while (n--) {         cin >> a;         if (s.count(a)) {           continue;         }         s.insert(a);         ans[r++] = a;         if (r > k) {           s.erase(ans[l++]);         }       }       n = min(r - l, k);       cout << n <<   n ;       while (n--) {         cout << ans[--r] <<    ;       }       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const double EPS = 1E-10;   inline int cmp(double x, double y = 0, double tol = EPS) {     return (x <= y + tol) ? (x + tol < y) ? -1 : 0 : 1;   }   char M[440][440];   int C[440][440];   int dy[] = {-1, 0, 1, 0};   int dx[] = {0, -1, 0, 1};   int main() {     int N, K;     cin >> N >> K;     N += 200;     for (int i = 200; i < N; i++)       for (int j = 200; j < N; j++) cin >> M[i][j];     for (int i = 200; i < N; i++)       for (int j = 200; j < N; j++)         if (M[i][j] ==  . ) {           for (int d = 0; d < 4; d++) {             bool ok = 1;             int y = i, x = j;             for (int k = 0; k < K && ok; k++, y += dy[d], x += dx[d])               if (M[y][x] !=  . ) ok = 0;             y = i, x = j;             if (ok)               for (int k = 0; k < K; k++) C[y][x]++, y += dy[d], x += dx[d];           }         }     int rety = 0, retx = 0, ret = 0;     for (int i = 200; i < N; i++)       for (int j = 200; j < N; j++)         if (ret < C[i][j]) ret = C[i][j], rety = i - 200, retx = j - 200;     printf( %d %d n , rety + 1, retx + 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long powi(long long base, int p) {     long long res = 1;     while (p--) res *= base;     return res;   }   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   bool is_vowel(char c) {     c = tolower(c);     return c ==  a  || c ==  e  || c ==  o  || c ==  i  || c ==  u  || c ==  y ;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     int n;     cin >> n;     map<int, int> dict;     for (int i = 0, a; i < n; i++) {       cin >> a;       dict[a]++;     }     bool hh = false;     int res = 0;     for (auto p : dict) {       res += p.second / 4;       int c = p.second % 4;       if (c >= 2 && hh) {         res++;         hh = false;       } else if (c >= 2)         hh = true;     }     cout << res;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<long, long> cmap;   map<long, long>::iterator it;   bool visit[200005];   long c[200005];   vector<long> graph[200005];   long res = 0, mac, counter;   void DFS(long node);   int main(void) {     long long n, m, k, i, l, r, j;     cin >> n >> m >> k;     memset(visit, false, sizeof(visit));     for (i = 1; i <= n; i++) cin >> c[i];     for (i = 1; i <= m; i++) {       cin >> l >> r;       graph[l].push_back(r);       graph[r].push_back(l);     }     for (i = 1; i <= n; i++) {       if (!visit[i]) {         mac = 0;         counter = 0;         DFS(i);         for (it = cmap.begin(); it != cmap.end(); it++) {           mac = max(mac, it->second);         }         res += counter - mac;         cmap.clear();       }     }     cout << res;     return 0;   }   void DFS(long node) {     visit[node] = true;     counter++;     cmap[c[node]]++;     long j, i;     for (i = 0; i < graph[node].size(); i++) {       j = graph[node][i];       if (!visit[j]) DFS(j);     }   }
#include <bits/stdc++.h>   int f[2010][2010][2];   int n, m;   int sr[2010][2010], sc[2010][2010];   int gr[2010], gc[2010];   int br[2010][2010], bc[2010][2010];   char s[2010][2010];   int main() {     scanf( %d%d , &n, &m);     for (int i = 1; i <= n; i++) scanf( %s , s[i] + 1);     if (n == 1 && m == 1) return 0 * puts( 1 );     for (int i = 1; i <= n; i++) gr[i] = 1;     for (int j = 1; j <= m; j++) gc[j] = 1;     for (int i = n; i >= 1; i--)       for (int j = m; j >= 1; j--) {         br[i][j] = br[i][j + 1] + (s[i][j] ==  R );         bc[i][j] = bc[i + 1][j] + (s[i][j] ==  R );       }     f[1][1][0] = f[1][1][1] = sr[1][1] = sc[1][1] = 1;     for (int i = 1; i <= n; i++)       for (int j = 1; j <= m; j++) {         if (i == 1 && j == 1) continue;         while (gr[i] < j && br[i][gr[i] + 1] > m - j) gr[i]++;         f[i][j][0] = (sr[i][j - 1] - sr[i][gr[i] - 1] + 1000000007) % 1000000007;         while (gc[j] < i && bc[gc[j] + 1][j] > n - i) gc[j]++;         f[i][j][1] = (sc[i - 1][j] - sc[gc[j] - 1][j] + 1000000007) % 1000000007;         sr[i][j] = (sr[i][j - 1] + f[i][j][1]) % 1000000007;         sc[i][j] = (sc[i - 1][j] + f[i][j][0]) % 1000000007;       }     printf( %d n , (f[n][m][0] + f[n][m][1]) % 1000000007);   }
#include <bits/stdc++.h>   using namespace std;   template <class T1, class T2>   inline istream& operator>>(istream& fin, pair<T1, T2>& pr) {     fin >> pr.first >> pr.second;     return fin;   }   template <class T0, class T1, class T2>   inline istream& operator>>(istream& fin, tuple<T0, T1, T2>& t) {     fin >> get<0>(t) >> get<1>(t) >> get<2>(t);     return fin;   }   template <class T>   inline istream& operator>>(istream& fin, vector<T>& a) {     if (!a.size()) {       size_t n;       fin >> n;       a.resize(n);     }     for (auto& u : a) fin >> u;     return fin;   }   string probB() {     vector<string> a(2);     cin >> a;     const auto n = a[0].length();     size_t diff = 0;     for (size_t i = 0; i < n; ++i) diff += a[0][i] != a[1][i];     if (diff & 1) return  impossible ;     string str;     size_t k = 0;     for (size_t i = 0; i < n; ++i)       if (a[0][i] == a[1][i])         str.push_back(a[0][i]);       else if (2 * k < diff)         str.push_back(a[0][i]), ++k;       else         str.push_back(a[1][i]);     return str;   }   int main(const int argc, char* argv[]) {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     ;     cout << probB();     return EXIT_SUCCESS;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   int head[maxn], deg[maxn], ans[maxn], tot = 1;   struct edge {     int v, nex, id;     bool f;   } e[maxn << 4];   void add(int u, int v, int id) {     e[++tot] = {v, head[u], id, 0}, head[u] = tot;     e[++tot] = {u, head[v], id, 0}, head[v] = tot;   }   struct Q {     int l, r;   } q[maxn];   vector<int> a;   void euler(int u) {     for (int &i = head[u]; i; i = e[i].nex) {       if (e[i].f) continue;       e[i].f = e[i ^ 1].f = 1;       int v = e[i].v;       ans[e[i].id] = (u < v);       euler(v);     }   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int n;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> q[i].l >> q[i].r;       q[i].r++;       a.push_back(q[i].l);       a.push_back(q[i].r);     }     sort(a.begin(), a.end());     a.erase(unique(a.begin(), a.end()), a.end());     for (int i = 0; i < n; i++) {       q[i].l = lower_bound(a.begin(), a.end(), q[i].l) - a.begin();       q[i].r = lower_bound(a.begin(), a.end(), q[i].r) - a.begin();       deg[q[i].l]++, deg[q[i].r]++;       add(q[i].l, q[i].r, i);     }     int len = a.size(), last = -1;     for (int i = 0; i < len; i++) {       if (deg[i] & 1) {         if (last == -1)           last = i;         else           add(last, i, n + 3), last = -1;       }     }     for (int i = 0; i < len; i++)       if (head[i]) euler(i);     for (int i = 0; i < n; i++) cout << ans[i] <<    ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string LexicographicallyMaximumSubsequence(string text) {     string localMax =   ;     char current = text.at(text.length() - 1);     localMax += current;     unsigned int size = text.length() - 2;     for (int i = size; i >= 0; i--) {       if (text.at(i) >= current) {         localMax = text.at(i) + localMax;         current = text.at(i);       }     }     return localMax;   }   int main() {     string text;     cin >> text;     cout << LexicographicallyMaximumSubsequence(text) << endl;     return 0;   }
#include <bits/stdc++.h>   int main() {     long int n, c = 0, d = 0, e = 0, f = 0, t, i, j;     scanf( %ld , &n);     int a[n], b[n];     for (i = 1; i <= n; i++) {       scanf( %d %d , &a[i], &b[i]);       if (a[i] < 0)         c++;       else         d++;     }     for (i = 1; i < n; i++) {       for (j = i + 1; j <= n; j++) {         if (a[i] > a[j]) {           t = a[i];           a[i] = a[j];           a[j] = t;           t = b[i];           b[i] = b[j];           b[j] = t;         }       }     }     if (c < d) {       for (i = 1; i <= 2 * c; i++) {         e += b[i];         f += b[i];       }       f += b[2 * c + 1];     } else if (c > d) {       for (i = c - d + 1; i <= c + d; i++) {         e += b[i];         f += b[i];       }       f += b[c - d];     } else {       for (i = 1; i <= c + d; i++) e += b[i];     }     if (e >= f)       printf( %d , e);     else       printf( %d , f);   }
#include <bits/stdc++.h>   using namespace std;   struct Binode {     Binode *z, *o;     void init() {       z = NULL;       o = NULL;     }   } T;   int n;   long long a[100010], o[100010], s[70];   void add(long long x) {     for (int i = 0; i < 61; i++) {       s[i] = x & 1;       x >>= 1;     }     Binode *p = &T;     for (int i = 60; i >= 0; i--) {       if (s[i]) {         if (!(p->o)) {           p->o = new Binode;           p->o->init();         }         p = p->o;       } else {         if (!(p->z)) {           p->z = new Binode;           p->z->init();         }         p = p->z;       }     }   }   long long find(long long x) {     for (int i = 0; i < 61; i++) {       s[i] = x & 1;       x >>= 1;     }     long long ans = 0;     Binode *p = &T;     for (int i = 60; i >= 0; i--) {       if (s[i]) {         if (p->z) {           ans ^= (1LL << i);           p = p->z;         } else           p = p->o;       } else {         if (p->o) {           ans ^= (1LL << i);           p = p->o;         } else           p = p->z;       }     }     return ans;   }   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) scanf( %I64d , &a[i]);     o[0] = 0;     for (int i = 1; i <= n; i++) o[i] = o[i - 1] ^ a[i];     long long x = 0, ans = 0;     T.init();     for (int i = n; i >= 0; i--) {       add(x);       ans = max(ans, find(o[i]));       x = x ^ a[i];     }     printf( %I64d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   vector<vector<int>> a;   bool inp(int i, int j) {     (i < n && j == n) && inp(i + 1, 0);     (i < n && j < n) && (cin >> a[i][j]);     (i < n && j < n) && (inp(i, j + 1));     return 0;   }   bool floyd(int k, int i, int j) {     (k < n && i < n && j == n) && floyd(k, i + 1, 0);     (k < n && i == n) && floyd(k + 1, 0, 0);     (k < n && i < n && j < n) && floyd(k, i, j + 1);     (k < n && i < n && j < n) && (a[i][j] = min(a[i][j], a[i][k] + a[k][j]));     return 0;   }   void go(int &ans, int i) {     ans = max(ans, *max_element(begin(a[i]), end(a[i])));   }   int main() {     cin.tie(nullptr);     ios_base::sync_with_stdio(0);     cin >> n;     a.assign(20, vector<int>(20));     inp(0, 0);     floyd(0, 0, 0);     int ans = 0;     go(ans, 0);     go(ans, 1);     go(ans, 2);     go(ans, 3);     go(ans, 4);     go(ans, 5);     go(ans, 6);     go(ans, 7);     go(ans, 8);     go(ans, 9);     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int T;   long long n, m;   inline long long Fin(long long w) {     if (!w) return m * (m + 1) / 2;     long long p = w / m, x = Fin(p), t = w % m;     return m * ((m * m + 1) * p + m * t) + m * (m + 1) / 2 +            max(0ll, min(m, (m * m + 1) * p + t * m + m - x + 1));   }   int main() {     scanf( %d , &T);     while (T--) {       scanf( %lld%lld , &n, &m);       long long p = (n - 1) / (m * m + 1), x = Fin(p),                 v1 = (p % m + 1 + m * (p / m)), o = (n - 1) % (m * m + 1);       printf( %lld n , (n == x)                            ? (v1 * (m + 1))                            : (n + p * m + ((o - (n > x)) / m) - v1 + (x > n)));     }     return 0;   }