用vector写的样例没问题,但全都WA了,求大佬指点

P1591 阶乘数码

roseCoder @ 2021-04-08 14:35:33

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;

vector<int> fact(int n) //求某个数的阶乘
{
    vector<int> res;
    res.push_back(1);   //先插入1
    for (int i=2;i<=n;i++){    //将每个数都乘进去
        int k=0;
        for (int j=0;j<res.size();j++){
            int t = res[j]*i+k;
            res[j]=t%10;
            k=t/10;
        }
        if (k) res.push_back(k);
    }
    return res;
}

int main()
{
    int t, n, a;
    cin>>t;
    while(t--){
        cin>>n>>a;
        vector<int> v = fact(n);
        long long res=0;
        for (int i=0;i<v.size();i++){
            if (v[i]==a) res++;
        }
        cout<<res<<endl;
    }
}

by ud2_ @ 2021-04-08 14:46:51

@Riser 高精乘时 int 溢出了。


by roseCoder @ 2021-04-08 14:57:36

@sjx233_

#include<iostream>
#include<algorithm>
#include<vector>

using namespace std;
typedef long long LL;

vector<LL> fact(LL n)
{
    vector<LL> res;
    res.push_back(1);
    for (LL i=2;i<=n;i++){
        LL k=0;
        for (LL j=0;j<res.size();j++){
            LL t = res[j]*i+k;
            res[j]=t%10;
            k=t/10;
        }
        if (k) res.push_back(k);
    }
    return res;
}

int main()
{
    LL t, n, a;
    cin>>t;
    while(t--){
        cin>>n>>a;
        vector<LL> v = fact(n);
        LL res=0;
        for (LL i=0;i<v.size();i++){
            if (v[i]==a) res++;
        }
        cout<<res<<endl;
    }
}

这样把int都换成了long long类型还是全部都是WA,大佬再占用您点时间帮忙看一下


|