zoukankan      html  css  js  c++  java
  • gjd

    #include <cstdio>
    #include <cstring>
    #include <malloc.h>
    #define radix (1u<<28)
    #define drdx  (radix-1)
    #define max_size 100000000u
    typedef unsigned int qt;
    typedef qt ul;
    inline qt max(qt a,qt b){ return ((-((a-b)>>31))&(b-a))+a; }
    inline qt min(qt a,qt b){ return ((-((a-b)>>31))&(a-b))+b; }
    struct bigint{
    	qt *d;
    	qt s,l;
    };
    template<class T> inline void alc ( T* &p, ul l ){
    	p=(T*)malloc( l * sizeof(T) );
    }
    template<class T> inline void palc( T* f, T* &t, ul l, ul a ){
    	if(t) free(t),t=NULL;
    	alc( t, a );
    	memcpy( t, f, sizeof(T) * l );
    	memset( t+l, 0, sizeof(T) * (a-l) );
    }
    template<class T> inline void palc( T* &t,  ul a ){
    	if(t) free(t),t=NULL;
    	alc( t, a );
    	memset( t, 0, sizeof(T) * a );
    }
    inline void larger(bigint& i){
    	qt *p=NULL;
    	palc(i.d,p,i.l,i.s<<1);
    	i.s<<=1;
    	free(i.d);
    	i.d=p;
    }
    inline qt add(bigint& a,bigint& b){//return carry
    	qt l=max(a.l,b.l),car=0;
    	while(a.s<l) larger(a);
    	a.l=l; qt i=0;
    	for(;i<l;++i){
    		if(b.l==i) break;
    		a.d[i]+=b.d[i]+car;
    		car=a.d[i]>>28;
    		a.d[i]&=drdx;
    	}
    	for(;i<l;++i){
    		a.d[i]+=car;
    		car=a.d[i]>>28;
    		a.d[i]&=drdx;
    	}
    	return car;
    }
    inline qt sub(bigint& a,bigint& b){//return borrow
    	qt l=max(a.l,b.l),bor=0;
    	while(a.s<l) larger(a);
    	a.l=l; qt i=0;
    	for(;i<l;++i){
    		if(b.l==i) break;
    		a.d[i]-=b.d[i]+bor;
    		bor=a.d[i]>>31;
    		a.d[i]+=(-bor)&radix;
    	}
    	for(;i<l;++i){
    		a.d[i]-=bor;
    		bor=a.d[i]>>31;
    		a.d[i]+=(-bor)&radix;
    	}
    	if(!bor){
    		while(!a.d[--a.l]);
    		++a.l;
    	}
    	return bor;
    }
    inline qt mul_basecase(bigint& c,bigint& a,bigint& b){//return carry, c=c+a*b
    	qt l=a.l+b.l,car=0;
    	while(c.s<l) larger(c);
    	for(qt i=0;i<a.l;++i) for(qt j=0,p=i;j<b.l;++j,++p) {
    		long long nk=(long long)a.d[i]*b.d[j]+c.d[p];
    		c.d[p]=nk&drdx;
    		c.d[p+1]+=nk>>28;
    		c.d[p+2]+=c.d[p+1]>>28;
    		c.d[p+1]&=drdx;
    	}
    	if(c.d[l-1]) c.l=l; else c.l=l-1;
    	return c.d[l-1]>>28;
    }
    inline qt mul_limb(bigint& a,qt limb){
    	qt car=0;
    	for(int i=0;i<a.l;++i){
    		long long nk=(long long)a.d[i]*limb+car;
    		a.d[i]=nk&drdx;
    		car=nk>>28;
    	}
    	return car;
    }
    inline qt sqr_basecase(bigint& c,bigint& a){//a speed-up of roughly a half
    	qt l=a.l+a.l,car=0;
    	while(c.s<l) larger(c);
    	for(qt i=0;i<a.l;++i){
    		qt p=i<<1;
    		long long nk=(long long)a.d[i]*a.d[i]+c.d[p];
    		c.d[p]=nk&drdx;
    		c.d[p+1]+=nk>>28;
    		c.d[p+2]+=c.d[p+1]>>28;
    		c.d[p+1]&=drdx;
    		++p;
    		for(qt j=i+1;j<a.l;++j,++p) {
    			long long nk=((long long)a.d[i]*(a.d[j])<<1)+c.d[p];
    			c.d[p]=nk&drdx;
    			c.d[p+1]+=nk>>28;
    			c.d[p+2]+=c.d[p+1]>>28;
    			c.d[p+1]&=drdx;
    		}
    	}
    	if(c.d[l-1]) c.l=l; else c.l=l-1;
    	return c.d[l-1]>>28;
    }
    inline qt mul(bigint& c,bigint& a,bigint& b); //automatically select the algorithm used
    inline qt sqr(bigint& c,bigint& a);
    inline qt div(bigint& a,bigint& b,bigint& r);
    inline bigint empty(qt ofsize){
    	bigint vt;
    	vt.l=1; vt.s=ofsize;
    	vt.d=NULL;
    	palc(vt.d,vt.s);
    	return vt;
    }
    inline bigint copy(bigint p){//obtaining a copy from p
    	bigint vt=p;
    	qt* t;
    	palc(vt.d,t,vt.l,vt.s);
    	vt.d=t;
    	return vt;
    }
    inline void pushCarry(bigint& p,qt car){
    	if(p.l==p.s) larger(p);
    	p.d[p.l++]=car;
    }
    inline void getBigInt_tm1(bigint& p){
    	scanf("%u",&p.l);
    	while(p.s<p.l) larger(p);
    	for(qt i=0;i<p.l;++i) scanf("%u",&p.d[i]);
    }
    inline void printBigInt_tm1(bigint& p){
    	printf("Length of BigInt: %u
     Size  of BigInt: %u int(s)
    ",p.l,p.s);
    	printf("     %-10d * 2^%-4d",p.d[0],0);
    	for(qt i=1,v=28;i<p.l;(++i)&3?0:putchar('
    '),v+=28) printf("  +  %-10d * 2^%-4d",p.d[i],v);
    	putchar('
    ');
    }
    inline void makeEmpty(bigint& n){
    	memset(n.d,0,sizeof(qt)*n.l);
    	n.l=1;
    }
    inline void makeIdentity(bigint& n){
    	memset(n.d,0,sizeof(qt)*n.l);
    	n.d[0]=1;
    	n.l=1;
    }
    inline void transfer(bigint& dest,bigint& src){
    	while(dest.s<src.l) larger(dest);
    	memcpy(dest.d,src.d,sizeof(qt)*src.l);
    	dest.l=src.l;
    }
    //fast powering error code: -1: result too large
    inline int fastPower(bigint& n,qt now){
    	qt size=n.l*now;
    	if(size>max_size) return -1;
    	bigint cs[2],cx;
    	qt fx=0;
    	cs[0]=empty(size),cs[1]=empty(size),cx=empty(size);
    	transfer(cs[0],n);
    	for(;now;(now>>=1),fx=1-fx){
    		makeEmpty(cx);
    		if(now&1){
    			mul(cx,cs[fx],n);
    			transfer(n,cx);
    		}
    		makeEmpty(cs[1-fx]);
    		sqr(cs[1-fx],cs[fx]);
    	}
    	return 0;
    }
    inline qt karatsuba_mul(bigint& c,bigint& a,bigint& b);
    inline qt toom_3_mul(bigint& c,bigint& a,bigint& b);
    inline qt toom_4_mul(bigint& c,bigint& a,bigint& b);
    inline qt ss_mul(bigint& c,bigint& a,bigint& b);
    
    ////////////Classical part ends.
    
  • 相关阅读:
    maven完成构建后,eclipse导入运行maven web
    maven构建java项目的过程【完全】
    maven配置【转载】
    iOS - 移动设备防丢失App
    iOS
    iOS
    iOS
    iOS
    iOS
    iOS
  • 原文地址:https://www.cnblogs.com/tmzbot/p/5031217.html
Copyright © 2011-2022 走看看