0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-12-27 08:12:00 +00:00
OI-codes/Luogu/P7893/P7893.cpp

26 lines
432 B
C++
Raw Normal View History

2021-10-03 11:21:46 +00:00
#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;
}