(hdu step 2.2.8)N!Again(求N!的阶乘%2009以后的结果)

题目:

N!Again

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

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


Author

WhereIsHeroFrom


Source

HDU女生专场公开赛——谁说女子不如男


Recommend

lcy

题目分析:

这道题,扫了一眼题目以后,再扫一遍数据规模,就知道直接暴力肯定是会超时的,因为n大的有点吓人啊。这种题,个人觉得可能还是得找规律。规律主要从以下两个方面找:

1)可能存在循环节。如果存在循环节,吧循环节找到(假设是m),那么输入n的时候,输出fac(n%m)即可。

2)可能某一项(假设是m)以后%2009以后的结果全是一个数k。那么就只需要计算前m项即可,当n>m时,直接输出相应结果就行。

为了确定是以上两种情况的哪一种,我写了一下的java打表程序(因为n为41是,阶乘的结果就是33452526613163807108170062053440751665152000000000了,这已经超出了证书的范围,所以需要用到java中的大数)。直达前100项,个人觉得前100项一般都能说明问题了。

在这里需要注意一点的是:

1*2*3.。。*i%2009所得到的结果与1%2009*2%2009......i%2009所得到的结果是完全一样的。fac(41)阶乘已经超过long 的范围了,所以这里去一下巧,每一项都%2009,用来避免超过long 的范围

打表程序

import java.math.BigInteger;
import java.util.Scanner;

public class Test {

	public static BigInteger fac(int n){
		BigInteger sum = new BigInteger("1");

		int i;
		for(i = 1 ;  i <= n; ++i){
			sum = sum.multiply(BigInteger.valueOf(i));
		}

		return sum.mod(BigInteger.valueOf(2009));
//		return sum;
	}
	public static void main(String[] args) {
//		Scanner scanner = new Scanner(System.in);
//
//		while(scanner.hasNext()){
//			int n = scanner.nextInt();
//			System.out.println(fac(n));
//		}

		int i;
		for(i = 1 ; i <= 100 ; i++){
			System.out.println(i + ": " + fac(i));
		}

	}
}

通过打表,确定了猜想2是符合这道题的规律。第41项以后的阶乘出来的数%2009的结果都是0。既然规律都找到了,

那么接下来就是编写代码了:

/*
 * f.cpp
 *
 *  Created on: 2015年2月2日
 *      Author: Administrator
 */

#include <iostream>
#include <cstdio>

using namespace std;

long long fac(long long n){
	int i;
	long long sum = 1;
	for(i = 1 ; i <= n ; ++i){
		sum *= i;
		sum %= 2009;
	}

	return sum;
}

int main(){
	int n;
	while(scanf("%d",&n)!=EOF){
		if(n < 41){
			printf("%lld\n",fac(n));
		}else{
			printf("0\n");
		}
	}

	return 0;
}
时间: 2024-11-08 04:23:48

(hdu step 2.2.8)N!Again(求N!的阶乘%2009以后的结果)的相关文章

(hdu step 3.1.5)Tiling_easy version(求用2*1、2*2两种骨格铺满2*n的网格的方案数)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Tiling_easy version Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 515 Accepted Submission(s): 447   Prob

(hdu step 4.2.3)Knight Moves(求从起点是否能够到达终点的最小步数)

题目: Knight Moves Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 453 Accepted Submission(s): 326   Problem Description A friend of you is doing research on the Traveling Knight Problem (TKP) where

(hdu step 5.1.5)Dragon Balls(求并查集的根节点、节点数和个结点的移动次数)

题目: Dragon Balls Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 562 Accepted Submission(s): 239   Problem Description Five hundred years later, the number of dragon balls will increase unexpected

(hdu step 7.1.5)Maple trees(求凸包的最小覆盖圆的半径)

题目: Maple trees Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 177 Accepted Submission(s): 63   Problem Description There are a lot of trees in HDU. Kiki want to surround all the trees with the m

(hdu step 5.1.3)Segment set(求与一条线段相交的线段集合中的线段的数量)

题目: Segment set Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 177 Accepted Submission(s): 82   Problem Description A segment and all segments which are connected with it compose a segment set. T

(hdu step 1.3.6)Wooden Sticks(求长度和重量都严格递增的数列的个数)

题目: Wooden Sticks Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 2374 Accepted Submission(s): 921   Problem Description There is a pile of n wooden sticks. The length and weight of each stick are

(hdu step 3.1.6)统计问题(求不断地左右走、向上走n步的方案数)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: 统计问题 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 732 Accepted Submission(s): 466   Problem Description

(hdu step 2.3.3)Big Number(求N!的位数)

在写题解之前给自己打一下广告哈~..抱歉了,希望大家多多支持我在CSDN的视频课程,地址如下: http://edu.csdn.net/course/detail/209 题目: Big Number Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 951 Accepted Submission(s): 640   Problem Des

(hdu step 6.3.1)Strategic Game(求用最少顶点数把所有边都覆盖,使用的是邻接表)

题目: Strategic Game Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 110 Accepted Submission(s): 75   Problem Description Bob enjoys playing computer games, especially strategic games, but sometim