malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   inline long long rd() {     long long x = 0;     int ch = getchar(), f = 1;     while (!isdigit(ch) && (ch !=  - ) && (ch != EOF)) ch = getchar();     if (ch ==  - ) {       f = -1;       ch = getchar();     }     while (isdigit(ch)) {       x = (x << 1) + (x << 3) + ch -  0 ;       ch = getchar();     }     return x * f;   }   inline void rt(long long x) {     if (x < 0) putchar( - ), x = -x;     if (x >= 10)       rt(x / 10), putchar(x % 10 +  0 );     else       putchar(x +  0 );   }   const int p = 1000000007;   long long n, k;   long long phi(long long n) {     long long ret = n;     for (long long i = 2; i * i <= n; i++)       if (n % i == 0) {         ret = ret / i * (i - 1);         while (n % i == 0) n /= i;       }     if (n != 1) ret = ret / n * (n - 1);     return ret;   }   int main() {     n = rd(), k = (rd() + 1) / 2;     while (n != 1 && k) n = phi(n), k--;     rt(n % p), putchar(  n );   }
#include <bits/stdc++.h>   using namespace std;   const long long inf = 1000000000000000000;   int c[1001][1001];   int main(void) {     int n, m;     scanf( %d %d , &n, &m);     long long s = 0, sij = 0, si = 0, sj = 0;     for (int i = 1; i <= n; ++i) {       for (int j = 1; j <= m; ++j) {         int p = (i - 1) * 4 + 2, q = (j - 1) * 4 + 2;         scanf( %d , &c[i][j]);         s += c[i][j];         sij += (long long)c[i][j] * (p * p + q * q);         si += c[i][j] * p;         sj += c[i][j] * q;       }     }     int x, y;     long long ans = inf;     for (int i = 0; i <= n; ++i) {       for (int j = 0; j <= m; ++j) {         int p = i * 4, q = j * 4;         long long temp = s * (p * p + q * q) - 2 * si * p - 2 * sj * q + sij;         if (temp < ans) {           ans = temp;           x = i;           y = j;         }       }     }     printf( %I64d n%d %d n , ans, x, y);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, m;   inline int read() {     int t = 0, f = 1;     char ch = getchar();     while (ch <  0  || ch >  9 ) f = (ch ==  - ) ? -1 : f, ch = getchar();     while (ch >=  0  && ch <=  9 ) t = t * 10 + ch -  0 , ch = getchar();     return t * f;   }   struct orz {     char s[16];     int x, y;     void in() { scanf( %s , s), x = read(), y = read(); }     friend bool operator<(orz a, orz b) {       return (a.x == b.x) ? a.y > b.y : a.x < b.x;     }   } a[200105];   int main() {     n = read(), m = read();     for (register int i = 1; i <= n; i++) a[i].in();     sort(a + 1, a + n + 1);     int now = 1;     for (register int i = 1; i <= m; i++) {       while (a[now].x != i) now++;       if (now + 2 <= n && a[now + 2].x == a[now + 1].x &&           a[now + 2].y == a[now + 1].y)         puts( ? );       else         printf( %s %s n , a[now].s, a[now + 1].s);       now++;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     string myNumber;     cin >> myNumber;     int temp;     int fhalf = 0, shalf = 0;     for (int i = 0; i < n / 2; ++i) {       temp = myNumber[i] -  0 ;       if (temp != 4 && temp != 7) {         cout <<  NO ;         goto exit;       }       shalf += temp;     }     for (int i = n / 2; i < n; ++i) {       temp = myNumber[i] -  0 ;       if (temp != 4 && temp != 7) {         cout <<  NO ;         goto exit;       }       fhalf += temp;     }     if (fhalf == shalf)       cout <<  YES ;     else       cout <<  NO ;   exit:     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, i, ans = 0, a[200001] = {0};     scanf( %d , &n);     for (i = 0; i < n; i++) {       int x;       scanf( %d , &x);       if (a[100000 + x] == 0 && x != 0) {         ans++;         a[100000 + x] = 1;       }     }     printf( %d n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     unordered_map<int, int> ma;     int x, y, z, l;     cin >> x >> y;     vector<int> vec(x);     vector<int> vec1(x);     for (int i = 0; i < x; i++) cin >> vec[i];     vec1 = vec;     for (int i = 0; i < y; i++) {       cin >> z >> l;       if (vec[z - 1] > vec[l - 1])         ma[z - 1]++;       else if (vec[l - 1] > vec[z - 1])         ma[l - 1]++;     }     sort(vec1.begin(), vec1.end());     for (int i = 0; i < x; i++) {       int r = lower_bound(vec1.begin(), vec1.end(), vec[i]) - vec1.begin();       cout << r - ma[i] <<    ;     }   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n, m;     cin >> n >> m;     for (int i = 0; i < n; ++i) {       for (int j = 0; j < m; ++j) {         if (i % 2 == 1) {           if (i % 4 == 1)             if (j == m - 1)               cout <<  # ;             else               cout <<  . ;           else if (j == 0)             cout <<  # ;           else             cout <<  . ;         } else           cout <<  # ;       }       cout << endl;     }   }
#include <bits/stdc++.h>   using namespace std;   int n, k;   struct node {     int mn[11], mx[11], sz;     bool operator<(const node &p) const {       for (int i = 1; i <= k; i++)         if (mx[i] > p.mn[i]) return 0;       return 1;     }   } now;   set<node> S;   set<node>::iterator it;   int main() {     scanf( %d%d , &n, &k);     for (int i = 1; i <= n; i++) {       now.sz = 1;       for (int j = 1; j <= k; j++) scanf( %d , &now.mn[j]), now.mx[j] = now.mn[j];       for (; (it = S.find(now)) != S.end(); S.erase(it)) {         now.sz += (*it).sz;         for (int j = 1; j <= k; j++)           now.mn[j] = min(now.mn[j], (*it).mn[j]),           now.mx[j] = max(now.mx[j], (*it).mx[j]);       }       S.insert(now);       printf( %d n , (*S.rbegin()).sz);     }   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 2e5 + 3;   vector<int> adj[MAXN];   int sz[MAXN], rt = -1, ok[MAXN], k;   int dfs1(int u, int p) {     for (int i = 0; i < adj[u].size(); i++)       if (adj[u][i] != p) sz[u] += dfs1(adj[u][i], u);     return sz[u];   }   pair<int, int> dfs2(int u, int p) {     vector<pair<int, int> > ans;     pair<int, int> ret;     ans.push_back(make_pair(sz[u], u));     for (int i = 0; i < adj[u].size(); i++)       if (adj[u][i] != p) {         if (sz[adj[u][i]] >= k) {           ans.push_back(make_pair(sz[adj[u][i]], adj[u][i]));           ans.push_back(dfs2(adj[u][i], u));         }       }     sort(ans.begin(), ans.end());     for (int i = 0; i < ans.size(); i++) {       if (ans[i].first >= k && (rt == -1 || sz[rt] > ans[i].first))         rt = ans[i].second, ret = ans[i];     }     return ret;   }   long long dfs3(int u, int p, int dep) {     long long ret = 0;     if (ok[u]) ret += dep;     for (int i = 0; i < adj[u].size(); i++)       if (adj[u][i] != p) ret += dfs3(adj[u][i], u, dep + 1);     return ret;   }   int main() {     int n, x;     scanf( %d%d , &n, &k);     for (int i = 0; i < 2 * k; i++) scanf( %d , &x), ok[x] = sz[x] = 1;     for (int i = 0; i < n - 1; i++) {       int a, b;       scanf( %d%d , &a, &b);       adj[a].push_back(b);       adj[b].push_back(a);     }     dfs1(1, -1);     dfs2(1, -1);     printf( %I64d n , dfs3(rt, -1, 0));   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int n;     cin >> n;     long long int g = 0;     pair<int, int> A[1000], B[1000];     for (int i = 1; i <= n; i++) {       cin >> A[i].first >> A[i].second;       cin >> B[i].first >> B[i].second;       g += (((B[i].first - A[i].first) + 1) * ((B[i].second - A[i].second) + 1));     }     cout << g;     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, m, x, nn, j, mm, y, z, i, tmp, p;     int a, b;     scanf( %d%d%d%d%d%d , &nn, &mm, &x, &y, &z, &p);     for (j = 1; j <= p; j++) {       n = nn;       m = mm;       scanf( %d%d , &a, &b);       x %= 4;       z %= 4;       if (y % 2 == 0) {         for (i = 1; i <= (x + 4 - z) % 4; i++) {           tmp = a;           a = b;           b = n - tmp + 1;           tmp = n;           n = m;           m = tmp;         }         printf( %d %d n , a, b);       } else {         for (i = 1; i <= (x + z) % 4; i++) {           tmp = a;           a = b;           b = n - tmp + 1;           tmp = n;           n = m;           m = tmp;         }         printf( %d %d n , a, m - b + 1);       }     }     return 0;   }
#include <bits/stdc++.h>   const int maxn = 400000 + 5;   int f[maxn];   int n, m, q;   int ans;   inline int Read() {     register int ret;     register char r;     register bool f = false;     while (r = getchar(), r <  0  || r >  9 )       if (r ==  - ) f = true;     ret = r - 48;     while (r = getchar(), r >=  0  && r <=  9 )       ret = (ret << 3) + (ret << 1) + r - 48;     return f ? ~ret + 1 : ret;   }   inline int Find(int x) { return x == f[x] ? x : f[x] = Find(f[x]); }   int main() {     n = Read();     m = Read();     q = Read();     for (register int i = n + m; i; i--) f[i] = i;     for (register int i = 1; i <= q; i++) {       int x = Find(Read()), y = Find(Read() + n);       if (x != y) f[x] = y;     }     for (register int i = n + m; i; i--) ans += (Find(i) == i);     printf( %d n , ans - 1);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using P = pair<int, int>;   int main() {     int q;     cin >> q;     vector<P> v(q);     for (int qi = 0; qi < q; qi++) {       int t;       cin >> t;       if (t == 1) {         int x;         cin >> x;         v[qi] = {x, 0};       } else {         int x, y;         cin >> x >> y;         v[qi] = {-x, y};       }     }     reverse(v.begin(), v.end());     map<int, int> to;     vector<int> ans;     for (auto now : v) {       if (now.first >= 0) {         if (to.count(now.first)) {           now.first = to[now.first];         }         ans.push_back(now.first);       } else {         now.first *= -1;         if (to.count(now.second)) {           to[now.first] = to[now.second];         } else {           to[now.first] = now.second;         }       }     }     reverse(ans.begin(), ans.end());     cout << ans[0];     for (int i = 1; i < (int)ans.size(); i++) {       cout <<     << ans[i];     }     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MOD = 1000000007;   const int MAXN = 305;   const int MAXT = 100005;   int a[MAXN], e[MAXN], d[MAXN];   long long dp[MAXT];   long long t, cnt;   void dfs(int u, int acc) {     if (t < 0) {       return;     }     ++cnt;     a[u] += acc;     if (e[u] != 0) {       dfs(e[u], a[u]);       t -= a[u];       if (t < 0) {         return;       }     }   }   int main() {     int n, q;     int u, v;     scanf( %d%d%d , &n, &q, &t);     for (int i = 1; i <= n; ++i) {       scanf( %d , a + i);     }     while (q--) {       scanf( %d%d , &u, &v);       e[u] = v;       d[v] = 1;     }     for (int i = 1; i <= n && t >= 0; ++i) {       if (d[i] == 0) {         dfs(i, 0);       }     }     if (t < 0 || cnt < n) {       printf( 0 n );       return 0;     }     dp[0] = 1;     for (int i = 1; i <= n; ++i) {       for (int j = a[i]; j <= t; ++j) {         dp[j] = (dp[j] + dp[j - a[i]]) % MOD;       }     }     printf( %d n , dp[t]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   queue<int> q[300005];   set<int> all;   int main() {     int n, quer;     scanf( %d%d , &n, &quer);     int t, x, v;     int in = 1;     for (int i = 1; i <= quer; i++) {       scanf( %d%d , &t, &x);       if (t == 1) {         q[x].push(in);         all.insert(in);         in++;       } else if (t == 2) {         while (!q[x].empty()) {           all.erase(q[x].front());           q[x].pop();         }       } else {         while (all.size() >= 1) {           v = *all.begin();           if (v <= x)             all.erase(v);           else             break;         }       }       printf( %d n , all.size());     }   }
#include <bits/stdc++.h>   using namespace std;   struct SegmentTree {    private:     int n;     vector<pair<int, int>> a;     vector<pair<int, int>> tree;       public:     SegmentTree(vector<pair<int, int>> &a) {       this->a = a;       this->n = (int)a.size();       tree.resize(4 * n);       build(1, 1, n);     }     void update(int l, int r, pair<int, int> x) { update(1, 1, n, l, r, x); }     pair<int, int> query(int i) { return query(1, 1, n, i); }       private:     void build(int i, int l, int r) {       if (l == r)         tree[i] = a[l - 1];       else {         int m = (l + r) / 2;         build(2 * i, l, m);         build(2 * i + 1, m + 1, r);         tree[i] = make_pair(0, 0);       }     }     void update(int i, int l, int r, int ul, int ur, pair<int, int> x) {       if (ul <= l && r <= ur)         tree[i] = x;       else {         if (tree[i] != make_pair(0, 0)) {           tree[2 * i] = tree[2 * i + 1] = tree[i];         }         int m = (l + r) / 2;         if (ul <= m) update(2 * i, l, m, ul, ur, x);         if (ur > m) update(2 * i + 1, m + 1, r, ul, ur, x);         tree[i] = tree[2 * i] == tree[2 * i + 1] ? tree[2 * i] : make_pair(0, 0);       }     }     pair<int, int> query(int i, int l, int r, int qi) {       if (tree[i] != make_pair(0, 0)) return tree[i];       int m = (l + r) / 2;       if (qi <= m)         return query(2 * i, l, m, qi);       else         return query(2 * i + 1, m + 1, r, qi);     }   };   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int n, m;     cin >> n >> m;     vector<int> a(2 * n);     for (auto &ai : a) cin >> ai;     vector<pair<int, int>> b(n);     for (int i = 0; i < n; i++) b[i] = make_pair(i + n + 1, i + 1);     SegmentTree tree(b);     while (m--) {       int t;       cin >> t;       if (t == 1) {         int x, y, k;         cin >> x >> y >> k;         tree.update(y, y + k - 1, make_pair(x, y));       } else {         int x;         cin >> x;         auto aux = tree.query(x);         int diff = x - aux.second;         int ans = a[aux.first + diff - 1];         cout << ans <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   int n, m, a[101], s[101], ans, l, r;   int main() {     ios::sync_with_stdio(false);     cin >> n >> m;     for (int i = 1; i < n + 1; i++) cin >> a[i];     for (int i = 1; i < n + 1; i++) s[i] = s[i - 1] + a[i];     ans = 0;     for (int i = 0; i < m; i++) {       cin >> l >> r;       if (s[r] - s[l - 1] > 0) ans += s[r] - s[l - 1];     }     cout << ans;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n;   long long sum = 0, m1 = 0;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       int x;       scanf( %d , &x);       sum += x;       m1 = max(m1, (long long)x);     }     if (m1 <= sum / 2 && sum % 2 == 0)       printf( YES n );     else       printf( NO n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 200004 * 4 + 11;   long long k[N], add[N];   int a[200004];   int n, m;   int x, y;   long long z;   long long ans;   int build(int l, int r, int o) {     add[o] = 0;     if (l == r) return k[o] = a[l];     int mid = ((l + r) >> 1);     return k[o] = build(l, mid, (o << 1)) + build(mid + 1, r, ((o << 1) | 1));   }   void pushdown(int l, int r, int o) {     add[(o << 1)] += add[o];     add[((o << 1) | 1)] += add[o];     add[o] = 0;   }   void maintain(int l, int r, int o) {     int mid = ((l + r) >> 1);     if (r > l)       k[o] = k[(o << 1)] + add[(o << 1)] * (mid - l + 1) + k[((o << 1) | 1)] +              add[((o << 1) | 1)] * (r - mid);     else       k[o] = a[l];   }   void update(int l, int r, int o) {     if (x <= l && r <= y) {       add[o] += z;     } else {       pushdown(l, r, o);       int mid = ((l + r) >> 1);       if (x <= mid)         update(l, mid, (o << 1));       else         maintain(l, mid, (o << 1));       if (y > mid)         update(mid + 1, r, ((o << 1) | 1));       else         maintain(mid + 1, r, ((o << 1) | 1));     }     maintain(l, r, o);   }   void query(int l, int r, int o) {     if (x <= l && r <= y)       ans += k[o] + add[o] * (r - l + 1);     else {       pushdown(l, r, o);       int mid = ((l + r) >> 1);       if (x <= mid) query(l, mid, (o << 1));       if (y > mid) query(mid + 1, r, ((o << 1) | 1));     }     maintain(l, r, o);   }   int main() {     long long lst = 0.0;     while (~scanf( %d , &n)) {       int i, c, e, f;       int d;       int tot = 1;       int tp = 0;       build(1, 200004, 1);       while (n--) {         scanf( %d , &c);         if (c == 1) {           scanf( %d%d , &e, &d);           z = d;           x = 1;           y = min(e, tot);           update(1, 200004, 1);           lst += e * d;         } else if (c == 2) {           scanf( %d , &d);           tot++;           x = y = tot;           z = d;           update(1, 200004, 1);           lst += d;         } else {           ans = 0;           x = y = tot;           query(1, 200004, 1);           z = -ans;           update(1, 200004, 1);           tot--;           lst += z;         }         ans = 0;         x = 1;         y = 200004;         query(1, n, 1);         printf( %.9lf n , ans * 1.0 / tot);       }     }     return 0;   }
#include <bits/stdc++.h>   int main() {     int n, q;     scanf( %d , &n);     scanf( %d , &q);     int i;     long ans[q + 1];     long val;     std::set<long> valores;     for (i = 0; i < n; i++) {       scanf( %ld , &val);       valores.insert(val);     }     std::set<long>::iterator ite;     std::multiset<long> dife;     for (ite = valores.begin(); ite != --valores.end(); ite++) {       dife.insert(*valores.upper_bound(*ite) - *ite);     }     int acc;     std::set<long>::iterator low, up;     for (i = 0; i < q; i++) {       if (valores.size() > 1)         ans[i] = *valores.rbegin() - *valores.begin() - *dife.rbegin();       else         ans[i] = 0;       scanf( %d , &acc);       scanf( %ld , &val);       if (acc == 0) {         up = valores.upper_bound(val);         low = valores.lower_bound(val);         if (up != valores.end()) {           dife.erase(dife.lower_bound(*up - val));         }         if (low != valores.begin()) {           low--;           dife.erase(dife.lower_bound(val - *low));           low++;         }         if ((up != valores.end()) && (low != valores.begin())) {           low--;           dife.insert(*up - *low);         }         valores.erase(val);       } else {         up = valores.upper_bound(val);         low = valores.lower_bound(val);         if (up != valores.end()) {           dife.insert(*up - val);         }         if (low != valores.begin()) {           low--;           dife.insert(val - *low);           low++;         }         if ((up != valores.end()) && (low != valores.begin())) {           low--;           dife.erase(dife.lower_bound(*up - *low));         }         valores.insert(val);       }     }     if (valores.size() > 1)       ans[q] = *valores.rbegin() - *valores.begin() - *dife.rbegin();     else       ans[q] = 0;     for (i = 0; i < q + 1; i++) {       printf( %d  n , ans[i]);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 2e5 + 5;   int n, m;   struct Query {     int l, r, x;     Query() {}     Query(int ll, int rr, int xx) { l = ll, r = rr, x = xx; }   };   vector<Query> q[N];   int lson[N], rson[N];   void init() {     int cnt = 0, p = 1;     for (int i = 1; i < N; ++i) {       if ((1 << cnt) == i) {         cnt++;         lson[i] = p++;       } else {         lson[i] = -1;       }       rson[i] = p++;     }   }   bool ok(int x1, int y1, int x2, int y2) {     if (x1 > y2 || x2 > y1) return false;     return true;   }   void solve(int k, int x) {     set<int> s;     int l = x, r = x;     for (int i = k; i < n + 1; ++i) {       for (int j = 0; j < (int)q[i].size(); ++j) {         if (ok(l, r, q[i][j].l, q[i][j].r)) {           s.insert(q[i][j].x);         }       }       if (lson[l] != -1)         l = lson[l];       else         l = rson[l];       r = rson[r];     }     printf( %d n , (int)s.size());   }   int main() {     init();     int op, k, l, r, x;     scanf( %d%d , &n, &m);     for (int i = 1; i < m + 1; ++i) {       scanf( %d , &op);       if (op == 1) {         scanf( %d%d%d%d , &k, &l, &r, &x);         q[k].push_back(Query(l, r, x));       } else {         scanf( %d%d , &k, &x);         solve(k, x);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     int q;     scanf( %d , &q);     while (q--) {       int n;       scanf( %d , &n);       int sum = 0, a;       for (int i = 0; i < n; ++i) {         scanf( %d , &a);         sum += a;       }       printf( %d n , (int)ceil(sum * 1.0 / n));     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int N = 1e5 + 20;   const int INF = 1e9;   struct Edge {     int to, next, w;   } e[2 * N];   int n, head[N], M, num;   map<int, int> mp, sp;   long long ans;   void add(int u, int v, int w) {     e[num].next = head[u];     e[num].to = v, e[num].w = w;     head[u] = num++;   }   bool ban[N];   int sz[N], f[N];   int root, siz;   void get_root(int u, int fa) {     sz[u] = 1, f[u] = 0;     for (int i = head[u]; i != -1; i = e[i].next) {       int v = e[i].to;       if (v == fa || ban[v]) continue;       get_root(v, u);       sz[u] += sz[v];       f[u] = max(sz[v], f[u]);     }     f[u] = max(f[u], siz - sz[u]);     if (f[u] < f[root]) root = u;   }   int dis[N], seq[N], tol, pos[N];   long long inv[N], path[N], ten[N], gao[N];   void dfs(int u, int fa) {     pos[u] = tol;     seq[tol++] = u;     sz[u] = 1;     for (int i = head[u]; i != -1; i = e[i].next) {       int v = e[i].to;       if (v == fa || ban[v]) continue;       dis[v] = dis[u] + 1;       path[v] = ((1LL * path[u]) * 10 + e[i].w) % M;       gao[v] = (gao[u] + ten[dis[v] - 1] * e[i].w) % M;       if (gao[v] == 0) ans++;       if (path[v] == 0) ans++;       dfs(v, u);       sz[u] += sz[v];     }   }   void sovle(int u, int ssz) {     root = 0, siz = ssz, f[0] = INF;     get_root(u, 0);     int rt = root;     dis[rt] = 0, path[rt] = 0, tol = 0;     gao[rt] = 0;     long long pre = ans;     dfs(rt, 0);     mp.clear(), sp.clear();     for (int i = head[rt]; i != -1; i = e[i].next) {       int v = e[i].to;       if (ban[v]) continue;       for (int j = pos[v]; j < pos[v] + sz[v]; j++) {         int u = seq[j];         int tmp = ((M - path[u]) * inv[dis[u]]) % M;         ans += mp[tmp] + sp[gao[u]];       }       for (int j = pos[v]; j < pos[v] + sz[v]; j++) {         int u = seq[j];         int tmp = ((M - path[u]) * inv[dis[u]]) % M;         mp[gao[u]]++, sp[tmp]++;       }     }     ban[rt] = true;     for (int i = head[rt]; i != -1; i = e[i].next) {       int v = e[i].to;       if (ban[v]) continue;       sovle(v, sz[v]);     }   }   long long extend_gcd(long long a, long long b, long long &x, long long &y) {     if (b == 0) {       x = 1;       y = 0;       return a;     } else {       long long gcd = extend_gcd(b, a % b, x, y);       long long t = x;       x = y;       y = t - a / b * x;       return gcd;     }   }   void init() {     memset(head, -1, sizeof(head));     memset(ban, 0, sizeof(ban));     num = 0;     inv[0] = 1, ten[0] = 1;     long long x, y;     extend_gcd(10, M, x, y);     x = (x % M + M) % M;     for (int i = 1; i <= n; i++) {       inv[i] = (inv[i - 1] * x) % M;       ten[i] = (ten[i - 1] * 10) % M;     }   }   int main() {     while (scanf( %d%d , &n, &M) != EOF) {       init();       for (int i = 0; i < n - 1; i++) {         int u, v, w;         scanf( %d%d%d , &u, &v, &w);         u++, v++;         add(u, v, w);         add(v, u, w);       }       ans = 0;       sovle(1, n);       printf( %I64d n , ans);     }     return 0;   }