直接当unsigned int用,还可以直接查询从右往左起的任意一位数只不过初始化不同,具体看代码
代码:
#include <iostream>
#include "longint.h"
using namespace std;
/*
支持操作:
--------------------------------a和b为longint或int----------------------------------------
01.cin
02.cout
03.a+b
04.a-b
05.a*b
06.a/b
07.a%b
08.a += b
09.a -= b
10.a *= b
11.a /= b
12.a %= b
13.a++
14.b++
15.++a
16.++b
17.a > b
18.a < b
19.a >= b
20.a <= b
21.a == b
22.a != b
23.pow (a,b)
24.fact (a)
*/
int main () {
cin >> a >> b;
cout << a+b << endl << a-b << endl << a*b << endl << a/b << endl << a%b << endl;
return 0;
}
$longint.h:$
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
struct longint {
int num[10010];
int len;
longint (int a = 0) {
len = 1;
memset (num,0,sizeof (num));
int l = 0;
while (a) {
num[++l] = a%10;
a /= 10;
}
len = l;
}
operator bool () {
return len == 1 && num[1] == 0;
}
longint (int n[10010],int l = 1) {
for (int i = 1;i < l;i++) num[i] = n[i];
len = l;
}
int size () {
return len;
}
void operator = (longint a) {
for (int i = 1;i <= a.size ();i++) num[i] = a[i];
len = a.len;
}
int &operator [] (int x) {
return num[x];
}
longint & operator ++ () {
longint a;
for (int i = 1;i <= len;i++) a[i] = num[i];
a.len = len;
longint b (1);
int l = max (len,b.len);
memset (num,0,sizeof (num));
for (int i = 1;i <= l;i++) {
num[i] += a[i]+b[i];
num[i+1] += num[i]/10;
num[i] %= 10;
}
if (num[l+1] > 0) l++;
len = l;
return *this;
}
longint operator ++ (int) {
longint a;
for (int i = 1;i <= len;i++) a[i] = num[i];
a.len = len;
longint b (1);
int l = max (a.len,b.len);
for (int i = 1;i <= l;i++) {
num[i] += a.num[i]+b.num[i];
num[i+1] += num[i]/10;
num[i] %= 10;
}
if (num[l+1] > 0) l++;
len = l;
return a;
}
longint & operator -- () {
longint a;
for (int i = 1;i <= len;i++) a[i] = num[i];
a.len = len;
longint b (1);
int l = max (len,b.len);
for (int i = 1;i <= l;i++) {
if (a[i] < b[i]) {
num[i] += 10;
num[i+1]--;
}
num[i] -= b[i];
}
while (num[l] == 0 && l > 1) l--;
len = l;
return *this;
}
longint operator -- (int) {
longint a;
for (int i = 1;i <= len;i++) a[i] = num[i];
a.len = len;
longint b (1);
int l = max (a.len,b.len);
for (int i = 1;i <= l;i++) {
if (a[i] < b[i]) {
num[i] += 10;
num[i+1]--;
}
num[i] -= b[i];
}
while (num[l] == 0 && l > 1) l--;
len = l;
return a;
}
};
longint chg (int a) {
longint ans;
if (a == 0) return ans;
int len = 0;
while (a) {
ans.num[++len] = a%10;
a /= 10;
}
ans.len = len;
return ans;
}
int chg (longint a) {
int ans = 0;
for (int i = a.size ();i >= 1;i--) ans = ans*10+a[i];
return ans;
}
longint chg (string s) {
longint a;
int len = s.size ();
for (int i = 1;i <= len;i++) {
a.num[i] = s[len-i]-'0';
}
a.len = len;
return a;
}
bool operator < (longint a,longint b) {
if (a.len != b.len) return a.len < b.len;
for (int i = a.len;i >= 1;i--) {
if (a[i] != b[i]) return a[i] < b[i];
}
return false;
}
bool operator < (longint a,int b) {return a < chg (b);}
bool operator < (int a,longint b) {return chg (a) < b;}
bool operator > (longint a,longint b) {
if (a.len != b.len) return a.len > b.len;
for (int i = a.len;i >= 1;i--) {
if (a[i] != b[i]) return a[i] > b[i];
}
return false;
}
bool operator > (longint a,int b) {return a > chg (b);}
bool operator > (int a,longint b) {return chg (a) > b;}
bool operator <= (longint a,longint b) {
if (a.len != b.len) return a.len <= b.len;
for (int i = a.len;i >= 1;i--) {
if (a[i] != b[i]) return a[i] <= b[i];
}
return true;
}
bool operator <= (longint a,int b) {return a <= chg (b);}
bool operator <= (int a,longint b) {return chg (a) <= b;}
bool operator >= (longint a,longint b) {
if (a.len != b.len) return a.len > b.len;
for (int i = a.len;i >= 1;i--) {
if (a[i] != b[i]) return a[i] >= b[i];
}
return true;
}
bool operator >= (longint a,int b) {return a >= chg (b);}
bool operator >= (int a,longint b) {return chg (a) >= b;}
bool operator == (longint a,longint b) {
if (a.len != b.len) return false;
for (int i = a.len;i >= 1;i--) {
if (a[i] != b[i]) return false;
}
return true;
}
bool operator == (longint a,int b) {return a == chg (b);}
bool operator == (int a,longint b) {return chg (a) == b;}
bool operator != (longint a,longint b) {
if (a.len != b.len) return true;
for (int i = a.len;i >= 1;i--) {
if (a[i] != b[i]) return true;
}
return false;
}
bool operator != (longint a,int b) {return a != chg (b);}
bool operator != (int a,longint b) {return chg (a) != b;}
istream &operator >> (istream &in,longint &a) {
string s;
in >> s;
a = chg (s);
return in;
}
ostream &operator << (ostream &out,longint a) {
for (int i = a.size ();i >= 1;i--) out << a[i];
return out;
}
longint operator + (longint a,longint b) {
longint c;
int len = max (a.len,b.len);
for (int i = 1;i <= len;i++) {
c.num[i] += a.num[i]+b.num[i];
c.num[i+1] += c.num[i]/10;
c.num[i] %= 10;
}
if (c.num[len+1] > 0) len++;
c.len = len;
return c;
}
longint operator + (longint a,int b) {return a+chg (b);}
longint operator + (int a,longint b) {return chg (a)+b;}
longint operator - (longint a,longint b) {
if (a == b) return chg (0);
int len = a.len;
for (int i = 1;i <= len;i++) {
if (a[i] < b[i]) {
a[i] += 10;
a[i+1]--;
}
a[i] -= b[i];
}
while (a.num[len] == 0 && len > 1) len--;
a.len = len;
return a;
}
longint operator - (longint a,int b) {return a-chg (b);}
longint operator - (int a,longint b) {return chg (a)-b;}
longint operator * (longint a,longint b) {
longint c;
int len = a.len+b.len;
for (int i = 1;i <= a.len;i++) {
for (int j = 1;j <= b.len;j++) {
c.num[i+j-1] += a.num[i]*b.num[j];
c.num[i+j] += c.num[i+j-1]/10;
c.num[i+j-1] %= 10;
}
}
while (c.num[len] == 0 && len > 1) len--;
c.len = len;
return c;
}
longint operator * (longint a,int b) {return a*chg (b);}
longint operator * (int a,longint b) {return chg (a)*b;}
void numcpy (longint a,longint &b,int idx) {
for (int i = 1;i <= a.size ();i++) b[i+idx-1] = a[i];
b.len = a.size ()+idx-1;
}
longint operator / (longint a,longint b) {
longint c;
int len = a.size ()-b.size ()+1;
for (int i = len;i >= 1;i--) {
longint tmp;
numcpy (b,tmp,i);
while (a >= tmp) {
c[i]++;
a = a-tmp;
}
}
while (len > 1 && c[len] == 0) len--;
c.len = len;
return c;
}
longint operator / (int a,longint b) {return chg (a)/b;}
longint operator / (longint a,int b) {return a/chg (b);}
longint operator % (longint a,longint b) {
longint c;
int len = a.size ()-b.size ()+1;
for (int i = len;i >= 1;i--) {
longint tmp;
numcpy (b,tmp,i);
while (a >= tmp) {
c[i]++;
a = a-tmp;
}
}
return a;
}
longint operator % (int a,longint b) {return chg (a)%b;}
longint operator % (longint a,int b) {return a%chg (b);}
void operator += (longint &a,longint b) {a = a+b;}
void operator += (longint &a,int b) {a = a+chg (b);}
void operator -= (longint &a,longint b) {a = a-b;}
void operator -= (longint &a,int b) {a = a-chg (b);}
void operator *= (longint &a,longint b) {a = a*b;}
void operator *= (longint &a,int b) {a = a*chg (b);}
void operator /= (longint &a,longint b) {a = a/b;}
void operator /= (longint &a,int b) {a = a/chg (b);}
void operator %= (longint &a,longint b) {a = a%b;}
void operator %= (longint &a,int b) {a = a%chg (b);}
longint max (longint a,longint b) {return a < b ? b : a;}
longint min (longint a,longint b) {return a > b ? b : a;}
longint fact (longint a) {
longint ans = chg (1);
while (a > 0) {
ans *= a;
a--;
}
return ans;
}
longint pow (longint a,longint b) {
longint ans = chg (1);
while (b) {
if (b & 1) ans *= a;
a *= a;
b /= 2;
}
return ans;
}
longint pow (int a,longint b) {return pow (chg (a),b);}
longint pow (longint a,int b) {return pow (a,chg (b));}
第281行是不是要把
void operator %= (longint &a,int b) {a = a/chg (b);}
改成void operator %= (longint &a,int b) {a = a%chg (b);}
对
写错了
感谢提醒
高精度除高精度好像会tle,过不了数据
要看多大,毕竟高精度除以高精度我写的是 $O(n^2)$ 的(太菜了)
$\Huge AZ$
感谢,解决了我的一个大问题(求高精度的最大公因数)
请问需要背下来吗(doge)
能用就行,但是。。。
$\tiny{很慢}$
stO
谢谢orz
$$\large\color{darkviolet}{qpzc}$$