malteklaes/cpp-code-code_search_net-style huggingface dataset:
#include <bits/stdc++.h>   using namespace std;   int32_t main() {     int n, h;     cin >> n >> h;     vector<int> v(n);     for (int i = 0; i < n; i++) cin >> v[i];     int lo = 0;     int hi = n - 1;     int ans, mid;     while (lo <= hi) {       mid = lo + (hi - lo) / 2;       vector<int> a(mid + 1);       for (int i = 0; i < mid + 1; i++) a[i] = v[i];       sort(a.begin(), a.end());       int c = 0;       int j = mid;       int leftheight = h;       while (j >= 0 && leftheight > 0) {         if (a[j] <= leftheight) {           leftheight -= a[j];           if (mid % 2 == 0 && j == 0) {             c++;             break;           } else {             j -= 2;             c += 2;           }         } else           break;       }       if (c >= (mid + 1)) {         lo = mid + 1;         ans = mid;       } else         hi = mid - 1;     }     cout << ans + 1 << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, flag, ln, prv = -1;   char in[100010][20], pr[100010][20];   int main() {     scanf( %d , &n);     for (int i = 0; i < n; i++) {       scanf( %s , in[i]);       ln = strlen(in[i]);       flag = 0;       if (ln < prv) {         printf( NO n );         return 0;       } else if (i == 0 || ln > prv) {         for (int j = 0; j < ln; j++) {           if (j == 0 && in[i][j] ==  ? )             pr[i][j] =  1 ;           else if (in[i][j] ==  ? )             pr[i][j] =  0 ;           else             pr[i][j] = in[i][j];         }       } else {         for (int j = 0; j < ln; j++) {           if (in[i][j] ==  ? ) {             if (flag == 0)               pr[i][j] = pr[i - 1][j];             else               pr[i][j] =  0 ;           } else if (in[i][j] > pr[i - 1][j]) {             pr[i][j] = in[i][j];             flag = 1;           } else if (in[i][j] < pr[i - 1][j] && flag == 0) {             for (int x = j - 1; x >= 0; x--) {               if (in[i][x] ==  ?  && pr[i][x] <  9 ) {                 pr[i][x]++;                 for (int z = x + 1; z < j; z++)                   if (in[i][z] ==  ? ) pr[i][z] =  0 ;                 flag = 1;                 break;               }             }             if (flag == 0) {               printf( NO n );               return 0;             }             pr[i][j] = in[i][j];           } else             pr[i][j] = in[i][j];         }         if (flag == 0) {           for (int j = ln - 1; j >= 0; j--) {             if (in[i][j] ==  ?  && pr[i][j] <  9 ) {               pr[i][j]++;               for (int z = j + 1; z < ln; z++)                 if (in[i][z] ==  ? ) pr[i][z] =  0 ;               flag = 1;               break;             }           }           if (flag == 0) {             printf( NO n );             return 0;           }         }       }       prv = ln;     }     printf( YES n );     for (int i = 0; i < n; i++) printf( %s n , pr[i]);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   using ll = long long;   using pi = pair<int, int>;   int main() {     ios_base::sync_with_stdio(false);     cin.tie(nullptr);     cout << setprecision(15) << fixed;     int n;     cin >> n;     vector<int> a(n);     for (int i = 0; i < n; i++) cin >> a[i];     sort(a.begin(), a.end());     int x = a[0];     for (int i = 1; i < n; i++) {       if (a[i] != x) {         if (a[i] < 30) {           ll y = (1ll << x) - 1;           if (y * y > (1ll << a[i]) - 1) {             cout <<  YES n ;             return 0;           }         } else if (2 * x > a[i]) {           cout <<  YES n ;           return 0;         }         x = a[i];       }     }     cout <<  NO n ;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXL = 5e5 + 1e3;   int n, t;   char dat[MAXL + 1];   int main() {     int n;     while (scanf( %d%d%s , &n, &t, dat) >= 1) {       int hl = n, hr = -1, hcnt = 0;       vector<int> shops;       for (int i = 0; i < n; i++)         if (dat[i] ==  H ) {           hl = min(hl, i);           hr = max(hr, i);           hcnt++;         } else if (dat[i] ==  S ) {           shops.push_back(i);         }       t--;       if (hr > t)         printf( -1 n );       else {         int L = -1, R = hcnt;         while (L + 1 < R) {           int M = (L + R) / 2;           assert(M < hcnt);           bool good = false;           int rem = hcnt - M;           if (rem <= ((int)(shops).size())) {             int maxpos = max(hr, shops[rem - 1]);             int ctim = maxpos;             int btim = 2e9;             int bal = M;             for (int i = 0; i <= maxpos; i++) {               if (dat[i] ==  H )                 bal--;               else if (dat[i] ==  S )                 bal++;               if (bal < 0) {                 btim = min(btim, ctim + maxpos - i);                 ctim += 2;               }             }             btim = min(btim, ctim);             if (btim <= t) good = true;           }           if (good)             R = M;           else             L = M;         }         printf( %d n , R);       }     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = 3e5 + 5;   const int INF = 1e9;   int N, p[MAXN], tag[MAXN << 2];   long long ans;   struct node {     int mn, cnt;     node(int mn = 0, int cnt = 0) : mn(mn), cnt(cnt) {}     friend node operator+(const node& l, const node& r) {       if (l.mn < r.mn)         return l;       else if (l.mn > r.mn)         return r;       else         return node(l.mn, l.cnt + r.cnt);     }   } t[MAXN << 2];   void pushup(int o) { t[o] = t[(o << 1)] + t[(o << 1 | 1)]; }   void add(int o, int k) { t[o].mn += k, tag[o] += k; }   void pushdown(int o) {     if (tag[o]) add((o << 1), tag[o]), add((o << 1 | 1), tag[o]), tag[o] = 0;   }   void build(int o, int l, int r) {     if (l == r)       t[o] = node(0, 1);     else       build((o << 1), l, ((l + r) >> 1)),           build((o << 1 | 1), ((l + r) >> 1) + 1, r), pushup(o);   }   void ins(int o, int l, int r, int L, int R, int K) {     if (r < L || l > R) return;     if (L <= l && r <= R) return add(o, K);     pushdown(o);     ins((o << 1), l, ((l + r) >> 1), L, R, K),         ins((o << 1 | 1), ((l + r) >> 1) + 1, r, L, R, K);     pushup(o);   }   node qry(int o, int l, int r, int L, int R) {     if (r < L || l > R) return node(INF, 0);     if (L <= l && r <= R) return t[o];     pushdown(o);     return qry((o << 1), l, ((l + r) >> 1), L, R) +            qry((o << 1 | 1), ((l + r) >> 1) + 1, r, L, R);   }   int s1[MAXN], l1, s2[MAXN], l2;   int main() {     scanf( %d , &N);     int x, y;     for (int i = 1; i <= N; ++i) scanf( %d%d , &x, &y), p[x] = y;     build(1, 1, N);     s1[0] = s2[0] = N + 1;     for (int i = N; i >= 1; --i) {       while (l1 && p[s1[l1]] < p[i]) {         ins(1, 1, N, s1[l1], s1[l1 - 1] - 1, p[i] - p[s1[l1]]), --l1;       }       s1[++l1] = i, ins(1, 1, N, i, i, p[i]);       while (l2 && p[s2[l2]] > p[i]) {         ins(1, 1, N, s2[l2], s2[l2 - 1] - 1, p[s2[l2]] - p[i]), --l2;       }       s2[++l2] = i, ins(1, 1, N, i, i, -p[i]);       if (i < N) ins(1, 1, N, i + 1, N, -1);       ans += qry(1, 1, N, i, N).cnt;     }     printf( %lld n , ans);     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long n, q, a[300010], f[300010], ans[300010], g[300010];   struct edge {     long long v, next;   } e[300010 << 1];   long long eid, p[300010];   void insert(long long u, long long v) {     e[++eid] = {v, p[u]};     p[u] = eid;   }   long long dep[300010], cnt, c[300010], d[300010], siz[300010];   pair<long long, long long> b[300010];   void dfs(long long u, long long f) {     siz[u] = 1;     c[u] = ++cnt, d[cnt] = u;     dep[u] = dep[f] + 1;     b[u] = make_pair(dep[u], c[u]);     for (int i = p[u]; i; i = e[i].next) {       long long v = e[i].v;       if (v == f) continue;       dfs(v, u);       siz[u] += siz[v];     }   }   void dfs1(long long u, long long f) {     g[u] += g[f];     for (int i = p[u]; i; i = e[i].next) {       long long v = e[i].v;       if (v == f) continue;       dfs1(v, u);     }   }   int main() {     ios::sync_with_stdio(false);     cin >> n;     for (int i = 1; i < n; ++i) {       long long u, v;       cin >> u >> v;       insert(u, v);       insert(v, u);     }     dfs(1, 0);     sort(b + 1, b + n + 1);     cin >> q;     while (q--) {       long long v, d, x;       cin >> v >> d >> x;       a[c[v]] += x, a[c[v] + siz[v]] -= x;       long long l =           lower_bound(b + 1, b + n + 1, make_pair(dep[v] + d + 1, c[v])) - b;       long long r = lower_bound(b + 1, b + n + 1,                                 make_pair(dep[v] + d + 1, c[v] + siz[v])) -                     b;       f[l] -= x, f[r] += x;     }     for (int i = 1; i <= n; ++i) a[i] += a[i - 1], f[i] += f[i - 1];     for (int i = 1; i <= n; ++i) g[d[b[i].second]] = f[i];     dfs1(1, 0);     for (int i = 1; i <= n; ++i) ans[d[i]] += a[i];     for (int i = 1; i <= n; ++i) cout << ans[i] + g[i] <<    ;     cout << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     string s1, s2;     cin >> s1;     cin >> s2;     int arr[27] = {};     int a[27] = {};     string s =   ;     for (int i = 0; i < s1.length(); i++) {       arr[int(s1[i]) - 97]++;     }     int count1 = 0;     for (int i = 0; i < s2.length(); i++) {       if (arr[int(s2[i]) - 97] == 0) {         cout << -1;         count1++;         break;       } else {         if (a[int(s2[i]) - 97] == 0) {           s += s2[i];         }         a[int(s2[i]) - 97]++;       }     }     long long int sum = 0;     if (count1 == 0) {       for (int i = 0; i < s.length(); i++) {         if (a[int(s[i]) - 97] <= arr[int(s[i]) - 97]) {           sum += a[int(s[i]) - 97];         } else {           sum += arr[int(s[i]) - 97];         }       }       cout << sum << endl;     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   void solve(int a[], int n) {     int cnt = 0, pos = 0;     while (pos < n) {       cnt++;       int mx = pos;       while (pos < n && pos <= mx) {         mx = max(mx, a[pos]);         pos++;       }     }     cout << cnt << endl;   }   int main() {     int n;     cin >> n;     int a[n];     for (int i = 0; i < n; i++) {       cin >> a[i];       a[i]--;     }     solve(a, n);     return 0;   }
#include <bits/stdc++.h>   struct DSU {     long long M[400005];     DSU(long long n) {       for (long long i = 0; i < n; i++) {         M[i] = i;       }     }     long long find(long long x) {       while (M[x] != x) {         long long p = M[x];         M[x] = M[p];         x = p;       }       return x;     }     bool join(long long x, long long y) {       long long xr = find(x);       long long yr = find(y);       if (xr == yr) {         return false;       } else {         M[xr] = yr;         return true;       }     }   };   using namespace std;   int main() {     long long n, m, q;     cin >> n >> m >> q;     DSU dsu(n + m);     long long z = n + m - 1;     for (long long i = 0; i < q; i++) {       long long r, c;       cin >> r >> c;       r--;       c--;       if (dsu.join(r, c + n)) z--;     }     cout << z;   }
#include <bits/stdc++.h>   const int N = 2e5;   using namespace std;   const int SIZE = 26;   int tot = 0, sub = 0;   struct node {     node* next[SIZE];     int strings;     int tin, tout;     node() {       for (int i = 0; i < SIZE; i++) {         next[i] = nullptr;       }       tin = 0;       tout = 0;       strings = 0;     }   };   void add(struct node* root, string key) {     struct node* cur_v = root;     for (size_t i = 0; i < key.length(); i++) {       char c = key[i];       if (cur_v->next[c -  a ] == nullptr) {         cur_v->next[c -  a ] = new node();       }       cur_v = cur_v->next[c -  a ];     }     cur_v->strings++;   }   int t[4 * N + 1], mx_id[4 * N + 1];   void pull(int v) {     if (t[2 * v] > t[2 * v + 1]) {       t[v] = t[2 * v];       mx_id[v] = mx_id[2 * v];     } else {       t[v] = t[2 * v + 1];       mx_id[v] = mx_id[2 * v + 1];     }   }   void upd(int v, int tl, int tr, int id, int x) {     if (tl == tr) {       t[v] = x;       mx_id[v] = id;       return;     }     int tm = (tl + tr) >> 1;     if (id <= tm) {       upd(2 * v, tl, tm, id, x);     } else {       upd(2 * v + 1, tm + 1, tr, id, x);     }     pull(v);   }   int pos = -1;   int mx = 0;   void calc(int v, int tl, int tr, int L, int R) {     if (tl == L && tr == R) {       if (mx < t[v]) {         mx = t[v];         pos = mx_id[v];       }       return;     }     int tm = (tl + tr) >> 1;     if (R <= tm) {       calc(2 * v, tl, tm, L, R);     } else if (L > tm) {       calc(2 * v + 1, tm + 1, tr, L, R);     } else {       calc(2 * v, tl, tm, L, tm);       calc(2 * v + 1, tm + 1, tr, tm + 1, R);     }   }   int t1, t2;   void dfs(struct node* v, int d = 0) {     t1++;     v->tin = t1;     if (v->strings > 0) {       upd(1, 1, N, t1, d);     }     for (int i = 0; i < SIZE; i++) {       if (v->next[i] == nullptr) {         continue;       }       dfs(v->next[i], d + 1);     }     if (d < 1) {       return;     }     v->tout = t1;     if (v->strings == 0) {       mx = 0;       pos = 0;       calc(1, 1, N, v->tin, v->tout);       upd(1, 1, N, pos, 0);       upd(1, 1, N, v->tin, d);       sub += mx - d;     }   }   char buf[N];   int main() {     int n;     scanf( %d , &n);     string s;     node* t = new node();     for (int i = 1; i <= n; i++) {       scanf( %s , buf);       s = buf;       int sz = s.size();       tot += sz;       add(t, s);     }     dfs(t);     printf( %d n , tot - sub);   }
#include <bits/stdc++.h>   using namespace std;   inline void setmin(int &x, int y) {     if (y < x) x = y;   }   inline void setmax(int &x, int y) {     if (y > x) x = y;   }   inline void setmin(long long &x, long long y) {     if (y < x) x = y;   }   inline void setmax(long long &x, long long y) {     if (y > x) x = y;   }   const int N = 100000;   const int inf = (int)1e9 + 1;   const long long big = (long long)1e18 + 1;   const int P = 239;   const int MOD = (int)1e9 + 7;   const int MOD1 = (int)1e9 + 9;   const double eps = 1e-9;   const double pi = atan2(0, -1);   const int ABC = 26;   vector<int> pos[N];   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.precision(20);     cout << fixed;     int n, m, s, e;     cin >> n >> m >> s >> e;     int a[n], b[m];     for (int i = 0; i < n; i++) {       cin >> a[i];       a[i]--;     }     for (int i = 0; i < m; i++) {       cin >> b[i];       b[i]--;       pos[b[i]].push_back(i);     }     int cnt = s / e;     int dp[n][cnt + 1];     for (int i = 0; i < n; i++)       for (int j = 0; j < cnt + 1; j++) dp[i][j] = inf;     for (int i = 0; i < n; i++) {       int p = 0;       dp[i][0] = -1;       for (int j = 1; j < min(i + 1, cnt) + 1; j++) {         if (i > 0) setmin(dp[i][j], dp[i - 1][j]);         int last_pos = (i == 0 ? -1 : dp[i - 1][j - 1]);         while (p < (int)pos[a[i]].size() && pos[a[i]][p] <= last_pos) p++;         if (p < (int)pos[a[i]].size()) setmin(dp[i][j], pos[a[i]][p]);       }     }     int ans = 0;     for (int i = 0; i < n; i++)       for (int j = 0; j < cnt + 1; j++)         if ((i + 1) + (dp[i][j] + 1) + j * e <= s) setmax(ans, j);     cout << ans <<   n ;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   long long modPow(long long a, long long b, long long m) {     if (b == 0) return 1;     if (b % 2 == 0) {       long long x = a * a;       x %= m;       return modPow(x, b / 2, m);     }     return (a * modPow(a, b - 1, m)) % m;   }   int main() {     ios_base::sync_with_stdio(0);     cin.tie(0);     cout.tie(0);     int t;     cin >> t;     while (t--) {       long long n, i, j;       cin >> n;       string s =   ;       for (i = 0; i < (53); i++) s +=  a ;       cout << s;       cout <<   n ;       for (i = 0; i < (n); i++) {         long long a;         cin >> a;         if (s[a] ==  a ) {           for (auto j = (a); j <= (52); j++) {             s[j] =  b ;           }         } else {           for (auto j = (a); j <= (52); j++) {             s[j] =  a ;           }         }         cout << s;         cout <<   n ;       }     }   }
#include <bits/stdc++.h>   using namespace std;   long long g, t;   long long gcd(long long a, long long b) { return b == 0 ? a : gcd(b, a % b); }   int main() {     int n;     long long Max = 0;     cin >> n;     for (int i = 0; i < n; i++) {       cin >> t;       if (i)         g = gcd(g, t);       else         g = t;       Max = max(Max, t);     }     long long cnt = Max / g - n;     if (cnt % 2 == 0)       printf( Bob n );     else       printf( Alice n );     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int n, T, a[200010], x, y, d[200010], q[200010], l, r;   vector<int> v[200010], Q;   int main() {     scanf( %d , &T);     while (T--) {       scanf( %d , &n);       for (int i = 1; i <= n; i++)         scanf( %d , &a[i]), a[i] = 2 - a[i], v[i].clear();       for (int i = 1; i < n; i++) {         scanf( %d%d , &x, &y);         v[x].push_back(y), v[y].push_back(x);       }       if (n == 1) {         puts( 1 );         continue;       }       int Ans = n;       for (int col = 0; col < 2; col++) {         int ans = 0;         for (int i = 1; i <= n; i++) d[i] = v[i].size();         l = 1, r = 0, Q.clear();         for (int i = 1; i <= n; i++)           if (d[i] == 1) {             if (a[i] != col)               q[++r] = i;             else               Q.push_back(i);           }         while (1) {           while (l <= r) {             x = q[l++];             for (int j = 0, sz = v[x].size(); j < sz; j++)               if ((--d[v[x][j]]) == 1) {                 if (a[v[x][j]] == (col ^ (ans & 1)))                   Q.push_back(v[x][j]);                 else                   q[++r] = v[x][j];               }           }           ans++;           if (r == n) {             Ans = min(Ans, ans);             break;           }           for (int j = 0, sz = Q.size(); j < sz; j++) q[++r] = Q[j];           Q.clear();         }       }       printf( %d n , Ans);     }     return 0;   }
#include <bits/stdc++.h>   using namespace std;   int main() {     ios_base::sync_with_stdio(0);     cin.tie(NULL);     ;     int t, n;     string s, p;     cin >> t;     while (t--) {       cin >> n;       cin >> s;       for (int j = 0; j < (2 * n - 1); j += 2) {         cout << s[j];       }       cout <<   n ;     }   }
#include <bits/stdc++.h>   using namespace std;   int dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};   void solve() {     int n;     cin >> n;     int a[n + 1];     for (int i = 0; i < n; i++) {       int x;       cin >> x;       a[x] = i;     }     int l = 0, r = n - 1;     for (int i = n; i > 0; i--) {       if (a[i] == l) {         l++;         continue;       }       if (a[i] == r) {         r--;         continue;       }       cout <<  YES  << endl            << a[i] - 1 + 1 <<     << a[i] + 1 <<     << a[i] + 1 + 1 << endl;       return;     }     cout <<  NO  << endl;   }   int main() {     ios::sync_with_stdio(false);     cin.tie(0);     cout.tie(0);     int t = 1;     cin >> t;     while (t--) {       solve();     }     return 0;   }
#include <bits/stdc++.h>   const int MAXN = 2e5 + 10;   int n;   int a[MAXN], b[MAXN];   namespace bit {   int a[MAXN];   void init() { std::fill(a + 1, a + n + 1, 0); }   void add(int x, int y) {     for (; x <= n; x += x & (-x)) {       a[x] += y;     }   }   int query(int x) {     int ans = 0;     for (; x > 0; x -= x & (-x)) {       ans += a[x];     }     return ans;   }   }  // namespace bit   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; i++) {       scanf( %d , a + i);       a[i]++;     }     for (int i = 1; i <= n; i++) {       scanf( %d , b + i);       b[i]++;     }     static int p[MAXN], q[MAXN];     for (int i = 1; i <= n; i++) {       p[i] = a[i] - bit::query(a[i]) - 1;       bit::add(a[i], 1);     }     bit::init();     for (int i = 1; i <= n; i++) {       q[i] = b[i] - bit::query(b[i]) - 1;       bit::add(b[i], 1);     }     static int sum[MAXN];     for (int i = n; i >= 1; i--) {       sum[i] += p[i] + q[i];       int k = n - i + 1;       sum[i - 1] = sum[i] / k;       sum[i] %= k;     }     bit::init();     for (int i = 1; i <= n; i++) {       int l = 1, r = n, ans = 0;       while (l <= r) {         int mid = (l + r) >> 1;         if (mid - bit::query(mid) - 1 >= sum[i]) {           ans = mid;           r = mid - 1;         } else {           l = mid + 1;         }       }       bit::add(ans, 1);       printf( %d  , ans - 1);     }     printf(  n );   }
#include <bits/stdc++.h>   using namespace std;   const int MAXN = int(1e4) + 4;   int a[MAXN], h[MAXN], c[MAXN];   int n;   int main() {     scanf( %d , &n);     for (int i = 1; i <= n; ++i) {       scanf( %d , &a[i]);       h[i] = a[i];       int j = i - 1;       while (h[j] > h[i] && j > 0) {         h[i] += a[j];         c[i]++;         j--;       }       c[i] += c[j];     }     cout << c[n] << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   #pragma GCC optimize( Ofast )   #pragma GCC target( avx,avx2,fma )   const int N = 2e5 + 3;   const int MOD = 998244353;   int max1 = 0, max2 = 0;   void solve() {     long long int a[3];     cin >> a[0] >> a[1] >> a[2];     sort(a, a + 3);     cout << (a[0] + a[1] + min(a[2], 2 * (a[1] + a[0]))) / 3 << endl;   }   int main() {     ios_base::sync_with_stdio(false);     cin.tie(NULL);     int t = 1;     while (t--) {       solve();     }   }
#include <bits/stdc++.h>   using namespace std;   vector<int> cc[100000];   int num[100000];   int main() {     int n, k, m;     cin >> n >> k >> m;     for (int j = 0; j < n; cin >> num[j++])       ;     sort(num, num + n);     for (int j = 0; j < m; cc[j++].clear())       ;     cc[0].push_back(0);     for (int j = 1; j < n; j++) cc[(num[j] - num[0]) % m].push_back(j);     for (int j = 0; j < m; j++) {       if (cc[j].size() >= k) {         cout <<  Yes  << endl;         for (int i = 0; i < k; i++) {           cout << num[cc[j][i]] <<    ;         }         return 0;       }     }     cout <<  No  << endl;     return 0;   }
#include <bits/stdc++.h>   using namespace std;   const int maxn = 31;   const int mod = 1000000007;   struct ma {     long long a[maxn][maxn];     int n;     ma(int m = 0) {       n = m;       memset(a, 0, sizeof(a));       for (int i = 0; i < n; i++) a[i][i] = 1;     }     ma(const ma& nn) {       n = nn.n;       memcpy(a, nn.a, sizeof(a));     }     ma operator*(const ma& nn) {       ma ret(n);       memset(ret.a, 0, sizeof(ret.a));       for (int i = 0; i < n; i++)         for (int j = 0; j < n; j++) {           for (int k = 0; k < n; k++) ret.a[i][j] += a[i][k] * nn.a[k][j] % mod;           ret.a[i][j] %= mod;         }       return ret;     }   };   ma mypow(ma a, long long n) {     ma ret(a.n);     while (n) {       if (n & 1) ret = ret * a;       n >>= 1;       a = a * a;     }     return ret;   }   ma m1[100], m2[100];   int main() {     long long n;     int k, i, j;     cin >> n >> k;     n--;     m1[1].n = k + 1;     for (i = 0; i <= k; i++) m1[1].a[i][i] = 1;     for (i = 1; i < 99; i++) {       int tmp = ((-i - 1) % k + k) % k + 1;       tmp = k - tmp;       memcpy(m2[i].a[0], m1[i].a[k], sizeof(m1[i].a[k]));       for (j = 1; j <= k; j++) {         for (int c = 0; c <= k; c++)           m2[i].a[j][c] =               ((m2[i].a[j - 1][c] * 2 - m1[i].a[tmp][c]) % mod + mod) % mod;         tmp++;         if (tmp >= k) tmp = 0;       }       m2[i].n = k + 1;       m1[i + 1] = m1[i] * mypow(m2[i], k - 1);     }     ma ans(k + 1);     int ind = 1, sheng = n % k;     n /= k;     while (n) {       ans = ans * mypow(m2[ind], n % k);       ind++, n /= k;     }     long long ret = 0;     for (i = 0; i <= k; i++) ret = (ret + ans.a[sheng + 1][i] * (1ll << i)) % mod;     cout << ret << endl;   }