LightOJ - 1234(分区间打表+枚举)

本文介绍了一种通过预处理哈希表来高效求解特定数学问题的算法。该算法将1到1e8范围内的数据按每50个一组进行分组,并预先计算各组的累积和,以加速后续查询过程中的计算效率。适用于需要频繁查询累加和的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

思路:把【1,1e8】分成每50一个区间,然后打表记录是第几个50,出的部分之间枚举,不会超过50,就是打表速度略蛋疼。

 1 #include <iostream>
 2 #include <queue>
 3 #include <stack>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <bitset>
 9 #include <algorithm>
10 #include <cmath>
11 #include <cstring>
12 #include <cstdlib>
13 #include <string>
14 #include <sstream>
15 #include <time.h>
16 #define x first
17 #define y second
18 #define pb push_back
19 #define mp make_pair
20 #define lson l,m,rt*2
21 #define rson m+1,r,rt*2+1
22 #define mt(A,B) memset(A,B,sizeof(A))
23 using namespace std;
24 typedef long long LL;
25 //const double PI = acos(-1);
26 const int N=1e8+10;
27 const LL mod=1e9+7;
28 const int inf = 0x3f3f3f3f;
29 const LL INF=0x3f3f3f3f3f3f3f3fLL;
30 double ant[N/50+10];
31 void init()
32 {
33     double ans=0;
34     ant[0]=0.0;
35     for(int i=1;i<N;i++)
36     {
37         ans+=(double)1.0/i;
38         if(i%50==0)ant[i/50]=ans;
39     }
40 }
41 int main()
42 {
43 #ifdef Local
44     freopen("data.txt","r",stdin);
45 #endif
46     //ios::sync_with_stdio(false);
47     //cin.tie(0);
48     int T;
49     cin>>T;
50     init();
51     LL n,t;
52     double ans;
53     for(int d=1; d<=T; d++)
54     {
55         printf("Case %d: ",d);
56         cin>>n;
57         t=n/50;
58         ans=ant[t];
59         for(LL i=t*50+1;i<=n;i++)
60         {
61             ans=ans+(double)1.0/i;
62         }
63         printf("%.10lf\n",ans);
64     }
65 #ifdef Local
66     cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
67 #endif
68 }
View Code

 

转载于:https://www.cnblogs.com/27sx/p/6666917.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值