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. GO获取随机数

    使用的"math/rand"包. 基本随机数 a := rand.Int() b := rand.Intn(100) //生成0-99之间的随机数 fmt.Println(a) f ...

  2. 巧用 CSS 实现酷炫的充电动画

    循序渐进,看看只使用 CSS ,可以鼓捣出什么样的充电动画效果. 画个电池 当然,电池充电,首先得用 CSS 画一个电池,这个不难,随便整一个: 欧了,勉强就是它了.有了电池,那接下来直接充电吧.最最 ...

  3. 根据udev的信息判断设备物理路径

    udev会生成by-path路径,根据这个就可以判断 dev目录下 [toybrick@localhost dev]$ find | grep platform-fe3c0000 ./disk/by- ...

  4. socket status 的状态变化详解

    原文链接: http://www.diranieh.com/SOCKETS/SocketStates.htm --------------------------------------------- ...

  5. Docs-.NET-C#-指南-语言参考-关键字-内置类型-值类型:值类型的功能

    ylbtech-Docs-.NET-C#-指南-语言参考-关键字-内置类型-值类型:值类型的功能 1.返回顶部 1. 值类型(C# 参考) 2018/11/26 有两种值类型: 结构 枚举 值类型的主 ...

  6. Python的开源人脸识别库:离线识别率高达99.38%(附源码)

    Python的开源人脸识别库:离线识别率高达99.38%(附源码) 转https://cloud.tencent.com/developer/article/1359073   11.11 智慧上云 ...

  7. prometheus + influxdb + grafana + mysql

    前言 本文介绍使用influxdb 作为prometheus持久化存储和使用mysql 作为grafana 持久化存储的安装方法 一 安装go环境 如果自己有go环境可以自主编译remote_stor ...

  8. 【转载】 clusterdata-2011-2 谷歌集群数据分析(一)

    原文地址: https://blog.csdn.net/yangss123/article/details/78298679 由于原文声明其原创文章不得允许不可转载,故这里没有转载其正文内容. --- ...

  9. ActiveMQ之三--JMS-Spring和ActiveMQ整合的完整实

    这篇博文,我们基于Spring+JMS+ActiveMQ+Tomcat,做一个Spring4.1.0和ActiveMQ5.11.1整合实例,实现了Point-To-Point的异步队列消息和PUB/S ...

  10. 零基础学Python-第二章 :Python基础语法-04.Python程序的书写规则

    #号后面的都是注释 import是导入一个模块 结束