POJ 3331 The Idiot of the Year Contest! 高精度阶乘

本博客详细介绍了如何解决 POJ 3331 题目,即在特定出生日期上选择一个数字,计算其在该日期阶乘中出现的次数。通过提供输入样例和输出样例,以及关键代码解析,旨在帮助读者理解并解决类似问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://poj.org/problem?id=3331

The Idiot of the Year Contest!
Time Limit:2000MS Memory Limit:65536K
Total Submissions:3072 Accepted:1590

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 integerTwhich is the number of test cases, followed byTlines 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 containsTlines, 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 
/* Author : yan
 * Question : POJ 3331 The Idiot of the Year Contest!
 * Date && Time : Monday, January 31 2011 08:07 PM
 * Compiler : gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
*/
#include<stdio.h>
#define MAX 1000
char mul1[MAX],mul2[4];
char ans[365][MAX];
void multiply(char* a,char* b,char* c)
{
    int i,j,ca,cb,* s;
    ca=strlen(a);
    cb=strlen(b);
    s=(int*)malloc(sizeof(int)*(ca+cb));
    for (i=0;i<ca+cb;i++)
        s[i]=0;
    for (i=0;i<ca;i++)
        for (j=0;j<cb;j++)
            s[i+j+1]+=(a[i]-'0')*(b[j]-'0');//i+j+1的目的就是为了防止最高位进位而产生错误
    for (i=ca+cb-1;i>=0;i--)
        if (s[i]>=10)
        {
            s[i-1]+=s[i]/10;
            s[i]%=10;
        }
    i=0;
    while (s[i]==0)
        i++;//去除前导0
       for (j=0;i<ca+cb;i++,j++)
           c[j]=s[i]+'0';
    c[j]='/0';//将结果存储到字符数组
    free(s);
}
int main()
{
	//freopen("input","r",stdin);
	int t,i;
	int day,digit,tmp,res;
	char cache[4];
	ans[0][0]='1',ans[0][1]='/0';
	for(i=1;i<366;i++)
	{
		sprintf(cache,"%d",i);
		multiply(ans[i-1],cache,ans[i]);
	}
	scanf("%d",&t);
	while(t--)
	{
		res=0;
		scanf("%d %d",&day,&digit);
		tmp=strlen(ans[day]);
		for(i=0;i<tmp;i++)
		{
			if(ans[day][i]-'0'==digit) res++;
		}
		printf("%d/n",res);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值