LA 5916 (GCD Guessing Game-prime grouping)

Source: Internet
Author: User

Now there is a number x, 1 ≤ x ≤ n, which tells you n. You can guess a number y each time. If x = y, it ends. Otherwise, gcd (x, y) is returned ), you can guess the answer only a few times.


Input
The input file contains several test cases, each of them as described below.

The input contains one integer n, 2n10000.


Output
For each test case, write to the output on a line by itself.

Output one integer -- the number of guesses Andrew will need to make in the worst case.


Sample Input

6

Sample Output


2

First, we group all the elements within n, and each time we ask for the product of a group of prime numbers -- this number is determined based on the nature of Gcd.

Each greedy match is a large prime number and a bunch of small prime numbers (rightmost X leftmost)


[Cpp]
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <functional>
# Include <iostream>
# Include <cstdlib>
# Include <cmath>
Using namespace std;
# Define MAXN (10000)
# Define eps (1e-9)
# Define For (I, n) for (int I = 1; I <= n; I ++)
# Define ForD (I, n) for (int I = n; I --)
# Define Fork (I, k, n) for (int I = k; I <= n; I ++)
Int n, a [MAXN], size = 0;
Bool B [MAXN];
Int main ()
{
Memset (B, 0, sizeof (B); B [1] = 1;
Fork (I, 2, MAXN)
{
If (! B [I]) B [I] = 1, a [++ size] = I;
For (j, size)
{
If (I * a [j]> MAXN) break;
B [I * a [j] = 1;
If (! I % a [j]) break;
}
}
// For (I, 100) cout <a [I] <'';
While (cin> n)
{
Int I = 0, head = 1, tail = size;
While (a [tail]> n) tail --;
While (head <= tail)
{
Int p = a [tail];
While (p * a [head] <= n) p * = a [head ++];
Tail --; I ++;
}
Cout <I <endl;
}
Return 0;
}

# Include <cstdio>
# Include <cstring>
# Include <algorithm>
# Include <functional>
# Include <iostream>
# Include <cstdlib>
# Include <cmath>
Using namespace std;
# Define MAXN (10000)
# Define eps (1e-9)
# Define For (I, n) for (int I = 1; I <= n; I ++)
# Define ForD (I, n) for (int I = n; I --)
# Define Fork (I, k, n) for (int I = k; I <= n; I ++)
Int n, a [MAXN], size = 0;
Bool B [MAXN];
Int main ()
{
Memset (B, 0, sizeof (B); B [1] = 1;
Fork (I, 2, MAXN)
{
If (! B [I]) B [I] = 1, a [++ size] = I;
For (j, size)
{
If (I * a [j]> MAXN) break;
B [I * a [j] = 1;
If (! I % a [j]) break;
}
}
// For (I, 100) cout <a [I] <'';
While (cin> n)
{
Int I = 0, head = 1, tail = size;
While (a [tail]> n) tail --;
While (head <= tail)
{
Int p = a [tail];
While (p * a [head] <= n) p * = a [head ++];
Tail --; I ++;
}
Cout <I <endl;
}
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.