HDU 3123 GCC 数学

我不是女神ヾ 2022-05-08 07:48 166阅读 0赞

GCC

Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 5544 Accepted Submission(s): 1837

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

当n>m时,n!中一定包含m,所以n!%m==0,所以只要考虑到m-1的阶乘就可以了。

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<iostream>
  5. #include<string>
  6. #include<algorithm>
  7. #include<map>
  8. #include<set>
  9. #include<queue>
  10. #include<vector>
  11. using namespace std;
  12. #define inf 0x3f3f3f3f
  13. #define LL long long
  14. int main()
  15. {
  16. int t;
  17. LL m,n,i,len;
  18. char a[105];
  19. scanf("%d",&t);
  20. while(t--)
  21. {
  22. scanf("%s%lld",a,&m);
  23. len=strlen(a);
  24. n=0;
  25. for(i=0;i<len;i++)
  26. {
  27. n=n*10+(a[i]-'0');
  28. if(n>=m)
  29. break;
  30. }
  31. LL k=1,sum=1;
  32. for(i=1;i<=m&&i<=n;i++)
  33. {
  34. k=(k*i)%m;
  35. sum=(sum+k)%m;
  36. }
  37. printf("%lld\n",sum%m);
  38. }
  39. }

发表评论

表情:
评论列表 (有 0 条评论,166人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HDU 1141(数学题)

    题意:1960年的计算机是4位处理器,1970年是8位,没过10年翻一倍,求当前年份的计算机处理器能存储的阶乘n!中n的大小。   思路:假设处理器的位数为x位,则n! <