• HDU-6217 BBP Formula 脑洞


    题目链接:https://cn.vjudge.net/problem/HDU-6217

    题意

    已知:

    [pi = sum_{k=0}^{infty }frac{1}{16^{k}}(frac{4}{8k+1}-frac{2}{8k+4}-frac{1}{8k+5}-frac{1}{8k+6}) ]

    求pi的16进制下小数点后第n位是多少。
    n<=1e5

    思路

    要算pi的第n位,首先把pi向前移n位,则个位上就是要求的数。
    但是我们很快发现难以计算这个个位数(乘法逆元可能不存在,精度也有问题)。
    然而通过脑洞,我们可以去算第一位小数,然后乘以16取个位即可。

    提交过程

    AC 可惜场上没想出来

    代码

    #include <cstdio>
    #include <cstring>
    double powmod(int idx, int mod){
        long long tmp=16, res=1;
        for (int i=0; (1<<i)<=idx; i++){
            if (idx&(1<<i)) res=(res*tmp)%mod;
            tmp=(tmp*tmp)%mod;
        }
        return res/(double)mod;
    }
    
    int solve(int n){
        double res=0;
        n--;
        for (int i=0; i<=n; i++){
            res+=4*powmod(n-i, 8*i+1)-2*powmod(n-i, 8*i+4)
                -powmod(n-i, 8*i+5)-powmod(n-i, 8*i+6);
            if (res<0) res+=1;
        }
        res*=16;
        return ((int)res)%16;
    }
    
    int main(void){
        int T, n, kase=0;
        char hex[20]="0123456789ABCDEF";
    
        scanf("%d", &T);
        while (T--){
            scanf("%d", &n);
            printf("Case #%d: %d %c
    ", ++kase, n, hex[solve(n)]);
        }
    
        return 0;
    }
    
    
    Time Memory Length Lang Submitted
    4180ms 1204kB 725 G++ 2018-08-30 18:47:46
  • 相关阅读:
    前端博客收集
    Oracle 数据库性能调优
    vue解决跨域问题
    IIS相关问题及解决方案
    《软件测试工程师》学习笔记
    Matlab学习笔记(一)
    排序算法及分析
    Silverlight学习笔记——跨域调用
    Matlab学习笔记(三)
    C#的一些必备技术
  • 原文地址:https://www.cnblogs.com/tanglizi/p/9623509.html
Copyright © 2020-2023  润新知