UVALive-8201-BBP Formula

8201-BBP Formula

Time limit: 3.000 seconds

In 1995, Simon Plouffe discovered a special summation style for some constants. Two year later, together with the paper of Bailey and Borwien published, this summation style was named as the Bailey-Borwein-Plouffe formula.Meanwhile a sensational formula appeared. That is

π = ∑k=0∞ 16-k (4/(8*k + 1) − 2/(8*k + 4) − 1/(8*k + 5) − 1/(8*k + 6))

  For centuries it had been assumed that there was no way to compute the n-th digit of π without calculating allof the preceding n − 1 digits, but the discovery of this formula laid out the possibility. This problem asks you to calculate the hexadecimal digit n of π immediately after the hexadecimal point. For example, the hexadecimalformat of n is 3.243F6A8885A308D313198A2E... and the 1-st digit is 2, the 11-th one is A and the 15-th one is D.

Input

The first line of input contains an integer T (1 ≤ T ≤ 32) which is the total number of test cases. Each of the following lines contains an integer n (1 ≤ n ≤ 100000).

Output

For each test case, output a single line beginning with the sign of the test case. Then output the integer n, and the answer which should be a character in {0, 1, ... , 9, A, B, C, D, E, F} as a hexadecimal number 

Sample Input

5

1

11

111

1111

11111

Sample Output

Case #1: 1 2

Case #2: 11 A

Case #3: 111 D

Case #4: 1111 A

Case # 5: 11111 E

 

answer

  For a decimal x , in order to obtain its n-bit hexadecimal decimal to hexadecimal x first just after the decimal point to the right by n bits, the decimal point to the left of a corresponding bit integer digital is the answer. But because n is large, direct calculation is clearly not, accuracy is not enough, then how to do it? In fact, if we want to find x n th decimal place, we direct the x multiplied by 10, is about the right of the decimal point x n bits. For hex, empathy, simply by multiplying 16 x, the decimal point of x is about to hex the right n bits. With the main idea, we can solve this question up.

  For Formula [pi] = [Sigma K = 0 ∞ 16 -k  (. 4 / (K +. 1. 8 *) - 2 / (K * +. 8. 4) -. 1 / (K +. 5. 8 *) -. 1 / (K +. 8 * 6)) , we can be converted to [pi] = 4 [sigma . 1 - 2 sigma] 2 -  sigma3 -  Σ4 , wherein

∑1 =  ∑k=0 16-k/(8*k + 1)

∑2 =  ∑k=0∞ 16-k/(8*k + 4)

∑3 =  ∑k=0∞ 16-k/(8*k + 5)

∑4 =  ∑k=0∞ 16-k/(8*k + 6)

 

We take Σ1 analysis, three other empathy. For Sigma] 1 , since we want is the n-th decimal number, so we first n-1 bits to the right of the decimal point, i.e.,

Σ1 =  Σ k = 0 N-1 16 n-1-k / (8 * k + 1) +  Σ k = n  16 n-1-k / (8 * k + 1)

Known from the foregoing analysis, the integer part no effect on the final answer, it may be removed by taking the integer part of the mold, to retain the fractional part.

Σ1 = Σ k = 0 N-1  [16 n-1-k  mod (8 * k + 1)] /  (8 * k + 1)    + Σ k = n  16 N-1-k / (8 * k + 1)

 Sigma] 1 front half portion is easily achieved by flash power; and the number of stages can be known by the knowledge, Sigma] 1 the second half will converge to a constant, i.e. with the increase of k, the corresponding entries become more and more 0. ∞ it may take a slightly larger number, such as 500 or the like.

 

 

 1 #include <bits/stdc++.h>
 2 #include <cstring>
 3 #include <cstdio>
 4 #include <cmath>
 5 #include <algorithm>
 6 #define re register
 7 #define il inline
 8 #define ll long long
 9 #define ld long double
10 using namespace std;
11 const ll MAXN = 1e2+5;
12 const ll TABLE = 26;
13 const ld INF = 1e9;
14 const ld EPS = 1e-9;
15 
16 //快速幂模
17 ll powmod(ll a, ll n, ll md)
18 {
19     ll ans = 1;
20     while(n)
21     {
22         if(n&1)
23         {
24             ans = (ans*a)%md;
25         }
26         a = (a*a)%md;
27         n >>= 1;
28     }
29     return ans;
30 }
31 
32 //BBP Formula
33 ll BBP(ll n)
34 {
35     ld ans = 0;
36     ld ans1 = 0, ans2 = 0, ans3 = 0, ans4 = 0;
37     for(re ll i = 0; i < n; ++i)
38     {
39         ll k = n-1-i;
40         ll a = 8*i+1;
41         ll b = a + 3;
42         ll c = a + 4;
 43 is          LL = A + D . 5 ;
 44 is          / * 
45          . // this is preferably (the sum of the first operator, the final addition and subtraction, in order to avoid truncation error
 46 is          ANS1 powmod + = (16, K, A) / (LD ) A;
 47          ANS2 + = powmod (16, K, B) / (LD) B;
 48          ANS3 + = powmod (16, K, C) / (LD) C;
 49          ANS4 + = powmod (16, K, D ) / (LD) D;
 50          * / 
51 is          ANS = + . 4 * powmod ( 16 , K, A) / (LD) A- 2 * powmod ( 16 , K, B) / (LD) B-powmod ( 16 , K, C) / (LD) C-powmod ( 16 , K, D) / (LD) D;
 52 is      }
 53 is      for(Re LL I = n-; I <= n-+ 10 ; ++ I)
 54 is      {
 55          LL K = N- . 1 - I;
 56 is          LL A = . 8 * I + . 1 ;
 57 is          LL B = A + . 3 ;
 58          LL C = + A . 4 ;
 59          LL = A + D . 5 ;
 60          / * 
61 is          // this is preferably (the sum of the first operator, the final addition and subtraction, in order to avoid truncation error.
 62 is          ANS1 POWL + = (16, K) / A ;
 63 is          ANS2 POWL + = (16, K) / B;
 64          ANS3 POWL + = (16, K) / C;
 65         + = POWL ANS4 (16, K) / D;
 66          * / 
67          ANS = + 4.0 * POWL ( 16.0 , (LD) K) / A- 2.0 * POWL ( 16.0 , (LD) K) / B- 1.0 * POWL ( 16.0 , (LD) K) / the C- 1.0 * POWL ( 16.0 , (LD) K) / D;
 68      }
 69      // best to use this (the sum of the first operator, the final addition and subtraction, in order to avoid truncation error.
 70      // LD ANS =. 4 * ANS1 - (2 * ANS2 + ANS3 + ANS4); 
71 is      ANS - = (LL) ANS;
 72      ANS = ANS < 0 ANS +? . 1 : ANS;
 73 is      return ((LL) (ANS *16 ))% 16 ;
 74  }
 75  
76  // This problem recommended scanf and the printf
 77  // metaphysical error cin and COUT
 78  // DETAILED find other reasons stated further 
79  int main ()
 80  {
 81      iOS :: sync_with_stdio ( to false );
 82      int T;
 83      // Scanf ( "% D", T); 
84      CIN >> T;
 85      for (Re int I = . 1 ; I <= T; ++ I)
 86      {
 87          int n- ;
 88          //scanf("%d", &n);
89         cin >> n;
90         ll ans = BBP(n);
91         cout << "Case #" << dec << i << ": " << dec << n << " ";
92         cout << setiosflags(ios::uppercase) << hex << ans << endl;
93         //printf("Case #%d: %d %c\n", i, n, out(ans));
94     }
95     return 0;
96 }
View Code

 

 

Guess you like

Origin www.cnblogs.com/BlueHeart0621/p/11812773.html