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 ...
随机推荐
- 常用spaceclaim脚本(二)
#创建一个草图 #第一个参数传入一个Frame对象 #通过一个点和两个向量创建Frame #Frame的类成员函数Create被重载 #重载函数1:Frame.Create(Point, Direct ...
- fluent meshing导入二维网格
meshing导入二维网格"> fluent meshing只能在Dimension为3D时才能使用 meshing导入二维网格"> 其实也可以导入二维网格,具体操作见 ...
- win10常用快捷键(热键)总结
win10常用快捷键总结: win+Ctrl+D 创建虚拟桌面 win+Ctrl+左(右)键 切换虚拟桌面 win+Ctrl+F4 关闭虚拟桌面 win+D 隐藏所有窗口/显示所有窗口 win+M 隐 ...
- PostgreSQL 增量备份详解以及相关示例
PostgreSQL 没有类似MySQL 的二进制日志, 但是有和MySQL 类似的REDO LOG,并且有MySQL 没有的REDO 归档功能.当然REDO 的归档已经MariaDB 和Percon ...
- ssh端口映射总结
版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/boliang319/article/det ...
- IntelliJ IDEA悬停鼠标显示方法详细信息
1.如果View -> Toolbar勾选情况下, 直接点击按钮打开设置, 或是直接点击File -> Settings(或是快捷键)打开设置窗口. 2.搜索栏中输入Show quick ...
- java和c# md5加密
MD5加密的方式有很多,加盐的方式更多,最近项目需要java和c#加密结果一致,形成方法如下: 1.c#加密方法/// <summary> /// MD5 加密字符串 /// </s ...
- Vuex 的使用 State Mutation Getter Action
import Vue from 'vue' import Vuex from 'vuex' Vue.use(Vuex); /*1.state在vuex中用于存储数据*/ var state={ cou ...
- ISO/IEC 9899:2011 条款6.5.15——条件操作符
6.5.15 条件操作符 语法 1.conditional-expression: logical-OR-expression logical-OR-expression ? expres ...
- 003-结构型-04-外观模式(Facade)
一.概述 Facade模式也叫外观模式,是由GoF提出的23种设计模式中的一种.Facade模式为一组具有类似功能的类群,比如类库,子系统等等,提供一个一致的简单的界面.这个一致的简单的界面被称作fa ...