LightOJ1234(调和级数、有通项公式的发散数列求和)
调和求和(1/1+1/2+1/3+……..+1/n)
可⽤于⼤数有通项公式的求和计算
利⽤分组来平衡和时间
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
usingnamespacestd;
#definelllonglong
constintMAXN=100000000+100;
constintmod=100;//组距
doublea[MAXN/mod];//存储每组开头的数的⼤⼩
voidInit()
{
doubletmp=1.0;
a[0]=0.0;
for(inti=2;i<=MAXN;i++)
{
tmp+=1.0/i;
if(i%mod==0)
{
a[i/mod]=tmp;
}
}
}
intmain()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
intT;
scanf("%d",&T);
Init();
for(intcas=1;cas<=T;cas++)
{
intn;
scanf("%d",&n);
intp=n/mod;
doubleans=a[p];
for(inti=p*mod+1;i<=n;i++)
{
ans+=1.0/i;
}
printf("Ca%d:%.10lfn",cas,ans);
}
return0;
}
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
本文发布于:2022-11-12 18:21:03,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/88/5908.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |