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
 
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
 
Sample Input
1
10 861017
 
Sample Output
593846
 
Source
 
数字看起来非常巨大,吓人。
但是如果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
",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
");
32             continue;
33         }
34         int sum = 0;
35         for(i=0;a[i]!='';i++)
36         {
37             sum=sum*10+a[i]-'0';
38             if(sum>=p)break;
39         }
40         solve(sum,p);
41     }
42     return 0;
43 }
原文地址:https://www.cnblogs.com/tom987690183/p/3862550.html