UVALive-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的更多相关文章
- 【HDOJ6217】BBP Formula(公式)
题意:给定一个无穷项的分式,它的和等于π,问π的十六进制表示的小数点后第n位是多少 1 ≤ n ≤ 100000 思路:From https://blog.csdn.net/meopass/artic ...
- HDU 6217 BBP Formula (数学)
题目链接: HDU 7217 题意: 题目给你可以计算 \(π\) 的公式: \(\pi = \sum_{k=0}^{\infty}[\frac{1}{16^k}(\frac{4}{8k+1})-(\ ...
- 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} ...
- HDU-6217 BBP Formula 脑洞
题目链接:https://cn.vjudge.net/problem/HDU-6217 题意 已知: \[ \pi = \sum_{k=0}^{\infty }\frac{1}{16^{k}}(\fr ...
- ACM-ICPC 2017 Asia Shenyang Solution
A: BBP Formula https://www.cnblogs.com/LzyRapx/p/7802790.html #include <bits/stdc++.h> using n ...
- Hook length formula 学习笔记 UVALive 6625
最近做到一个关于杨氏矩阵的题目. UVALive 6625 题目大意是用n以内的数填充杨氏矩阵,要求行严格递增,列不严格递增. 求方案数. 数据范围很小,我直接上爆搜,结果TLE了. 后来发现一位学长 ...
- UVALive - 4108 SKYLINE[线段树]
UVALive - 4108 SKYLINE Time Limit: 3000MS 64bit IO Format: %lld & %llu Submit Status uDebug ...
- UVALive - 3942 Remember the Word[树状数组]
UVALive - 3942 Remember the Word A potentiometer, or potmeter for short, is an electronic device wit ...
- UVALive - 3942 Remember the Word[Trie DP]
UVALive - 3942 Remember the Word Neal is very curious about combinatorial problems, and now here com ...
随机推荐
- pip安装报错Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-e_k8hq6a/pynacl/
Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-e_k8hq6a/pyn ...
- (信贷风控九)行为评分卡模型python实现
python信用评分卡建模(附代码,博主录制) https://study.163.com/course/introduction.htm?courseId=1005214003&utm_ca ...
- Web 性能压力测试工具之 Siege 详解
Siege是一款开源的压力测试工具,设计用于评估WEB应用在压力下的承受能力.可以根据配置对一个WEB站点进行多用户的并发访问,记录每个用户所有请求过程的相应时间,并在一定数量的并发访问下重复进行.s ...
- openresty开发系列25--openresty中使用json模块
openresty开发系列25--openresty中使用json模块 web开发过程中,经常用的数据结构为json,openresty中封装了json模块,我们看如何使用 一)如何引入cjson模块 ...
- 背诵四种清净明诲断淫(愿众生断淫得究竟解脱) (转自学佛网:http://www.xuefo.net/nr/article50/495158.html)
一.为什么要戒邪淫.断淫欲 <寿康宝鉴>:盖淫念一生,诸念皆起.邪缘未凑生妄心;勾引无计,生机械心;少有阻碍,生嗔恨心;欲情颠倒,生贪着心;羡人有之,生嫉妒心;夺人之爱,生杀害心.廉耻丧尽 ...
- SQL语句 update 字段=字段+字符串 拼接
update user_info set user_name = concat(user_name,'呵呵呵') where user_name = '哈哈哈';
- Dockerfile-server1
[root@lab2 docker-file]# cd server1/ [root@lab2 server1]# ls a.sh ddbes-server1-0.0.1-SNAPSHOT.jar D ...
- require.js的基本用法
一.为什么要用require.js? 最早的时候,所有Javascript代码都写在一个文件里面,只要加载这一个文件就够了.后来,代码越来越多,一个文件不够了,必须分成多个文件,依次加载.下面的网页代 ...
- 智能指针.Qt测试
1.Qt598x64vs2017(或 Qt598x86vs2015[配置使用vs2017]).Win10x64 2.测试代码: 2.1.MainWindow.h class MainWindow : ...
- SQL触发器中的inserted表和deleted表
开发也有年头了,但是触发器确实用的比较少,但是无容置疑触发器确实不错, 最近项目要求需要用到的触发器特别多.频繁,觉得很有必要记录和积累下. 在触发器语句中用两个特殊的表一个是deleted表和ins ...