首页 > 代码库 > hdu 3123
hdu 3123
GCC
Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3754 Accepted Submission(s): 1216
Problem Description
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
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.
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
Constrains
0 < T <= 20
0 <= n < 10^100 (without leading zero)
0 < m < 1000000
Sample Input
1
10 861017
Sample Output
593846
Source
2009 Asia Wuhan Regional Contest Online
数字看起来非常巨大,吓人。
但是如果n!%p =0 那么 (n+1)!%p =0;
就这样。
1 #include<iostream> 2 #include<stdio.h> 3 #include<cstring> 4 #include<cstdlib> 5 using namespace std; 6 typedef __int64 LL; 7 8 void solve(int sum,int p) 9 {10 int i;11 LL ans = 1 ,cur = 1 % p ;12 for(i=1;i<=sum;i++)13 {14 ans = (ans * i)%p;15 cur = (cur + ans)%p;16 if(ans == 0)break;17 }18 printf("%I64d\n",cur);19 }20 int main()21 {22 int T;23 int i,p;24 char a[1002];25 scanf("%d",&T);26 while(T--)27 {28 scanf("%s%d",a,&p);29 if(p==1)30 {31 printf("0\n");32 continue;33 }34 int sum = 0;35 for(i=0;a[i]!=‘\0‘;i++)36 {37 sum=sum*10+a[i]-‘0‘;38 if(sum>=p)break;39 }40 solve(sum,p);41 }42 return 0;43 }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。