Ultraviolet A 1521-GCD guessing game (number theory + greedy)

Source: Internet
Author: User
Ultraviolet A 1521-GCD guessing game

Question Link

Assume that the number X is between 1 and N. Now, you can guess the number a, tell the answer to gcd (X, A), and guess the number several times in the worst case.

Idea: In terms of prime numbers, you can guess the numbers composed of these prime numbers by guessing the product of a group of prime numbers. The answer is the number of groups, this problem is how to minimize the number of groups by grouping the last one. Try to merge the last one with a smaller number to minimize the number of groups.

Code:

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 10005;int vis[N], prime[N], pn = 0;void getprime() {for (int i = 2; i < N; i++) {if (vis[i]) continue;prime[pn++] = i;for (int j = i * i; j < N; j += i)vis[j] = 1;}}int n;int main() {getprime();while (~scanf("%d", &n)) {int ans = 0, head = 0, rear = pn - 1;while (prime[rear] > n) rear--;while (head <= rear) {int p = prime[rear];while (p * prime[head] <= n) p *= prime[head++];ans++;rear--;}printf("%d\n", ans);}return 0;}


Ultraviolet A 1521-GCD guessing game (number theory + greedy)

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.