CodeVs 3150 (大数 + 递推)

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
//#include<bits/std c++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
const LL MOD = 1e7 + 7;
const LL maxn = 1e6 + 131;
const LL Bits = 1e10;
const double Pi = acos(-1.0);
struct BigInt
{
    vector<LL> val;
    void Set(LL n) { val.clear(); val.push_back(n); }
    void operator += (const BigInt a)
    {
        int i;
        for(i = 0; i < val.size() && i < a.val.size() ; ++i)
            val[i] = val[i] + a.val[i];
        if(i < a.val.size()) for(; i < a.val.size(); ++i) val.push_back(a.val[i]);
        int len = val.size();
        for(i = 0; i < len; ++i)
        {
            if(val[i] >= Bits)
            {
                if(i == len-1) val.push_back(val[i]/Bits), val[i] %= Bits;
                else {
                    val[i+1] += (val[i] / Bits);
                    val[i] %= Bits;
                }
            }
        }
    }
    void operator *= (const LL k)
    {
        int i;
        for(i = 0; i < val.size(); ++i) val[i] *= k;
        i = 0;
        while(i != val.size())
        {
            if(val[i] >= Bits)
            {
                if(i == val.size() -1)
                {
                    val.push_back(val[i] / Bits);
                    val[i] %= Bits;
                }
                else
                {
                    val[i+1] += (val[i] / Bits);
                    val[i]   %= Bits;
                }
            }
            ++i;
        }
    }
    void operator /= (const LL k)
    {
        for(int i = val.size() - 1; i >= 1; --i)
        {
            val[i-1] += (val[i] % k)*Bits;
            val[i] /= k;
        }
        val[0] /= k;
        int i = val.size() - 1;
        while(val.size() > 1)
        {
            if(val[i] == 0) val.erase(--val.end());
            else break;
            i--;
        }
    }
    void Show()
    {
        cout << val[val.size() - 1];
        for(int i = val.size() - 2; i >= 0 ; --i)
        {
            LL k = Bits / 10;
            while( k > 1 )
            {
                if(val[i] < k) cout << "0";
                k /= 10;
            }
            cout << val[i];
        }
        cout << endl;
    }
};

int main()
{
    BigInt A, B;
    LL n;
    /*A.Set(1), B.Set(Bits - 1);
    A += B;
    A.Show();*/
    //cout << Pi << endl;
    while(cin >> n){
    if(n < 4) cout << "1\n";
    else {
        n -= 4;
        A.Set(1), B.Set(1);
        LL i = 1, j = (n + Pi), Last;
        while(i <= (LL)((n + Pi)/ Pi))
        {
            Last = j;
            j = (n + Pi - i*Pi);
            for(LL k = Last - 1; k >= j + 1; k--)
            {
                B *= (k+1);
                B /= (i + k);

            }
            B *= (j + 1);
            B /= i;
            i ++;
            A += B;
        }
        A.Show();
    }
    }
    return 0;
}
时间: 2024-10-21 20:13:34

CodeVs 3150 (大数 + 递推)的相关文章

POJ 1737 Connected Graph (大数+递推)

题目链接: http://poj.org/problem?id=1737 题意: 求 \(n\) 个点的无向简单连通图的个数.\((n<=50)\) 题解: 这题你甚至能OEIS. http://oeis.org/A001187 但不支持这样做.TAT 间接做. 总方案数减去不合法方案. 因为\(n\)个点的完全图有 \(C(n,2)={n(n-1) \over 2}\) 条边,显然就有 \(2^{C(n,2)}\) 种子图,即枚举每条边是否选择. 设$ f[i]$ 表示每个点都和点 \(1\)

UVA 10254-The Priest Mathematician(大数+递推)

题目大意:在原本的汉诺塔游戏基础上加一根柱子,移动策略是:要移动N个汉诺塔,先用4根柱子把K个到一个柱子,然后用其余3根把剩下的N-K个移动到目标柱子,再用4根把初始的K个移动到目标柱子. 关键的问题是找到每个N的K是多少,观察可以发现规律是:随着K的递增,其实移动的次数Fn(K)先递增后递减,然后F1(K),F2(K),...的最大值随着K的增大递增.要形式化证明似乎比较困难..不过在题目的范围内这是可以AC的.程序中维护这个当前使得Fi(K)最大的K,然后递推. import java.ut

1sting 大数 递推

You will be given a string which only contains '1'; You can merge two adjacent '1' to be '2', or leave the '1' there. Surly, you may get many different results. For example, given 1111 , you can get 1111, 121, 112,211,22. Now, your work is to find th

UVa 10328 Coin Toss(Java大数+递推)

https://vjudge.net/problem/UVA-10328 题意: 有H和T两个字符,现在要排成n位的字符串,求至少有k个字符连续的方案数. 思路:这道题目和ZOJ3747是差不多的,具体做法可以参考另一篇博客http://www.cnblogs.com/zyb993963526/p/7203833.html 但是这道题目的话是要用大数来做的,c++感觉不太好写,就学了下java的做法. 1 import java.math.BigInteger; 2 import java.ut

poj2506 大数递推

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8205   Accepted: 3974 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

2506Tiling(大数递推)

Tiling Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8200   Accepted: 3970 Description In how many ways can you tile a 2xn rectangle by 2x1 or 2x2 tiles? Here is a sample tiling of a 2x17 rectangle. Input Input is a sequence of lines,

uva 1478 - Delta Wave(递推+大数+卡特兰数+组合数学)

题目链接:uva 1478 - Delta Wave 题目大意:对于每个位置来说,可以向上,水平,向下,坐标不能位负,每次上下移动最多为1, 给定n问说有多少种不同的图.结果对10100取模. 解题思路:因为最后都要落回y=0的位置,所以上升的次数和下降的次数是相同的,并且上升下降的关系满足出栈入栈的关系.即卡特兰数. 所以每次枚举i,表示有i个上升,i个下降,用组合数学枚举出位置,然后累加求和. C(2?in)?f(i)=C(2?i?2n)?f(i?1)?(n?2?i+1)?(n?2?i+2)

ACM学习历程—HDU1041 Computer Transformation(递推 &amp;&amp; 大数)

Description A sequence consisting of one digit, the number 1 is initially written into a computer. At each successive time step, the computer simultaneously tranforms each digit 0 into the sequence 1 0 and each digit 1 into the sequence 0 1. So, afte

ACdream 1420 High Speed Trains【Java大数高精度 + 递推】

High Speed Trains Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) 链接:http://acdream.info/problem?pid=1420 Problem Description The kingdom of Flatland has n cities. Recently the king of Flatland visited Japan and was a