当然,怎么可能只是阶乘和呢?
高精度来了:
#include<cstdio> #include<iostream> using namespace std; int a[1001]={0},b[1001]={0}; int main () { int i,j,k,l,n,s,t,i1,j1; cin>>n; a[1]=1; for (i=1;i<=n;i++) { for (j=1;j<=1001;j++) a[j]*=i; for (k=1;k<=1001;k++) { a[k+1]+=a[k]/10; a[k]%=10; } t=0; for (l=1;l<=1001;l++) { b[l]+=a[l]+t; t=b[l]/10; b[l]%=10; } } i=1000; while (b[i]==0) i--; for (j=i;j>=1;j--) cout<<b[j]; return 0; }
既然大家都看了迭代器和STL的东西了,给大家一个奆佬的代码(可以试试输入10000……):
#ifndef GJD_H #define GJD_H #include<vector> #include<cstring> #include<string> #include<cstdio> #include<iostream> #include<cmath> #include<stack> struct lint; extern lint abs(lint x); extern lint operator*(const lint &lhs, const int &rhs); extern lint operator*(const lint &lhs, const lint &rhs); struct lint { static const int BASE = 100000000; static const int WIDTH = 8; std::vector<int> lst; bool nega; lint operator= (long long in) { lst.clear(); int cur; nega = in < 0 ? true : false; if(nega) in = -1 * in; while(in) { cur = in % BASE; in /= BASE; lst.push_back(cur); } if(lst.empty()) lst.push_back(0); return *this; } lint operator= (const char *s) { lst.clear(); nega = false; int len = strlen(s); const char *cur = s + len - 1; for(int j = 0;;++j) { lst.push_back(0); for(int i = 1; i < BASE; i *= 10, --cur) { if(*cur == '-') {nega = true;return *this;} lst[j] += (*cur - '0') * i; if(cur == s) return *this; } } for(;;) if(lst.back() == 0 && lst.size() != 1) lst.pop_back(); else break; return *this; } std::string tostr() const { std::string ans; if(nega)ans += '-'; bool qd0 = true; for(int i = lst.size() -1 ; i >= 0; --i) { int cur = lst[i]; for(int j = BASE/10; j >= 1; j /= 10) { int curr = cur / j; if(!curr && qd0) continue; else qd0 = false; cur %= j; ans += char(curr + '0'); } } return ans; } lint(long long ll = 0){*this = ll;} lint(const char *c){*this = c;} bool operator<(const lint &rhs) const { if(nega) { if(rhs.nega) { if(lst.size() != rhs.lst.size()) return lst.size() > rhs.lst.size(); for(int i = lst.size() - 1; i >= 0; --i) if(lst[i] != rhs.lst[i]) return lst[i] > rhs.lst[i]; } else return true; }else { if(rhs.nega) return false; else { if(lst.size() != rhs.lst.size()) return lst.size() < rhs.lst.size(); for(int i = lst.size() - 1; i >= 0; --i) if(lst[i] != rhs.lst[i]) return lst[i] < rhs.lst[i]; } } return false; } bool operator>(const lint &rhs) const{return rhs < *this;} bool operator>=(const lint &rhs) const{return !(*this < rhs);} bool operator<=(const lint &rhs) const{return !(rhs < *this);} bool operator==(const lint &rhs) const{return !(*this < rhs || rhs < *this);} bool operator!=(const lint &rhs) const{return *this < rhs || rhs < *this;} lint operator+(const lint &rhs) const { lint ans; ans.lst.clear(); if(nega) { if(rhs.nega) { ans.nega = true; for(int i = 0, j = 0;;++i) { if(j == 0 && i >= lst.size() && i >= rhs.lst.size()) break; int x = j; if(i < lst.size()) x += lst[i]; if(i < rhs.lst.size()) x += rhs.lst[i]; ans.lst.push_back(x%BASE); j = x/BASE; } } else { if(-(*this) > rhs){ans = -(*this) - rhs;ans.nega = true;} else{ ans = rhs - -(*this);ans.nega = false;} } }else{ if(rhs.nega) { if(*this >= -rhs){ ans = *this - -rhs;ans.nega = false;} else{ ans = -rhs - *this;ans.nega = true;} } else { ans.nega = false; for(int i = 0, j = 0;;++i) { if(j == 0 && i >= lst.size() && i >= rhs.lst.size()) break; int x = j; if(i < lst.size()) x += lst[i]; if(i < rhs.lst.size()) x += rhs.lst[i]; ans.lst.push_back(x%BASE); j = x/BASE; } } } return ans; } lint operator+=(const lint &rhs){*this = *this + rhs;return *this;} lint operator++(){*this = *this + 1; return *this;} lint operator++(int){lint tmp = *this;++(*this);return tmp;} lint operator-() const { lint ans = *this; ans.nega = !ans.nega; return ans; } lint operator-(const lint &rhs) const { lint ans; ans.lst.clear(); if(nega) { if(rhs.nega) { if(*this >= rhs) { ans.nega = false; for(int i = 0, j = 0;;++i) { if(i >= rhs.lst.size()) break; int x = rhs.lst[i] - j; j = 0; if(i < lst.size()) x -= lst[i]; if(x < 0){x += BASE; j = 1;} ans.lst.push_back(x); } }else{ ans.nega = true; for(int i = 0, j = 0;;++i) { if(i >= lst.size()) break; int x = lst[i] - j; j = 0; if(i < rhs.lst.size()) x -= rhs.lst[i]; if(x < 0){x += BASE; j = 1;} ans.lst.push_back(x); } } }else{ ans = -(*this) + rhs;ans.nega = true;} }else{ if(rhs.nega){ ans = *this + -rhs;ans.nega = false;} else{ if(*this >= rhs) { ans.nega = false; for(int i = 0, j = 0;;++i) { if(i >= lst.size()) break; int x = lst[i] - j; j = 0; if(i < rhs.lst.size()) x -= rhs.lst[i]; if(x < 0){x += BASE; j = 1;} ans.lst.push_back(x); } }else { ans.nega = true; for(int i = 0, j = 0;;++i) { if(i >= rhs.lst.size()) break; int x = rhs.lst[i] - j; j = 0; if(i < lst.size()) x -= lst[i]; if(x < 0){x += BASE; j = 1;} ans.lst.push_back(x); } } } } for(;;) if(ans.lst.back() == 0 && ans.lst.size() != 1) ans.lst.pop_back(); else break; return ans; } lint operator-=(const lint &rhs){*this = *this - rhs; return *this;} lint operator--(){*this = *this - 1; return *this;} lint operator--(int){lint tmp = *this; --(*this); return tmp;} lint operator*=(const int &rhs){*this = *this * rhs;return *this;} lint operator*=(const lint &rhs){*this = *this * rhs; return *this;} lint operator/(const int &rhs) const { lint res; res.lst.clear(); std::stack<int> ans; if(!rhs) throw(2333); if((nega || rhs < 0) && !(nega && rhs < 0)) res.nega = true; else res.nega = false; for(int i = lst.size() - 1, j = 0;i>=0;--i) { long long x = j*BASE; x += lst[i]; ans.push(x/abs(rhs)); j = x % abs(rhs); } while(!ans.empty()) { int i = ans.top(); ans.pop(); res.lst.push_back(i); } for(;;) if(!res.lst.back() && res.lst.size() != 1) res.lst.pop_back(); else break; return res; } lint operator/=(const int &rhs){*this = *this / rhs; return *this; } lint operator/(const lint &rhs) const { if(abs(*this) < abs(rhs)) return "0"; lint res, l = 1, r = abs(*this) + 1; while(l < r) { lint i = l + (r-l)/2; if(i * abs(rhs) == abs(*this)) {res = i; break;} else if(i * abs(rhs) < abs(*this)) l = i + 1; else r = i; } if(res == "0") res = l - 1; if((nega || rhs.nega) && !(nega && rhs.nega)) res.nega = true; else res.nega = false; return res; } lint operator/=(const lint &rhs){*this = *this / rhs; return *this;} lint operator%(const lint &rhs) const { if(abs(*this) < abs(rhs)) return *this; lint res, l = 1, r = abs(*this) + 1; while(l < r) { lint i = l + (r-l)/2; if(i * abs(rhs) == abs(*this)) {return "0";} else if(i * abs(rhs) < abs(*this)) l = i + 1; else r = i; } res = abs(*this) - (l - 1) * abs(rhs); if(nega) res.nega = true; else res.nega = false; return res; } lint operator%=(const lint &rhs){*this = *this % rhs; return *this;} }; std::istream& operator>> (std::istream &in, lint &x) { std::string st; if(!(in >> st)) return in; x = st.c_str(); for(;;) if(!x.lst.back() && x.lst.size() != 1) x.lst.pop_back(); else break; return in; } std::ostream& operator<< (std::ostream &out, const lint &x) { if(x.nega) out << '-'; out << x.lst.back(); for(int i = x.lst.size()-2; i >= 0; --i) { char buf[10]; sprintf(buf, "%08d", x.lst[i]); for(int j = 0; j < strlen(buf); ++j) out << buf[j]; } return out; } lint operator*(const lint &lhs, const int &rhs) { lint res; res.lst.clear(); if((lhs.nega || rhs < 0) && !(lhs.nega && rhs < 0)) res.nega = true; else res.nega = false; for(int i = 0, j = 0;;++i) { if(j == 0 && i >= lhs.lst.size()) break; long long x = j; if(i < lhs.lst.size()) x += lhs.lst[i] * (long long)(abs(rhs)); res.lst.push_back(x%lint::BASE); j = x/lint::BASE; } for(;;) if(!res.lst.back() && res.lst.size() != 1) res.lst.pop_back(); else break; return res; } lint operator*(const lint &lhs, const lint &rhs) { lint res; if((lhs.nega || rhs.nega) && !(lhs.nega && rhs.nega)) res.nega = true; else res.nega = false; for(int k = 0;k < rhs.lst.size(); ++k) { lint ls = abs(lhs) * rhs.lst[k]; for(int i = k, j = 0;;++i) { int x; if(j == 0) { if(i >= ls.lst.size() + k) break; else x = ls.lst[i-k]; }else { if(i >= ls.lst.size() + k) x = j; else x = j + ls.lst[i-k]; } if(i >= res.lst.size()) {res.lst.push_back(x%lint::BASE);j = x/lint::BASE;} else {x += res.lst[i]; res.lst[i] = x%lint::BASE; j = x/lint::BASE;} } } return res; } lint abs(lint x) { x.nega = false; return x; } lint sqrt(lint x) { if(x.nega) throw(2333); lint l = 1, r = x; while(l < r) { lint i = l + (r-l)/2; if(i * i == x) return i; else if(i * i < x) l = i + 1; else r = i; } return l - 1; } #endif using namespace std; lint jc(lint x) {return x == 1 ? "1" : jc(x-1)*x;} int main() { int x; lint ans; cin >> x; for(int i = 1; i <= x; ++i) ans += jc(i); cout << ans << endl; return 0; }
这个代码包含了许多东西,自己调调试试???