您的位置:首页 > 其它

高精度模板

2016-05-19 18:43 309 查看
by spark:



#include<iostream>
#include<cstdio>
#include<vector>
#include<cstring>
#define LL long long
using namespace std;
struct sparkint{
static const int BASE=1000000000;
static const int width=9;
vector<LL> s;

sparkint (LL num=0){*this = num;}
sparkint operator = (LL num){
s.clear();
do{
s.push_back(num%BASE);
num/=BASE;
}while(num>0);
return *this;
}
sparkint operator = (const string& str){
s.clear();
int x,len=(str.length()-1)/width+1;
for(int i=0;i<len;i++){
int end=str.length()-i*width;
int start=max(0,end-width);
sscanf(str.substr(start,end-start).c_str(),"%d",&x);
s.push_back(x);
}
return *this;
}
//四则运算
sparkint operator + (const sparkint &b)const {
sparkint c;
c.s.clear();
for(int i=0,g=0;;i++){
if(i>=s.size()&&i>=b.s.size()&&g==0) break;
int x=g;
if(i<s.size())x+=s[i];
if(i<b.s.size()) x+=b.s[i];
c.s.push_back(x%BASE);
g=x/BASE;
}
return c;
}
sparkint operator - (const sparkint &b)const {
sparkint c;
c.s.clear();
for(int i=0,g=0;;i++){
if(i>=s.size()&&i>=b.s.size()&&g==0) break;
int x=g;
if(i<s.size()) x+=s[i];
if(i<b.s.size()) x-=b.s[i];
if(x<0) g=-1,x+=BASE;
else g=0;
c.s.push_back(x);
}
return c;
}

sparkint operator * (const sparkint &b)const {
LL i,j,g=0,cur,size=s.size()+b.s.size();
LL buf[size+5];
sparkint c;
c.s.clear();
memset(buf,0,sizeof(buf));
if((*this)==0||b==0) return c;
for(i=s.size()-1;i>=0;i--)
for(j=b.s.size()-1;j>=0;j--)
buf[i+j]+=b.s[j]*s[i];
for(i=0;i<size;i++){
buf[i+1]+=buf[i]/BASE;
buf[i]%=BASE;
}
if(buf[size-1]==0)size--;
for(i=0;i<size;i++)c.s.push_back(buf[i]);
return c;
}
sparkint operator / (const sparkint &b)const {

}
sparkint operator % (const sparkint &b)const {
}
//比较运算符
bool operator < (const sparkint &b)const {
if(s.size()!=b.s.size()) return s.size()<b.s.size();
for(int i=s.size()-1;i>=0;i--)
if(s[i]!=b.s[i]) return s[i]<b.s[i];
return false;
}
bool operator > (const sparkint &b)const {
return b<(*this);
}
bool operator == (const sparkint &b)const {
return !(*this<b) && !(b< *this);
}
bool operator <= (const sparkint &b)const {
return *this<b || *this == b;
}
bool operator >= (const sparkint &b)const {
return *this>b || *this == b;
}
bool operator != (const sparkint &b)const {
return *this<b || *this > b;
}
sparkint operator *= (const sparkint &b){
*this=(*this)*b; return *this;
}
sparkint operator -= (const sparkint &b){
*this=(*this)-b; return *this;
}
sparkint operator += (const sparkint &b){
*this=(*this)+b; return *this;
}
sparkint operator ++ (int) {
*this=*this+1; return *this;
}
sparkint& operator ++ () {
*this=*this+1; return *this;
}
sparkint operator -- (int) {
*this=*this-1; return *this;
}
sparkint& operator -- () {
*this=*this-1; return *this;
}
};

ostream& operator <<(ostream &out,const sparkint& x){
out<<x.s.back();
for(int i=x.s.size()-2;i>=0;i--){
char buf[20];
sprintf(buf,"%09d",x.s[i]);
for(int j=0;j<strlen(buf);j++)out<<buf[j];
}
return out;
}
istream&  operator >> (istream &in,sparkint& x){
string s;
if(!(in>>s)) return in;
x=s;
return in;
}

int main(){
sparkint ans=1,i,n;
cin>>n;
for(i=1;i<=n;i++)ans=ans*i;
cout<<ans<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: