-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10852.cpp
More file actions
49 lines (46 loc) · 687 Bytes
/
10852.cpp
File metadata and controls
49 lines (46 loc) · 687 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include<cstdio>
#include<vector>
#include<cstring>
using namespace std;
vector<int> prime;
bool sieve[10005];
void linear_sieve()
{
memset(sieve,0,10005);
sieve[0] = sieve[1] = true;
for(int i=2;i<10005;i++)
{
if(!sieve[i])
{
prime.push_back(i);
}
for(int j=0;j<prime.size() && i *prime[j] <10005;j++)
{
sieve[i*prime[j]] =true;
if(i % prime[j]==0)
break;
}
}
}
int main()
{
int p,x,n,m;
scanf("%d",&m);
linear_sieve();
while(m--)
{
scanf("%d",&n);
int ans=0,diff=0;
for(int i=0;i<prime.size() && prime[i]<n;i++)
{
p = n % prime[i];
if(p > diff)
{
diff = p;
ans = prime[i];
}
}
printf("%d\n",ans);
}
return 0;
}