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)