羅区P1009(ハンズフリーを探ります)

免責事項:この記事はブロガーのオリジナルの記事、再現され、ボーエンのリンクを添付してください!https://blog.csdn.net/weixin_44090305/article/details/89557484

説明

高精度Sで計算= 1!2!3! + ... + N!(N≤50)= 1!2!3!+ ... + N S!(N≤50)
ここでは、 "!"を示します例えば階乗、:! = 5×4× 3×2×1。

入力

正の整数NN。

出力

正の整数SSは、計算結果を示しています。

入力サンプル

3

出力サンプル

9

高精度の水問題、それは水の問題であることから、なぜべきこの質問?私は「妻」のタイトルの後にそれを終了することを決定したので→_→

まず++は、C:
C ++ ACコード(著者:羅区ユーザー: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コード

/*
 * 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コード:

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