HDU 6217 BBP Formula 【快速幂】

Description

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
这里写图片描述
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, andthe 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

题意:

题目给你可以计算 π 的公式,让你求十六进制下的小数点后 π 的第 n 位,而不用计算前 n−1项。

思路:

这里写图片描述

ac代码:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <string>
#include <algorithm>
#include <cmath>
#include <functional>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <set>
//#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

char print(int x)
{
    if (x >= 0 && x <= 9)
        return x + '0';
    return x + 55;
}
ll qpower(ll a, ll b, ll mod)
{
    ll res = 1;
    while (b)
    {
        if (b & 1)
            res = a * res % mod;
        b >>= 1;
        a = a * a % mod;
    }
    return res;
}
double bbp(int n, ll k, ll b)
{
    double res = 0;
    for (int i = 0; i <= n; i++)
        res += (qpower(16, n - i, 8 * i + b) * 1.0 / (8 * i + b));

    for (int i = n + 1; i <= n + 1000 + 1; i++)
        res += (powf(16, n - i) * 1.0 / (8 * i + b));
    return k * res;
}

int main()
{
    int t, n; cin >> t;
    int cas = 1;
    while (t--)
    {
        double ans = 0;
        cin >> n; n--;
        ans = bbp(n, 4, 1) - bbp(n, 2, 4) - bbp(n, 1, 5) - bbp(n, 1, 6);
        ans = ans - (int)ans;
        if (ans < 0)
            ans += 1.0;
        ans *= 16.0;
        char c;
        c = print(ans);
        printf("Case #%d: %d %c\n", cas++, n + 1, c);
    }
    return 0;
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值