2 条题解

  • 1
    @ 2023-11-5 8:59:51
    #include<bits/stdc++.h>
    using namespace std;
    struct bigint {
    	int size;
    	vector<int> a;
    	bool re;
    	bigint() {
    		a.clear(),size=0,re=1;
    	}
    	void operator=(string s) {
    		a.clear();
    		if (s[0]=='-') {
    			re=0,size=s.size()-1;
    			for (int i=s.size()-1,j=0; i; i--,j++)a.push_back(s[i]^48);
    		} else {
    			re=1,size=s.size();
    			for (int i=s.size()-1,j=0; i>=0; i--,j++)a.push_back(s[i]^48);
    		}
    		if(s=="0")size=0;
    		return;
    	}
    	bigint(long long n) {
    		string s=to_string(n);
    		a.clear();
    		if (s[0]=='-') {
    			re=0,size=s.size()-1;
    			for (int i=s.size()-1,j=0; i; i--,j++)a.push_back(s[i]^48);
    		} else {
    			re=1,size=s.size();
    			for (int i=s.size()-1,j=0; i>=0; i--,j++)a.push_back(s[i]^48);
    		}
    		if(s=="0")size=0;
    		return;
    	}
    	bool operator==(bigint b) {
    		if (re!=b.re)return 0;
    		if (size!=b.size)return 0;
    		for (int i=0; i<size; i++)if (a[i]!=b.a[i])return 0;
    		return 1;
    	}
    	bool operator!=(bigint b) {
    		if (re!=b.re)return 1;
    		if (size!=b.size)return 1;
    		for (int i=0; i<size; i++)if (a[i]!=b.a[i])return 1;
    		return 0;
    	}
    	bool operator<(bigint b) {
    		if (re!=b.re)return re<b.re;
    		if (size!=b.size)return size<b.size;
    		for (int i=size-1; i>=0; i--)if (a[i]!=b.a[i])return a[i]<b.a[i];
    		return 0;
    	}
    	bool operator<=(bigint b) {
    		if (re!=b.re)return re<b.re;
    		if (size!=b.size)return size<b.size;
    		for (int i=size-1; i>=0; i--)if (a[i]!=b.a[i])return a[i]<b.a[i];
    		return 1;
    	}
    	bool operator>(bigint b) {
    		if (re!=b.re)return re>b.re;
    		if (size!=b.size)return size>b.size;
    		for (int i=size-1; i>=0; i--)if (a[i]!=b.a[i])return a[i]>b.a[i];
    		return 0;
    	}
    	bool operator>=(bigint b) {
    		if (re!=b.re)return re>b.re;
    		if (size!=b.size)return size>b.size;
    		for (int i=size-1; i>=0; i--)if (a[i]!=b.a[i])return a[i]>b.a[i];
    		return 1;
    	}
    	bigint operator+(bigint b) {
    		bigint c;
    		if (re&&b.re) {
    			c.size=max(size,b.size);
    			for (int i=0; i<min(size,b.size); i++) c.a.push_back(a[i]+b.a[i]);
    			if (size>b.size) for (int i=b.size; i<size; i++)c.a.push_back(a[i]);
    			else for (int i=size; i<b.size; i++)c.a.push_back(b.a[i]);
    			for (int i=1; i<c.size; i++) c.a[i]+=c.a[i-1]/10,c.a[i-1]%=10;
    			if(c.size)if (c.a[c.size-1]>9) c.a.push_back(1),c.a[c.size-1]%=10,c.size++;
    			return c;
    		} else if (!re&&!b.re) {
    			c.size=max(size,b.size);
    			for (int i=0; i<min(size,b.size); i++) c.a.push_back(a[i]+b.a[i]);
    			if (size>b.size) for (int i=b.size; i<size; i++)c.a.push_back(a[i]);
    			else for (int i=size; i<b.size; i++)c.a.push_back(b.a[i]);
    			for (int i=1; i<c.size; i++) c.a[i]+=c.a[i-1]/10,c.a[i-1]%=10;
    			if(c.size)if (c.a[c.size-1]>9)c.a.push_back(1),c.a[c.size-1]%=10,c.size++;
    			c.re=0;
    			return c;
    		}
    	}
    	void operator+=(bigint b) {
    		*this=*this+b;
    		return;
    	}
    	bigint operator-(bigint b) {
    		bigint c;
    		if (re&&!b.re) {
    			c.size=max(size,b.size);
    			for (int i=0; i<min(size,b.size); i++) c.a.push_back(a[i]+b.a[i]);
    			if (size>b.size) for (int i=b.size; i<size; i++)c.a.push_back(a[i]);
    			else for (int i=size; i<b.size; i++)c.a.push_back(b.a[i]);
    			for (int i=1; i<c.size; i++) c.a[i]+=c.a[i-1]/10,c.a[i-1]%=10;
    			if(c.size)if (c.a[c.size-1]>9) c.a.push_back(1),c.a[c.size-1]%=10,c.size++;
    			return c;
    		}
    		if (!re&&b.re) {
    			c.size=max(size,b.size);
    			for (int i=0; i<min(size,b.size); i++) c.a.push_back(a[i]+b.a[i]);
    			if (size>b.size) for (int i=b.size; i<size; i++)c.a.push_back(a[i]);
    			else for (int i=size; i<b.size; i++)c.a.push_back(b.a[i]);
    			for (int i=1; i<c.size; i++) c.a[i]+=c.a[i-1]/10,c.a[i-1]%=10;
    			if(c.size)if (c.a[c.size-1]>9)c.a.push_back(1),c.a[c.size-1]%=10,c.size++;
    			c.re=0;
    			return c;
    		}
    		if (re&&b.re) {
    			if (*this >= b) {
    				c.size = max(size, b.size);
    				for (int i=0;i<c.size;i++)c.a.push_back(0);
    				for (int i = 0; i < c.size; i++) {c.a[i] += a[i] - b.a[i]; if (c.a[i] < 0)c.a[i + 1]--, c.a[i] += 10;
    				}
    				for (int i = 0; i <= c.size + 10; i++) c.a[i + 1] += c.a[i] / 10,c.a[i] %= 10;
    				for (; !c.a[c.size - 1] && c.size;)c.size--;
    				return c;
    			} else {
    				c.size = max(size, b.size),c = b - *this,c.re = 0;return c;
    			}
    		}
    		re=1,b.re=1,c=b-*this,re=0,b.re=0;
    		return c;
    	}
    	bigint operator*(bigint b) {
    		bigint c,zero;
    		zero="0";
    		if (*this==zero||b==zero)return zero;
    		if (!(re^b.re)) {
    			c.size=size+b.size;
    			for (int i=0; i<c.size; i++) c.a.push_back(0);
    			for (int i=0; i<size; i++) for (int j=0; j<b.size; j++) c.a[i+j]+=a[i]*b.a[j];
    			for (int i=0; i<c.size-1; i++) c.a[i+1]+=c.a[i]/10,c.a[i]%=10;
    			if (c.a[c.size-1]==0) c.size--;
    			return c;
    		}
    		c.size=size+b.size,c.re=0;
    		for (int i=0; i<c.size; i++) c.a.push_back(0);
    		for (int i=0; i<size; i++) for (int j=0; j<b.size; j++) c.a[i+j]+=a[i]*b.a[j];
    		for (int i=0; i<c.size-1; i++) c.a[i+1]+=c.a[i]/10,c.a[i]%=10;
    		if (c.a[c.size-1]==0) c.size--;
    		return c;
    	}
    	void operator*=(bigint b) {
    		*this=*this*b;
    		return;
    	}
    	bigint operator/(bigint b) {
    
    	}
    };
    istream& operator>>(istream &inp,bigint &n) {
    	string s;
    	inp>>s;
    	if (s[0]=='-') {
    		n.re=0,n.size=s.size()-1;
    		for (int i=s.size()-1,j=0; i; i--,j++)n.a.push_back(s[i]^48);
    	} else {
    		n.re=1,n.size=s.size();
    		for (int i=s.size()-1,j=0; i>=0; i--,j++)n.a.push_back(s[i]^48);
    	}
    	if(s=="0")n.size=0;
    	return inp;
    }
    ostream& operator<<(ostream &out,bigint n) {
    	if (n.size==0)return putchar('0'),out;
    	if (n.re==0)putchar('-');
    	for (int i=n.size-1; i>=0; i--) {
    		putchar(n.a[i]+48);
    	}
    	return out;
    }
    bigint i=1,sum;
    int n;
    int main() {
        freopen("factorial.in","r",stdin),freopen("factorial.out","w",stdout);
    	cin>>n;
    	for (int j=1;j<=n;j++){
    		bigint k=j;
    		i*=k,sum+=i;
    	}
    	cout<<sum;
    	return 0;
    }
    

    信息

    ID
    429
    时间
    1000ms
    内存
    256MiB
    难度
    8
    标签
    (无)
    递交数
    130
    已通过
    23
    上传者