首页 > 其他 > 详细

CodeVs 3150 (大数 + 递推)

时间:2015-08-11 23:18:20      阅读:225      评论:0      收藏:0      [点我收藏+]
#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;
}

CodeVs 3150 (大数 + 递推)

原文:http://www.cnblogs.com/aoxuets/p/4722514.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!