Ultraviolet A 11246-K-multiple free Set
Question Link
Question: a set of {1. n}. Calculate a subset so that the number of elements is the maximum and there are no two elements X1 * k = x2. Then, find the maximum number of elements.
Train of Thought: reasoning,
N at the beginning
Delete the K times, which is {k, 2 K, 3 K, 4 K, 5 K, 6 K ...}, the excess K ^ 2 will be deleted, so the number of K ^ 2 will be added back.
Then, only the multiples of K ^ 2 are displayed in the current set. Therefore, you can perform this operation repeatedly on the number k ^ 2 times as a new set, therefore, the final answer is N-N/K + N/(K ^ 2)-N/(K ^ 3) + N/(K ^ 4 )...
Code:
#include <stdio.h>#include <string.h>int t, n, k;int solve(int n, int k) { int sign = 1, ans = 0; while (n) {ans += sign * n;n /= k;sign = - sign; } return ans;}int main() { scanf("%d", &t); while (t--) {scanf("%d%d", &n, &k);printf("%d\n", solve(n, k)); } return 0;}