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 ...
随机推荐
- div双击全屏,再双击恢复到原来的状态vue,js来做
需求是这样的: 有四个视频,视频是在4个区域,点击之后就全屏 <!DOCTYPE html> <html lang="en"> <head> & ...
- [教程] Packt - Create a Game Environment with Blender and Unity by Darrin Lile
学习了解如何使用Blender,photoshop和Unity创建自己的游戏环境!了解如何通过比以往更加集成的方式使用Blender和Unity,将自己的游戏设计变为现实.在Unity中创建测试版本 ...
- useEffect代替常用生命周期函数(三)
在用Class制作组件时,经常会用生命周期函数,来处理一些额外的事情(副作用:和函数业务主逻辑关联不大,特定时间或事件中执行的动作,比如Ajax请求后端数据,添加登录监听和取消登录,手动修改DOM等等 ...
- Page directive: invalid value for import
原有项目启动正常,正常访问:后来换成tomcat7.0.70:后启动正常,登陆正常,然而点进去任何菜单都会报错: java.lang.IllegalArgumentException: Page di ...
- JVM 初始化阶段例子 final常量
1.创建FinalTest类,里面有一个final常量x class FinalTest{ public static final int x = 3; static { System.out.pri ...
- 用sublime cmd 快速打开hosts文件
:: notepad start subl "%systemdrive%\WINDOWS\system32\drivers\etc\hosts" 文章来源:刘俊涛的博客 ...
- web常用服务架构
架构风格就是一种项目的设计模式.常见的架构风格有基于客户端与服务端的.基于组件模型的(EJB).分层架构(MVC).面向服务架构(SOA)等. 一.单体架构 单体架构也称为单体系统或单体应用,就是一种 ...
- python : takes 0 positional arguments but 1 was given
def 的要加self, https://blog.csdn.net/u010269790/article/details/78834410
- EDAS Serverless & Kubernetes SLB LVS Nginx
分布式缓存负载均衡的规则处理:虚拟节点对一致性哈希的改进 - yanghuahui - 博客园https://www.cnblogs.com/yanghuahui/p/3755460.html EDA ...
- openresty开发系列13--lua基础语法2常用数据类型介绍
openresty开发系列13--lua基础语法2常用数据类型介绍 一)boolean(布尔)布尔类型,可选值 true/false: Lua 中 nil 和 false 为"假" ...