思路:把【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 ",ans); 64 } 65 #ifdef Local 66 cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl; 67 #endif 68 }