【HDOJ6217】BBP Formula(公式)

题意:给定一个无穷项的分式,它的和等于π,问π的十六进制表示的小数点后第n位是多少

1 ≤ n ≤ 100000

思路:From https://blog.csdn.net/meopass/article/details/78327614

C++double写起来细节真是烦

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<string>
 4 #include<cmath>
 5 #include<iostream>
 6 #include<algorithm>
 7 #include<map>
 8 #include<set>
 9 #include<queue>
10 #include<vector>
11 using namespace std;
12 typedef long long ll;
13 typedef unsigned int uint;
14 typedef unsigned long long ull;
15 typedef pair<int,int> PII;
16 typedef vector<int> VI;
17 #define fi first
18 #define se second 
19 #define MP make_pair
20 #define N   11000
21 #define M   41
22 #define eps 1e-8
23 #define pi acos(-1)
24 
25 
26 
27 int read()
28 { 
29    int v=0,f=1;
30    char c=getchar();
31    while(c<48||57<c) {if(c=='-') f=-1; c=getchar();}
32    while(48<=c&&c<=57) v=(v<<3)+v+v+c-48,c=getchar();
33    return v*f;
34 }
35 
36 ll Pow(ll a,ll b,ll MOD)
37 {
38     ll ans=1;
39     while(b)
40     {
41         if(b&1) ans=ans*a%MOD;
42         a=a*a%MOD;
43         b>>=1;
44     }
45     return ans;
46 }
47 
48 double BBP(int n,ll k,ll b)
49 {
50     double ans=0;
51     for(int i=0;i<=n;i++) ans+=(Pow(16,n-i,8*i+b)*1.0/(8*i+b));
52     for(int i=n+1;i<=n+1001;i++) ans+=(powf(16,n-i)/(8*i+b));
53     return k*ans;
54 }
55 
56 int main()
57 {
58     //freopen("hdoj6217.in","r",stdin);
59     //freopen("hdoj6217.out","w",stdout);
60     int cas;
61     scanf("%d",&cas);
62     for(int v=1;v<=cas;v++)
63     {
64         int n;
65         scanf("%d",&n);
66         n--;
67         double ans=0.0;
68         ans=BBP(n,4,1)+BBP(n,-2,4)+BBP(n,-1,5)+BBP(n,-1,6);
69         ans-=int(ans);
70         if(ans<0) ans++;
71         ans*=16;
72         char ch;
73         int p=int(ans);
74         if(0<=p&&p<=9) ch=p+'0';
75          else if(p==10) ch='A';
76          else if(p==11) ch='B';
77          else if(p==12) ch='C';
78          else if(p==13) ch='D';
79          else if(p==14) ch='E';
80          else if(p==15) ch='F';
81         printf("Case #%d: %d %c\n",v,n+1,ch);
82         
83     }
84     return 0;
85 }
86      

 

posted on 2018-10-08 14:15  myx12345  阅读(802)  评论(0编辑  收藏  举报

导航