天天看点

POJ3331解题报告 高精度幂

The Idiot of the Year Contest!

Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 3074 Accepted: 1591

Description

There is just one basic rule in the Idiot of the Year Contest (IYC)! The contestant picks a random digit between 0 and 9, computes the factorial of the day of the year he/she is born, and counts the how many times the digit picked appears in the factorial. The contestant with highest count is the Idiot of the Year! For example, if you are born on 5th of Mordad which is the 129th day of the year, and you pick the digit 6, your score will be the number of times the digit 6 appears in 129! (that is 1 × 2 × 3 × ... × 129).

The chief judge of IYC wants you to write a program to get an integer which is the day of the year a contestant is born on and a digit and report the number of times the digit appears in the factorial of the first number.

Input

The first line of the input contains a single integer T which is the number of test cases, followed by T lines each containing the data for a test case having two numbers. The first number is the day of the year a contestant is born and the second one is the digit he/she has picked.

Output

The output contains T lines, each having one integer which is the number of times the digit appears in the factorial of the first number.

Sample Input

2
5 2
7 0
      

Sample Output

1
2 
      
题意:求n!中含有多少个指定的数字。      
高精度幂水过。。。      
#include<iostream>
using namespace std;
char s1[10000],s2[10000],s3[10000];

void BigIntPlus(char a[],char b[],char c[])
{
	char d[10000];	int i,j,k=0,t;
	if(strlen(a)>strlen(b))strcpy(c,a),strcpy(d,b);
	else strcpy(c,b),strcpy(d,a);
	i=strlen(c)-1;	j=strlen(d)-1;
	while(j>=0){	t=c[i]+d[j--]-96;	c[i--]=(t+k)%10+48;	k=(t+k)/10;	}
	while(i>=0&&k){		t=c[i]-48+k;	c[i--]=t%10+48;	k=t/10;		}
	if(k){	for(j=strlen(c)+1;j>0;j--)	c[j]=c[j-1];	c[0]=k+48;	}
}

void BigIntMul(char a[],char b[],char c[])
{
	int i,j,k=0,t;	char temp[10000],temp2[10000];	
	strcpy(c,"0");
	for(i=strlen(a)-1;i>-1;i--,k=0)
	{
		strcpy(temp,b);	for(j=1;j<strlen(a)-i;j++)	strcat(temp,"0");
		for(j=strlen(temp)-1;j>-1;j--)
		{
			t=(temp[j]-48)*(a[i]-48);
			temp[j]=(t+k)%10+48;
			k=(t+k)/10;
		}
		if(k){	for(j=strlen(temp)+1;j>0;j--)	temp[j]=temp[j-1];	temp[0]=k+48;	}
		strcpy(temp2,c);
		BigIntPlus(temp,temp2,c);
	}
}

int main()
{
	int i,t,j,l,n;	char c,k;
	cin>>t;
	while(t--)
	{
		cin>>n>>c;
		strcpy(s2,"1");
		for(i=n;i>0;i--)
		{
			l=i;
			s1[0]='/0';
			while(l)
			{
				s1[strlen(s1)+1]='/0';
				s1[strlen(s1)]=l%10+48;
				l/=10;
			}
			for(l=0;l<strlen(s1)/2;l++)
			{
				k=s1[l];	s1[l]=s1[strlen(s1)-l-1];	s1[strlen(s1)-l-1]=k;

			}
			BigIntMul(s1,s2,s3);
			strcpy(s2,s3);
		}
		for(i=0,l=0;i<strlen(s3);i++)
			if(s3[i]==c)
				l++;
		cout<<l<<endl;
	}	
}