hdu 4710 Balls Rearrangement()

http://acm.hdu.edu.cn/showproblem.php?pid=4710

【code】:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 
 6 using namespace std;
 7 #define N 1000151
 8 
 9 int prim[N+10];
10 int hash[1000000];
11 int mark[1000010];
12 int cjsb[1000010];
13 int hash_cnt=0;
14 int lowbit(int i)
15 {
16     return i&-i;
17 }
18 void add(int i,int a)
19 {
20     while(i<=N)
21     {
22         cjsb[i]+=a;
23         i+=lowbit(i);
24     }
25 }
26 int sum(int i)
27 {
28     int s=0;
29     while(i>0)
30     {
31         s+=cjsb[i];
32         i-=lowbit(i);
33     }
34     return s;
35 }
36 int main()
37 {
38     int i,j;
39     hash_cnt=0;
40     for(i=2;i<=N;i++)
41     {
42         if(!prim[i])
43         {
44             hash[hash_cnt++]=i;
45             for(j=2;j*i<=N;j++)
46             {
47                 prim[j*i]=1;
48             }
49         }
50     }
51    // cout<<hash_cnt<<" "<<hash[hash_cnt-1]<<endl;
52     int tou=0;
53     memset(cjsb,0,sizeof(cjsb));
54     for(i=1;i<hash_cnt;i++)
55     {
56         for(j=1;j<=i-1;j++)
57         {
58             int temp = hash[i]-hash[j];
59             if(temp<=tou)
60                 break;
61             if(mark[temp])  continue;
62             else
63             {
64                 mark[temp] = hash[i];
65                 add(temp,1);
66                 if(sum(temp)==temp/2)
67                     tou=temp;
68             }
69         }
70     }
71     int n;
72     scanf("%d",&n);
73     while(n--)
74     {
75         int m;
76         scanf("%d",&m);
77         if(m==0)
78         {
79             puts("2 2");
80             continue;
81         }
82         if(m<0)
83         {
84             m=-m;
85             printf("%d %d
",mark[m]-m,mark[m]);
86         }
87         else
88         {
89             printf("%d %d
",mark[m],mark[m]-m);
90         }
91     }
92     return 0;
93 }
原文地址:https://www.cnblogs.com/crazyapple/p/3310975.html