-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy path10311.cpp
More file actions
87 lines (81 loc) · 1.22 KB
/
10311.cpp
File metadata and controls
87 lines (81 loc) · 1.22 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include<stdio.h>
#include<math.h>
#define max(a,b) (a>b ?a:b)
#define min(a,b) (a<b ?a:b)
#define si 100000010
#define si_pr 6000000
int ara[si/32],ll=1,pr[si_pr];
int check(int m,int pos)
{
return (m&(1<<pos));
}
int set(int m,int pos)
{
return (m|(1<<pos));
}
void sieve()
{
int i,j,num=si-5,root=sqrt(num);
pr[ll++]=2;
ara[0/32]=ara[1/32]=1;
for(i=3;i<=num;i+=2)
{
if(check(ara[i>>5],i&31)==0)
{
pr[ll++]=i;
if(i<=root)
{
for(j=i*i;j<=num;j+=2*i)
ara[j>>5]=set(ara[j>>5],j&31);
}
}
}
}
int main()
{
int l,h,i,j,k,fg,nn,m,n;
sieve();
while(~scanf("%d",&n))
{
if(n%2)
{
m=n-2;
if(check(ara[m>>5],m&31)==0&&m>2)
printf("%d is the sum of 2 and %d.\n",n,m);
else
printf("%d is not the sum of two primes!\n",n);
continue;
}
l=1;h=ll-1;
nn=n/2;
while(l<=h)
{
m=(l+h)/2;
if(pr[m]>nn)
h=m-1;
else
l=m+1;
}
fg=0;
for(i=h;i>=1;i--)
{
m=n-pr[i];
if(m<=1)
break;
if((m%2==0&&m!=2)||m==pr[i])
continue;
if(check(ara[m>>5],m&31)==0)
{
j=min(pr[i],m);
k=max(m,pr[i]);
fg=1;
break;
}
}
if(fg)
printf("%d is the sum of %d and %d.\n",n,j,k);
else
printf("%d is not the sum of two primes!\n",n);
}
return 0;
}