hdoj 2647 N!Again

N!Again

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

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

此题仍然用同余定理:前边文章中已经做出详细解释这里就不解释了

此题还有一个技巧就是41(包括41)之后的所有数据结果都是0

因为40求出的结果是245==49*5      2009==49*41

下一步对41求阶乘并对2009取模就等于(41%2009*245%2009)%2009==(41*245)%2009==0

所以之后的每一步都等0

AC代码:

#include<stdio.h>
#include<string.h>
int main()
{
	int n,m,j,i,s,t;
	while(scanf("%d",&n)!=EOF)
	{
		if(n>=41)
		printf("0\n");
		else
		{
		    s=1;
		    for(i=2;i<=n;i++)
		    {
			    s=s%2009*i%2009;
			    s=s%2009;
		    }
		    printf("%d\n",s);
	    }
	}
	return 0;
}

  

时间: 2024-08-10 17:17:37

hdoj 2647 N!Again的相关文章

hdoj 2647 Reward

Reward Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5940    Accepted Submission(s): 1827 Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is coming , he w

HDOJ 2647 Reward 【逆拓扑排序+分层】

题意:每一个人的基础工资是888. 因为一部分人要显示自己水平比較高,要求发的工资要比其它人中的一个人多.问你能不能满足他们的要求,假设能的话终于一共要发多少钱,假设不能就输出-1. 策略:拓扑排序. 这道题有些难点:一:数据大,建二维数组肯定不行,要换其它的数据结构(vector, 或者是链式前向星(本题代码用的是链式前向星)): 二:要逆拓扑排序(就是将++in[b]换成++in[a]). 三要分层次(依据上一个的钱数+1就可以). 不懂什么是链式前向星 移步:http://blog.csd

HDOJ 2647(拓扑排序)

#include <iomanip> #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> #include <queue> #include <stack> #include <vector> using namespace std; int n,m,inbug[10100],d[10100],sum; vecto

Is It A Tree?------HDOJ杭电1325(两种方法,可以用也可以不用并查集!!!!!!详解)

Problem Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a set of one or more nodes connected by directed edges between nodes satisfying the following properties. There is exactly one node, called the

饭卡------HDOJ杭电2546(还是01背包!!!!!!)

Problem Description 电子科大本部食堂的饭卡有一种非常诡异的设计,即在购买之前推断剩余金额. 假设购买一个商品之前,卡上的剩余金额大于或等于5元,就一定能够购买成功(即使购买后卡上剩余金额为负),否则无法购买(即使金额足够).所以大家都希望尽量使卡上的剩余金额最少. 某天,食堂中有n种菜出售.每种菜可购买一次. 已知每种菜的价格以及卡上的剩余金额,问最少可使卡上的剩余金额为多少. Input 多组数据.对于每组数据: 第一行为正整数n.表示菜的数量.n<=1000. 第二行包含

HDOJ(HDU) 2137 circumgyrate the string(此题用Java-AC不过!坑)

此题如果有用JavaACDSee,请评论,谢谢了. Problem Description Give you a string, just circumgyrate. The number N means you just circumgyrate the string N times, and each time you circumgyrate the string for 45 degree anticlockwise. Input In each case there is string

图论五百题!

生死看淡不服就淦,这才是人生! =============================以下是最小生成树+并查集======================================[HDU]1213 How Many Tables 基础并查集★1272 小希的迷宫 基础并查集★1325&&poj1308 Is It A Tree? 基础并查集★1856 More is better 基础并查集★1102 Constructing Roads 基础最小生成树★1232 畅通工程 基

【HDOJ】【3709】Balanced Bumber

数位DP 题解:http://www.cnblogs.com/algorithms/archive/2012/09/02/2667637.html dfs的地方没太看懂……(也就那里是重点吧喂!)挖个坑……回头再看看 1 //HDOJ 3709 2 #include<cmath> 3 #include<vector> 4 #include<cstdio> 5 #include<cstring> 6 #include<cstdlib> 7 #inc

魔咒词典------HDOJ杭电1880(字符串的处理,很简单)

Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词典.当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能:当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒.如果他要的魔咒不在词典中,就输出"what?" Input 首先列出词典中不超过100000条不同的魔咒词条,每条格式