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

 

题解

  对于一个十进制小数x,要想获取其第n位的十六进制小数,只需先将x转为十六进制后再将小数点右移n位,则小数点左边第一个整数位对应的数字即为答案。但由于n很大,直接计算显然不行,精度不够,那该怎么办呢?事实上,如果要我们求x的第n位十进制小数,我们直接将x乘以10,即将x小数点右移了n位。对于十六进制,同理,只需将x乘以16,即将十六进制下x的小数点右移了n位。主要思想有了,我们就可以解这道题了。

  对于公式π = ∑k=0∞ 16-k (4/(8*k + 1) − 2/(8*k + 4) − 1/(8*k + 5) − 1/(8*k + 6)),我们可以转化为π = 4∑1 - 2∑2 - ∑3 - ∑4,其中

∑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)

我们取∑1分析,其他三个同理。对于∑1,由于我们要求的是第n位小数数字,故我们先将小数点右移n-1位,即

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

由前面分析知,整数部分对最终答案没有影响,故可以通过取模去除整数部分,保留小数部分。

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

∑1的前半部分通过快速幂很容易实现;而由级数的知识可以知道,∑1的后半部分会收敛到一个常数,即随着k的增大,对应的项则越来越趋于0。故∞可以取为一个稍大的数,比如500之类的。

 #include <bits/stdc++.h>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define re register
#define il inline
#define ll long long
#define ld long double
using namespace std;
const ll MAXN = 1e2+;
const ll TABLE = ;
const ld INF = 1e9;
const ld EPS = 1e-; //快速幂模
ll powmod(ll a, ll n, ll md)
{
ll ans = ;
while(n)
{
if(n&)
{
ans = (ans*a)%md;
}
a = (a*a)%md;
n >>= ;
}
return ans;
} //BBP Formula
ll BBP(ll n)
{
ld ans = ;
ld ans1 = , ans2 = , ans3 = , ans4 = ;
for(re ll i = ; i < n; ++i)
{
ll k = n--i;
ll a = *i+;
ll b = a + ;
ll c = a + ;
ll d = a + ;
/*
//最好用这种(先算总和,最后作加减,以免产生截断误差。
ans1 += powmod(16,k,a)/(ld)a;
ans2 += powmod(16,k,b)/(ld)b;
ans3 += powmod(16,k,c)/(ld)c;
ans4 += powmod(16,k,d)/(ld)d;
*/
ans += *powmod(,k,a)/(ld)a-*powmod(,k,b)/(ld)b-powmod(,k,c)/(ld)c-powmod(,k,d)/(ld)d;
}
for(re ll i = n; i <= n+; ++i)
{
ll k = n--i;
ll a = *i+;
ll b = a + ;
ll c = a + ;
ll d = a + ;
/*
//最好用这种(先算总和,最后作加减,以免产生截断误差。
ans1 += powl(16,k)/a;
ans2 += powl(16,k)/b;
ans3 += powl(16,k)/c;
ans4 += powl(16,k)/d;
*/
ans += 4.0*powl(16.0,(ld)k)/a-2.0*powl(16.0,(ld)k)/b-1.0*powl(16.0,(ld)k)/c-1.0*powl(16.0,(ld)k)/d;
}
//最好用这种(先算总和,最后作加减,以免产生截断误差。
//ld ans = 4*ans1 - (2*ans2 + ans3 + ans4);
ans -= (ll)ans;
ans = ans < ? ans+ : ans;
return ((ll)(ans*))%;
} //这题推荐用scanf和printf
//cin和cout出现玄学错误
//具体原因等找到再作说明
int main()
{
ios::sync_with_stdio(false);
int T;
//scanf("%d", T);
cin >> T;
for(re int i = ; i <= T; ++i)
{
int n;
//scanf("%d", &n);
cin >> n;
ll ans = BBP(n);
cout << "Case #" << dec << i << ": " << dec << n << " ";
cout << setiosflags(ios::uppercase) << hex << ans << endl;
//printf("Case #%d: %d %c\n", i, n, out(ans));
}
return ;
}

UVALive-8201-BBP Formula的更多相关文章

  1. 【HDOJ6217】BBP Formula(公式)

    题意:给定一个无穷项的分式,它的和等于π,问π的十六进制表示的小数点后第n位是多少 1 ≤ n ≤ 100000 思路:From https://blog.csdn.net/meopass/artic ...

  2. HDU 6217 BBP Formula (数学)

    题目链接: HDU 7217 题意: 题目给你可以计算 \(π\) 的公式: \(\pi = \sum_{k=0}^{\infty}[\frac{1}{16^k}(\frac{4}{8k+1})-(\ ...

  3. hdu 6217 A BBP Formula 公式题

    题意 已知公式:$\pi=\sum_{k=0}^{\infty}\left[\frac{1}{16^{k}}\left(\frac{4}{8 k+1}-\frac{2}{8 k+4}-\frac{1} ...

  4. HDU-6217 BBP Formula 脑洞

    题目链接:https://cn.vjudge.net/problem/HDU-6217 题意 已知: \[ \pi = \sum_{k=0}^{\infty }\frac{1}{16^{k}}(\fr ...

  5. ACM-ICPC 2017 Asia Shenyang Solution

    A: BBP Formula https://www.cnblogs.com/LzyRapx/p/7802790.html #include <bits/stdc++.h> using n ...

  6. Hook length formula 学习笔记 UVALive 6625

    最近做到一个关于杨氏矩阵的题目. UVALive 6625 题目大意是用n以内的数填充杨氏矩阵,要求行严格递增,列不严格递增. 求方案数. 数据范围很小,我直接上爆搜,结果TLE了. 后来发现一位学长 ...

  7. UVALive - 4108 SKYLINE[线段树]

    UVALive - 4108 SKYLINE Time Limit: 3000MS     64bit IO Format: %lld & %llu Submit Status uDebug ...

  8. UVALive - 3942 Remember the Word[树状数组]

    UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...

  9. UVALive - 3942 Remember the Word[Trie DP]

    UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...

随机推荐

  1. select下拉框多选取值

    本来是单选取值,现改为多选 其中<select> 标签新增multiple属性,如<select id = "sel"  multiple = "mul ...

  2. 记录vsCode配置node开发环境

    环境:win10,idea:vscode 1:安装Visual Studio Code 下一步下一步.(很早之前就安装了). 2:安装node. http://nodejs.cn/download/  ...

  3. 【转】Python 深入浅出 - PyPDF2 处理 PDF 文件

    实际应用中,可能会涉及处理 pdf 文件,PyPDF2 就是这样一个库,使用它可以轻松的处理 pdf 文件,它提供了读,割,合并,文件转换等多种操作. 文档地址:http://pythonhosted ...

  4. QtCore概述

    所有其他Qt模块都依赖于这个模块. 要包含模块类的定义,请使用以下指令: include < QtCore > 如果您使用qmake来构建您的项目,则默认将QtCore包含在内. 核心功能 ...

  5. fork 可能导致subprocess崩溃

    https://docs.python.org/zh-cn/3/library/multiprocessing.html 在 3.8 版更改: 对于 macOS,spawn 启动方式是默认方式. 因为 ...

  6. tomcat NIOEndpoint中的Acceptor实现

    EndPoint的组件就是属于连接器Connector里面的.它是一个通信的端点,就是负责对外实现TCP/IP协议.EndPoint是个接口,它的具体实现类就是AbstractEndpoint,而Ab ...

  7. Java基础 import 要在所有的class前面

        JDK :OpenJDK-11      OS :CentOS 7.6.1810      IDE :Eclipse 2019‑03 typesetting :Markdown   code ...

  8. SBX(Simulated binary crossover)模拟二进制交叉算子和DE(differential evolution)差分进化算子

    一起来学演化计算-SBX(Simulated binary crossover)模拟二进制交叉算子和DE(differential evolution)差分进化算子 觉得有用的话,欢迎一起讨论相互学习 ...

  9. iOS App Store网址的命名规则

    App Store 命名规则:https://itunes.apple.com/cn/app/ +  拼音(最多6个,全部小写,空格要-) +  /id  +appID 一些参考实例: http:// ...

  10. python中多层循环的一键退出

    在单层循环的退出中,使用break即能退出,那么多层循环呢?机智的人们使用flag标识符的方式,例如: a=['a',1,2,3,4] b=['b',1,2,3,4] c=['c',1,2,3,4] ...