#include #include#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using
namespace
std;
const
int maxl = 500
;
struct
bignum
;
//高精度比較 a > b return 1, a == b return 0; a < b return -1;
int comp(bignum &a, bignum &b)
//高精度加法
bignum add(bignum &a, bignum &b)
}
if(c.num[len])
len++;
c.len =len;
return
c;
}
//高精度減法,保證a >= b
bignum sub(bignum &a, bignum &b)
}
while(c.num[len] == 0 && len > 1
) len--;
c.len =len;
return
c;
}
//高精度乘以低精度,當b很大時可能會發生溢位int範圍,具體情況具體分析
//如果b很大可以考慮把b看成高精度
bignum mul1(bignum &a, int &b)
for(i = 0; i < len; i++)
}
while(c.num[len] > 0
)
c.len =len;
return
c;
}
//高精度乘以高精度,注意要及時進製,否則肯能會引起溢位,但這樣會增加演算法的複雜度,
//如果確定不會發生溢位, 可以將裡面的while改成if
bignum mul2(bignum &a, bignum &b)
} }
len = a.len+b.len-1
;
while(c.num[len-1] == 0 && len > 1
) len--;
if(c.num[len])
len++;
c.len =len;
return
c;
}
//高精度除以低精度,除的結果為c, 餘數為f
void div1(bignum &a, int &b, bignum &c, int &f)
while(len > 1 && c.num[len-1] == 0
) len--;
c.len =len;
}
//高精度*10
void mul10(bignum &a)
//高精度除以高精度,除的結果為c,餘數為f
void div2(bignum &a, bignum &b, bignum &c, bignum &f)
}
while(len > 1 && c.num[len-1] == 0
) len--;
c.len =len;
}
void print(bignum &a) //
輸出大數
//將字串轉為大數存在bignum結構體裡面
bignum tonum(char *s)
void init(bignum &a, char *s, int &tag) //
將字串轉化為大數
a.len =j;
for(; s[i] != '
\0'; i++, j--)
a.num[j-1] = s[i]-'0'
; }
int main(void
)
else
}
return
0;
}
高精度板子
include include include include include include include include include include include include include includeusing namespace std const int max 10001...
高精度板子
乘法學會fft以後,應該乘除都能優化很多。include include include include using namespace std int mi 28 const int maxlen 2e5 16,maxbit 1e8 int max int a,int b void plus1 並...
高精度板子
本章即將介紹的是一章高精度的板子,並沒有深奧的道理,大函式大 inline 者,暫且靠邊站,筆者沒有那麼高水平,滿足不了這些需求。本文如有理念上與邏輯上的錯誤,請多多包涵並且指出。什麼是高精度?高精度,雖然聽起來感覺很生熟,很深奧,但是,原理不過是小學二三年級時的加減法豎式和四五年級的乘法算式罷了,...