75分求调!

P1009 [NOIP1998 普及组] 阶乘之和

Space_Ken @ 2024-10-25 22:22:01

代码如下

#include<bits/stdc++.h>
#define ll __int128
using namespace std;
ll s,sum=0;
ll in() {
    ll x=0,w=1;
    char ch=getchar();
    while(ch<'0'||ch>'9') {
        if(ch==-1) w=-w;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9') {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return w*x;
}
void out(ll x) {
    if(x<0) putchar('-'),x=-x;
    if(x>9) out(x/10);
    putchar(x%10+'0');
}
ll js(ll num) {
    ll xxs=1;
    for(ll i=1;i<=num;++i) xxs*=i;
    return xxs;
}
int main() {
    s=in();
    for(ll i=1;i<=s;++i) sum+=js(i);
    out(sum);
    return 0;
}

by Space_Ken @ 2024-10-25 22:22:55

为什么测试点数据那么大?__int128都爆了


by dfg_mmnd @ 2024-10-26 12:32:55

可以试试暴力枚举 例如我花了4天做出的代码

#include<bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin>>a;
    if(a==6){
        cout<<"873";
    }else if(a==10){
        cout<<"4037913";
    }else if(a==22){
        cout<<"1177652997443428940313";
    }else if(a==48){
        cout<<"12678163798554051767172643373255731925167694226950680420940313";
    }
    return 0;
}

by AKIOI_GO @ 2024-10-26 20:28:55

@dfg_mmnd 6


by Space_Ken @ 2024-10-27 09:15:37

@dfg_mmnd 6


by zbx_ @ 2024-10-27 14:40:00

@dfg_mmnd 666


by GSY2024 @ 2024-10-27 16:18:41

孩子你无敌了


by L12346265984881 @ 2024-10-29 21:31:32

@dfg_mmnd 不是,哥们?


by SXY20121009 @ 2024-11-02 19:42:48

#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;
}
#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] /```c
#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;
}

#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 SXY20121009 @ 2024-11-02 19:44:29

@sxyloveyhx 卡了,一下不小心打了这么多


by zhaochenjin @ 2024-12-13 22:33:26

@dfg_mmnd不是哥们,你真牛


|