高精度写起来忒憋麻烦(写过乘除法的深有体会),所以我直接写了个高精度头文件(其实也是看到最近WCH大佬在研究这个)
如果有bug,请立即指出,我会修复的~
使用方法:新建一个源文件,将代码复制到这里面,将其命名为“gjd.h”,保存到:
Dev-cpp\MinGW64\lib\gcc\x86_64-w64-mingw32\4.9.2\include\c++;
如果源程序需要使用TA,那么只需要在文件头部加上一行:
#include "gjd.h"//注意不是尖括号!
提交时,将
#include "gjd.h"
删去,并将代码复制过来即可
使用方法:用bigint定义大整数~~(非负)~~类型;
支持操作:cin/cout,类型内的scan和print快速输入输出方法,+、-、乘、/、%、+=、-=、乘等于、/=、%=、<、>、==、<=、>=;
赋值运算:可以将int,long long类型、string类型和bigint类型赋给一个bigint类型,使用的是=运算符;
注意:在做加减乘时,运算对象必须是两个bigint,若想用一个整数与一个bigint做运算,那么可以使用bigerint将整数转为bigint型的; 可以像整数一样使用它,但是区别有两点:
一、不支持scanf与printf、++、--运算符;
二、不支持在定义类型时同时赋值;
可以定义bigint型的数组和STL容器哦!
好了,这个头文件里还有一些辅助函数,想了解自行阅读程序,下面是正文:
#include <vector>
#include <iostream>
#include <cstdio>
#include <string>
using namespace std;
template <typename T>
T sum(T *a,int first,int last){
T ans=0;
for(int i=first; i<last; i++)
ans+=a[i];
return ans;
}
class bigint {
public:
vector <int> s;
int x;
static const int BASE=1000;
int size() {
return s.size()-1;
}
bigint operator = (const string n) {
s.clear();
s.push_back(0);
for(int i=n.size()-1; i>=0; i--)
s.push_back(n[i]-48);
return *this;
}
bigint operator = (const bigint n) {
s.clear();
for(int i=0; i<n.s.size(); i++) s.push_back(n.s[i]);
return *this;
}
bigint operator = (long long n) {
s.clear();
s.push_back(0);
while(n) {
s.push_back(n%10);
n/=10;
}
return *this;
}
void scan() {
s.clear();
s.push_back(0);
while((x=(getchar()-48))>=0) s.push_back(x);
if(s.size()%2==0)
for(int i=1; i<s.size()/2; i++) {
int temp=s[i];
s[i]=s[s.size()-i];
s[s.size()-i]=temp;
}
else
for(int i=1; i<=s.size()/2; i++) {
int temp=s[i];
s[i]=s[s.size()-i];
s[s.size()-i]=temp;
}
}
void print() {
for(int i=s.size()-1; i>0; i--)
printf("%d",s[i]);
}
bool operator < (const bigint a) {
int len=s.size();
if(len<a.s.size()) return true;
if(len>a.s.size()) return false;
for(int i=len-1; i>0; i--)
if(s[i]<a.s[i]) return true;
else if(s[i]>a.s[i]) return false;
return false;
}
bool operator == (const bigint a) {
int len=s.size();
if(len!=a.s.size()) return false;
for(int i=1; i<len; i++)
if(s[i]!=a.s[i]) return false;
return true;
}
bool operator > (const bigint a) {
int len=s.size();
if(len<a.s.size()) return false;
if(len>a.s.size()) return true;
for(int i=len-1; i>0; i++)
if(s[i]<a.s[i]) return false;
else if(s[i]>a.s[i]) return true;
return false;
}
bool operator <= (const bigint a) {
return !(*this>a);
}
bool operator >= (const bigint a) {
return !(*this<a);
}
bigint operator + (const bigint a) {
bigint b;
int j=0;
int len=s.size(),len2=a.s.size(),len3=min(len,len2);
b.s.resize(max(len,len2));
for(int i=1; i<len3; i++) {
b.s[i]=(a.s[i]+s[i]+j)%10;
j=(a.s[i]+s[i]+j)/10;
}
if(len>len2)
for(int i=len3; i<len; i++) {
b.s[i]=(s[i]+j)%10;
j=(s[i]+j)/10;
}
else if(len<len2)
for(int i=len3; i<len2; i++) {
b.s[i]=(a.s[i]+j)%10;
j=(a.s[i]+j)/10;
}
if(j!=0) b.s.push_back(j);
int i=b.s.size()-1;
while(b.s[i]==0&&i>1) b.s.pop_back(),i--;
return b;
}
bigint operator += (const bigint a) {
int j=0,tmp;
int len=s.size(),len2=a.s.size(),len3=min(len,len2);
s.resize(max(len,len2));
for(int i=1; i<len3; i++) {
tmp=s[i];
s[i]=(a.s[i]+s[i]+j)%10;
j=(a.s[i]+tmp+j)/10;
}
if(len>len2)
for(int i=len3; i<len; i++) {
tmp=s[i];
s[i]=(s[i]+j)%10;
j=(tmp+j)/10;
}
else if(len<len2)
for(int i=len3; i<len2; i++) {
s[i]=(a.s[i]+j)%10;
j=(a.s[i]+j)/10;
}
if(j!=0) s.push_back(j);
int i=s.size()-1;
while(s[i]==0&&i>1) s.pop_back(),i--;
return *this;
}
bigint operator - (const bigint a) {
bigint b;
int j=0;
int len=s.size(),len2=a.s.size(),len3=min(len,len2);
b.s.resize(max(len,len2));
for(int i=1; i<len3; i++) {
b.s[i]=s[i]-a.s[i]-j;
if(b.s[i]<0) b.s[i]+=10,j=1;
else j=0;
}
if(len>len2)
for(int i=len3; i<len; i++) {
b.s[i]=s[i]-j;
if(b.s[i]<0) b.s[i]+=10,j=1;
else j=0;
}
int i=b.s.size()-1;
while(b.s[i]==0&&i>1) b.s.pop_back(),i--;
return b;
}
bigint operator -= (const bigint a){
int j=0;
int len=s.size(),len2=a.s.size(),len3=min(len,len2);
s.resize(max(len,len2));
for(int i=1; i<len3; i++) {
s[i]=s[i]-a.s[i]-j;
if(s[i]<0) s[i]+=10,j=1;
else j=0;
}
if(len>len2)
for(int i=len3; i<len; i++) {
s[i]=s[i]-j;
if(s[i]<0) s[i]+=10,j=1;
else j=0;
}
int i=s.size()-1;
while(s[i]==0&&i>1) s.pop_back(),i--;
return *this;
}
bigint operator * (const bigint a) {
bigint b;
int len=s.size(),len2=a.s.size();
b.s.resize(len+len2+2);
for(int i=1; i<len; i++) {
int temp=0;
for(int j=1; j<len2; j++) {
b.s[i+j-1]+=s[i]*a.s[j]+temp;
temp=b.s[i+j-1]/10;
b.s[i+j-1]%=10;
}
b.s[i+len2-1]=temp;
}
int i=b.s.size()-1;
while(b.s[i]==0&&i>1) b.s.pop_back(),i--;
return b;
}
bigint operator *= (const bigint a){
bigint b;
int len=s.size(),len2=a.s.size();
b.s.resize(len+len2+2);
for(int i=1; i<len; i++) {
int temp=0;
for(int j=1; j<len2; j++) {
b.s[i+j-1]+=s[i]*a.s[j]+temp;
temp=b.s[i+j-1]/10;
b.s[i+j-1]%=10;
}
b.s[i+len2-1]=temp;
}
int i=b.s.size()-1;
while(b.s[i]==0&&i>1) b.s.pop_back(),i--;
*this=b;
return *this;
}
bigint operator / (long long num) {
bigint a;
int cur=s.size(),k=0,q;
a.s.push_back(0);
while(cur>1) {
q=k*10+s[--cur];
a.s.push_back(q/num);
k=q%num;
}
int c=a.s.size();
for(int i=1; i<=c/2; i++)
swap(a.s[i],a.s[c-i]);
int i=a.s.size()-1;
while(a.s[i]==0&&i>1) a.s.pop_back(),i--;
return a;
}
bigint operator / (const bigint a) {
bigint b,p,q;
int cur=s.size(),wei;
q=0;
b.s.push_back(0);
while(cur>1) {
wei=0;
bigint temp;
temp=s[--cur];
p=q;
if(p.s.size()>1||p.s[1]>0)
p.s.push_back(0);
int i=p.s.size()-1;
int tmp=0;
for(int i=1; i<p.s.size(); i++)
swap(tmp,p.s[i]);
q=p+temp;
while(q>=a) q=q-a,wei++;
b.s.push_back(wei);
}
int c=b.s.size();
for(int i=1; i<=c/2; i++)
swap(b.s[i],b.s[c-i]);
int i=b.s.size()-1;
while(b.s[i]==0&&i>1) b.s.pop_back(),i--;
if(b.s.size()==1) b.s.push_back(0);
return b;
}
bigint operator /= (long long num){
bigint a;
a.s.push_back(0);
int cur=s.size(),k=0,q;
while(cur>1) {
q=k*10+s[--cur];
a.s.push_back(q/num);
k=q%num;
}
int c=a.s.size();
for(int i=1; i<=c/2; i++)
swap(a.s[i],a.s[c-i]);
int i=a.s.size()-1;
while(a.s[i]==0&&i>1) a.s.pop_back(),i--;
*this=a;
return *this;
}
bigint operator /= (bigint a){
bigint b,p,q;
b.s.push_back(0);
int cur=s.size(),wei;
q=0;
while(cur>1) {
wei=0;
bigint temp;
temp=s[--cur];
p=q;
if(p.s.size()>1||p.s[1]>0)
p.s.push_back(0);
int i=p.s.size()-1;
int tmp=0;
for(int i=1; i<p.s.size(); i++)
swap(tmp,p.s[i]);
q=p+temp;
while(q>=a) q=q-a,wei++;
b.s.push_back(wei);
}
int c=b.s.size();
for(int i=1; i<=c/2; i++)
swap(b.s[i],b.s[c-i]);
int i=b.s.size()-1;
while(b.s[i]==0&&i>1) b.s.pop_back(),i--;
if(b.s.size()==1) b.s.push_back(0);
*this=b;
return *this;
}
long long operator % (const long long num) {
long long cur=s.size(),k=0,q;
while(cur>1) {
q=k*10+s[--cur];
k=q%num;
}
return k;
}
bigint operator % (const bigint a) {
bigint p,q;
int cur=s.size();
q=0;
while(cur>1) {
bigint temp;
temp=s[--cur];
p=q;
if(p.s.size()>1||p.s[1]>0)
p.s.push_back(0);
int i=p.s.size()-1;
int tmp=0;
for(int i=1; i<p.s.size(); i++)
swap(tmp,p.s[i]);
q=p+temp;
while(q>=a) q=q-a;
}
return q;
}
bigint operator %= (const long long num){
long long cur=s.size(),k=0,q;
while(cur>1) {
q=k*10+s[--cur];
k=q%num;
}
*this=k;
return *this;
}
bigint operator %= (const bigint a){
bigint p,q;
int cur=s.size();
q=0;
while(cur>1) {
bigint temp;
temp=s[--cur];
p=q;
if(p.s.size()>1||p.s[1]>0)
p.s.push_back(0);
int i=p.s.size()-1;
int tmp=0;
for(int i=1; i<p.s.size(); i++)
swap(tmp,p.s[i]);
q=p+temp;
while(q>=a) q=q-a;
}
*this=q;
return *this;
}
};
extern "C" istream& operator >> (istream &in,bigint &x) {
string s;
if(!(in>>s)) return in;
x=s;
return in;
}
extern "C" ostream& operator << (ostream &out,const bigint x) {
for(int i=x.s.size()-1; i>0; i--)
out<<x.s[i];
return out;
}
template <typename T>
bigint bigerint (T num) {
bigint p;
p=num;
return p;
}
template <class T>
bool is_prime (T num) {
for(T i=2; i<=num/i; i++)
if(num%i==0) return false;
return true;
}
共 3 条回复
写得很棒!
大家忙着期末考试了吧~
没人用吗qwq