首页 > 其他 > 详细

hdu 3123 GCC 阶乘

时间:2015-03-31 21:48:13      阅读:165      评论:0      收藏:0      [点我收藏+]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3123

The GNU Compiler Collection (usually shortened to GCC) is a compiler system produced by the GNU Project supporting various programming languages. But it doesn’t contains the math operator “!”.
In mathematics the symbol represents the factorial operation. The expression n! means "the product of the integers from 1 to n". For example, 4! (read four factorial) is 4 × 3 × 2 × 1 = 24. (0! is defined as 1, which is a neutral element in multiplication, not multiplied by anything.)
We want you to help us with this formation: (0! + 1! + 2! + 3! + 4! + ... + n!)%m
Input
The first line consists of an integer T, indicating the number of test cases.
Each test on a single consists of two integer n and m.
Output
Output the answer of (0! + 1! + 2! + 3! + 4! + ... + n!)%m.
Constrains
0 < T <= 20
0 <= n < 10^100 (without leading zero)
0 < m < 1000000
 
题意描述:给出n和m,计算(0!+1!+2!+......+n!)%m。
算法分析:由于是模m,m的范围不是很大,即使n很大,想想如果n一旦大于m,那么(n!)%m就等于0了,所以没有必要考虑比m大的时候了。
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cstdlib>
 5 #include<cmath>
 6 #include<algorithm>
 7 #define inf 0x7fffffff
 8 using namespace std;
 9 typedef long long LL;
10 
11 LL n,m;
12 char str[111];
13 
14 int main()
15 {
16     int t ;scanf("%d",&t) ;
17     while (t--)
18     {
19         scanf("%s%I64d",str,&m);
20         if (str[0]==0) {printf("%I64d\n",(LL)1%m);continue; }
21         LL sum=0,len=strlen(str);
22         for (LL i= len-7>=0 ? len-7 : 0 ;i<=len-1 ;i++) sum=sum*10+str[i]-0;
23         LL ans=1,a=1;
24         for (LL i=1 ;i<=sum && i<=m ;i++)
25         {
26             a=a*i%m;
27             ans=(ans+a)%m;
28         }
29         printf("%I64d\n",ans%m);
30     }
31     return 0;
32 }

 

hdu 3123 GCC 阶乘

原文:http://www.cnblogs.com/huangxf/p/4381818.html

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