• sss


    <更新提示>

    <第一次更新>


    <正文>

    高精度计算

    在计数类题目或者有些最优解题目中,需要输出的答案很可能会爆(longlong),这时候就需要用到高精度了。高精度计算较为简单,不再讲解,以下给出辅助常用的高精度计算模板:支持正整数的加,减,乘运算,读入输出,比较,以(1e8)压位计算,结构体封装。

    (Code:)

    #include<bits/stdc++.h>
    using namespace std;
    const int base=1e8,Maxlen=1e4;
    struct bign
    {
    	int d[Maxlen],len;
    	inline void clear(void)
    	{
    		len=0;
    		memset(d,0,sizeof d);
    	}
    	inline void print(void)
    	{
    		printf("%d",d[len]);
    		for(int i=len-1;i>=1;i--)
    			printf("%08d",d[i]);
    	}
    	inline bign read(void)
    	{
    		clear();
    		char s[5000];
    		scanf("%s",s);
    		int last=strlen(s)-1,temp;
    		while(last>=7)
    		{
    			temp=0;
    			for(int i=last-8+1;i<=last;i++)
    				temp=temp*10+s[i]-'0';
    			d[++len]=temp;
    			last-=8;
    		}
    		temp=0;
    		for(int i=0;i<=last;i++)
    			temp=temp*10+s[i]-'0';
    		d[++len]=temp;
    		while(!d[len]&&len>1)len--;
    		return *this;
    	}
    	bign operator = (int a)
    	{
    		clear();
    		do
    		{
    			d[++len]=a%base;
    			a/=base;
    		}
    		while(a);
    		return *this;
    	}
    	bign operator * (bign a)
    	{
    		bign res;
    		res.clear();
    		long long temp;
    		for(int i=1;i<=len;i++)
    		{
    			temp=0;
    			for(int j=1;j<=a.len;j++)
    			{
    				temp+=1LL*d[i]*a.d[j]+res.d[i+j-1];
    				res.d[i+j-1]=temp%base;
    				temp/=base;
    			}
    			if(temp)
    				res.d[i+a.len]=temp;
    		}
    		res.len=len+a.len;
    		while(!res.d[res.len]&&res.len>1)res.len--;
    		return res;
    	}
    	bign operator + (bign a)
    	{
    		for(int i=1;i<=max(a.len,len);i++)
    		{
    			d[i]+=a.d[i];
    			d[i+1]+=d[i]/base;
    			d[i]%=base;
    		}
    		len=max(a.len,len)+5;
    		while(!d[len]&&len>1)len--;
    		return *this;
    	} 
    	bign operator - (bign a)
    	{
    		for(int i=1;i<=len;i++)
    		{
    			d[i]-=a.d[i];
    			if(d[i]<0)d[i+1]--,d[i]+=base;
    		}
    		while(!d[len]&&len>1)len--;
    		return *this;
    	} 
    	bool operator < (const bign a)const
    	{
    		if(a.len^len)
    			return len<a.len;
    		for(int i=len;i>=1;i--)
    			if(d[i]^a.d[i])
    				return d[i]<a.d[i];
    		return false;
    	}
    };
    

    <后记>

  • 相关阅读:
    四川省选2012 day1 喵星球上的点名 (后缀数组,并不是完全的正解)
    6.2.1 最短路
    5.3.3 敌兵布阵
    6.1.1 Constructing Roads
    6.2.4 Arbitrage
    6.1.6 Jungle Roads
    5.3.6 Cow Sorting (HDU 及 POJ)
    6.2.5 Trucking
    6.1.4 还是畅通工程
    6.1.3 畅通工程
  • 原文地址:https://www.cnblogs.com/Parsnip/p/10526364.html
Copyright © 2020-2023  润新知