intmain(){ int T; scanf("%d", &T); while (T--) { ll n, k; cin >> n >> k; ll ans = 0; while (n) { ans += n % k; n -= n % k; if (n) ans++; n /= k; } cout << ans << '\n'; } return0; }
#include<iostream> #include<cstdio> #include<cstring> #include<cmath> #include<algorithm> #include<vector> #include<utility> #ifdef XLor #define dbg(args...) do {cout << #args << " ->"; err(args);} while (0) #else #define dbg(...) #endif void err() {std::cout << std::endl;} template<typename T, typename...Args> void err(T a, Args...args){std::cout << a << ' '; err(args...);} #define ms(a,b) memset(a,b,sizeof(a)) using namespace std; typedef long long ll; typedef pair<int,int> PII; const int mod = 998244353; const int inf = 1 << 30; const int maxn = 200000 + 5; int n, k, a[maxn]; PII b[maxn]; int tot; int check(int x) { int l = 1, r = 1, c = 0; while (r <= n) { if (a[r] - x <= a[l] + x) { r++; c++; if (c == k + 1) { tot = a[l] + x; return 1; } } else { l++; c--; } } return 0; } int main() { int T; scanf("%d", &T); while (T--) { scanf("%d%d", &n, &k); for (int i = 1; i <= n; i++) scanf("%d", a + i); if (k == 0) { printf("%d\n", a[1]); continue; } int l = 1, r = 1e9, ans = 0; while (l <= r) { int m = (l + r) / 2; if (check(m)) r = m - 1, ans = tot; else l = m + 1; } printf("%d\n", ans); } return 0; }
int dp[maxn][20]; voidinit(){ for (int i = 1; i <= n; i++) dp[i][0] = i; for (int j = 1; j < 20; j++) for (int i = 1; i + (1 << j) <= n + 1; i++) { if (suf[dp[i][j - 1]] < suf[dp[i + (1 << (j - 1))][j - 1]]) dp[i][j] = dp[i + (1 << (j - 1))][j - 1]; else dp[i][j] = dp[i][j - 1]; } } intrmq(int l, int r){ int k = 0; while ((1 << (k + 1)) <= r - l + 1) k++; returnmax(dp[l][k], dp[r - (1 << k) + 1][k]); }
intmain(){ scanf("%d%d", &n, &k); ll ans = 0; for (int i = 1; i <= n; i++) scanf("%d", a + i); for (int i = n; i >= 1; i--) suf[i] = a[i] + suf[i + 1]; ans = suf[1]; vector<ll> v; for (int i = 2; i <= n; i++) v.push_back(suf[i]); sort(v.begin(), v.end(), [](ll a, ll b) { return a > b; }); for (int i = 0; i < k - 1; i++) ans += v[i]; cout << ans; return0; }
int n, q, l[maxn], r[maxn], nxt[maxl][20]; vector<int> seg[maxl];
intmain(){ scanf("%d%d", &n, &q); for (int i = 1; i <= n; i++) { scanf("%d%d", l + i, r + i); seg[l[i]].push_back(r[i]); } int mx = -1; for (int i = 0; i < maxl; i++) { for (int& x: seg[i]) mx = max(mx, x); if (mx >= i) nxt[i][0] = mx; else nxt[i][0] = i; } for (int j = 1; j < 20; j++) { for (int i = 0; i < maxl; i++) { nxt[i][j] = nxt[nxt[i][j - 1]][j - 1]; } } while (q--) { int x, y; scanf("%d%d", &x, &y); int now = x, ans = 0; if (nxt[x][19] < y) { puts("-1"); continue; } for (int i = 19; i >= 0; i--) { if (nxt[now][i] < y) { ans += 1 << i; now = nxt[now][i]; } } if (now < y) ans++; printf("%d\n", ans); } return0; }