0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-19 21:45:25 +00:00
OI-codes/Luogu/P7893/P7893.cpp

26 lines
432 B
C++

#include <bits/stdc++.h>
using namespace std;
int t;
long long n, p, f, ans;
int main() {
scanf("%d", &t);
while (t--) {
ans = 0;
f = 1;
scanf("%lld%lld", &n, &p);
if (p == 1) {
printf("0\n");
continue;
}
for (long long k = n; k; k /= p) {
ans += f * k;
f = -f;
}
printf("%lld\n", ans);
}
return 0;
}