标签:
#include <stdio.h> #include <math.h> #define LL long long LL a[100005]; int main() { LL n, k; int i, j; while(~scanf("%lld %lld", &n, &k)) { int Len = 0; while(n) a[Len++] = n % k, n /= k; if(Len % 2 == 0) { LL ans = pow(k, Len/2); printf("%lld\n", ans); continue; } i = Len - 2; for(int flag = 1; i >= 1 && flag; i -= 2) { if(a[i]) { for(j = i-1; j >= 0; j -=2) { a[j] = k-1; flag = 0; } } } LL ans = 0, mul = 1; for(i = 0; i < Len; i += 2) { ans += mul * a[i]; mul *= k; } printf("%lld\n", ans+1); } return 0; }
标签:
原文地址:http://www.cnblogs.com/tenlee/p/4907450.html