HDU1215 七夕节 筛法
七夕节
1 /*
2 写于13年3月28日,最朴素直接的算法
3 改用筛法重写了一遍,没想到朴素法居然不超时,毁三观啊
4 */
5
6 #include <iostream>
7 #include <stdio.h>
8 #include <string.h>
9 #include <math.h>
10 using namespace std;
11 const int maxn=500005;
12 long long data[maxn];
13 int main()
14 {
15 int t;
16 int n;
17 memset(data,0,sizeof(data));
18 for(int i=2;i<maxn/2;i++)
19 for(int j=i+i;j<maxn;j+=i)
20 data[j]+=i;
21 /*
22 long long ans;
23 while(scanf("%d",&t)!=EOF)
24 {
25 while(t--)
26 {
27 ans=0;
28 scanf("%d",&n);
29 int sqr=(int )sqrt((double)n);
30 for(int i=2;i<=sqr;i++)
31 {
32 if(n%i==0)
33 {
34 if(i!=n/i)
35 ans+=i+n/i;//sqrt(n)两侧的数都算
36 else
37 ans+=i;
38 }
39 }
40 ans++;//加1
41 printf("%lld\n",ans);
42 }
43 }
44 */
45 while(scanf("%d",&t)!=EOF)
46 {
47 while(t--)
48 {
49 scanf("%d",&n);
50 if(n==1)
51 printf("0\n");
52 else
53 printf("%lld\n",data[n]+1);
54 }
55 }
56
57 return 0;
58 }