Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3123
The GNU Compiler Collection (usually shortened to GCC) are a Compiler system produced by the GNU Project supporting VAR IOUs 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 4x3x2x1 = 24. (0! is defined as 1, which are a neutral element in multiplication, not multiplied by anything.)
We want you and help us with this formation: (0! + 1! + 2! + 3! + 4! + ... + n!) %mInputThe first line consists of an integer T, indicating the number of test cases.
Each test in a single consists of both integer n and M.OutputOutput The answer of (0! + 1! + 2! + 3! + 4! + ... + n!) %m.
constrains
0 < T <=
0 <= N < 10^100 (without leading zero)
0 < m < 1000000Test Instructions Description: Give n and M, calculate (0!+1!+2!+......+n!)%m. algorithm Analysis: Because the range of the mold m,m is not very large, even if n is large, think if n once greater than M, then (n!)%m is equal to 0, so there is no need to consider the time than M large.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7 #defineINF 0x7fffffff8 using namespacestd;9typedefLong LongLL;Ten One LL n,m; A Charstr[111]; - - intMain () the { - intt; scanf ("%d",&t); - while(t--) - { +scanf"%s%i64d",str,&m); - if(str[0]=='0') {printf ("%i64d\n", (LL)1%M);Continue; } +LL sum=0, len=strlen (str); A for(LL i= len-7>=0? len-7:0; i<=len-1; i++) sum=sum*Ten+str[i]-'0'; atLL ans=1, a=1; - for(LL i=1; i<=sum && i<=m; i++) - { -a=a*i%m; -ans= (ans+a)%m; - } inprintf"%i64d\n", ans%m); - } to return 0; +}
HDU 3123 GCC Factorial