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 }

 

转载于:https://www.cnblogs.com/caoyuanlang/archive/2013/04/03/2997291.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值