首页 > 其他 > 详细

hdu 2674 N!Again 数论水题啊~~~

时间:2015-03-13 16:39:19      阅读:305      评论:0      收藏:0      [点我收藏+]

N!Again

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3803    Accepted Submission(s): 2036


Problem Description
WhereIsHeroFrom:             Zty, what are you doing ?
Zty:                                     I want to calculate N!......
WhereIsHeroFrom:             So easy! How big N is ?
Zty:                                    1 <=N <=1000000000000000000000000000000000000000000000…
WhereIsHeroFrom:             Oh! You must be crazy! Are you Fa Shao?
Zty:                                     No. I haven‘s finished my saying. I just said I want to calculate N! mod 2009


Hint : 0! = 1, N! = N*(N-1)!
 

Input
Each line will contain one integer N(0 <= N<=10^9). Process to end of file.
 

Output
For each case, output N! mod 2009
 

Sample Input
4 5
 

Sample Output
24 120
 这题最容易想到的是2009以后全是0。。。至少我第一次A的代码就是这个思路。2009以前的打表~~
其次,2009=41*7*7。所以40以后全都是0了。至于40以前的也是打表呗~
附上我的代码:
#include <stdio.h>
#define MAX 2009

int ans[MAX] ;

int main()
{
	int n ;
	ans[0] = 1 ;
	for(int i = 1 ; i < MAX ; ++i )
	{
		ans[i] = (ans[i-1]*i)%MAX ;
	}
	while(~scanf("%d",&n))
	{
		if(n>=2009)
		{
			puts("0") ;
		}
		else
		{
			printf("%d\n",ans[n]) ;
		}
	}
	return 0;
}

与君共勉

hdu 2674 N!Again 数论水题啊~~~

原文:http://blog.csdn.net/lionel_d/article/details/44241493

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!