struct bigint{
static const int MAXN = 1005;
int d[MAXN], len;
void clean() { while(len > 1 && !d[len-1]) len--; }
bigint() { memset(d, 0, sizeof(d)); len = 1; }
bigint(int num) { *this = num; }
bigint(char* num) { *this = num; }
bigint operator = (const char* num){
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean();
return *this;
}
bigint operator = (int num) {
char s[20]; sprintf(s, "%d", num);
*this = s; return *this;
}
bigint operator + (const bigint& b) {
bigint c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bigint operator - (const bigint& b) {
bigint c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bigint operator * (const bigint& b)const {
int i, j; bigint c; c.len = len + b.len;
for(j = 0; j < b.len; j++) for(i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bigint operator / (const bigint& b) const {
int i, j;
bigint c = *this, a = 0;
for (i = len - 1; i >= 0; i--) {
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bigint operator % (const bigint& b) const {
int i, j;
bigint a = 0;
for (i = len - 1; i >= 0; i--) {
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
bool operator <(const bigint& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bigint operator /= (const bigint &b) { *this = *this / b; return *this; }
bigint operator *= (const bigint &b) { *this = *this * b; return *this; }
bigint operator += (const bigint &b) { *this = *this + b; return *this; }
bigint operator -= (const bigint &b) { *this = *this - b; return *this; }
bool operator >(const bigint& b) const{return b < *this;}
bool operator<=(const bigint& b) const{return !(b < *this);}
bool operator>=(const bigint& b) const{return !(*this < b);}
bool operator!=(const bigint& b) const{return b < *this || *this < b;}
bool operator==(const bigint& b) const{return !(b < *this) && !(b > *this);}
string str() const {
char s[MAXN]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
};
istream& operator >> (istream& in, bigint& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bigint& x)
{
out << x.str();
return out;
}
以上就是高精类模板了。
效率如下:
运行10000000次加法:1.716s
运行10000000次乘法:2.719s
运行100000次除法:10.8s
运行100000次%:11.3s
应用:
卡特兰数(n <= 100) :
#pragma GCC optimize(3)
#include<bits/stdc++.h>
#define mst(a,b) memset(a,b,sizeof(a))
#define For(i, k, j) for(int i = (k); i <= (j); i++)
#define INF 2147483647/3
#define ll long long
using namespace std;
inline int read()
{
int num = 0, flag = 1;
char c=' ';
for(;c>'9'||c<'0';c=getchar()) if(c=='-') flag = -1;
for(;c>='0'&&c<='9';num=(num<<1)+(num<<3)+c-48,c=getchar());
return num * flag;
}
struct bigint{
#define MAXN 105
int d[MAXN], len;
inline void clean() { while(len > 1 && !d[len-1]) len--; }
bigint() { memset(d, 0, sizeof(d)); len = 1; }
bigint(int num) { *this = num; }
bigint(char* num) { *this = num; }
bigint operator = (const char* num) {
memset(d, 0, sizeof(d)); len = strlen(num);
for(int i = 0; i < len; i++) d[i] = num[len-1-i] - '0';
clean(); return *this;
}
bigint operator = (int num) {
char s[20]; sprintf(s, "%d", num);
*this = s; return *this;
}
bigint operator + (const bigint& b) {
bigint c = *this; int i;
for (i = 0; i < b.len; i++){
c.d[i] += b.d[i];
if (c.d[i] > 9) c.d[i]%=10, c.d[i+1]++;
}
while (c.d[i] > 9) c.d[i++]%=10, c.d[i]++;
c.len = max(len, b.len);
if (c.d[i] && c.len <= i) c.len = i+1;
return c;
}
bigint operator - (const bigint& b) {
bigint c = *this; int i;
for (int i = 0; i < b.len; i++){
c.d[i] -= b.d[i];
if (c.d[i] < 0) c.d[i]+=10, c.d[i+1]--;
}
while (c.d[i] < 0) c.d[i++]+=10, c.d[i]--;
c.clean();
return c;
}
bigint operator * (const bigint& b)const {
bigint c; c.len = len + b.len;
for(int j = 0; j < b.len; j++) for(int i = 0; i < len; i++)
c.d[i+j] += d[i] * b.d[j];
for(int i = 0; i < c.len-1; i++)
c.d[i+1] += c.d[i]/10, c.d[i] %= 10;
c.clean();
return c;
}
bigint operator / (const bigint& b) const {
bigint c = *this, a = 0; int j;
for (int i = len - 1; i >= 0; i--) {
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
c.d[i] = j;
a = a - b*j;
}
c.clean();
return c;
}
bigint operator % (const bigint& b) const {
bigint a = 0;int j;
for (int i = len - 1; i >= 0; i--) {
a = a*10 + d[i];
for (j = 0; j < 10; j++) if (a < b*(j+1)) break;
a = a - b*j;
}
return a;
}
bool operator <(const bigint& b) const{
if(len != b.len) return len < b.len;
for(int i = len-1; i >= 0; i--)
if(d[i] != b.d[i]) return d[i] < b.d[i];
return false;
}
bigint operator /= (const bigint &b) { *this = *this / b; return *this; }
bigint operator *= (const bigint &b) { *this = *this * b; return *this; }
bigint operator += (const bigint &b) { *this = *this + b; return *this; }
bigint operator -= (const bigint &b) { *this = *this - b; return *this; }
bool operator >(const bigint& b) const{return b < *this;}
bool operator<=(const bigint& b) const{return !(b < *this);}
bool operator>=(const bigint& b) const{return !(*this < b);}
bool operator!=(const bigint& b) const{return b < *this || *this < b;}
bool operator==(const bigint& b) const{return !(b < *this) && !(b > *this);}
string str() const {
char s[MAXN]={};
for(int i = 0; i < len; i++) s[len-1-i] = d[i]+'0';
return s;
}
};
istream& operator >> (istream& in, bigint& x)
{
string s;
in >> s;
x = s.c_str();
return in;
}
ostream& operator << (ostream& out, const bigint& x)
{
out << x.str();
return out;
}
#define N 100005
bigint ans[N];
int main()
{
int n = read();
ans[0] = ans[1] = 1;
For(i, 2, n) {
ans[i] = 0;
}
for(int i = 2; i <= n; i ++) {
for(int j = 0; j < i; j ++) {
ans[i] += ans[j] * ans[i-1-j];
}
}
cout << ans[n] << endl;
return 0;
}