高精度模板整理
-
超级复杂各种高精度操作大合集(希望整个生涯都不会完整的敲一遍)
#include<iostream> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<math.h> #include<string> #include<iosfwd> #define MAX_L 505 //最大长度,可以修改 using namespace std; class bign { public: int len, s[MAX_L];//数的长度,记录数组 //构造函数 bign(); bign(const char*); bign(int); bool sign;//符号 1正数 0负数 string toStr() const;//转化为字符串,主要是便于输出 friend istream& operator>>(istream &,bign &);//重载输入流 friend ostream& operator<<(ostream &,bign &);//重载输出流 //重载复制 bign operator=(const char*); bign operator=(int); bign operator=(const string); //重载各种比较 bool operator>(const bign &) const; bool operator>=(const bign &) const; bool operator<(const bign &) const; bool operator<=(const bign &) const; bool operator==(const bign &) const; bool operator!=(const bign &) const; //重载四则运算 bign operator+(const bign &) const; bign operator++(); bign operator++(int); bign operator+=(const bign&); bign operator-(const bign &) const; bign operator--(); bign operator--(int); bign operator-=(const bign&); bign operator*(const bign &)const; bign operator*(const int num)const; bign operator*=(const bign&); bign operator/(const bign&)const; bign operator/=(const bign&); //四则运算的衍生运算 bign operator%(const bign&)const;//取模(余数) bign factorial()const;//阶乘 bign Sqrt(const bign& num)const;//整数开根(向下取整) bign pow(const bign&)const;//次方 //一些乱乱的函数 void clean(); ~bign(); }; #define max(a,b) a>b ? a : b #define min(a,b) a<b ? a : b bign::bign() { memset(s, 0, sizeof(s)); len = 1; sign = 1; } bign::bign(const char *num) { *this = num; } bign::bign(int num) { *this = num; } string bign::toStr() const { string res; res = ""; int j; for(j=len-1;s[j]==0 && j>=0;j--); for (int i = 0; i <= j; i++) res = (char)(s[i] + '0') + res; if (res == "") res = "0"; if (!sign&&res != "0") res = "-" + res; return res; } istream &operator>>(istream &in, bign &num) { string str; in>>str; num=str; return in; } ostream &operator<<(ostream &out, bign &num) { out<<num.toStr(); return out; } bign bign::operator=(const char *num) { memset(s, 0, sizeof(s)); char a[MAX_L] = ""; if (num[0] != '-') strcpy(a, num); else { int len=strlen(num); for (int i = 1; i < len; i++) a[i - 1] = num[i]; } sign = !(num[0] == '-'); len = strlen(a); for (int i = 0; i < len; i++) s[i] = a[len - i - 1] - 48; return *this; } bign bign::operator=(int num) { if (num < 0) sign = 0, num = -num; else sign = 1; char temp[MAX_L]; sprintf(temp, "%d", num); *this = temp; return *this; } bign bign::operator=(const string num) { const char *tmp; tmp = num.c_str(); *this = tmp; return *this; } bool bign::operator<(const bign &num) const { if (sign^num.sign) return num.sign; if (len != num.len) return len < num.len; for (int i = len - 1; i >= 0; i--) if (s[i] != num.s[i]) return sign ? (s[i] < num.s[i]) : (!(s[i] < num.s[i])); return !sign; } bool bign::operator>(const bign&num)const { return num < *this; } bool bign::operator<=(const bign&num)const { return !(*this>num); } bool bign::operator>=(const bign&num)const { return !(*this<num); } bool bign::operator!=(const bign&num)const { return *this > num || *this < num; } bool bign::operator==(const bign&num)const { return !(num != *this); } //luogu openjudge 验证 bign bign::operator+(const bign &num) const { if (sign^num.sign) { bign tmp = sign ? num : *this; tmp.sign = 1; return sign ? *this - tmp : num - tmp; } bign result; result.len = 0; int temp = 0; for (int i = 0; temp || i < (max(len, num.len)); i++) { int t = s[i] + num.s[i] + temp; result.s[result.len++] = t % 10; temp = t / 10; } result.sign = sign; return result; } bign bign::operator++() { *this = *this + 1; return *this; } bign bign::operator++(int) { bign old = *this; ++(*this); return old; } bign bign::operator+=(const bign &num) { *this = *this + num; return *this; } //luogu openjudge 验证 bign bign::operator-(const bign &num) const { bign b=num,a=*this; if (!num.sign && !sign) { b.sign=1; a.sign=1; return b-a; } if (!b.sign) { b.sign=1; return a+b; } if (!a.sign) { a.sign=1; b=bign(0)-(a+b); return b; } if (a<b) { bign c=(b-a); c.sign=false; return c; } bign result; result.len = 0; for (int i = 0, g = 0; i < a.len; i++) { int x = a.s[i] - g; if (i < b.len) x -= b.s[i]; if (x >= 0) g = 0; else { g = 1; x += 10; } result.s[result.len++] = x; } result.clean(); return result; } bign bign::operator * (const bign &num)const { bign result; result.len = len + num.len; for (int i = 0; i < len; i++) for (int j = 0; j < num.len; j++) result.s[i + j] += s[i] * num.s[j]; for (int i = 0; i < result.len; i++) { result.s[i + 1] += result.s[i] / 10; result.s[i] %= 10; } result.clean(); result.sign = !(sign^num.sign); return result; } bign bign::operator*(const int num)const { bign x = num; bign z = *this; return x*z; } bign bign::operator*=(const bign&num) { *this = *this * num; return *this; } bign bign::operator /(const bign&num)const { bign ans; ans.len = len - num.len + 1; if (ans.len < 0) { ans.len = 1; return ans; } bign divisor = *this, divid = num; divisor.sign = divid.sign = 1; int k = ans.len - 1; int j = len - 1; while (k >= 0) { while (divisor.s[j] == 0) j--; if (k > j) k = j; char z[MAX_L]; memset(z, 0, sizeof(z)); for (int i = j; i >= k; i--) z[j - i] = divisor.s[i] + '0'; bign dividend = z; if (dividend < divid) { k--; continue; } int key = 0; while (divid*key <= dividend) key++; key--; ans.s[k] = key; bign temp = divid*key; for (int i = 0; i < k; i++) temp = temp * 10; divisor = divisor - temp; k--; } ans.clean(); ans.sign = !(sign^num.sign); return ans; } bign bign::operator/=(const bign&num) { *this = *this / num; return *this; } bign bign::operator%(const bign& num)const { bign a = *this, b = num; a.sign = b.sign = 1; bign result, temp = a / b*b; result = a - temp; result.sign = sign; return result; } bign bign::pow(const bign& num)const { bign result = 1; for (bign i = 0; i < num; i++) result = result*(*this); return result; } bign bign::factorial()const { bign result = 1; for (bign i = 1; i <= *this; i++) result *= i; return result; } void bign::clean() { if (len == 0) len++; while (len > 1 && s[len - 1] == '\0') len--; } //luogu验证,应该差不多,只是乘法太慢t了。 bign bign::Sqrt(const bign& num)const { if(*this<0)return -1; if(*this<=1)return *this; bign l=0,r=*this,mid; while(r-l>1) { mid=(l+r)/2; if(num*mid.len-num+1>(*this).len || mid.pow(num)>*this) r=mid; else l=mid; } return l; } bign::~bign() { } int main() { }
-
只可以进行正整数操作的高精度模板,实测速度一般
#include<iostream> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<math.h> #include<string> #include<iosfwd> #define ll short using namespace std; const ll N=221; struct bignum { ll len,num[N]; bignum() { len=0; memset(num,0,sizeof(num)); } } one,zero,ans; inline bignum read() { bignum ans; string s; cin>>s; int len=s.size(); for(ll i=0; i<len; i++) ans.num[s.size()-i]=s[i]-48; ans.len=s.size(); return ans; } inline void write(bignum s) { for(ll i=s.len; i>=1; i--) putchar(s.num[i]+48); } inline void operator ==(bignum &a,ll b) { for(; b; b/=10) a.num[++a.len]=b%10; } inline bool operator <=(bignum a,bignum b) { if(a.len>b.len) return 0; if(a.len<b.len) return 1; for(ll i=a.len; i>=1; i--) { if(a.num[i]>b.num[i]) return 0; if(a.num[i]<b.num[i]) return 1; } return 1; } inline bool operator >(bignum a,bignum b) { if(a.len>b.len) return 1; if(a.len<b.len) return 0; for(ll i=a.len; i>=1; i--) { if(a.num[i]>b.num[i]) return 1; if(a.num[i]<b.num[i]) return 0; } return 0; } inline bignum operator +(bignum a,bignum b) { bignum c; c.len=max(a.len,b.len); for(ll i=1; i<=c.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[c.len+1]) c.len++; return c; } inline bignum operator -(bignum a,bignum b) { bignum c; c.len=a.len; for(ll i=1; i<=c.len; i++) { c.num[i]=a.num[i]-b.num[i]; if(c.num[i]<0) { c.num[i]+=10; a.num[i+1]--; } } while(c.len>1&&c.num[c.len]==0) c.len--; return c; } inline bignum operator *(bignum a,bignum b) { bignum c; for(ll i=1; i<=a.len; i++) { for(ll 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; } } c.len=a.len+b.len; while(c.len>1&&c.num[c.len]==0) c.len--; return c; } inline bignum operator /(bignum a,ll b) { ll sum=0; bignum c; for(ll i=a.len; i>=1; i--) { sum=sum*10+a.num[i]; c.num[i]=sum/b; sum%=b; } c.len=a.len; while(c.len>1&&c.num[c.len]==0) c.len--; return c; } int main() { }
-
只可以进行正整数操作的高精度模板, f f t fft fft使用高精度乘法,实测速度巨慢,比2还慢。
//测试之后发现跑得巨慢... #include<iostream> #include<stdlib.h> #include<stdio.h> #include<string.h> #include<math.h> #include<string> #include<iosfwd> #define ll long long using namespace std; const ll N=20010; const double pi=acos(-1); ll m; struct bignum { ll len,num[N]; bignum() { len=0; memset(num,0,sizeof(num)); } } n,one,zero,ans,c; inline void clear(bignum &a) { memset(a.num,0,sizeof(a.num)); a.len=0; } inline bignum read() { bignum ans; string s; cin>>s; for(ll i=0; i<s.size(); i++) ans.num[s.size()-i]=s[i]-48; ans.len=s.size(); return ans; } inline void write(bignum s) { for(ll i=s.len; i>=1; i--) cout<<s.num[i]; } inline void operator ==(bignum &a,ll b) { for(; b; b/=10) a.num[++a.len]=b%10; } inline bool operator <=(bignum a,bignum b) { if(a.len>b.len) return 0; if(a.len<b.len) return 1; for(ll i=a.len; i>=1; i--) { if(a.num[i]>b.num[i]) return 0; if(a.num[i]<b.num[i]) return 1; } return 1; } inline bool operator >(bignum a,bignum b) { if(a.len>b.len) return 1; if(a.len<b.len) return 0; for(ll i=a.len; i>=1; i--) { if(a.num[i]>b.num[i]) return 1; if(a.num[i]<b.num[i]) return 0; } return 0; } inline bignum operator +(bignum a,bignum b) { clear(c); c.len=max(a.len,b.len); for(ll i=1; i<=c.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[c.len+1]) c.len++; return c; } inline bignum operator -(bignum a,bignum b) { clear(c); c.len=a.len; for(ll i=1; i<=c.len; i++) { c.num[i]=a.num[i]-b.num[i]; if(c.num[i]<0) { c.num[i]+=10; a.num[i+1]--; } } while(c.len>1&&c.num[c.len]==0) c.len--; return c; } void FFT(complex<double> *a,ll n,ll op) { if(!n) return; complex<double> a0[n],a1[n]; for(ll i=0; i<n; i++) { a0[i]=a[i<<1]; a1[i]=a[i<<1|1]; } FFT(a0,n>>1,op); FFT(a1,n>>1,op); complex<double> W(cos(pi/n),sin(pi/n)*op),w(1,0); for(ll i=0; i<n; i++,w*=W) { a[i]=a0[i]+w*a1[i]; a[i+n]=a0[i]-w*a1[i]; } } complex<double> x[N],y[N]; inline bignum operator *(bignum a,bignum b) { memset(x,0,sizeof(x)); memset(y,0,sizeof(y)); ll n=a.len-1,m=b.len-1; for(ll i=0; i<=n; i++) x[i]=a.num[i+1]; for(ll i=0; i<=m; i++) y[i]=b.num[i+1]; for(m+=n,n=1; n<=m; n<<=1); FFT(x,n>>1,1); FFT(y,n>>1,1); for(ll i=0; i<n; i++) x[i]*=y[i]; FFT(x,n>>1,-1); bignum c; for(ll i=0; i<=m; i++) c.num[i+1]=(ll)(fabs(x[i].real()/n+0.5)); c.len=m+1; while(c.len>1&&c.num[c.len]==0) c.len--; for(ll i=1; i<=c.len; i++) { c.num[i+1]+=c.num[i]/10; c.num[i]%=10; } while(c.num[c.len+1]) { c.len++; c.num[c.len+1]+=c.num[c.len]/10; c.num[c.len]%=10; } return c; } inline bignum operator /(bignum a,ll b) { ll sum=0; clear(c); for(ll i=a.len; i>=1; i--) { sum=sum*10+a.num[i]; c.num[i]=sum/b; sum%=b; } c.len=a.len; while(c.len>1&&c.num[c.len]==0) c.len--; return c; } inline bool check(bignum a,ll b) { bignum ans=one; for(ll i=1; i<=b; i++) { ans=ans*a; if(ans>n) return 0; } return 1; } int main() { cin>>m; n=read(); one==1; zero==0; bignum l=one,r=n; while(l<=r) { bignum mid=(l+r)/2; if(check(mid,m)) { ans=mid; l=mid+one; } else r=mid-one; } write(ans); return 0; }
-
跑得超级快的压位高精
#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<complex>
#include<string.h>
#include<math.h>
#include<string>
#include<iosfwd>
#define ll long long
using namespace std;
const ll N=30;
const double pi=acos(-1);
const ll mod=100000000,length=log(mod)/log(10);
struct bignum
{
ll len,num[N];
bignum()
{
len=0;
memset(num,0,sizeof(num));
}
} one,zero,ans,QAQ;
inline bignum read()
{
bignum ans;
string s;
cin>>s;
for(ll r=s.size()-1; r>=0; r-=length)
{
ans.len++;
ll l;
if(r>=length-1) l=r-length+1;
else l=0;
for(ll i=l; i<=r; i++) ans.num[ans.len]=ans.num[ans.len]*10+s[i]-48;
}
return ans;
}
inline void write(bignum s)
{
printf("%lld",s.num[s.len]);
for(ll i=s.len-1; i>=1; i--)
{
for(ll j=mod/10; j>s.num[i]; j/=10) putchar('0');
if(s.num[i]) printf("%lld",s.num[i]);
}
}
inline void operator ==(bignum &a,ll b)
{
for(; b; b/=mod) a.num[++a.len]=b%mod;
}
inline bool operator <=(bignum a,bignum b)
{
if(a.len>b.len) return 0;
if(a.len<b.len) return 1;
for(ll i=a.len; i>=1; i--)
{
if(a.num[i]>b.num[i]) return 0;
if(a.num[i]<b.num[i]) return 1;
}
return 1;
}
inline bool operator >(bignum a,bignum b)
{
if(a.len>b.len) return 1;
if(a.len<b.len) return 0;
for(ll i=a.len; i>=1; i--)
{
if(a.num[i]>b.num[i]) return 1;
if(a.num[i]<b.num[i]) return 0;
}
return 0;
}
inline bignum operator +(bignum a,bignum b)
{
bignum c;
c.len=max(a.len,b.len);
for(ll i=1; i<=c.len; i++)
{
c.num[i]+=a.num[i]+b.num[i];
c.num[i+1]+=c.num[i]/mod;
c.num[i]%=mod;
}
if(c.num[c.len+1]) c.len++;
return c;
}
inline bignum operator -(bignum a,bignum b)
{
bignum c;
c.len=a.len;
for(ll i=1; i<=c.len; i++)
{
c.num[i]=a.num[i]-b.num[i];
if(c.num[i]<0)
{
c.num[i]+=mod;
a.num[i+1]--;
}
}
while(c.len>1&&c.num[c.len]==0) c.len--;
return c;
}
inline bignum operator *(bignum a,bignum b)
{
bignum c;
for(ll i=1; i<=a.len; i++)
{
for(ll 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]/mod;
c.num[i+j-1]%=mod;
}
}
c.len=a.len+b.len;
while(c.len>1&&c.num[c.len]==0) c.len--;
return c;
}
inline bignum operator /(bignum a,ll b)
{
ll sum=0;
bignum c;
for(ll i=a.len; i>=1; i--)
{
sum=sum*mod+a.num[i];
c.num[i]=sum/b;
sum%=b;
}
c.len=a.len;
while(c.len>1&&c.num[c.len]==0) c.len--;
return c;
}
int main()
{
}
模板均来自网络,并略作修改。
模板1 来自 c s d n csdn csdn 用户 H a r r y G u o 2012 HarryGuo2012 HarryGuo2012的博文 https://blog.youkuaiyun.com/harryguo2012/article/details/42036829
模板2、3、4来自 l u o g u luogu luogu 用户 x u k u a n xukuan xukuan的题解 https://www.luogu.com.cn/problemnew/solution/P2293?page=2