CF449 C. Jzzhu and Apples

 1 /*
 2 http://codeforces.com/problemset/problem/449/C
 3 cf 449 C. Jzzhu and Apples
 4 数论+素数+贪心
 5 */
 6 #include <cstdio>
 7 #include <algorithm>
 8 using namespace std;
 9 const int Nmax=100005;
10 int is_prime[Nmax];
11 int book[Nmax];
12 int cnt[Nmax];
13 int n,ans;
14 void get__prime()
15 {
16     for(int i=2;i<=n;i++)
17         is_prime[i]=1;
18     for(int i=2;i<=n;i++)
19     {
20         if(is_prime[i])
21         {
22             for(int j=2;j*i<=n;j++)
23             {
24                 is_prime[i*j]=0;
25             }
26         }
27     }
28 }
29 
30 int main()
31 {
32     //freopen("cf449C.in","r",stdin);
33     scanf("%d",&n);
34     get__prime();
35     for(int i=n;i>=2;i--)
36     {
37         if(is_prime[i])
38         {
39             cnt[i]=1;
40             for(int j=2;i*j<=n;j++)
41             {
42                 if(!book[i*j])
43                 {
44                     cnt[i]++;
45                     book[i*j]=1;
46                 }
47             }
48             if(cnt[i]&1)
49                 book[2*i]=0;
50             ans+=cnt[i]>>1;
51         }
52     }
53     printf("%d\n",ans);
54     for(int i=1;i<=n;i++)
55         book[i]=0;
56     for(int i=n;i>=2;i--)
57     {
58         if(is_prime[i] && cnt[i]>1)
59         {
60             if(cnt[i]&1)
61             {
62                 int flag=1;
63                 for(int j=1;i*j<=n;j++)
64                 {
65                     if(j==2)
66                         continue;    
67                     if(!book[i*j])
68                     {
69                         printf("%d",i*j);
70                         if(flag)
71                             printf(" ");
72                         else
73                             printf("\n");
74                         flag^=1;
75                         book[i*j]=1;
76                     }
77                 }
78             }
79             else
80             {
81                 int flag=1;
82                 for(int j=1;i*j<=n;j++)
83                 {    
84                     if(!book[i*j])
85                     {
86                         printf("%d",i*j);
87                         if(flag)
88                             printf(" ");
89                         else
90                             printf("\n");
91                         flag^=1;
92                         book[i*j]=1;
93                     }
94                 }
95             }
96         }
97     }
98     return 0;
99 }

 

posted @ 2017-03-24 13:22  BBBob  阅读(295)  评论(0编辑  收藏  举报