求大佬

P1009 [NOIP1998 普及组] 阶乘之和

yuanbingtao @ 2022-05-31 17:38:54

#include <iostream>
#include <cstring>
#include <cmath>

using namespace std;

int m;

struct Begin {
    int len, a[100];
    Begin(int x = 0) {
        memset(a, 0, sizeof(a));
        for (len = 1; x; len++) {
            a[len] = x % 10, x /= 10;
        }
        len--;
    }
    int& operator[](int i) {
        return a[i];
    }
    void flatten(int L) {
        len = L;
        for (int i = 1; i <= len; i++) {
            a[i + 1] += a[i] / 10, a[i] %= 10;
        }
        for (; !a[len]; len--);
    }
    void print() {
        for (int i = max(len, 1); i >= 1; cout << a[i--]);
    }
};
Begin operator+(Begin a, Begin b) {
    Begin ans;
    int len = max(a.len, b.len);
    for (int i = 1; i <= len; i++) {
        ans[i] += a[i] + b[i];
    }
    ans.flatten(len + 1);
    return ans;
}
Begin operator*(Begin a, Begin b) {
    Begin ans;
    int len = a.len;
    for (int i = 1; i <= len; i++) {
        ans[i] = a[i] * b[i];
    }
    ans.flatten(len + 11);
    return ans;
}

int main() {
    cin >> m;
    Begin ans(0), fac(1);
    for(int i = 1; i <= m; i++){
        fac = fac * i;
        ans = ans + fac;
    }
    ans.print();
    return 0;
}

求大佬看看是什么问题,这是深入浅出上的


by Wilson_Lee @ 2022-05-31 17:56:55

@yuanbingtao 你乘法不能这样写啊


by Wilson_Lee @ 2022-05-31 18:01:12

其实你高精乘低精完全可以开一个高精数组


by yuanbingtao @ 2022-05-31 21:02:52

那该怎么改呢?大佬


by wnsyou @ 2022-06-02 17:14:39

@yuanbingtao

#include<bits/stdc++.h>

using namespace std;

int n;
string cnt;

string ADD(string a, string b){
  int a_len = a.size(), b_len = b.size();
  string ans;
  if (a_len <= b_len){
    string t = a;
    a = b, b = t;
    swap(a_len, b_len);
  }
  for(int i = 0; i < a_len - b_len; i++){
    b = '0' + b;
  }
  int jw = 0;
  for(int i = a_len - 1; i >= 0; i--){
    int num;
    num = (a[i] - '0') + (b[i] - '0') + jw;
    jw = num / 10;
    num %= 10;
    ans = char(num + '0') + ans;
  }
  if(jw){
    ans = char(jw + '0') + ans;
  }
  return ans;
}

string MUL(string a, string b){
  string ans;
  int a_len = a.size(), b_len = b.size();
  int jw = 0;
  for(int i = a_len - 1; i >= 0; i--){
    string t;
    for(int j = b_len - 1; j >= 0; j--){
      int num = (a[i] - '0') * (b[j] - '0') + jw;
      jw = num / 10;
      num %= 10;
      t = char(num + '0') + t;
    }
    if(jw){
      t = char(jw + '0') + t;
      jw = 0;
    }
    for(int j = 0; j < (a_len - 1) - i; j++){
      t += '0';
    }
    ans = ADD(ans, t);
  }
  return ans;
}

string FAC(int n){
    string ans = "1";
    for(int i = 1; i <= n; i++){
      string a;
      int t = i;
      while(t){
        a = char(t % 10 + '0') + a;
        t /= 10;
      }
      ans = MUL(ans, a);
    }
    return ans;
}

int main(){
  cin >> n;
  for (int i = 1; i <= n; i++){
    cnt = ADD(cnt, FAC(i));
  }
  cout << cnt;
  return 0;
}

尝试理解亿下?今日才写的AC code


by yuanbingtao @ 2022-06-02 17:16:33

谢谢


by wnsyou @ 2022-06-02 17:16:41

庆祝一下在电脑前苦写1.5h后获得的AC记录


by yuanbingtao @ 2022-06-02 17:26:24

你这个好像是string去模拟的吧


by yuanbingtao @ 2022-06-02 17:27:08

@luogu_wsy0704


by wnsyou @ 2022-06-02 18:42:33

才看到,我觉得string方便一些,用深基上的感觉理解起来复杂一些


by wnsyou @ 2022-06-02 18:42:45

@yuanbingtao


|