Ultraviolet A 11246-K-multiple free set (number theory reasoning)

Source: Internet
Author: User
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;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.