Let n be an integer, 100 ≤ n ≤ 10000, find the prime number x, x ≤ n, so that n − p ∗ x is maximum, where p is an integer such that p ∗ x ≤ n < (p + 1) ∗ x.
Input
The first line of the input contains an integer, M, indicating the number of test cases. For each test case, there is a line with a number N, 100 ≤ N ≤ 10000.
Output
For each test case, the output should consist of one line showing the prime number that verifies the condition above.
Sample Input
5
4399
614
8201
101
7048
Sample Output
2203
311
4111
53
3527
问题链接:UVA10852 Less Prime
问题简述:给定一个整数n,求一个素数x,满足p*x ≤ n < (p+1)x,且n - px 最大。
问题分析:
简单题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* UVA10852 Less Prime */
#include <bits/stdc++.h>
using namespace std;
const int N = 10000;
bool isprime[N + 1];
int prime[N / 3], pcnt = 0, maxm[N + 1];
// 欧拉筛
void eulersieve(void)
{
memset(isprime, true, sizeof(isprime));
isprime[0] = isprime[1] = false;
for(int i = 2; i <= N; i++) {
if(isprime[i])
prime[pcnt++] = i;
for(int j = 0; j < pcnt && i * prime[j] <= N; j++) { //筛选
isprime[i * prime[j]] = false;
if(i % prime[j] == 0) break;
}
}
for (int i = 2 ; i <= N ; i++) {
maxm[i] = prime[0];
for (int j = 1 ; j < pcnt ; j++) {
if (prime[j] >= i) break;
if (i % maxm[i] < i % prime[j])
maxm[i] = prime[j];
}
}
}
int main()
{
eulersieve();
int m, n;
scanf("%d", &m);
while(m--) {
scanf("%d", &n);
printf("%d\n", maxm[n]);
}
return 0;
}
来源:CSDN
作者:海岛Blog
链接:https://blog.csdn.net/tigerisland45/article/details/104551057