Why it don't out answer?

P1009 [NOIP1998 普及组] 阶乘之和

___njr___ @ 2023-03-02 13:09:05

#include<iostream>
#include<string>
#include<vector>
#include<stack>
using namespace std;
typedef unsigned long long u;
const u um = 1e16;
class Big {
        vector<u> val;
        int len;
    public:
//      void operator=(Big other);
//      Big operator+(Big other);
//      Big operator*(Big other);
        Big TB(string tmp) {
            Big other;
            u Tmp;
            int Len = 0 ;
            stack<char>t;
            for(int i = 0 ; i < tmp.size(); ++i)t.push(tmp[i]);
            while(t.size()) {
                Tmp = Tmp<<3+Tmp<<1+(t.top()^48);
                if(Tmp>=um) {
                    other.val[Len++] = Tmp;
                    Tmp = 0;
                }
            }
            other.len = Len;
            return other;

        }
        void operator=(Big other) {
            len = other.len;
            val.clear();
            for(int i = 0 ; i <len; ++i)val[i] = other.val[i];
        }
        Big operator+(Big other) {
            Big c;
            c.len = max(len , other.len) + 1;
            for(int i = 0 ; i < c.len; ++i)c.val[i] = val[i] + other.val[i];
            for(int i = 0 ; i < c.len; ++i)while(c.val[i]>9) {
                    c.val[i]-=10;
                    ++c.val[i+1];
                }
            while(!c.val[--c.len]);
            ++c.len;
            return c;
        }
        Big operator*(Big other) {
            Big c;
            c.len = len+other.len+1;
            for(int i = 0;  i<len; ++i)for(int j = 0 ; j < other.len ; ++j)c.val[i+j]+=val[i]*other.val[j];
            for(int i = 0 ; i<c.len; ++i)while(c.val[i]>9) {
                    c.val[i]-=10;
                    ++c.val[i+1];
                }
            if(!c.val[c.len-1])--len;
            return c;
        }
        void out(Big other) {
            for(int i = 0 ; i < other.len ; ++i)cout<<other.val [i];
        }
        Big in(Big other) {
            string tmp;
            cin>>tmp;
            return TB(tmp);
        }
        bool operator--() {
            if(len == 1 && val[0]==0)return false;
            int i = 0 ;
            while(!val[i])++i;
            --val[i];
            return true;
        }

};
int main() {
    Big n;
    n.in(n);
    Big i = n + n.TB("2") ;
    Big ans = ans.TB("0") ;
    while(--i) {
        ans = ans*i;
        ans = ans+i;
    }
    ans.out(ans); 
    return 0;
}

by 2011Andy @ 2023-03-02 13:22:01

提示提示


|