1. 程式人生 > >大整數模板(+ - * / == <)

大整數模板(+ - * / == <)

技術 one sta max tip return img emp closed

技術分享圖片
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include <vector>
#include <string>
#include <iostream>

using namespace std;
typedef long long ll;

const int ten[4] = {1,10,100,1000};
const int maxl = 1010;
struct BigNumber
{
    
int d[maxl]; BigNumber(string s) { int len = s.size(); d[0] = (len-1)/4+1; int i,j,k; for(i=1;i<maxl;i++) { d[i] = 0; } for(i=len-1;i>=0;--i) { j=(len-i-1)/4+1; k=(len-i-1)%4; d[j]
+=ten[k]*(s[i]-0); } while(d[0]>1 && d[d[0]]==0) --d[0]; } BigNumber() { *this=BigNumber(string("0")); } string toString() { string s(""); int i,j,temp; for(i=3;i>=1;--i) if(d[d[0]]>=ten[i]) break; temp = d[d[0
]]; for(j=i;j>=0;--j) { s = s+(char)(temp/ten[j]+0); temp %=ten[j]; } for(i=d[0]-1;i>0;--i) { temp = d[i]; for(j=3;j>=0;--j) { s = s+(char)(temp/ten[j]+0); temp %= ten[j]; } } return s; } }zero("0"),d,temp,mid1[15]; bool operator <(const BigNumber &a, const BigNumber &b) { if(a.d[0]!=b.d[0]) return a.d[0]<b.d[0]; int i; for(i = a.d[0];i>0;--i) if(a.d[i]!=b.d[i]) return a.d[i]<b.d[i]; return false; } BigNumber operator +(const BigNumber &a,const BigNumber &b) { BigNumber c; c.d[0] = max(a.d[0],b.d[0]); int i,x=0; for(i=1;i<=c.d[0];++i) { x = a.d[i]+b.d[i]+x; c.d[i] = x%10000; x/=10000; } while(x!=0) { c.d[++c.d[0]]=x%10000; x/=10000; } return c; } BigNumber operator -(const BigNumber &a, const BigNumber &b) { BigNumber c; c.d[0] = a.d[0]; int i,x=0; for(i=1;i<=c.d[0];++i) { x = 10000+a.d[i]-b.d[i]+x; c.d[i] = x%10000; x=x/10000-1; } while((c.d[0]>1)&&(c.d[c.d[0]]==0)) --c.d[0]; return c; } BigNumber operator *(const BigNumber &a,const BigNumber &b) { BigNumber c; c.d[0] = a.d[0]+b.d[0]; int i,j,x; for(int i=1;i<=a.d[0];++i) { x = 0; for(j=1;j<=b.d[0];++j) { x = a.d[i]*b.d[j]+x+c.d[i+j-1]; c.d[i+j-1] = x%10000; x/=10000; } c.d[i+b.d[0]] = x; } while((c.d[0]>1)&&(c.d[c.d[0]]==0)) --c.d[0]; return c; } bool smaller(const BigNumber &a,const BigNumber &b,int delta) { if(a.d[0]+delta!=b.d[0]) return a.d[0]+delta<b.d[0]; int i; for(i=a.d[0];i>0;--i) if(a.d[i]!=b.d[i+delta]) return a.d[i]<b.d[i+delta]; return true; } void Minus(BigNumber &a,const BigNumber &b,int delta) { int i,x=0; for(i=1;i<=a.d[0]-delta;++i) { x = 10000+a.d[i+delta]-b.d[i]+x; a.d[i+delta] = x%10000; x = x/10000-1; } while((a.d[0]>1)&&(a.d[a.d[0]]==0)) --a.d[0]; } BigNumber operator *(const BigNumber &a,const int &k) { BigNumber c; c.d[0] = a.d[0]; int i,x=0; for(i=1;i<=a.d[0];++i) { x = a.d[i]*k+x; c.d[i] = x%10000; x/=10000; } while(x>0) { c.d[++c.d[0]] = x%10000; x/=10000; } while((c.d[0]>1)&&(c.d[c.d[0]]==0)) --c.d[0]; return c; } BigNumber operator /(const BigNumber &a,const BigNumber &b) { BigNumber c; d = a; int i,j,temp; mid1[0] = b; for(i=1;i<=13;i++) { mid1[i] = mid1[i-1]*2; } for(i=a.d[0]-b.d[0];i>=0;--i) { temp = 8192; for(j=13;j>=0;--j) { if(smaller(mid1[j],d,i)) { Minus(d,mid1[j],i); c.d[i+1]+=temp; } temp /=2; } } c.d[0] = max(1,a.d[0]-b.d[0]+1); while((c.d[0]>1)&&(c.d[c.d[0]]==0)) --c.d[0]; return c; } bool operator ==(const BigNumber &a,const BigNumber &b) { int i; if(a.d[0]!=b.d[0]) return false; for(int i=1;i<=a.d[0];++i) if(a.d[i]!=b.d[i]) return false; return true; } string m; int main() { cin>>m; BigNumber M = BigNumber(m); BigNumber n1 = BigNumber("0"); BigNumber n2 = BigNumber("2"); BigNumber ans1,ans2; if(M==n1) ans1 = n1; else { BigNumber tp; while(n2<M) { tp = n2; n2 = n2*6-n1; n1 = tp; } ans1 = n2; } BigNumber nn1 = BigNumber("0"); BigNumber nn2 = BigNumber("6"); if(M==nn1) ans2 = nn1; else { BigNumber tp; while(nn2<M) { tp = nn2; nn2 = nn2*14-nn1; nn1 = tp; } ans2 = nn2; } string ans; if(ans1<ans2) ans = ans1.toString(); else ans = ans2.toString(); cout<<ans<<endl; }
View Code

java 對應使用:

技術分享圖片
import java.math.BigInteger;
import java.util.Scanner;

public class Main {

    static Scanner in = new Scanner(System.in);
    public static void main(String[] args) {
        BigInteger m = in.nextBigInteger();
        BigInteger n0 = BigInteger.ZERO;
        BigInteger n1 = new BigInteger("2");
        while(n1.compareTo(m)<0){
            BigInteger n2 = new BigInteger("6").multiply(n1).subtract(n0);
            n0 = n1;
            n1 = n2;
        }
        BigInteger nn0 = BigInteger.ZERO;
        BigInteger nn1 = new BigInteger("6" );
        while(nn1.compareTo(m)<0){
            BigInteger nn2 = new BigInteger("14").multiply(nn1).subtract(nn0);
            nn0 = nn1;
            nn1 = nn2;
        }
        if(n1.compareTo(nn1)<0) System.out.println(n1);
        else System.out.println(nn1);
    }
}
View Code

大整數模板(+ - * / == <)