大数板子
大数比较
int compare(const char *a,int len1,const char *b,int len2){//正常的比较顺序
//0表示等于,1大于,-1小于
if(len1>len2)return 1;
else if(len1 < len2)return -1;
else{
for(int i = 0;i <= len1 - 1;i++){
if(a[i]>b[i])return 1;
else if(a[i]<b[i])return -1;
}
}
return 0;
}
大数加法(大数+大数)
复 杂 度 O ( n ) 复杂度O(n) 复杂度O(n)
void add(const char *s1,int len1,const char *s2,int len2,char *s){//只支持正数加减
//传入的参数不变
int i = len1 - 1,j = len2 - 1,k = 0;
int jin = 0;
while(i >= 0 && j >= 0){
int tp = s1[i] - '0' + s2[j] - '0' + jin;
jin = tp/10 ;
s[k++] = tp%10 + '0';
i--,j--;
}
while(i>=0){
int tp = s1[i] - '0' + jin;
jin = tp/10;
tp = tp%10;
s[k++] = tp + '0';
i--;
}
while(j>=0){
int tp = s2[j] - '0' + jin;
jin = tp/10;
tp = tp%10;
s[k++] = tp + '0';
j--;
}
if(jin)s[k++] = jin + '0';
for(int j = 0;j < k/2;j++)swap(s[j],s[k-j-1]);
s[k] = '\0';
}
大数减法(大数-大数)
复 杂 度 O ( n ) 复杂度O(n) 复杂度O(n)
void sub(const char *a,int len1,const char *b,int len2,char *c){//只支持正数减,并且a要大于b
//传入参数不变
int i = len1 - 1,j = len2 - 1;
int jie = 0;
char tpa,tpb;
while(j>= 0){
tpa = a[i] + jie;
tpb = b[j];
if(tpa>=tpb){
c[i] = tpa - tpb + '0';
jie = 0;
}else{
c[i] = tpa + 10 - tpb + '0';
jie = -1;
}
i--;j--;
}
while(i >= 0){
tpa = a[i] + jie;
if(tpa>='0'){
c[i] = tpa ;
jie = 0;
}else{
c[i] = tpa + 10;
jie = -1;
}
i--;
}
j = 0;
for(int i = 0;i < len1;i++){//去前导0
if(c[i]!='0'||j){
c[j++] = c[i];
}
}
if(!j){
c[0] = '0';c[1] = '\0';
}else{
c[j] = '\0';
}
}
大数乘法(大数*大数)
复 杂 度 O ( n ∗ m ) 复杂度O(n*m) 复杂度O(n∗m)
void mul(const char *a,int len1,const char *b,int len2,char *c){//只支持正数相乘
int res[len1+len2] = {0};//比较大的时候可以放外面
//必须int,char存的可能会爆(一字节)
for(int i = 0;i < len1;i++){
for(int j = 0;j < len2;j++){
res[i+j+1] += (a[i]-'0')*(b[j]-'0');
}
}
for(int i = len1 + len2 - 1;i > 0;i--){
res[i-1] += (res[i] >= 10 ? res[i]/10 : 0);
res[i] = (res[i] >= 10 ? res[i]%10 : res[i]);
}
int cnt = 0;
for(int i = 0;i < len1 + len2;i++){
if(res[i]==0&&!cnt)continue;
c[cnt++] = res[i] + '0';
}
if(!cnt)c[cnt++] = '0';//答案为0,得处理一下
c[cnt] = '\0';
}
大数除法(大数div大数)
复 杂 度 O ( n 2 ) ( 估 计 ) 复杂度O(n^2)(估计) 复杂度O(n2)(估计)
int del(char *a,int len1,const char *b,int len2,char *c){//除法中的减
int clen = len1-len2; //商加的位置
for(int i = 1;i <= len2;i++){//判断当前位置是否能够取
if(a[len1-i]>b[len2-i])break;
else if(a[len1-i]<b[len2-i]){clen--;break;}
}
if(clen<0)return len1;
int i = clen,j = 0;
while(i <len1&&j < len2){//进行减
while(a[i]<b[j]){
a[i] += 10;
a[i+1]--;
}
a[i] = a[i] - b[j] + '0';
i++,j++;
}
c[clen]++;
while(a[len1-1]=='0')len1--;
a[len1] = '\0';
if(len1<=0)len1 = 1;
//cout << a <<endl;
return len1;
}
int div_compare(char *a,int len1,char *b,int len2){//对于除法的compare
if(len1>len2)return 1;
else if(len1 < len2)return -1;
else{
for(int i = len1-1;i >= 0;i--){//因为之前倒过来了,所以现在倒着比较
if(a[i]>b[i])return 1;
else if(a[i]<b[i])return -1;
}
}
return 0;
}
void div(char *a,int len1,char *b,int len2,char *c){//a与b最后都为逆序,如果需要则倒置回去
for(int i = 0;i < len1/2 ;i++)swap(a[i],a[len1-1-i]);
for(int i = 0;i < len2/2 ;i++)swap(b[i],b[len2-1-i]);
int clen = len1 - len2 + 1;//商的最大长度
while(div_compare(a,len1,b,len2)>=0){
len1 = del(a,len1,b,len2,c);
}
while(clen&&c[clen-1]==0)clen--;//去0
if(!clen)c[clen++] = 0;
for(int i = 0;i < clen;i++)c[i] += '0';//转换为字符串
for(int i = 0;i < clen/2;i++)swap(c[i],c[clen-i-1]);//逆序
//如果要求余数,对a取一下前导0,倒置过来.
c[clen] = '\0';
}
大数除long long
复 杂 度 O ( n ) 复杂度O(n) 复杂度O(n)
void div(const char *a,int len1,ll b,char *c){//大数除ll
ll ans = 0;
bool f = 0;
int cnt = 0;
for(int i = 0;i < len1;i++){
ans = (ans*10 + a[i] - '0');
if(ans >= b){
f = 1;
c[cnt++] = ans/b + '0';
ans %= b;
}else if(f)c[cnt++] = '0';
}
if(!cnt)c[cnt++] = '0';
c[cnt] = '\0';
}
大数乘long long
复 杂 度 O ( n ) 复杂度O(n) 复杂度O(n)
void mul(const char *a,int len1,ll b,char *c){//大数乘ll
int cnt = 0;
ll ans = 0;
for(int i = len1 - 1;i >= 0;i--){
ans = (a[i]-'0')*b + ans;
c[cnt++] = ans % 10 + '0';
ans /= 10;
}
while(ans){
c[cnt++] = ans%10 + '0';
ans /= 10;
}
for(int i = 0;i < cnt/2;i++)swap(c[i],c[cnt-1-i]);
c[cnt] = '\0';
}
大数对long long 取模
复 杂 度 O ( n ) 复杂度O(n) 复杂度O(n)
void Mod(const char *a,int len1,ll p,ll &ans){//大数模ll
for(int i = 0;i < len1;i++){
ans = ans*10 + (a[i] - '0');
ans = (ans >= p ? ans % p : ans);
}
}