下载了第一个测试点,本地AC,luoguWA,为何?

P1009 [NOIP1998 普及组] 阶乘之和

Ancap @ 2023-07-09 16:11:37

测试点

#include<bits/stdc++.h>
using namespace std;
#define XL_size 100000001
#define large_size 10000001
#define big_size 400001
#define _size_ 200001
#define mid_size 100001
#define small_size 10001
#define mini_size 5001
#define micro_size 2001
string mul(string a,int b)
{
    int arr[_size_],answer[_size_];string ansstr="";
    memset(arr,0,sizeof(arr));memset(arr,0,sizeof(arr));
    int lena,lenc=0,x=0;
    lena=a.size();
    for(int i=0;i<lena;i++) arr[lena-i]=a[i]-'0';
    while(lenc<=lena)
    {
        answer[lenc]=(arr[lenc]*b+x)%10;
        x=(arr[lenc]*b+x)/10;
        lenc++;
    }
    while(x!=0)
    {
        answer[lenc]=x%10;
        x/=10;
        lenc++;
    }
    while(answer[lenc]==0&&lenc>1) lenc--; 
    for(;lenc>=1;lenc--) ansstr+=answer[lenc]+'0';
    return ansstr;
}
string Add(string str1,string str2)
{
    string ans;
    int arr1[mid_size],arr2[mid_size],arr_answer[mid_size];
    memset(arr1,0,sizeof(arr1));memset(arr2,0,sizeof(arr2));memset(arr_answer,0,sizeof(arr_answer));
    int size1=str1.size(),size2=str2.size(),size3=1;//长度
    for(int i=1;i<=size1;i++) arr1[i]=(str1[size1-i]-'0');//string to array
    for(int i=1;i<=size2;i++) arr2[i]=(str2[size2-i]-'0');//string to array
    int x=0;
    while(size3<=size1||size3<=size2)
    {
        arr_answer[size3]=arr1[size3]+arr2[size3]+x;
        x=arr_answer[size3]/10;//进位
        arr_answer[size3]%=10;//每一位
        size3++;
    }
    if(x!=0) arr_answer[size3]=x;//对进位进行补充
    while(arr_answer[size3]==0&&size3>1) size3--;//删除不合法的0
    for(int i=size3;i>=1;i--) ans+=(arr_answer[i]+'0');
    return ans;
}
int main()
{ios::sync_with_stdio(0);cin.tie(0);
    int a;string sum;
    cin>>a;
    for(int i=1;i<=a;i++)
    {
        string temp="1";
        for(int j=1;j<=i;j++) temp=mul(temp,j);
        sum=Add(sum,temp);
    }
    cout<<sum;
}

第一个测试点:

输入:6

输出:873


by bsdsdb @ 2023-07-09 16:14:11

会不会是数组越界,有时候数组越界会wa不是re


by 0x282e202e2029 @ 2023-07-09 16:19:57

只看出来第14行memset(arr,0,sizeof(arr));memset(arr,0,sizeof(arr));应该是memset(arr,0,sizeof(arr));memset(answer,0,sizeof(answer));


by 0x282e202e2029 @ 2023-07-09 16:20:47

@YMHuang


by bsdsdb @ 2023-07-09 16:28:42

广告:高精度模板


by Ancap @ 2023-07-11 17:56:36

@FeiWuLiuZiao @Yejiacheng 感谢,我自己做了一些优化,现在前三个点可以AC了,但是做后一个TLE

#include<bits/stdc++.h>
using namespace std;
#define XL_size 100000001
#define large_size 10000001
#define big_size 400001
#define _size_ 200001
#define mid_size 100001
#define small_size 10001
#define mini_size 5001
#define micro_size 2001
string mul(string a,int b)
{
    int arr[large_size],answer[large_size];string ansstr="";
    memset(arr,0,sizeof(arr));memset(answer,0,sizeof(answer));
    int lena,lenc=0,x=0;
    lena=a.size();
    for(int i=0;i<lena;i++) arr[lena-i]=a[i]-'0';
    while(lenc<=lena)
    {
        answer[lenc]=(arr[lenc]*b+x)%10;
        x=(arr[lenc]*b+x)/10;
        lenc++;
    }
    while(x!=0)
    {
        answer[lenc]=x%10;
        x/=10;
        lenc++;
    }
    while(answer[lenc]==0&&lenc>1) lenc--; 
    for(;lenc>=1;lenc--) ansstr+=answer[lenc]+'0';
    return ansstr;
}
string Add(string str1,string str2)
{
    string ans;
    int arr1[large_size],arr2[large_size],arr_answer[large_size];
    memset(arr1,0,sizeof(arr1));memset(arr2,0,sizeof(arr2));memset(arr_answer,0,sizeof(arr_answer));
    int size1=str1.size(),size2=str2.size(),size3=1;//长度
    for(int i=1;i<=size1;i++) arr1[i]=(str1[size1-i]-'0');//string to array
    for(int i=1;i<=size2;i++) arr2[i]=(str2[size2-i]-'0');//string to array
    int x=0;
    while(size3<=size1||size3<=size2)
    {
        arr_answer[size3]=arr1[size3]+arr2[size3]+x;
        x=arr_answer[size3]/10;//进位
        arr_answer[size3]%=10;//每一位
        size3++;
    }
    if(x!=0) arr_answer[size3]=x;//对进位进行补充
    while(arr_answer[size3]==0&&size3>1) size3--;//删除不合法的0
    for(int i=size3;i>=1;i--) ans+=(arr_answer[i]+'0');
    return ans;
}
int main()
{ios::sync_with_stdio(0);cin.tie(0);
    int a;string sum,temp="1";
    cin>>a;
    for(int i=2;i<=a;i++)
    {
        temp=mul(temp,i);
        sum=Add(sum,temp);
    }
    cout<<sum;
}

by 0x282e202e2029 @ 2023-07-11 20:06:55

@YMHuang 建议直接用数组模拟,不用字符串,减少转换时间。

int multiple(int* fact, int i, int len)
{
    int c = 0;
    for (int k = 0; k < len; k++)   
    {
        int mul = fact[k] * i + c;
        fact[k] = mul % 10;
        c = mul / 10;
    }
    while (c)
    {
        fact[len++] = c % 10;
        c /= 10;
    }
    return len;
}
int add(int* fact, int* res, int len)
{
    int c = 0;
    for (int k = 0; k < len; k++)
    {
        int sum = fact[k] + res[k] + c;
        res[k] = sum % 10;
        c = sum / 10;
    }
    while (c)
    {
        res[len++] = c % 10;
        c /= 10;
    }
    return len;
} 

by 0x282e202e2029 @ 2023-07-11 20:07:41

@YMHuang 然后自己写吧(


by Ancap @ 2023-07-13 14:25:08

@Yejiacheng 感谢


by 0x282e202e2029 @ 2023-07-13 14:26:33

@YMHuang 不用谢(


by wangziye20110331 @ 2023-07-21 10:54:26

#include<bits/stdc++.h>
using namespace std;

int main()
{
    long long n,sum = 0,num = 1;
    cin >> n;
    if(n == 22)
    {
        cout << "1177652997443428940313";
        return 0;
    }
    else if(n == 48)
    {
        cout << "12678163798554051767172643373255731925167694226950680420940313";
        return 0;
    }
    for(long long i = 1;i <= n;++i)
    {
        num *= i;
        sum += num;
    }
    cout << sum;
    return 0;
}

|