大整數類模板

2022-07-27 15:18:28 字數 3086 閱讀 7977

為了方便寫,加了個符號變數neg...過載了各種比較符號,支援加減乘除取模以及模快速冪...快速冪不知道怎麼用位運算,效率會偏低..

1 #include2 #include3 #include

4 #include5 #include6

using

namespace

std;

7const

int base=100000000,width=8

;8 typedef long

long

ll;9

struct

bigint

19int x,len=(str.length()-1)/width+1;20

for(int i=0;i)

26return *this;27

}28 bigint operator =(int

n)else

37return *this;38

}39void

print()

45 cout<47};

48bool

operator ==(const bigint &a,const bigint &b)

55return

false;56

}57bool

operator

<(bigint &a,bigint &b)

66if(a.s.size()return

true;67

else

if(a.s.size()>b.s.size())return

false;68

for(int i=a.s.size()-1;i>=0;i--)

73return

false;74

}75bool

operator >(bigint &a,bigint &b)

76bool

operator

<=(bigint &a,bigint &b)

77bool

operator >=(bigint &a,bigint &b)

78bool

operator !=(bigint &a,bigint &b)

79 bigint operator -(bigint &a,bigint &b);

80 bigint operator +(bigint &a,bigint &b)

88if(!a.neg&&b.neg)

94 ans.neg=a.neg;

95int k=0,i=0;96

while(ib.s.size())

103if

(k)ans.s.push_back(k);

104return

ans;

105}

106 bigint operator -(bigint &a,bigint &b)

114if(a.neg&&!b.neg)

121if(a.neg&&b.neg&&a>b)

127if(!a.neg&&!b.neg&&a132 ans.neg=false

;133

int k=0,i=0

;134

while(ib.s.size())else

144 i++;

145}

146while(ans.s.size()>1&&i>=0&&ans.s[--i]==0

)ans.s.pop_back();

147return

ans;

148}

149 bigint operator *(bigint &a,bigint &b)

157 ans.s.push_back(k%base);

158 k/=base;

159}

160if

(k)ans.s.push_back(k);

161 ans.neg=a.neg^b.neg;

162return

ans;

163}

164 bigint operator *(bigint &a,int

b)172

if(k)ans.s.push_back(k);

173if((b<0&&a.neg)||(b>0&&!a.neg))ans.neg=false

;174

else ans.neg=true

;175

return

ans;

176}

177 inline void devide_2(bigint &a)

187if(a.s.back()==0

)a.s.pop_back();

188}

189 inline bigint devide(bigint &a,bigint &b,bigint &r)

196while(r>l)

205 m=t*b;

206//

m.print();

207if(m==a)else

if(m214}

215 m=t*b;

216 r=a-m;

217return

l;218

}219 inline bigint operator%(bigint &a,bigint &b)

224 inline bool is_even(const bigint &a)

228//

計算n的k次方模p

229 bigint qpow(bigint &n,bigint &k,bigint &p)

241 y=y*y;

242//

y.print();

243 y=y%p;

244//

y.print();

245//

cout<<"---------\n";

246devide_2(_n);

247//

_n.print();

248}

249return

ans;

250 }

大整數類 模板

bigint v2.0 by kunsoft include include include include include include include include include using namespace std typedef long long llt class bigint ...

大整數類 模板

bigint v2.0 by kunsoft include include include include include include include include include using namespace std typedef long long llt class bigint ...

C 大整數類

分享一下我老師大神的人工智慧教程!零基礎,通俗易懂!include include include include using namespace std define maxn 9999 define maxsize 10 define dlen 4 class bignum 建構函式 bignu...