题目链接:http://codeforces.com/problemset/problem/1175/A
AC代码:
1 #include<cstdio> 2 #include<iostream> 3 #include<set> 4 using namespace std; 5 int main() 6 { 7 int t; 8 scanf("%d",&t); 9 while(t--) 10 { 11 long long n; 12 long long k; 13 long long ans = 0; 14 scanf("%lld %lld",&n,&k); 15 while(n) 16 { 17 if(n % k) 18 ans += n%k, n = n/k*k; 19 else 20 { 21 ans++; 22 n /= k; 23 } 24 25 } 26 cout << ans << endl; 27 } 28 29 return 0; 30 }