标签:
#include<bits/stdc++.h> #define clr(x,y) memset((x),(y),sizeof(x)) using namespace std; typedef long long LL; const int MaxL=400; //大数长度 //大整数 //减法只能大减小 struct bign { int len, s[MaxL]; //构造函数 bign () { memset(s, 0, sizeof(s)); len = 1; } bign (int num) { *this = num; } bign (const char *num) { *this = num; } //*初始化 void init(int num) //int初始化 { *this=num; } void init(char *num) // 字符串初始化 { *this=num; } void init(string num) { *this=num; } //去前导0 void clean() { while(len>1 && !s[len-1]) len--; } //*赋值 bign operator = (const bign &b) { clr(s,0); len=b.len; for (int i=0;i<len;++i) s[i]=b.s[i]; return *this; } bign operator = (const int num) { char s[MaxL]; sprintf(s,"%d",num); *this = s; return *this; } bign operator = (const char *num) { while (num[0]==‘0‘) ++num; //去前导0 clr(s,0); len = strlen(num); for(int i=0;i<len;i++) s[i] = num[len-i-1]-‘0‘; return *this; } bign operator = (string num) { *this=num.c_str(); return *this; } //大整数四则运算 //加法 bign operator + (const bign &b) const //+ { bign c; c.len=0; for(int i=0, g=0; g || i<max(len,b.len); i++) { int x=g; if(i<len) x+=s[i]; if(i<b.len) x+=b.s[i]; c.s[c.len++]=x%10; g=x/10; } return c; } bign operator + (const int &b) const { bign c; c.init(b); return *this+c; } //乘法 bign operator * (const bign &b) //* { bign c; c.len=len+b.len; for(int i=0;i < len;i++) { for(int j=0; j<b.len; j++) { c.s[i+j]+=s[i]*b.s[j]; } } for(int i=0; i<c.len;i++) { c.s[i+1]+=c.s[i]/10; c.s[i]%=10; } c.clean(); return c; } bign operator * (const int &b) { bign c; c.init(b); return (*this)*c; } //减法 bign operator - (const bign &b) { bign c; c.len=0; for(int i=0,g=0;i<len;i++) { int x=s[i]-g; if(i<b.len) x-=b.s[i]; if(x>=0) g=0; else { g=1; x+=10; } c.s[c.len++]=x; } c.clean(); return c; } bign operator - (const int &b) { bign c; c.init(b); return *this-c; } //除法 bign operator / (const bign &b) { bign c,f; for(int i=len-1;i>=0;i--) { f=f*10; f.s[0]=s[i]; while(f >= b) { f=f-b; c.s[i]++; } } c.len=len; c.clean(); return c; } bign operator / (const int &b) { bign c; c.init(b); return (*this)/c; } //取余 bign operator % (const bign &b) { bign r=*this / b; r=*this-r*b; return r; } bign operator % (const int &b) { bign c; c.init(b); return (*this)%c; } //大小比较 //大于号 bool operator < (const bign &b) { if(len!=b.len) return len<b.len; for(int i=len-1;i>=0;i--) { if(s[i]!=b.s[i]) return s[i]<b.s[i]; } return false; } bool operator < (const int &b) { bign c; c.init(b); return *this<c; } //小于号 bool operator > (const bign &b) { if(len!=b.len) return len>b.len; for(int i=len-1;i>=0;i--) { if(s[i]!=b.s[i]) return s[i]>b.s[i]; } return false; } bool operator > (const int &b) { bign c; c.init(b); return *this>c; } //等于号 bool operator == (const bign &b) { if (len!=b.len) return false; for (int i=len-1;i>=0;--i) if (s[i]!=b.s[i]) return false; return true; } bool operator == (const int &b) { bign c; c.init(b); return *this==c; } bool operator != (const bign &b) { return !(*this == b); } bool operator != (const int &b) { bign c; c.init(b); return *this!=c; } bool operator <= (const bign &b) { return *this<b || *this== b; } bool operator <= (const int &b) { bign c; c.init(b); return *this<c || *this==c; } bool operator >= (const bign &b) { return *this>b || *this==b; } bool operator >= (const int &b) { bign c; c.init(b); return *this>c || *this==b; } //转化为字符串 string str() const { string res = ""; for(int i=0;i<len;i++) res=char(s[i]+‘0‘)+res; return res; } //转化为LL LL Integer() const { LL res=0; for (int i=len-1;i>=0;--i) { res=(10*res+s[i]); } return res; } }; istream& operator >> (istream &in, bign &x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator << (ostream &out, const bign &x) { out << x.str(); return out; } //大整数取模 bign Sqrt(bign x) { bign remain=0; bign odd=0; bign ans=0; int group=0,k=0; string s=x.str(); int len=s.length(); if (len%2==1) { group=s[0]-‘0‘; k=-1; } else { group=(s[0]-‘0‘)*10+s[1]-‘0‘; k=0; } for (int j=0;j<(len+1)/2;++j) { if (j!=0) group=((s[2*j+k]-‘0‘)*10+(s[2*j+k+1]-‘0‘)); odd=ans*20+1; remain=remain*100+group; int cnt=0; while (remain>=odd) { ++cnt; remain=remain-odd; odd=odd+2; } ans=ans*10+cnt; } return ans; } int main(void) { #ifdef ex //freopen ("../in.txt","r",stdin); //freopen ("../out.txt","w",stdout); #endif }
标签:
原文地址:http://www.cnblogs.com/123-123/p/5582486.html