码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2015-03-13 16:41:44      阅读:100      评论:0      收藏:0      [点我收藏+]

标签:hdu2674   nagain   数论   

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 数论水题啊~~~

标签:hdu2674   nagain   数论   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!