求条,玄关,急!

P1009 [NOIP1998 普及组] 阶乘之和

__Digger__ @ 2024-10-01 11:06:53

rt,25分

#include<bits/stdc++.h>
#include<string>

using namespace std;

int n;
string ans;
string add(string a, string b) {
    string res = "";
    if(a.size() > b.size()) {
        swap(a, b);
    }
    for(int i = 1; a.size() < b.size(); i++) {
        a = '0' + a;
    }
    for(int i = b.size() - 1; i >= 0; i--) {
        res += (b[i] - '0') + (a[i] - '0') + '0';
    }
    for(int i = 0; i < res.size(); i++) {
        if(res[i] > '9') {
            res[i] -= 10;
            res[i + 1]++;
        }
    }
    while(res.size() > 0 && res[res.size() - 1] == '0') {
        res.erase(res.size() - 1);
    }
    reverse(res.begin(), res.end());
    return res;
}

string mul(string a, string b) {
    string res = "";
    res.resize(a.size() + b.size());
    for(int i = 0; i < res.size(); i++) {
        res[i] = '0';
    }
    reverse(a.begin(), a.end());
    reverse(b.begin(), b.end());
    for(int i = 0; i < a.size(); i++) {
        for(int j = 0; j < b.size(); j++) {
            int x = (a[i] - '0') * (b[j] - '0');
            res[i + j] += x % 10;
            res[i + j] = (res[i + j] - '0') % 10 + '0';
            res[i + j + 1] += x / 10;
        }
    }
    while(res.size() > 0 && res[res.size() - 1] == '0') {
        res.erase(res.size() - 1);
    }
    reverse(res.begin(), res.end());
    return res;
}

int main() {
    cin >> n;
    string x = "1", ans = "0";
    for(int i = 1; i <= n; i++) {
        x = mul(x, to_string(i));
        ans = add(ans, x);
    }
    cout << ans; 
    return 0;
}

by crz_qwq @ 2024-10-01 11:11:06

@Digger %%%


by hcy0716 @ 2024-10-01 11:19:09

///


by O_v_O @ 2024-10-01 11:20:21

@Digger 要不看看我的代码吧,比你的简洁很多。。。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e5 + 10;
int n, sum[N], ans[N], len1, len2;
void mul(int a){
  for(int i = 1;i <= len1 + 2;i++){
    sum[i] *= a;
  }
  for(int i = 1;i <= len1 + 2;i++){
    sum[i + 1] += sum[i] / 10;
    sum[i] %= 10;
  }
  if(sum[len1 + 2] != 0){
    len1 += 2;
  }else if(sum[len1 + 1] != 0){
    len1++;
  }
}
void add(){
  for(int i = 1;i <= max(len1, len2);i++){
    ans[i] += sum[i];
  }
  for(int i = 1;i <= max(len1, len2);i++){
    ans[i + 1] += ans[i] / 10;
    ans[i] %= 10;
  }
  len2 = max(len2, len1);
  if(ans[len2 + 1] != 0){
    len2++;
  }
}
signed main(){
  cin >> n;
  sum[1] = 1;
  len1 = 1;
  for(int i = 1;i <= n;i++){
    mul(i);
    add();
  }
  for(int i = len2;i >= 1;i--){
    cout << ans[i];
  }
  return 0;
}

by __Digger__ @ 2024-10-01 11:40:08

@O_v_O thx,已关


by yhylivedream @ 2024-10-01 14:31:04

@O_v_O 要不看看我的代码吧,比你的简洁很多。。。

#include <bits/stdc++.h>

using namespace std;

string a[] = {"0", "1", "3", "9", "33", "153", "873", "5913", "46233", "409113", "4037913", "43954713", "522956313", "6749977113", "93928268313", "1401602636313", "22324392524313", "378011820620313", "6780385526348313", "128425485935180313", "2561327494111820313", "53652269665821260313", "1177652997443428940313", "27029669736328405580313", "647478071469567844940313", "16158688114800553828940313", "419450149241406189412940313", "11308319599659758350180940313", "316196664211373618851684940313", "9157958657951075573395300940313", "274410818470142134209703780940313", "8497249472648064951935266660940313", "271628086406341595119153278820940313", "8954945705218228090637347680100940313", "304187744744822368938255957323620940313", "10637335711130967298604907294846820940313", "382630662501032184766604355445682020940313", "14146383753727377231082583937026584420940313", "537169001220328488991089808037100875620940313", "20935051082417771847631371547939998232420940313", "836850334330315506193242641144055892504420940313", "34289376947494122614363304694584807557656420940313", "1439295494700374021157505910939096377494040420940313", "61854558558074209658512637979453093884758552420940313", "2720126133346522977702138448994068984204397080420940313", "122342346998826717539665299944651784048588130840420940313", "5624964506810915667389970728744906677010239883800420940313", "264248206017979096310354325882356886646207872272920420940313", "12678163798554051767172643373255731925167694226950680420940313", "620960027832821612639424806694551108812720525606160920420940313", "31035053229546199656252032972759319953190362094566672920420940313"};

int main() {
  int n;
  cin >> n, cout << a[n];
}

by O_v_O @ 2024-10-01 14:35:03

@yhylivedream 好好好,直接打表是吧!!!


|