蒟蒻五十分求加高精悬赏一关注

P1009 [NOIP1998 普及组] 阶乘之和

sakura_erii @ 2023-08-24 14:45:59

#include<bits/stdc++.h>
using namespace std;
int main(){
    unsigned long long S=0,n,s=0;
    cin>>n;
    for(int i=1;i<=n;i++)
   {
        s=1;
        for(int j=1;j<=i;j++)
        {
            s=s*j;

        }
        S=S+s;
    }
    cout<<S;
    return 0;
}

by ForgotDream_CHN @ 2023-08-24 14:51:25

高精度板子网上一搜一大把啊


by drinktowind @ 2023-08-24 14:51:29

#include <bits/stdc++.h>
using namespace std;
int a[10005]={1},b[10005]={1},la=1,lb;
int main ()
{
    int n,cnt;
    cin>>n;
    for(int i=2;i<=n;i++)
    {
        //cout<<i<<endl; 
        la++;
        for(int j=0;j<la;j++)
            a[j]*=i; 
        for(int j=0;j<la;j++)
        {
            a[j+1]+=a[j]/10;
            a[j]%=10;
        }
        for(int j=0;j<=max(la,lb);j++)
        {
            b[j]+=a[j];
            b[j+1]+=b[j]/10;
            b[j]%=10;
        }
        while(a[la])
            la++;
        while(!a[la-1])
            la--;
        while(b[lb])
            lb++;
        while(!b[lb-1])
            lb--;
        /*
        for(int i=la-1;i+1;i--)
            cout<<a[i];
        cout<<" "<<la<<" ";
        for(int i=lb-1;i+1;i--)
            cout<<b[i];
        cout<<" "<<lb;
        cout<<endl;
        */
    }
    while(!b[lb-1])
        lb--;
    /*
    for(int i=la-1;i+1;i--)
        cout<<a[i];
    cout<<" "<<la<<" ";
    */
    for(int i=lb-1;i+1;i--)
        cout<<b[i];
    /*
    cout<<" "<<lb;
    cout<<endl;
    */
    return 0;
}

by drinktowind @ 2023-08-24 14:52:14

高精代码,其实你也可以ctj阅读tj


by ForgotDream_CHN @ 2023-08-24 14:53:27

@sakura_erii

这是一份高精模板

struct BigInt {
  static constexpr int N = 10000;
  int d[N], len;
  void clean() {
    while (len > 1 && !d[len - 1])
      len--;
  }
  BigInt() {
    memset(d, 0, sizeof(d));
    len = 1;
  }
  BigInt(int num) { *this = num; }
  BigInt(char *num) { *this = num; }
  BigInt operator=(const char *num) {
    memset(d, 0, sizeof(d));
    len = strlen(num);
    for (int i = 0; i < len; i++)
      d[i] = num[len - 1 - i] - '0';
    clean();
    return *this;
  }
  BigInt operator=(int num) {
    char s[20];
    sprintf(s, "%d", num);
    *this = s;
    return *this;
  }
  BigInt operator+(const BigInt &b) {
    BigInt c = *this;
    int i;
    for (i = 0; i < b.len; i++) {
      c.d[i] += b.d[i];
      if (c.d[i] > 9) c.d[i] %= 10, c.d[i + 1]++;
    }
    while (c.d[i] > 9) c.d[i++] %= 10, c.d[i]++;
    c.len = std::max(len, b.len);
    if (c.d[i] && c.len <= i) c.len = i + 1;
    return c;
  }
  BigInt operator-(const BigInt &b) {
    BigInt c = *this;
    int i;
    for (i = 0; i < b.len; i++) {
      c.d[i] -= b.d[i];
      if (c.d[i] < 0) c.d[i] += 10, c.d[i + 1]--;
    }
    while (c.d[i] < 0) c.d[i++] += 10, c.d[i]--;
    c.clean();
    return c;
  }
  BigInt operator*(const BigInt &b) const {
    int i, j;
    BigInt c;
    c.len = len + b.len;
    for (j = 0; j < b.len; j++)
      for (i = 0; i < len; i++)
        c.d[i + j] += d[i] * b.d[j];
    for (i = 0; i < c.len - 1; i++) c.d[i + 1] += c.d[i] / 10, c.d[i] %= 10;
    c.clean();
    return c;
  }
  BigInt operator/(const BigInt &b) {
    int i, j;
    BigInt c = *this, a = 0;
    for (i = len - 1; i >= 0; i--) {
      a = a * 10 + d[i];
      for (j = 0; j < 10; j++)
        if (a < b * (j + 1)) break;
      c.d[i] = j;
      a = a - b * j;
    }
    c.clean();
    return c;
  }
  BigInt operator%(const BigInt &b) {
    int i, j;
    BigInt a = 0;
    for (i = len - 1; i >= 0; i--) {
      a = a * 10 + d[i];
      for (j = 0; j < 10; j++)
        if (a < b * (j + 1)) break;
      a = a - b * j;
    }
    return a;
  }
  BigInt operator+=(const BigInt &b) {
    *this = *this + b;
    return *this;
  }
  bool operator<(const BigInt &b) const {
    if (len != b.len) return len < b.len;
    for (int i = len - 1; i >= 0; i--)
      if (d[i] != b.d[i]) return d[i] < b.d[i];
    return false;
  }
  bool operator>(const BigInt &b) const { return b < *this; }
  bool operator<=(const BigInt &b) const { return !(b < *this); }
  bool operator>=(const BigInt &b) const { return !(*this < b); }
  bool operator!=(const BigInt &b) const { return b < *this || *this < b; }
  bool operator==(const BigInt &b) const {
    return !(b < *this) && !(b > *this);
  }

  std::string str() const {
    char s[N] = {};
    for (int i = 0; i < len; i++) s[len - 1 - i] = d[i] + '0';
    return s;
  }
};
std::istream &operator>>(std::istream &in, BigInt &x) {
  std::string s;
  in >> s;
  x = s.c_str();
  return in;
}
std::ostream &operator<<(std::ostream &out, const BigInt &x) {
  out << x.str();
  return out;
}

by drinktowind @ 2023-08-24 14:54:39

@ForgotDream_CHN 6


by 残阳如血 @ 2023-08-24 15:02:31

@sakura_erii 不如这个:

#include <iostream>
#include <vector>
#include <string>
#include <queue>
class DividedByZeroException {};
class BigInteger {
private:
    bool sign;
    void trim();
public:
    std::vector<char> digits;
    BigInteger(int);
    BigInteger(std::string &) ;
    BigInteger();
    BigInteger(const BigInteger &);
    BigInteger operator=(const BigInteger &op2);
    BigInteger abs() const;
    BigInteger pow(int a);
    friend BigInteger operator+=(BigInteger &, const BigInteger &);
    friend BigInteger operator-=(BigInteger &, const BigInteger &);
    friend BigInteger operator*=(BigInteger &, const BigInteger &);
    friend BigInteger operator/=(BigInteger &, const BigInteger &);
    inline friend BigInteger operator%=(BigInteger &, const BigInteger &);
    inline friend BigInteger operator+(const BigInteger &, const BigInteger &);
    inline friend BigInteger operator-(const BigInteger &, const BigInteger &);
    inline friend BigInteger operator*(const BigInteger &, const BigInteger &);
    inline friend BigInteger operator/(const BigInteger &, const BigInteger &);
    inline friend BigInteger operator%(const BigInteger &, const BigInteger &);
    inline friend BigInteger operator-(const BigInteger &);
    inline friend BigInteger operator++(BigInteger &);
    inline friend BigInteger operator++(BigInteger &, int);
    inline friend BigInteger operator--(BigInteger &);
    inline friend BigInteger operator--(BigInteger &, int);
    inline friend bool operator>(const BigInteger &, const BigInteger &);
    friend bool operator<(const BigInteger &, const BigInteger &);
    friend bool operator==(const BigInteger &, const BigInteger &);
    inline friend bool operator!=(const BigInteger &, const BigInteger &);
    inline friend bool operator>=(const BigInteger &, const BigInteger &);
    inline friend bool operator<=(const BigInteger &, const BigInteger &);
    friend std::ostream &operator<<(std::ostream &, const BigInteger &);
    friend std::istream &operator>>(std::istream &, BigInteger &);
    std::string get_string();
    static const BigInteger ZERO;
    static const BigInteger ONE;
    static const BigInteger TEN;
};
const BigInteger BigInteger::ZERO = BigInteger(0);
const BigInteger BigInteger::ONE = BigInteger(1);
const BigInteger BigInteger::TEN = BigInteger(10);
BigInteger::BigInteger() {
    sign = true;
}
BigInteger::BigInteger(int val) {
    if (val >= 0) sign = true;
    else {
        sign = false;
        val *= (-1);
    }
    do {
        digits.push_back((char)(val % 10));
        val /= 10;
    } while (val != 0);
}
BigInteger::BigInteger(std::string &def) {
    sign = true;
    for (std::string::reverse_iterator iter = def.rbegin(); iter < def.rend(); iter++) {
        char ch = (*iter);
        if (iter == def.rend() - 1) {
            if (ch == '+') break;
            if (ch == '-') {
                sign = false;
                break;
            }
        }
        digits.push_back((char)((*iter) - '0'));
    }
    trim();
}
void BigInteger::trim() {
    std::vector<char>::reverse_iterator iter = digits.rbegin();
    while (!digits.empty() && (*iter) == 0) {
        digits.pop_back();
        iter = digits.rbegin();
    }
    if (digits.size() == 0) {
        sign = true;
        digits.push_back(0);
    }
}
BigInteger::BigInteger(const BigInteger &op2) {
    sign = op2.sign;
    digits = op2.digits;
}
BigInteger BigInteger::operator=(const BigInteger &op2) {
    digits = op2.digits;
    sign = op2.sign;
    return (*this);
}
BigInteger BigInteger::abs() const {
    if (sign) return *this;
    else return -(*this);
}
BigInteger BigInteger::pow(int a) {
    BigInteger res(1);
    for (int i = 0; i < a; i++) res *= (*this);

    return res;
}
BigInteger operator+=(BigInteger &op1, const BigInteger &op2) {
    if (op1.sign == op2.sign) {
        std::vector<char>::iterator iter1;
        std::vector<char>::const_iterator iter2;
        iter1 = op1.digits.begin();
        iter2 = op2.digits.begin();
        char to_add = 0;
        while (iter1 != op1.digits.end() && iter2 != op2.digits.end()) {
            (*iter1) = (*iter1) + (*iter2) + to_add;
            to_add = ((*iter1) > 9);
            (*iter1) = (*iter1) % 10;
            iter1++;
            iter2++;
        }
        while (iter1 != op1.digits.end()) {
            (*iter1) = (*iter1) + to_add;
            to_add = ((*iter1) > 9);
            (*iter1) %= 10;
            iter1++;
        }
        while (iter2 != op2.digits.end()) {
            char val = (*iter2) + to_add;
            to_add = (val > 9) ;
            val %= 10;
            op1.digits.push_back(val);
            iter2++;
        }
        if (to_add != 0) op1.digits.push_back(to_add);
        return op1;
    } else {
        if (op1.sign) return op1 -= (-op2);
        else return op1 = op2 - (-op1);
    }
}
BigInteger operator-=(BigInteger &op1, const BigInteger &op2) {
    if (op1.sign == op2.sign) {
        if (op1.sign) if (op1 < op2) return  op1 = -(op2 - op1);
        else {
            if (-op1 > -op2) return op1 = -((-op1) - (-op2));
            else return op1 = (-op2) - (-op1);
        }
        std::vector<char>::iterator iter1;
        std::vector<char>::const_iterator iter2;
        iter1 = op1.digits.begin();
        iter2 = op2.digits.begin();
        char to_substract = 0;
        while (iter1 != op1.digits.end() && iter2 != op2.digits.end()) {
            (*iter1) = (*iter1) - (*iter2) - to_substract;
            to_substract = 0;
            if ((*iter1) < 0) {
                to_substract = 1;
                (*iter1) += 10;
            }
            iter1++;
            iter2++;
        }
        while (iter1 != op1.digits.end()) {
            (*iter1) = (*iter1) - to_substract;
            to_substract = 0;
            if ((*iter1) < 0) {
                to_substract = 1;
                (*iter1) += 10;
            } else break;
            iter1++;
            op1.trim();
            return op1;
        } 
    } else {
        if (op1 > BigInteger::ZERO) return op1 += (-op2);
        else return op1 = -(op2 + (-op1));
    }
}
BigInteger operator*=(BigInteger & op1, const BigInteger & op2) {
    BigInteger result(0);
    if (op1 == BigInteger::ZERO || op2 == BigInteger::ZERO) result = BigInteger::ZERO;
    else {
        std::vector<char>::const_iterator iter2 = op2.digits.begin();
        while (iter2 != op2.digits.end()) {
            if (*iter2 != 0) {
                std::deque<char> temp(op1.digits.begin(), op1.digits.end());
                char to_add = 0;
                std::deque<char>::iterator iter1 = temp.begin();
                while (iter1 != temp.end()) {
                    (*iter1) *= (*iter2);
                    (*iter1) += to_add;
                    to_add = (*iter1) / 10;
                    (*iter1) %= 10;
                    iter1++;
                }
                if (to_add != 0) temp.push_back(to_add);
                int num_of_zeros = iter2 - op2.digits.begin();
                while (num_of_zeros--) temp.push_front(0);
                BigInteger temp2;
                temp2.digits.insert(temp2.digits.end(), temp.begin(), temp.end());
                temp2.trim();
                result = result + temp2;
            }
            iter2++;
        }
        result.sign = ((op1.sign && op2.sign) || (!op1.sign && !op2.sign));
    }
    op1 = result;
    return op1;
}
BigInteger operator/=(BigInteger & op1, const BigInteger & op2) {
    if (op2 == BigInteger::ZERO) throw DividedByZeroException();
    BigInteger t1 = op1.abs(), t2 = op2.abs();
    if (t1 < t2) {
        op1 = BigInteger::ZERO;
        return op1;
    }
    std::deque<char> temp;
    std::vector<char>::reverse_iterator iter = t1.digits.rbegin();
    BigInteger temp2(0);
    while (iter != t1.digits.rend()) {
        temp2 = temp2 * BigInteger::TEN + BigInteger((int)(*iter));
        char s = 0;
        while (temp2 >= t2) {
            temp2 = temp2 - t2;
            s = s + 1;
        }
        temp.push_front(s);
        iter++;
    }
    op1.digits.clear();
    op1.digits.insert(op1.digits.end(), temp.begin(), temp.end());
    op1.trim();
    op1.sign = ((op1.sign && op2.sign) || (!op1.sign && !op2.sign));
    return op1;
}
inline BigInteger operator%=(BigInteger & op1, const BigInteger & op2) {
    return op1 -= ((op1 / op2) * op2);
}
inline BigInteger operator+(const BigInteger & op1, const BigInteger & op2) {
    BigInteger temp(op1);
    temp += op2;
    return temp;
}
inline BigInteger operator-(const BigInteger & op1, const BigInteger & op2) {
    BigInteger temp(op1);
    temp -= op2;
    return temp;
}
inline BigInteger operator*(const BigInteger & op1, const BigInteger & op2) {
    BigInteger temp(op1);
    temp *= op2;
    return temp;
}

inline BigInteger operator/(const BigInteger & op1, const BigInteger & op2) {
    BigInteger temp(op1);
    temp /= op2;
    return temp;
}
inline BigInteger operator%(const BigInteger & op1, const BigInteger & op2) {
    BigInteger temp(op1);
    temp %= op2;
    return temp;
}
inline BigInteger operator-(const BigInteger & op) {
    BigInteger temp = BigInteger(op);
    temp.sign = !temp.sign;
    return temp;
}
inline BigInteger operator++(BigInteger & op) {
    op += BigInteger::ONE;
    return op;
}
inline BigInteger operator++(BigInteger & op, int x) {
    BigInteger temp(op);
    ++op;
    return temp;
}
inline BigInteger operator--(BigInteger & op) {
    op -=  BigInteger::ONE;
    return op;
}
inline BigInteger operator--(BigInteger & op, int x) {
    BigInteger temp(op);
    --op;
    return temp;
}
bool operator<(const BigInteger & op1, const BigInteger & op2) {
    if (op1.sign != op2.sign) return !op1.sign;
    else {
        if (op1.digits.size() != op2.digits.size()) return (op1.sign && op1.digits.size() < op2.digits.size()) || (!op1.sign && op1.digits.size() > op2.digits.size());
        std::vector<char>::const_reverse_iterator iter1, iter2;
        iter1 = op1.digits.rbegin();
        iter2 = op2.digits.rbegin();
        while (iter1 != op1.digits.rend()) {
            if (op1.sign &&  *iter1 < *iter2) return true;
            if (op1.sign &&  *iter1 > *iter2) return false;
            if (!op1.sign &&  *iter1 > *iter2) return true;
            if (!op1.sign &&  *iter1 < *iter2) return false;
            iter1++;
            iter2++;
        }
        return false;
    }
}
bool operator==(const BigInteger & op1, const BigInteger & op2) {
    if (op1.sign != op2.sign  || op1.digits.size() != op2.digits.size()) return false;
    std::vector<char>::const_iterator iter1, iter2;
    iter1 = op1.digits.begin();
    iter2 = op2.digits.begin();
    while (iter1 != op1.digits.end()) {
        if (*iter1 != *iter2) return false;
        iter1++;
        iter2++;
    }
    return true;
}
bool operator!=(const BigInteger & op1, const BigInteger & op2) {
    return !(op1 == op2);
}
bool operator>=(const BigInteger & op1, const BigInteger & op2) {
    return (op1 > op2) || (op1 == op2);
}
bool operator<=(const BigInteger & op1, const BigInteger & op2) {
    return (op1 < op2) || (op1 == op2);
}
bool operator>(const BigInteger & op1, const BigInteger & op2) {
    return !(op1 <= op2);
}
std::ostream &operator<<(std::ostream & stream, const BigInteger & val) {
    if (!val.sign) stream << "-";
    for (auto iter = val.digits.rbegin(); iter != val.digits.rend(); iter++) stream << (char)((*iter) + '0');
    return stream;
}
std::istream &operator>>(std::istream & stream, BigInteger & val) {
    std::string str;
    stream >> str;
    val = BigInteger(str);
    return stream;
}

by sakura_erii @ 2023-08-24 15:27:26

你们人还怪好的嘞


by sakura_erii @ 2023-08-24 15:29:04

关注了


by fztt_r9 @ 2023-08-25 11:22:53

@sakura_erii *****


by sakura_erii @ 2023-08-25 14:14:33

@fztt_r9 ????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????


|