malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int main() {     map<int, int> pos1;     map<int, int> pos2;     int n;     cin >> n;     int a[n], b[n];     for (int i = 0; i < n; i++) {       cin >> a[i];       pos1[a[i]] = i;     }     for (int i = 0; i < n; i++) {       cin >> b[i];       pos2[b[i]] = i;     }     map<int, int> h;     for (int i = 1; i <= n; i++) {       if (pos1[i] <= pos2[i]) {         h[pos2[i] - pos1[i]]++;       } else {         h[n - pos1[i] + pos2[i]]++;       }     }     int mx = 0;     for (int i = 0; i <= 2 * n; i++) {       if (h[i] > mx) mx = h[i];     }     cout << mx;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 51000;   int a[1000000], b[1000000], t[1000000], n, flag = 0;   void dfs(int m) {     if (flag) return;     if (m == n) {       cout <<  YES  << endl;       for (int i = 1; i < n; i++) {         cout << t[i] <<    ;       }       cout << t[n] << endl;       flag = 1;       return;     }     for (int i = 0; i <= 3; i++) {       t[m + 1] = i;       if (a[m] == (t[m + 1] | t[m]) && b[m] == (t[m + 1] & t[m])) {         dfs(m + 1);       }     }   }   int main() {     cin >> n;     for (int i = 1; i < n; i++) {       cin >> a[i];     }     for (int i = 1; i < n; i++) {       cin >> b[i];     }     for (int i = 0; i <= 3; i++) {       t[1] = i;       dfs(1);       if (flag) break;     }     if (!flag) cout <<  NO  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 155;   int a[N], n, k, s, oo = 1e9, dp[N][N * N], ans;   int main() {     cin >> n >> k >> s;     for (int i = 1; i <= n; i++) cin >> a[i];     s = min(s, n * (n - 1) / 2);     for (int i = 0; i <= n; i++)       for (int j = 0; j <= n * n; j++) dp[i][j] = oo;     dp[0][0] = 0;     for (int l = 1; l <= n; l++) {       for (int i = l; i; i--) {         for (int j = l - i; j <= i * n; j++) {           dp[i][j] = min(dp[i][j], dp[i - 1][j - (l - i)] + a[l]);         }       }     }     ans = dp[k][0];     for (int i = 0; i <= s; i++) ans = min(ans, dp[k][i]);     cout << ans << endl;   }
#include <bits/stdc++.h>   using namespace std;   int fun(pair<int, int> x, pair<int, int> y) { return x.second < y.second; }   int main() {     vector<pair<int, int> > vii;     int n, k;     cin >> n >> k;     for (int i = 0; i < n; i++) {       int x;       cin >> x;       vii.push_back(make_pair(x, x % 10));     }     int sum;     while (k > 0) {       sort(vii.begin(), vii.end(), fun);       sum = 0;       int flg = 1;       for (int i = 0; i < n; i++) {         if (vii[i].first == 100)           ;         else           flg = 0;       }       if (!flg) {         for (int i = n - 1; i >= 0; i--) {           if (vii[i].first >= 100) {             sum += 10;             continue;           }           if (k > 0) {             int ink = ((vii[i].first + 10) / 10) * 10 - vii[i].first;             if (ink > k) ink = k;             k = k - ink;             vii[i].first += ink;             vii[i].second = vii[i].first % 10;           }           sum += vii[i].first / 10;         }       } else {         sum = n * 10;         break;       }     }     printf( %d n , sum);     return 0;   }
#include <bits/stdc++.h>   int primes[] = {2,   3,   5,   7,   11,  13,  17,  19,  23,  29,  31,  37,  41,                   43,  47,  53,  59,  61,  67,  71,  73,  79,  83,  89,  97,  101,                   103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167,                   173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239,                   241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313,                   317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397,                   401, 409, 419, 421, 431, 433, 439, 443};   int mgcd = 0;   int n;   std::vector<int> a;   std::vector<std::vector<int>> e;   int work(int v, int p, int d, bool strict) {     if (d < 2) {       return 0;     }     bool ok = a[v] % d == 0;     if (ok) {       while (a[v] % d == 0) {         a[v] /= d;       }     }     if (strict && !ok) {       return 0;     }     int m1 = 0;     int m2 = 0;     std::vector<int> children = e[v];     for (std::vector<int>::const_iterator i = children.begin();          i != children.end(); ++i) {       if (*i != p) {         int ch = work(*i, v, d, strict);         if (ch > m2) {           m1 = m2;           m2 = ch;         } else if (ch > m1) {           m1 = ch;         }       }     }     if (ok && m1 + m2 + 1 > mgcd) {       mgcd = m1 + m2 + 1;     }     return ok ? m2 + 1 : 0;   }   int main() {     std::cin >> n;     a.resize(n);     e.resize(n);     for (int i = 0; i < n; ++i) {       std::cin >> a[i];     }     for (int i = 0; i < n - 1; ++i) {       int x, y;       std::cin >> x >> y;       --x;       --y;       e[x].push_back(y);       e[y].push_back(x);     }     for (int i = 0; i < sizeof(primes) / sizeof(int); ++i) {       int d = primes[i];       work(0, -1, d, false);     }     for (int i = 0; i < n; ++i) {       if (a[i] > 444) {         work(i, -1, a[i], true);       }     }     std::cout << mgcd << std::endl;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 2e5 + 5;   int ans[maxn];   struct node {     int l, r;     bool operator<(const node& a) const {       int s1 = r - l + 1;       int s2 = a.r - a.l + 1;       if (s1 == s2)         return a.l < l;       else         return a.r - a.l + 1 > r - l + 1;     }   };   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     int t;     cin >> t;     while (t--) {       int n;       cin >> n;       int tot = 0;       priority_queue<node> q;       q.push({1, n});       while (!q.empty()) {         node u = q.top();         q.pop();         int mid = (u.l + u.r) / 2;         ans[mid] = ++tot;         if (mid + 1 <= u.r) {           q.push({mid + 1, u.r});         }         if (mid - 1 >= u.l) {           q.push({u.l, mid - 1});         }       }       for (int i = 1; i <= n; i++) {         cout << ans[i] <<    ;       }       cout <<   n ;     }   }
#include <bits/stdc++.h>   const int mod = 1e9 + 7;   const int maxn = 1e6 + 3;   using namespace std;   int bj[maxn];   long long a[100010], f[maxn], inv[maxn], pri[maxn];   long long pre[maxn], per[maxn];   long long BM(long long k) {     pre[0] = 1;     per[0] = 1;     for (int i = 1; i <= maxn; i++) {       pre[i] = (k - i) % mod;       per[i] = (k - (maxn + 1 - i)) % mod;     }     for (int i = 1; i <= maxn; i++) {       pre[i] = pre[i - 1] * pre[i] % mod;       per[i] = per[i - 1] * per[i] % mod;     }     long long ans = 0;     for (int i = 1; i <= maxn; i++) {       if ((maxn - i) & 1)         ans = (ans -                f[i] * pre[i - 1] % mod * per[maxn - i] % mod * inv[i - 1] % mod *                    inv[maxn - i] % mod +                mod) %               mod;       else         ans = (ans + f[i] * pre[i - 1] % mod * per[maxn - i] % mod * inv[i - 1] %                          mod * inv[maxn - i] % mod) %               mod;     }     return ans;   }   int ksm(int a, int b) {     int res = 1;     while (b) {       if (b & 1) res = 1ll * res * a % mod;       a = 1ll * a * a % mod;       b >>= 1;     }     return res;   }   int main() {     inv[0] = 1;     inv[1] = 1;     for (int i = 2; i <= maxn; i++)       inv[i] = (mod - mod / i) * 1ll * inv[mod % i] % mod;     for (int i = 2; i <= maxn; i++) inv[i] = inv[i - 1] * inv[i] % mod;     int n, k;     scanf( %d%d , &n, &k);     if (n <= maxn) {       long long ans = 0;       for (int i = 1; i <= n; i++) {         ans += ksm(i, k);         ans %= mod;       }       printf( %lld n , ans);       return 0;     }     int cnt = 0;     f[0] = 1;     f[1] = 1;     for (int i = 2; i <= maxn; i++) {       if (!bj[i]) f[i] = ksm(i, k), pri[++cnt] = i;       for (int j = 1; j <= cnt && i * pri[j] <= maxn; j++) {         bj[i * pri[j]] = 1;         f[i * pri[j]] = f[i] * f[pri[j]] % mod;         if (i % pri[j] == 0) break;       }     }     for (int i = 2; i <= maxn; i++) f[i] = (f[i - 1] + f[i]) % mod;     printf( %lld n , BM(n));     return 0;   }
#include <bits/stdc++.h>   unsigned long long u64;   using namespace std;   int n, m, qr;   long long c[2 * 200000], cnt[2 * 200000];   void lazy_update(int v, int i, int j, int qi, int qj, long long x) {     if (qi > j || qj < i) return;     if (qi <= i && j <= qj) {       c[v] += (x + i - qi), ++cnt[v];       return;     }     int k = (i + j) / 2;     lazy_update(2 * v, i, k, qi, qj, x),         lazy_update(2 * v + 1, k + 1, j, qi, qj, x);   }   long long query(int v, int i, int j, int pos) {     if (pos < i || pos > j) return 0;     if (i == j) {       assert(pos == i);       return c[v];     }     int k = (i + j) / 2;     if (c[v] > 0) {       c[2 * v] += c[v], cnt[2 * v] += cnt[v];       c[2 * v + 1] += (c[v] + cnt[v] * (k + 1 - i)), cnt[2 * v + 1] += cnt[v];       c[v] = cnt[v] = 0;     }     return max(query(2 * v, i, k, pos), query(2 * v + 1, k + 1, j, pos));   }   int main() {     int i, j, k, l, r;     long long x;     while (cin >> n >> m >> qr) {       memset(c, 0, sizeof(c));       memset(cnt, 0, sizeof(cnt));       while (m--) {         cin >> l >> r >> x;         lazy_update(1, 1, n, l, r, x);       }       long long ax = 0;       while (qr--) {         cin >> i;         ax += query(1, 1, n, i);       }       cout << ax << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long int mod = 1e9 + 7;   const int size = 1e6 + 1;   int n;   double a[size];   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     ;     cin >> n;     double l, r, c1, c2;     l = r = c1 = c2 = 0;     string s;     cin >> s;     for (int i = int(0); i < int(n); i++) {       if (s[i] ==  ? )         a[i] = -1;       else         a[i] = s[i] -  0 ;       if ((i) < n / 2) {         if (a[i] != -1)           l += a[i];         else           c1++;       } else {         if (a[i] != -1)           r += a[i];         else           c2++;       }     }     bool ans = false;     if (l + 9 * ceil(double(c1 / 2.0)) != r + 9 * ceil(double(c2 / 2.0)))       ans = true;     if (!ans)       cout <<  Bicarp ;     else       cout <<  Monocarp ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const long long int N = 1000000000;   int main(int argc, char const *argv[]) {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t;     cin >> t;     while (t-- > 0) {       int n;       cin >> n;       string s, s1;       cin >> s >> s1;       int x = 0;       int op = 0;       vector<int> opr;       for (int i = n - 1; i >= 0; i--) {         if (op % 2 == 0) {           if (s[x + i] == s1[i]) continue;           if (s[x] != s[x + i]) opr.push_back(1);           opr.push_back(i + 1);           op++;           x = x + i;         } else {           char c = s[x - i] ==  0  ?  1  :  0 ;           char c1 = s[x] ==  0  ?  1  :  0 ;           if (c == s1[i]) continue;           if (c != c1) opr.push_back(1);           opr.push_back(i + 1);           op++;           x = x - i;         }       }       cout << opr.size() <<    ;       for (auto i : opr) cout << i <<    ;       cout << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   long long U[10001], V[10000], dis[10000];   long long n, m;   vector<long long> adj[10001];   bool bfs() {     queue<long long> q;     for (long long i = 1; i <= n; i++) {       if (U[i] == 0) {         dis[i] = 0;         q.push(i);       } else         dis[i] = INT_MAX;     }     dis[0] = INT_MAX;     while (!q.empty()) {       long long x = q.front();       q.pop();       if (dis[x] < dis[0]) {         for (long long j = 0; j < adj[x].size(); j++) {           if (dis[V[adj[x][j]]] == INT_MAX) {             dis[V[adj[x][j]]] = dis[x] + 1;             q.push(V[adj[x][j]]);           }         }       }     }     return (dis[0] != INT_MAX);   }   bool dfs(long long u) {     if (u != 0) {       for (long long j = 0; j < adj[u].size(); j++) {         if (dis[V[adj[u][j]]] == (dis[u] + 1)) {           if (dfs(V[adj[u][j]])) {             V[adj[u][j]] = u;             U[u] = adj[u][j];             return true;           }         }       }       dis[u] = INT_MAX;       return false;     }     return true;   }   long long hopcroft() {     for (long long i = 0; i <= 10000; i++) {       U[i] = 0;       V[i] = 0;     }     long long ans = 0;     while (bfs()) {       for (long long u = 1; u <= n; u++) {         if (U[u] == 0 && dfs(u)) ans++;       }     }     return ans;   }   void solve() {     cin >> n >> m;     vector<pair<long long, pair<long long, long long> > > edges;     for (long long i = 0; i < m; i++) {       long long a, b, c;       cin >> a >> b >> c;       edges.push_back({c, {a, b}});     }     long long l = 0, r = 1000000000, f = -1;     while (l <= r) {       long long mid = (l + r) / 2;       for (long long i = 1; i <= n; i++) adj[i].clear();       for (auto x : edges) {         if (x.first <= mid) {           adj[x.second.first].push_back(x.second.second);         }       }       if (hopcroft() == n) {         r = mid - 1;         f = mid;       } else         l = mid + 1;     }     cout << f <<   n ;   }   signed main() {     ios_base::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     long long test = 0, t;     t = 1;     while (test++ < t) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   const int mod = (int)1e9 + 7;   const int MOD = (int)998244353;   const int INF = (int)1e18;   bool cmp(const pair<long long, long long>& a,            const pair<long long, long long>& b) {     if (a.second == b.second) return a.first < b.first;     return a.second > b.second;   }   void solve() {     long long a, b, x, y, p, q;     cin >> a >> b >> x >> y >> p >> q;     vector<long long> v;     long long cnt = 0;     map<pair<long long, int>, int> m1;     long long n, m;     n = a + x - p;     m = b + y - q;     v.push_back(n);     v.push_back(m);     m1[{n, m}] = 1;     cnt++;     n = a - x + p;     m = b - y + q;     if (m1[{n, m}] == 0) {       cnt++;       m1[{n, m}] = 1;       v.push_back(n);       v.push_back(m);     }     n = -a + x + p;     m = -b + y + q;     if (m1[{n, m}] == 0) {       cnt++;       m1[{n, m}] = 1;       v.push_back(n);       v.push_back(m);     }     cout << cnt <<   n ;     cnt = ((int)(v).size());     for (long long i = 0; i < cnt; i++) {       cout << v[i] <<    ;       if (i & 1) cout <<   n ;     }     return;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     long long t = 1, m = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   ll gcd(ll a, ll b) {     while (b) {       a %= b;       swap(a, b);     }     return a;   }   ll nCr(ll n, ll r) {     ll a = 1, b = 1;     if (n < r) return 0;     if (r == 0) return 1;     if (n - r < r) r = n - r;     while (r) {       a *= n;       b *= r;       ll m = gcd(a, b);       a /= m;       b /= m;       n--, r--;     }     return a;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     int n, d;     cin >> n >> d;     vector<int> a(n);     for (int i = 0; i < n; i++) {       cin >> a[i];     }     vector<int> b, c;     ll ans = 0;     int j = 1, prevJ = 0;     for (int i = 0; j < n; i++) {       for (j; j < n; j++) {         if (a[j] - a[i] > d) break;       }       ans += (nCr(j - i, 3) - nCr((j - i) - (j - prevJ), 3));       prevJ = j;     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = (int)2e5 + 5;   const int INF = (int)1e9 + 7;   int dx[8] = {1, 0, -1, 0, -1, -1, 1, 1};   int dy[8] = {0, 1, 0, -1, -1, 1, -1, 1};   int ini() {     int x;     scanf( %d , &x);     return x;   }   long long inl() {     long long x;     scanf( %lld , &x);     return x;   }   vector<int> v;   int main() {     int n, i, j, m;     n = ini();     m = ini();     for (i = 1; i <= n; i++) {       int x = ini();       v.push_back(x);     }     sort(v.begin(), v.end());     for (i = 1; i <= m; i++) {       int x;       std::vector<int>::iterator low, up;       x = ini();       up = upper_bound(v.begin(), v.end(), x);       cout << (up - v.begin()) <<    ;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 105;   int N, M;   int A[MAXN];   int ans[MAXN][MAXN];   void gen(int x) {     if (x <= 2) {       for (int i = 0; i < x; i++) {         A[i] = 3 + i;       }       return;     } else if (x == 3) {       A[0] = 1;       A[1] = A[2] = 2;       return;     }     if (x % 2) {       A[0] = x - 2;       fill(A + 1, A + x, 2);     } else {       A[0] = x / 2 - 1;       fill(A + 1, A + x, 1);     }   }   int main() {     if (fopen( input.txt ,  r )) {       freopen( input.txt ,  r , stdin);     }     ios::sync_with_stdio(false);     cin >> N >> M;     gen(M);     for (int i = 0; i < N; i++) {       copy_n(A, M, ans[i]);     }     gen(N);     for (int j = 0; j < M; j++) {       for (int i = 0; i < N; i++) {         ans[i][j] *= A[i];       }     }     for (int i = 0; i < N; i++) {       for (int j = 0; j < M; j++) {         cout << ans[i][j] <<    ;       }       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 1000 + 17;   int n, A[MAXN], L[7], R[7];   void Solve(int u) {     bool st = true;     for (int i = 1; i <= n; ++i)       if (A[i] != i) st = false;     if (st) {       cout << u - 1 <<   n ;       for (int i = u - 1; i >= 1; --i) cout << L[i] <<     << R[i] <<   n ;       exit(0);     }     if (u > 3) return;     for (int i = 1; i <= n; ++i)       if (A[i] != i && (abs(A[i] - A[i - 1]) > 1 || abs(A[i] - A[i + 1]) > 1)) {         L[u] = i;         for (int j = i + 1; j <= n; ++j)           if ((A[j] != j && (abs(A[j] - A[j - 1]) > 1)) ||               abs(A[j] - A[j + 1]) > 1) {             reverse(A + i, A + j + 1);             R[u] = j;             Solve(u + 1);             reverse(A + i, A + j + 1);           }       }   }   int main() {     cin >> n;     A[0] = A[n + 1] = -1;     for (int i = 1; i <= n; ++i) cin >> A[i];     Solve(1);   }
#include <bits/stdc++.h>   using namespace std;   int n, res[300010], c[300010], trie[300010][30], dif[300010], ans, cans;   vector<vector<int> > gr;   string s;   void merge(int big, int small) {     dif[big] = 1;     for (int i = 0; i < 26; ++i) {       if (trie[small][i] and trie[big][i]) {         merge(trie[big][i], trie[small][i]);       } else if (trie[small][i] and trie[big][i] == 0) {         trie[big][i] = trie[small][i];       }       dif[big] += dif[trie[big][i]];     }   }   void dfs(int x, int par) {     dif[x] = 1;     for (auto i : gr[x]) {       if (i == par) {         continue;       }       dfs(i, x);       int xCh = trie[x][s[i - 1] -  a ];       if (!xCh) {         trie[x][s[i - 1] -  a ] = i;         dif[x] += dif[i];       } else {         dif[x] -= dif[xCh];         merge(xCh, i);         dif[x] += dif[xCh];       }     }     res[x] = dif[x] + c[x];   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cin >> n;     gr.resize(n + 1, vector<int>());     for (int i = 1; i <= n; ++i) {       cin >> c[i];     }     cin >> s;     for (int i = 0; i < n - 1; ++i) {       int u, v;       cin >> u >> v;       gr[u].push_back(v);       gr[v].push_back(u);     }     dfs(1, -1);     for (int i = 1; i <= n; ++i) {       ans = max(ans, res[i]);     }     for (int i = 1; i <= n; ++i) {       if (res[i] == ans) {         ++cans;       }     }     cout << ans <<   n ;     cout << cans;   }
#include <bits/stdc++.h>   using namespace std;   long long fact[200001], inv_fact[200001], dp[2010][26];   bool cmp(pair<int, int> a, pair<int, int> b) {     return a.first + a.second < b.first + b.second;   }   long long ModExp(int a, int n) {     long long x = a % 1000000007, y = 1 % 1000000007;     while (n > 0) {       if (n % 2) y = (y * x) % 1000000007;       x = (x * x) % 1000000007;       n /= 2;     }     return y;   }   long long ModInverse(int a) { return ModExp(a, 1000000007 - 2); }   void PreCompute() {     fact[0] = inv_fact[0] = 1;     for (int i = 1; i < 200001; i++) {       fact[i] = (i * fact[i - 1]) % 1000000007;       inv_fact[i] = (ModInverse(i) * inv_fact[i - 1]) % 1000000007;     }   }   long long nCr(int n, int r) {     if (n < 0 || r < 0 || n < r)       return 0;     else       return (fact[n] * ((inv_fact[r] * inv_fact[n - r]) % 1000000007)) %              1000000007;   }   long long NumPaths(pair<int, int> a, pair<int, int> b) {     return nCr((b.first - a.first) + (b.second - a.second), (b.first - a.first));   }   int main() {     int n, m, k, p, i, j, x, y, z;     long long P, Q, ans;     set<pair<int, int> > s;     vector<pair<int, int> > v;     scanf( %d%d%d%d , &n, &m, &k, &p);     for (x = 1; x <= k; x++) {       scanf( %d%d , &i, &j);       s.insert(make_pair(i, j));     }     s.insert(make_pair(1, 1));     if (s.size() == k) p = (p - 1) / 2 + 1;     for (x = p, k = 0; x > 1; k++, x = (x - 1) / 2 + 1)       ;     v.push_back(make_pair(0, 0));     for (auto it = s.begin(); it != s.end(); it++) v.push_back(*it);     PreCompute();     for (i = v.size() - 1; i > 0; i--) {       long long sum = 0;       for (j = 0; j < k; j++) {         dp[i][j] = NumPaths(v[i], make_pair(n, m));         for (x = i + 1; x < v.size(); x++)           dp[i][j] = (dp[i][j] + (1000000007 - (NumPaths(v[i], v[x]) * dp[x][j]) %                                                    1000000007)) %                      1000000007;         dp[i][j] = (dp[i][j] + (1000000007 - sum)) % 1000000007;         sum = (sum + dp[i][j]) % 1000000007;       }     }     long long sum = 0;     for (i = 1, j = 0, P = 0; j < k; j++, p = (p - 1) / 2 + 1) {       P = (P + dp[i][j] * p) % 1000000007;       sum = (sum + dp[i][j]) % 1000000007;     }     Q = NumPaths(make_pair(1, 1), make_pair(n, m));     sum = (Q + 1000000007 - sum) % 1000000007;     P = (P + sum) % 1000000007;     ans = (P * ModInverse((int)Q)) % 1000000007;     printf( %lld n , ans);     return 0;   }