首先上模板(不断更新中...)(根据刘汝佳AOAPCII修改)
1 #include <iostream> 2 #include <sstream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cmath> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <cctype> 10 #include <algorithm> 11 #include <cmath> 12 #include <deque> 13 #include <queue> 14 #include <map> 15 #include <stack> 16 #include <list> 17 #include <iomanip> 18 using namespace std; 19 /// 20 #define lc o << 1 21 #define rc o << 1 | 1 22 #define lson L , M , o << 1 23 #define rson M + 1 , R , o << 1 | 1 24 #define PI acos(-1.0) 25 #define INF 0x7fffffff 26 #define esp 1e-6 27 #define maxn 2001 28 29 typedef long long ll; 30 /// 31 struct BigInteger 32 { 33 //初始化. 34 static const int BASE = 100000000; 35 static const int WIDTH = 8; 36 vector<int> s; 37 38 int len; 39 BigInteger(ll num = 0) { 40 *this = num; 41 } 42 BigInteger operator = (ll num) 43 { 44 s.clear(); 45 do 46 { 47 s.push_back(num%BASE); 48 num /= BASE; 49 } 50 while(num > 0); 51 return *this; 52 } 53 BigInteger operator = (const string& num) 54 { 55 s.clear(); 56 int x, len = (num.length() - 1) / WIDTH + 1;//这里就是把一个“1221434 125432 52526266”这样的大数转换成3个8位int型存入s中 57 58 for(int i = 0; i < len; i++) 59 { 60 int end = num.length() - i*WIDTH; 61 int start = max(0, end-WIDTH); 62 sscanf(num.substr(start, end-start).c_str(), "%d", &x); 63 s.push_back(x); 64 } 65 return *this; 66 } 67 68 //加减乘除 69 BigInteger operator + (const BigInteger &b) const 70 { 71 BigInteger c; 72 c.s.clear(); 73 for(int i = 0, g = 0; ; ++i) 74 { 75 if(!g && i >= s.size() && i >= b.s.size()) break; 76 int x = g; 77 if(i < s.size()) x += s[i]; 78 if(i < b.s.size()) x += b.s[i]; 79 80 c.s.push_back(x % BASE); 81 g = x / BASE; 82 } 83 return c; 84 } 85 86 BigInteger operator += (const BigInteger &b) 87 { 88 *this = *this + b; 89 return *this; 90 } 91 92 //注意前提:两个数都没有前导零 93 bool operator < (const BigInteger& b) const 94 { 95 if(!s.size() != b.s.size()) 96 return s.size() < b.s.size(); 97 for(int i = s.size()-1; i >= 0; i--) 98 if(s[i] != b.s[i]) 99 return s[i] < b.s[i]; 100 return false;//相等 101 } 102 bool operator > (const BigInteger& b) const 103 { 104 return b < *this; 105 } 106 bool operator <= (const BigInteger& b) const 107 { 108 return !(b < *this); 109 } 110 bool operator >= (const BigInteger& b) const 111 { 112 return !(*this < b); 113 } 114 bool operator != (const BigInteger& b) const 115 { 116 return (b < *this) || (*this < b); 117 } 118 bool operator == (const BigInteger& b) const 119 { 120 return !(b < *this) && !(*this < b); 121 } 122 }; 123 124 istream & operator >> (istream &in, BigInteger &x) 125 { 126 string s; 127 in >> s; 128 x = s.c_str(); 129 return in; 130 } 131 132 ostream & operator << (ostream &out, const BigInteger &x) 133 { 134 out << x.s.back(); 135 for(int i=x.s.size()-2;i>=0;i--) 136 { 137 char buf[20]; 138 sprintf(buf,"%08d",x.s[i]); 139 for(int j=0;j<strlen(buf);j++) out<<buf[j]; 140 } 141 return out; 142 }
例题:UVA 11375 Matches(高精度 + 递推)
代码如下
1 tupedef maxn 2001 2 3 BigInteger f[maxn]; 4 BigInteger ans[maxn]; 5 const int c[]={6,2,5,5,4,5,6,3,7,6}; 6 int n; 7 8 void init() 9 { 10 for (int i = 0; i <= maxn; i++) 11 f[i] = 0; 12 f[0] = 1; 13 for(int i = 0; i <= maxn; i++) 14 { 15 for(int j = 0; j <= 9; j++) 16 { 17 if(i+c[j] < maxn && !(i==0 && j==0)) 18 { 19 f[i+c[j]] += f[i]; 20 } 21 } 22 } 23 ans[0]=0; 24 for (int i = 1; i <= maxn; i++) 25 { 26 ans[i] = ans[i-1] + f[i]; 27 } 28 } 29 int main() 30 { 31 //freopen("out.txt", "w", stdout); 32 init(); 33 while (cin >> n) 34 { 35 if (n >= 6) 36 cout << ans[n]+1 << endl; 37 else 38 cout << ans[n] << endl; 39 } 40 return 0; 41 }
超强模板(网络转载)
#include <deque> #include <vector> #include <iostream> #include <string> #include <algorithm> using namespace std; class DividedByZeroException {}; class BigInteger { private: vector<char> digits; bool sign; // true for positive, false for negitive void trim(); // remove zeros in tail, but if the value is 0, keep only one:) public: BigInteger(int); // construct with a int integer BigInteger(string&) ; BigInteger(); BigInteger (const BigInteger&); BigInteger operator=(const BigInteger& op2); BigInteger abs() const; BigInteger pow(int a); //binary operators friend BigInteger operator+=(BigInteger&,const BigInteger&); friend BigInteger operator-=(BigInteger&,const BigInteger&); friend BigInteger operator*=(BigInteger&,const BigInteger&); friend BigInteger operator/=(BigInteger&,const BigInteger&) throw(DividedByZeroException); friend BigInteger operator%=(BigInteger&,const BigInteger&) throw(DividedByZeroException); friend BigInteger operator+(const BigInteger&,const BigInteger&); friend BigInteger operator-(const BigInteger&,const BigInteger&); friend BigInteger operator*(const BigInteger&,const BigInteger&); friend BigInteger operator/(const BigInteger&,const BigInteger&) throw(DividedByZeroException); friend BigInteger operator%(const BigInteger&,const BigInteger&) throw(DividedByZeroException); //uniary operators friend BigInteger operator-(const BigInteger&); //negative friend BigInteger operator++(BigInteger&); //++v friend BigInteger operator++(BigInteger&,int); //v++ friend BigInteger operator--(BigInteger&); //--v friend BigInteger operator--(BigInteger&,int); //v-- friend bool operator>(const BigInteger&,const BigInteger&); friend bool operator<(const BigInteger&,const BigInteger&); friend bool operator==(const BigInteger&,const BigInteger&); friend bool operator!=(const BigInteger&,const BigInteger&); friend bool operator>=(const BigInteger&,const BigInteger&); friend bool operator<=(const BigInteger&,const BigInteger&); friend ostream& operator<<(ostream&,const BigInteger&); //print the BigInteger friend istream& operator>>(istream&, BigInteger&); // input the BigInteger public: static const BigInteger ZERO; static const BigInteger ONE; static const BigInteger TEN; }; // BigInteger.cpp const BigInteger BigInteger::ZERO=BigInteger(0); const BigInteger BigInteger::ONE =BigInteger(1); const BigInteger BigInteger::TEN =BigInteger(10); BigInteger::BigInteger() { sign=true; } BigInteger::BigInteger(int val){// construct with a int integer if (val >= 0) sign = true; else{ sign = false; val *= (-1); } do{ digits.push_back( (char)(val%10) ); val /= 10; } while ( val != 0 ); } BigInteger::BigInteger(string& def){ sign=true; for ( string::reverse_iterator iter = def.rbegin() ; iter < def.rend(); iter++){ char ch = (*iter); if (iter == def.rend()-1){ if ( ch == '+' ) break; if(ch == '-' ){ sign = false; break; } } digits.push_back( (char)((*iter) - '0' ) ); } trim(); } void BigInteger::trim(){ vector<char>::reverse_iterator iter = digits.rbegin(); while(!digits.empty() && (*iter) == 0){ digits.pop_back(); iter=digits.rbegin(); } if( digits.size()==0 ){ sign = true; digits.push_back(0); } } BigInteger::BigInteger(const BigInteger& op2){ sign = op2.sign; digits=op2.digits; } BigInteger BigInteger::operator=(const BigInteger& op2){ digits = op2.digits; sign = op2.sign; return (*this); } BigInteger BigInteger::abs() const { if(sign) return *this; else return -(*this); } BigInteger BigInteger::pow(int a) { BigInteger res(1); for(int i=0; i<a; i++) res*=(*this); return res; } //binary operators BigInteger operator+=(BigInteger& op1,const BigInteger& op2){ if( op1.sign == op2.sign ){ //只处理相同的符号的情况,异号的情况给-处理 vector<char>::iterator iter1; vector<char>::const_iterator iter2; iter1 = op1.digits.begin(); iter2 = op2.digits.begin(); char to_add = 0; //进位 while ( iter1 != op1.digits.end() && iter2 != op2.digits.end()){ (*iter1) = (*iter1) + (*iter2) + to_add; to_add = ((*iter1) > 9); // 大于9进一位 (*iter1) = (*iter1) % 10; iter1++; iter2++; } while ( iter1 != op1.digits.end() ){ // (*iter1) = (*iter1) + to_add; to_add = ( (*iter1) > 9 ); (*iter1) %= 10; iter1++; } while ( iter2 != op2.digits.end() ){ char val = (*iter2) + to_add; to_add = (val > 9) ; val %= 10; op1.digits.push_back(val); iter2++; } if( to_add != 0 ) op1.digits.push_back(to_add); return op1; } else{ if (op1.sign) return op1 -= (-op2); else return op1= op2 - (-op1); } } BigInteger operator-=(BigInteger& op1,const BigInteger& op2){ if( op1.sign == op2.sign ){ //只处理相同的符号的情况,异号的情况给+处理 if(op1.sign) { if(op1 < op2) // 2 - 3 return op1=-(op2 - op1); } else { if(-op1 > -op2) // (-3)-(-2) = -(3 - 2) return op1=-((-op1)-(-op2)); else // (-2)-(-3) = 3 - 2 return op1= (-op2) - (-op1); } vector<char>::iterator iter1; vector<char>::const_iterator iter2; iter1 = op1.digits.begin(); iter2 = op2.digits.begin(); char to_substract = 0; //借位 while ( iter1 != op1.digits.end() && iter2 != op2.digits.end()){ (*iter1) = (*iter1) - (*iter2) - to_substract; to_substract = 0; if( (*iter1) < 0 ){ to_substract=1; (*iter1) += 10; } iter1++; iter2++; } while ( iter1 != op1.digits.end() ){ (*iter1) = (*iter1) - to_substract; to_substract = 0; if( (*iter1) < 0 ){ to_substract=1; (*iter1) += 10; } else break; iter1++; } op1.trim(); return op1; } else{ if (op1 > BigInteger::ZERO) return op1 += (-op2); else return op1 = -(op2 + (-op1)); } } BigInteger operator*=(BigInteger& op1,const BigInteger& op2){ BigInteger result(0); if (op1 == BigInteger::ZERO || op2==BigInteger::ZERO) result = BigInteger::ZERO; else{ vector<char>::const_iterator iter2 = op2.digits.begin(); while( iter2 != op2.digits.end() ){ if(*iter2 != 0){ deque<char> temp(op1.digits.begin() , op1.digits.end()); char to_add = 0; deque<char>::iterator iter1 = temp.begin(); while( iter1 != temp.end() ){ (*iter1) *= (*iter2); (*iter1) += to_add; to_add = (*iter1) / 10; (*iter1) %= 10; iter1++; } if( to_add != 0) temp.push_back( to_add ); int num_of_zeros = iter2 - op2.digits.begin(); while( num_of_zeros--) temp.push_front(0); BigInteger temp2; temp2.digits.insert( temp2.digits.end() , temp.begin() , temp.end() ); temp2.trim(); result = result + temp2; } iter2++; } result.sign = ( (op1.sign && op2.sign) || (!op1.sign && !op2.sign) ); } op1 = result; return op1; } BigInteger operator/=(BigInteger& op1 , const BigInteger& op2 ) throw(DividedByZeroException) { if( op2 == BigInteger::ZERO ) throw DividedByZeroException(); BigInteger t1 = op1.abs(), t2 = op2.abs(); if ( t1 < t2 ){ op1 = BigInteger::ZERO; return op1; } //现在 t1 > t2 > 0 //只需将 t1/t2的结果交给result就可以了 deque<char> temp; vector<char>::reverse_iterator iter = t1.digits.rbegin(); BigInteger temp2(0); while( iter != t1.digits.rend() ){ temp2 = temp2 * BigInteger::TEN + BigInteger( (int)(*iter) ); char s = 0; while( temp2 >= t2 ){ temp2 = temp2 - t2; s = s + 1; } temp.push_front( s ); iter++; } op1.digits.clear(); op1.digits.insert( op1.digits.end() , temp.begin() , temp.end() ); op1.trim(); op1.sign = ( (op1.sign && op2.sign) || (!op1.sign && !op2.sign) ); return op1; } BigInteger operator%=(BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException) { return op1 -= ((op1 / op2)*op2); } BigInteger operator+(const BigInteger& op1,const BigInteger& op2){ BigInteger temp(op1); temp += op2; return temp; } BigInteger operator-(const BigInteger& op1,const BigInteger& op2){ BigInteger temp(op1); temp -= op2; return temp; } BigInteger operator*(const BigInteger& op1,const BigInteger& op2){ BigInteger temp(op1); temp *= op2; return temp; } BigInteger operator/(const BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException) { BigInteger temp(op1); temp /= op2; return temp; } BigInteger operator%(const BigInteger& op1,const BigInteger& op2) throw(DividedByZeroException) { BigInteger temp(op1); temp %= op2; return temp; } //uniary operators BigInteger operator-(const BigInteger& op){ //negative BigInteger temp = BigInteger(op); temp.sign = !temp.sign; return temp; } BigInteger operator++(BigInteger& op){ //++v op += BigInteger::ONE; return op; } BigInteger operator++(BigInteger& op,int x){ //v++ BigInteger temp(op); ++op; return temp; } BigInteger operator--(BigInteger& op){ //--v op -= BigInteger::ONE; return op; } BigInteger operator--(BigInteger& op,int x){ //v-- BigInteger temp(op); --op; return temp; } bool operator<(const BigInteger& op1,const BigInteger& op2){ if( op1.sign != op2.sign ) return !op1.sign; else{ if(op1.digits.size() != op2.digits.size()) return (op1.sign && op1.digits.size()<op2.digits.size()) || (!op1.sign && op1.digits.size()>op2.digits.size()); vector<char>::const_reverse_iterator iter1,iter2; iter1 = op1.digits.rbegin();iter2 = op2.digits.rbegin(); while( iter1 != op1.digits.rend() ){ if( op1.sign && *iter1 < *iter2 ) return true; if( op1.sign && *iter1 > *iter2 ) return false; if( !op1.sign && *iter1 > *iter2 ) return true; if( !op1.sign && *iter1 < *iter2 ) return false; iter1++; iter2++; } return false; } } bool operator==(const BigInteger& op1,const BigInteger& op2){ if( op1.sign != op2.sign || op1.digits.size() != op2.digits.size() ) return false; vector<char>::const_iterator iter1,iter2; iter1 = op1.digits.begin(); iter2 = op2.digits.begin(); while( iter1!= op1.digits.end() ){ if( *iter1 != *iter2 ) return false; iter1++; iter2++; } return true; } bool operator!=(const BigInteger& op1,const BigInteger& op2){ return !(op1==op2); } bool operator>=(const BigInteger& op1,const BigInteger& op2){ return (op1>op2) || (op1==op2); } bool operator<=(const BigInteger& op1,const BigInteger& op2){ return (op1<op2) || (op1==op2); } bool operator>(const BigInteger& op1,const BigInteger& op2){ return !(op1<=op2); } ostream& operator<<(ostream& stream,const BigInteger& val){ //print the BigInteger if (!val.sign) stream << "-"; for ( vector<char>::const_reverse_iterator iter = val.digits.rbegin(); iter != val.digits.rend() ; iter++) stream << (char)((*iter) + '0'); return stream; } istream& operator>>(istream& stream, BigInteger& val){ //Input the BigInteger string str; stream >> str; val=BigInteger(str); return stream; } ////////////////////////////////////////////////////////////