malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   void read(long long &t) {     char c;     while ((c = getchar()) <  0  || c >  9 )       ;     t = c -  0 ;     while ((c = getchar()) >=  0  && c <=  9 ) (t *= 10) += c -  0 ;   }   static const long long N = 100010;   long long a[N], cnt[45];   bool is_power(long long t) { return !(t - (t & -t)); }   long long get_cnt(long long t) {     long long ret = -1;     while (t) t >>= 1, ++ret;     return ret;   }   signed main() {     long long n;     read(n);     for (long long i = 0; i < n; ++i) read(a[i]);     for (long long i = 0; i < n; ++i)       if (is_power(a[i])) ++cnt[get_cnt(a[i])];     long long mx = cnt[0], mn = 0;     for (long long i = n - 1; i >= 0; --i)       if (!is_power(a[i])) {         long long c = get_cnt(a[i]);         for (long long j = 44; j >= c; --j)           if (cnt[j] > 0) {             for (long long k = j; ~k; --k) --cnt[k];             goto find;           }         printf( -1 n );         return 0;       find:         ++mn;       }     while (cnt[0] > 0)       for (long long i = 1; i < 45; ++i)         if (cnt[i] <= 0) {           for (long long j = 0; j < i; ++j) --cnt[j];           long long mx = 0, p = -1;           for (long long j = i - 1; j >= 0; --j)             if (cnt[j] > mx) mx = cnt[p = j];           if (mx) --cnt[p];           ++mn;           break;         }     for (long long i = 0; i < 45; ++i)       if (cnt[i]) {         printf( -1 n );         return 0;       }     for (long long i = mn; i <= mx; ++i) printf( %lld  , i);     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   const int Mod = 1000000007;   const int INF = 0x3f3f3f3f;   const long long LL_INF = 0x3f3f3f3f3f3f3f3f;   const double e = exp(1);   const double PI = acos(-1);   const double ERR = 1e-10;   int head[maxn], cnt, n, tot, mstnum;   int dfn[maxn], father[maxn], rank_[maxn], sz[maxn];   pair<int, int> range[maxn];   long long c[maxn];   bool mark[maxn];   set<int> se;   unordered_map<long long, int> mp;   struct node {     int v, nxt;   } Edge[2 * maxn];   struct edge {     int u, v, id;     long long w;     edge() {}     edge(int _id, int _u, int _v, long long _w) : id(_id), u(_u), v(_v), w(_w) {}   } mst[2 * maxn];   bool operator<(edge a, edge b) { return a.w < b.w; }   vector<edge> ve[maxn];   void init() {     for (int i = 0; i <= n; i++) {       head[i] = -1;       father[i] = i;       rank_[i] = 0;     }     cnt = 0;     tot = 0;     mstnum = 0;   }   void addedge(int u, int v) {     Edge[cnt].v = v;     Edge[cnt].nxt = head[u];     head[u] = cnt++;   }   void dfs(int id, int fa) {     sz[id] = 1;     range[id].first = 0x3f3f3f3f;     range[id].second = -1;     for (int i = head[id]; i != -1; i = Edge[i].nxt) {       int v = Edge[i].v;       if (v == fa) continue;       dfs(v, id);       range[id].first = min(range[id].first, range[v].first);       range[id].second = max(range[id].second, range[v].second);       sz[id] += sz[v];     }     if (sz[id] == 1) {       dfn[id] = ++tot;       range[id].first = range[id].second = tot;     }   }   int find(int id) {     return father[id] == id ? id : father[id] = find(father[id]);   }   void merge(int x, int y) {     x = find(x);     y = find(y);     if (x != y) {       if (rank_[x] > rank_[y])         father[y] = x;       else {         father[x] = y;         if (rank_[x] == rank_[y]) rank_[y]++;       }     }   }   int main() {     int u, v;     scanf( %d , &n);     init();     for (int i = 1; i <= n; i++) {       scanf( %lld , &c[i]);     }     for (int i = 1; i < n; i++) {       scanf( %d%d , &u, &v);       addedge(u, v);       addedge(v, u);     }     dfs(1, -1);     long long ans = 0;     int num = 0;     for (int i = 1; i <= n; i++) {       mst[i] = edge(i, range[i].first - 1, range[i].second, c[i]);     }     sort(mst + 1, mst + 1 + n);     for (int i = 1; i <= n; i++) {       if (mp.find(mst[i].w) == mp.end()) {         mp[mst[i].w] = ++num;       }       ve[mp[mst[i].w]].push_back(mst[i]);     }     for (int i = 1; i <= num; i++) {       for (auto ed : ve[i]) {         int x = find(ed.u);         int y = find(ed.v);         int id = ed.id;         if (x != y) {           se.insert(id);         }       }       for (auto ed : ve[i]) {         int x = find(ed.u);         int y = find(ed.v);         if (x != y) {           ans += ed.w;           merge(x, y);         }       }     }     printf( %lld %d n , ans, (int)se.size());     for (auto x : se) {       printf( %d  , x);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int N, Q, T;   bool matr[300][300];   int amounts[300];   vector<int> chains[300];   bool was[300];   int sorted[300];   int s = 0;   long long mas[2][100010];   void srt(int v) {     was[v] = true;     for (int i = 0; i < N; i++)       if (!was[i] && matr[v][i]) srt(i);     sorted[s] = v;     s++;   }   bool z = false;   void f(int v, int c) {     was[v] = true;     for (int i = 0; i < N; i++)       if (matr[v][i]) {         if (was[i])           z = true;         else           f(i, c);       }     chains[c].push_back(v);   }   int main() {     scanf( %d%d%d , &N, &Q, &T);     for (int i = 0; i < N; i++) scanf( %d , &amounts[i]);     for (int i = 0; i < 300; i++)       for (int j = 0; j < 300; j++) matr[i][j] = false;     for (int i = 0; i < Q; i++) {       int a, b;       scanf( %d%d , &a, &b);       a--;       b--;       matr[a][b] = true;     }     for (int i = 0; i < 300; i++) was[i] = false;     for (int i = 0; i < N; i++)       if (!was[i]) srt(i);     reverse(sorted, sorted + N);     for (int i = 0; i < 300; i++) was[i] = false;     int cCnt = 0;     for (int i = 0; i < N; i++)       if (!was[sorted[i]]) {         f(sorted[i], cCnt);         cCnt++;       }     for (int i = 0; i < cCnt; i++) {       for (int j = chains[i].size() - 1; j >= 0; j--) {         chains[i][j] = amounts[chains[i][j]];         if (j + 1 < chains[i].size()) chains[i][j] += chains[i][j + 1];       }     }     if (z) {       printf( %d n , 0);       return 0;     }     memset(mas, 0, sizeof(long long) * 100010 * 2);     mas[0][0] = 1;     int index = 0;     for (int i = 0; i < cCnt; i++) {       for (int j = 0; j < chains[i].size(); j++) {         int amount = chains[i][j];         for (int k = 0; k < 100010; k++) mas[(index + 1) % 2][k] = 0;         for (int k = 0; k < 100010; k++) {           if (k - amount >= 0)             mas[index % 2][k] =                 (mas[index % 2][k] + mas[index % 2][k - amount]) % 1000000007;           if (j == 0)             mas[(index + 1) % 2][k] = mas[index % 2][k];           else if (k - amount >= 0)             mas[(index + 1) % 2][k] = mas[index % 2][k - amount];         }         index++;       }     }     cout << mas[index % 2][T] <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   map<long long int, vector<long long int>> v;   map<long long int, long long int> vis;   void dfs(long long int n) {     long long int i;     cout << n <<    ;     vis[n] = 1;     for (i = 0; i < v[n].size(); i++) {       if (!vis[v[n][i]]) {         vis[v[n][i]] = 1;         dfs(v[n][i]);       }     }   }   int main() {     long long int n, i, j, k, p, q, ans = 0;     cin >> n;     map<long long int, long long int> make_pair;     for (i = 1; i <= n; i++) {       cin >> p >> q;       v[p].push_back(q);       v[q].push_back(p);       make_pair[p]++;       make_pair[q]++;     }     for (auto it : make_pair) {       if (it.second == 1) {         ans = it.first;         break;       }     }     dfs(ans);   }
#include <bits/stdc++.h>   using namespace std;   int n, m, x, y;   int p[100010], q[100010];   vector<int> t[100010], v[100010];   int mn[100010];   bool flag[100010];   inline int dist(int x, int y) {     if (p[x] - q[y] >= 0)       return p[x] - q[y];     else       return q[y] - p[x];   }   int main() {     scanf( %d%d%d%d , &n, &m, &x, &y);     memset(mn, 63, sizeof(mn));     for (int i = 1; i <= n; i++) scanf( %d , &p[i]);     for (int i = 1; i <= m; i++) scanf( %d , &q[i]);     q[m + 1] = 1e9;     q[0] = -1e9;     int pos = 1;     for (int i = 1; i <= n; i++) {       while (pos <= m && q[pos] <= p[i]) pos++;       pos--;       if (dist(i, pos) == dist(i, pos + 1)) {         t[i].push_back(pos);         t[i].push_back(pos + 1);       } else if (dist(i, pos) < dist(i, pos + 1)) {         t[i].push_back(pos);       } else {         t[i].push_back(pos + 1);       }     }     for (int i = 1; i <= n; i++)       if (t[i].size() == 1) {         int t1 = t[i][0];         v[t1].push_back(i);         mn[t1] = min(mn[t1], dist(i, t1));       }     for (int i = 1; i <= n; i++)       if (t[i].size() == 1) {         if (dist(i, t[i][0]) > mn[t[i][0]]) flag[i] = 1;       }     for (int i = 1; i <= n; i++)       if (t[i].size() == 2) {         bool flag1 = 0, flag2 = 0;         int cnt1 = 0, cnt2 = 0;         int t1 = t[i][0], t2 = t[i][1], d = dist(i, t1);         for (int j = 0; j < v[t1].size(); j++) {           if (dist(v[t1][j], t1) < d) flag1 = 1;           if (d < dist(v[t1][j], t1))             if (flag[v[t1][j]] == 0) cnt1++;         }         for (int j = 0; j < v[t2].size(); j++) {           if (dist(v[t2][j], t2) < d) flag2 = 1;           if (d < dist(v[t2][j], t2))             if (flag[v[t2][j]] == 0) cnt2++;         }         if (cnt1 + flag1 <= cnt2 + flag2) {           if (flag1) flag[i] = 1;           for (int j = 0; j < v[t1].size(); j++) {             if (d < dist(v[t1][j], t1))               if (flag[v[t1][j]] == 0) flag[v[t1][j]] = 1;           }           mn[t1] = min(mn[t1], dist(i, t1));           v[t1].push_back(i);         } else {           if (flag2) flag[i] = 1;           for (int j = 0; j < v[t2].size(); j++) {             if (d < dist(v[t2][j], t2))               if (flag[v[t2][j]] == 0) flag[v[t2][j]] = 1;           }           mn[t2] = min(mn[t2], dist(i, t2));           v[t2].push_back(i);         }       }     int ans = 0;     for (int i = 1; i <= n; i++) ans += flag[i];     printf( %d , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int mx[4] = {0, 0, 1, -1};   int my[4] = {-1, 1, 0, 0};   int n, m, cnt, cou;   char a[55][55];   bool used[55][55];   bool good(int x, int y) { return (x >= 0 && x < n && y >= 0 && y < m); }   void dfs(int i, int j) {     used[i][j] = true;     cou++;     for (int k = 0; k < 4; k++)       if (good(i + mx[k], j + my[k]) && a[i + mx[k]][j + my[k]] ==  #  &&           !used[i + mx[k]][j + my[k]])         dfs(i + mx[k], j + my[k]);   }   int main() {     scanf( %d%d n , &n, &m);     for (int i = 0; i < n; i++) {       scanf( %s n , &a[i]);       for (int j = 0; j < m; j++)         if (a[i][j] ==  # ) cnt++;     }     if (cnt <= 2)       printf( -1 n );     else {       int ans = 2;       for (int i = 0; i < n; i++)         for (int j = 0; j < m; j++)           if (a[i][j] ==  # ) {             a[i][j] =  . ;             cou = 0;             memset(used, 0, sizeof(used));             for (int si = 0; si < n; si++)               for (int sj = 0; sj < m; sj++)                 if (a[si][sj] ==  # ) dfs(si, sj), si = n, sj = m;             if (cou != cnt - 1) ans = 1;             a[i][j] =  # ;           }       printf( %d n , ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   template <class T>   inline T abs(T a) {     return a > 0 ? a : -a;   }   int n;   int m;   int main() {     int last;     cin >> n >> last;     if (n == 1) {       if (last == 1) {         cout << 0 << endl <<  T  << endl;       } else {         cout <<  IMPOSSIBLE  << endl;       }       return 0;     }     int ans = -1;     int errans = 1e9;     for (int i = 1; i <= last; i++) {       int a = last, b = i;       int err = 0, len = 0;       while (b) {         err += (a / b) - 1;         len += a / b;         a %= b;         swap(b, a);       }       if (a != 1) continue;       err--;       if (len == n) {         if (errans > err) {           errans = err;           ans = i;         }       }     }     if (ans < 0) {       cout <<  IMPOSSIBLE  << endl;       return 0;     }     vector<int> res;     int a = last, b = ans;     while (b) {       res.push_back(a / b);       a %= b;       swap(b, a);     }     reverse((res).begin(), (res).end());     res[0]--;     cout << errans << endl;     cout <<  T ;     for (int i = 0; i < (((int)(res).size())); i++)       for (int j = 0; j < (res[i]); j++) printf( %c , ((i + 1) % 2 ?  B  :  T ));     printf(  n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int RLEN = 1 << 18 | 1;   inline char nc() {     static char ibuf[RLEN], *ib, *ob;     (ib == ob) && (ob = (ib = ibuf) + fread(ibuf, 1, RLEN, stdin));     return (ib == ob) ? -1 : *ib++;   }   inline int rd() {     char ch = nc();     int i = 0, f = 1;     while (!isdigit(ch)) {       if (ch ==  - ) f = -1;       ch = nc();     }     while (isdigit(ch)) {       i = (i << 1) + (i << 3) + ch -  0 ;       ch = nc();     }     return i * f;   }   inline void W(long long x) {     static int buf[50];     if (!x) {       putchar( 0 );       return;     }     if (x < 0) {       putchar( - );       x = -x;     }     while (x) {       buf[++buf[0]] = x % 10;       x /= 10;     }     while (buf[0]) putchar(buf[buf[0]--] +  0 );   }   const int N = 4e5 + 50, M = 1e7 + 50;   int n, a[N], b[N], li[N], ri[N];   int rt[N], lc[M], rc[M], c1[M], tot;   long long s1[M], s2[M], ans;   inline int ga(int *anc, int x) {     return (anc[x] == x) ? x : (anc[x] = ga(anc, anc[x]));   }   inline void upt(int k) {     c1[k] = c1[lc[k]] + c1[rc[k]];     s1[k] = s1[rc[k]] + s1[lc[k]] + c1[rc[k]] * s2[lc[k]];     s2[k] = s2[lc[k]] + s2[rc[k]];   }   inline void inc(int &k, int l, int r, int p) {     if (!k) k = ++tot;     if (l == r) {       c1[k] = 1;       s1[k] = s2[k] = p;       return;     }     int mid = (l + r) >> 1;     if (p <= mid)       inc(lc[k], l, mid, p);     else       inc(rc[k], mid + 1, r, p);     upt(k);   }   inline int merge(int x, int y) {     if (!x || !y) return (x + y);     lc[x] = merge(lc[x], lc[y]);     rc[x] = merge(rc[x], rc[y]);     upt(x);     return x;   }   inline void inc(int x, int y) {     int r = ga(ri, x);     li[r + 1] = r;     ri[r] = ri[r + 1] = r + 1;     ans -= s1[rt[x]] + (long long)(x - 1) * s2[rt[x]];     inc(rt[x], 1, n, y);     ans += s1[rt[x]] + (long long)(x - 1) * s2[rt[x]];   }   inline void _merge(int x, int y) {     ans -= s1[rt[x]] + (long long)(x - 1) * s2[rt[x]];     ans -= s1[rt[y]] + (long long)(y - 1) * s2[rt[y]];     rt[x] = merge(rt[x], rt[y]);     ans += s1[rt[x]] + (long long)(x - 1) * s2[rt[x]];     ri[y - 1] = y;     li[y] = y - 1;   }   int main() {     n = rd();     for (int i = 1; i <= n; i++) {       a[i] = rd(), b[i] = rd();       ans -= (long long)a[i] * b[i];       if (li[a[i] - 1]) {         inc(ga(li, a[i] - 1), b[i]);       } else {         if (!li[a[i]]) {           li[a[i]] = ri[a[i]] = a[i];           inc(rt[a[i]], 1, n, b[i]);           ans += s1[rt[a[i]]] + (a[i] - 1) * s2[rt[a[i]]];         } else           inc(a[i], b[i]);       }       while (li[ga(ri, a[i]) + 1]) _merge(ga(li, a[i]), ga(ri, a[i]) + 1);       W(ans);       putchar(  n );     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 24;   int f[1 << MAXN], a[MAXN], N;   vector<pair<int, int> > p[MAXN];   int dp(int state, int pos) {     if (f[state] != -1) return f[state];     if (state == 1) return 1;     if (p[pos].size() == 0) {       f[state] = 0x3F3F3F3F;       return f[state];     }     int tot = 0, flag = 0;     for (int i = 0; i <= pos; i++)       if ((state >> i) & 1) tot++;     int ret = 0x3F3F3F3F;     for (int i = 0; i < p[pos].size(); i++) {       int nxtstate = ((state & (~(1 << pos))) | (1 << pos - 1) |                       (1 << p[pos][i].first) | (1 << p[pos][i].second));       ret = min(dp(nxtstate, pos - 1), ret);     }     return f[state] = max(ret, tot);   }   int main() {     scanf( %d , &N);     memset(f, -1, sizeof(f));     for (int i = 0; i < N; i++) scanf( %d , a + i);     for (int i = 1; i < N; i++) {       for (int j = 0; j < i; j++)         for (int k = j; k < i; k++)           if (a[j] + a[k] == a[i]) p[i].push_back(make_pair(j, k));     }     int ans = dp(1 << (N - 1), N - 1);     if (ans == 0x3F3F3F3F)       printf( -1 n );     else       printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   string A, B;   int has[10];   string res;   void Create(int from) {     for (int i = 0; i < 10; i++) {       for (int j = 0; j < has[i]; j++) {         res[from++] = i +  0 ;       }     }   }   int main() {     cin >> A >> B;     int st = 0;     for (int i = 0; i < A.length(); i++) has[A[i] -  0 ]++;     while (A.length() < B.length()) {       A =  0  + A;       st++;     }     res = A;     for (int i = st; i < A.length(); i++) {       for (int j = 10 - 1; j >= 0; j--) {         if (has[j] > 0) {           has[j]--;           res[i] = j +  0 ;           Create(i + 1);           if (res <= B) break;           has[j]++;         }       }     }     int pos = 0;     while (res[pos] ==  0 ) pos++;     cout << res.substr(pos) << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   mt19937 rnd(chrono::system_clock::now().time_since_epoch().count());   const int MAXN = 200100, MAXK = 25;   const long long MOD = 998244353;   int st[MAXN][MAXK];   int n, q;   int a[MAXN];   pair<int, int> lastP[MAXN];   int lg[MAXN];   void buildST() {     for (int j = (1); j <= (MAXK); j++)       for (int i = 0; i + (1 << j) <= q; i++)         st[i][j] = max(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);   }   int getMax(int l, int r) {     int p = lg[r - l + 1];     return max(st[l][p], st[r - (1 << p) + 1][p]);   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(nullptr);     cin >> n;     for (int i = (1); i <= (n); i++) cin >> a[i];     for (int i = (1); i <= (n); i++) lastP[i] = {a[i], -1};     cin >> q;     for (int i = (2); i <= (q); i++) lg[i] = lg[i / 2] + 1;     for (int i = (0); i <= (q - 1); i++) {       int t;       cin >> t;       if (t == 1) {         int p, x;         cin >> p >> x;         lastP[p] = {x, i};       } else {         int x;         cin >> x;         st[i][0] = x;       }     }     buildST();     for (int i = (1); i <= (n); i++) {       int cur = lastP[i].first, wh = lastP[i].second;       cur = max(cur, getMax(wh + 1, q - 1));       cout << cur <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long maxn = 1e5 + 1;   long long n, a[maxn], b[maxn], f1, f2;   signed main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     cin >> n;     for (long long i = 0; i < n; ++i) cin >> a[i];     for (long long i = 0; i < n; ++i) cin >> b[i];     long long max1 = a[0], max2 = b[0];     f1 = a[0];     f2 = b[0];     for (long long i = 1; i < n; ++i) {       f1 = max2 + a[i];       f2 = max1 + b[i];       max1 = max(max1, f1);       max2 = max(max2, f2);     }     cout << max(max1, max2) <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2010;   int n, m, k, p, q, d[maxn][maxn];   bool vis[maxn][maxn];   bool solve(int x, int y, int r) {     for (int i = max(1, x - r); i <= min(n, x + r); i++) {       int tmp = r - abs(i - x);       if (y - tmp >= 1 && !vis[i][y - tmp]) {         p = i, q = y - tmp;         return 1;       }       if (y + tmp <= m && !vis[i][y + tmp]) {         p = i, q = y + tmp;         return 1;       }     }     return 0;   }   int main() {     scanf( %d %d %d , &n, &m, &k);     for (int x, y, i = 1; i <= k; i++) {       scanf( %d %d , &x, &y);       for (int j = -1; j <= 1; j++)         for (int k = -1; k <= 1; k++) {           if (x + j < 1 || x + j > n || y + j < 1 || y + j > m) continue;           d[x][y] = max(d[x][y], d[x + j][y + k] - abs(j) - abs(k));         }       while (!solve(x, y, d[x][y])) d[x][y]++;       printf( %d %d n , p, q);       vis[p][q] = 1;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int pro(int *a, int *b, int t, int s) {     int k = 0;     for (int i = 1; i < t; i++) {       if (b[i] != s) {         return 2;       }       if (b[i] == 0) {         k++;       }     }     if (k == (t - 1)) {       return 3;     }     return 1;   }   int main() {     int t, a[105], b[105];     a[0] = 0;     cin >> t;     for (int i = 1; i <= t; i++) {       cin >> a[i];       b[i - 1] = a[i] - a[i - 1];     }     if (t == 1) {       cout << a[1];     }     int c = pro(a, b, t, b[1]);     if (c == 3) {       cout << a[1];     } else if (c == 1) {       cout << a[t] + b[1];     } else {       cout << a[t];     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1e9 + 7;   const int maxn = 1e6 + 5;   const int inf = 0x3f3f3f3f;   const long long INF = 0x3f3f3f3f3f3f3f3f;   const double eps = 1e-8;   const double PI = acos(-1.0);   long long dp[1005][105];   long long d, res, tot = 9, tmp = 1;   int main() {     int n, k, m;     scanf( %d%d%d , &n, &k, &m);     for (int i = 1; i <= n; i++) {       for (int j = 0; j < k; j++) {         for (int t = 0 + (i == n); t <= 9; t++) {           d = (t * tmp + j) % k;           if (d) dp[i][d] = (dp[i][d] + dp[i - 1][j] + (j == 0)) % m;         }       }       tmp = (tmp * 10) % k;     }     for (int i = 1; i < n; i++) tot = tot * 10 % m;     for (int i = 1; i < k; i++) res = (res + dp[n][i]) % m;     printf( %lld n , (m + tot - res) % m);     return 0;   }