洛谷P1009 (解放双手的探索)

版权声明:本文为博主原创文章,转载请附上博文链接! https://blog.csdn.net/weixin_44090305/article/details/89557484

Description

用高精度计算出S=1!+2!+3!+…+n! (n≤50)S=1!+2!+3!+…+n!(n≤50)
其中“!”表示阶乘,例如:5! = 5×4×3×2×1。

Input

一个正整数NN。

Output

一个正整数SS,表示计算结果。

Input Sample

3

Output Sample

9

一道高精度水题, 既然是水题, 为什么要做这题? 因为做完这题后我决定换一个"老婆" →_→

首先是C++:
C++ AC Code(原作者:洛谷用户: AThinker)(原谅我懒得写大数直接引用了一个封装在类里重载了运算符看起来比较舒服的代码哈哈)

#include <iostream>  
#include <algorithm> 
#include <cassert>   
#include <cstdio>    
#include <cstring>   
#include <string>    
#include <vector>    
using namespace std;
struct BigInteger {
    typedef unsigned long long LL;
    static const int BASE = 100000000;
    static const int WIDTH = 8;
    vector<int> s;
    BigInteger& clean(){while(!s.back()&&s.size()>1)s.pop_back(); return *this;}
    BigInteger(LL num = 0) {*this = num;}
    BigInteger(string s) {*this = s;}
    BigInteger& operator = (long long num) {
        s.clear();
        do {
            s.push_back(num % BASE);
            num /= BASE;
        } while (num > 0);
        return *this;
    }
    BigInteger& operator = (const string& str) {
        s.clear();
        int x, len = (str.length() - 1) / WIDTH + 1;
        for (int i = 0; i < len; i++) {
            int end = str.length() - i*WIDTH;
            int start = max(0, end - WIDTH);
            sscanf(str.substr(start,end-start).c_str(), "%d", &x);
            s.push_back(x);
        }
        return (*this).clean();
    }
    BigInteger operator + (const BigInteger& b) const {
        BigInteger c; c.s.clear();
        for (int i = 0, g = 0; ; i++) {
            if (g == 0 && i >= s.size() && i >= b.s.size()) break;
            int x = g;
            if (i < s.size()) x += s[i];
            if (i < b.s.size()) x += b.s[i];
            c.s.push_back(x % BASE);
            g = x / BASE;
        }
        return c;
    }
    BigInteger operator - (const BigInteger& b) const {
        assert(b <= *this);
        BigInteger c; c.s.clear();
        for (int i = 0, g = 0; ; i++) {
            if (g == 0 && i >= s.size() && i >= b.s.size()) break;
            int x = s[i] + g;
            if (i < b.s.size()) x -= b.s[i];
            if (x < 0) {g = -1; x += BASE;} else g = 0;
            c.s.push_back(x);
        }
        return c.clean();
    }
    BigInteger operator * (const BigInteger& b) const {
        int i, j; LL g;
        vector<LL> v(s.size()+b.s.size(), 0);
        BigInteger c; c.s.clear();
        for(i=0;i<s.size();i++) for(j=0;j<b.s.size();j++) v[i+j]+=LL(s[i])*b.s[j];
        for (i = 0, g = 0; ; i++) {
            if (g ==0 && i >= v.size()) break;
            LL x = v[i] + g;
            c.s.push_back(x % BASE);
            g = x / BASE;
        }
        return c.clean();
    }
    BigInteger operator / (const BigInteger& b) const {
        assert(b > 0);
        BigInteger c = *this;
        BigInteger m;
        for (int i = s.size()-1; i >= 0; i--) {
            m = m*BASE + s[i];
            c.s[i] = bsearch(b, m);
            m -= b*c.s[i];
        }
        return c.clean();
    }
    BigInteger operator % (const BigInteger& b) const {
        BigInteger c = *this;
        BigInteger m;
        for (int i = s.size()-1; i >= 0; i--) {
            m = m*BASE + s[i];
            c.s[i] = bsearch(b, m);
            m -= b*c.s[i];
        }
        return m;
    }
    int bsearch(const BigInteger& b, const BigInteger& m) const{
        int L = 0, R = BASE-1, x;
        while (1) {
            x = (L+R)>>1;
            if (b*x<=m) {if (b*(x+1)>m) return x; else L = x;}
            else R = x;
        }
    }

ostream& operator << (ostream& out, const BigInteger& x) {
    out << x.s.back();
    for (int i = x.s.size()-2; i >= 0; i--) {
        char buf[20];
        sprintf(buf, "%08d", x.s[i]);
        for (int j = 0; j < strlen(buf); j++) out << buf[j];
    }
    return out;
}
istream& operator >> (istream& in, BigInteger& x) {
    string s;
    if (!(in >> s)) return in;
    x = s;
    return in;
}
int main()
{
    int n;
    cin >> n;
    BigInteger ans=0,s=1;
    for(register int i=1;i<=n;i++)
    {
        s *= i;
        ans += s;
    }
    cout << ans << endl;
    return 0;
}

java AC code

/*
 * Copyright (c) 2019 Ng Kimbing, HNU, All rights reserved. May not be used, modified, or copied without permission.
 * @Author: Ng Kimbing, HNU.
 * @LastModified:2019-04-26 T 18:30:52.107 +08:00
 */
package ACMProblems.LuoGuTrainning;

import java.io.*;
import java.math.BigInteger;
import java.util.StringTokenizer;
import static ACMProblems.ACMIO.*;

public class P1009 {

    public static void main(String[] args) throws Exception {
        setStream(System.in);
        BigInteger facts[] = new BigInteger[51];
        BigInteger curr = new BigInteger("1");
        for (int i = 1; i < 51; ++i) {
            curr = curr.multiply(new BigInteger(Integer.toString(i)));
            facts[i] = curr;
        }
        int n = nextInt();
        BigInteger sum = facts[1];
        for (int i = 2; i <= n; ++i)
            sum = sum.add(facts[i]);
        out.println(sum);
        out.flush();
    }
}

Python AC Code:

fc, curr, n = [0], 1, int(input())
for i in range(1, n+1):
    curr = curr * i
    fc.append(curr)
print(sum(fc))

我宣布从现在开始我的大老婆是python! ^ _ ^
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44090305/article/details/89557484