hdoj-3123-GCC

来源:互联网 发布:linux 监控系统性能 编辑:程序博客网 时间:2024/05/29 08:16

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

当输入的数字长度大于7时,可以是可以除尽m的
然后分开讨论,边乘边mod就好了

#include<cstring> #include<cstdio> #include<algorithm> #include<iostream> using namespace std; typedef long long ll; int main() {     int t;     scanf("%d",&t);     while(t--)     {         char s[1000];         int m;         ll sum;         scanf("%s%d",s,&m);         int l=strlen(s);         if(l>=7)         {             sum=m;         }         else         {             sum=0;             for(int i=0;i<l;i++)             {                 sum*=10;                 sum+=s[i]-'0';             }         }         //printf("%I64d\n",sum);         ll ans1=1,ans2=1;         for(ll i=1;i<=sum;i++)         {             ans1=(ans1*i)%m;             ans2=(ans1+ans2)%m;         }         ans2%=m;         printf("%I64d\n",ans2);     }     return 0; }
0 0
原创粉丝点击