题太垃圾了,就不放了,思路也不放了,权当发一个高精度的板子。。

高精乘法和加法。 支持多种格式读入,快速输出。

#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<string>
#include<iterator>
#include<utility>
using namespace std;

struct BNUM
{
	short mainstr[1010],len;
	
	BNUM() { memset(mainstr,0,sizeof(mainstr)); len=0; }
	
	BNUM operator * (BNUM& b)
	{
		BNUM c;
		for (int i=1000;i>=1000-b.len+1;i--)
			for (int j=1000;j>=1000-this->len+1;j--)
			{
				c.mainstr[j+i-1000]+=this->mainstr[j]*b.mainstr[i];
				if (c.mainstr[j+i-1000]>=10)
				{
					c.mainstr[j+i-1001]+=c.mainstr[j+i-1000]/10;
					c.mainstr[j+i-1000]%=10;
				}
			}
		c.len=this->len+b.len;
		if (c.mainstr[1001-c.len]==0) c.len--;
		return c;
	}
	
	BNUM operator + (BNUM& b)
    {
        BNUM c;
        c.len=max(this->len,b.len);
        for (int i=1000;i>1000-c.len;i--)
        {
            c.mainstr[i]+=this->mainstr[i]+b.mainstr[i];
            if (c.mainstr[i]>=10) { c.mainstr[i-1]+=1; c.mainstr[i]-=10; }
        }
        if (c.mainstr[1000-c.len]==1) c.len++;
        return c;
    }
	
	BNUM operator = (BNUM b)
	{
		this->len=b.len;
		memcpy(this->mainstr,b.mainstr,sizeof(this->mainstr));
		return *this;
	}
	
	void readnum(long long qaq)
	{
		while (qaq>0)
		{
			this->mainstr[1000-(this->len)]=qaq%10;
			qaq/=10;
			this->len++;
		}
	}
	
	void readstr()
	{
		register char ch=0;
		register int c1=0;
		char str[1010]={0};
		while (ch!=' ' && ch!='\n')
		{
			ch=getchar();
			str[c1++]=ch-'0';
		}
		c1-=2;
		for (int i=0;i<=c1;i++) this->mainstr[1000-c1+i]=str[i];
		this->len=c1+1;
	}
	
	void output()
	{
		if (len==0) putchar('0');
		for (int i=1000-this->len+1;i<=1000;i++) putchar(this->mainstr[i]+'0');
		putchar('\n');
	}
	
};

int rules[20][3]={0},rnum;
bool vis[20]={false};
BNUM base,plan[350],ansall;

long long go(int crtnum)
{
	long long ans=0;
	for (int i=1;i<=rnum;i++)
	{
		if (rules[i][1]==crtnum && !vis[rules[i][2]])
		{
			vis[rules[i][2]]=true;
			ans+=go(rules[i][2])+1;
		}
	}
	return ans;
}

int main()
{
	base.readstr();
	scanf("%d",&rnum);
	for (int i=1;i<=rnum;i++) scanf("%d%d",&rules[i][1],&rules[i][2]);
	
	ansall.readnum(1);
	for (int i=1000-base.len+1;i<=1000;i++)
	{
		memset(vis,0,sizeof(vis));
		vis[base.mainstr[i]]=true;
		plan[i+base.len-1000].readnum(go(base.mainstr[i])+1);
		ansall=ansall*plan[i+base.len-1000];
	}
	
	ansall.output();
	return 0;
}

 

分类: 高精度

发表评论

电子邮件地址不会被公开。 必填项已用*标注

This site uses Akismet to reduce spam. Learn how your comment data is processed.