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

InputThe 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.

OutputOutput 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. 1
  2. 10 861017

Sample Output

  1. 593846
  2.  
  3. 唬人的题目,当x>=m, x! =0 mod m
    我们就是在求小于m x!
    提取公因式化简得
    1+(1*2)+(1*2*3)+.....+(1*2*3*....*x)=1*(1+2*(1+3*(.....x-1*(1+x))))
  1. #include<iostream>
  2. #include<cstdio>
  3. #include<cstring>
  4. #include<queue>
  5. #include<algorithm>
  6. using namespace std;
  7. int main()
  8. {
  9. char n[105];
  10. int m,t;
  11. scanf("%d",&t);
  12. while(t--)
  13. {
  14. scanf("%s",n);
  15. getchar();
  16. scanf("%d",&m);
  17. int s=strlen(n);
  18. int n1=0;
  19. for(int i=0;i<s;i++)
  20. {
  21. n1*=10;
  22. n1+=n[i]-'0';
  23. if(n1>=m)
  24. break;
  25. }
  26. long long ans;
  27. if(n1>=m)
  28. {
  29. ans=m-1;
  30. for(int i=m-2;i>=1;i--)
  31. {
  32. ans=(ans+1)%m*i%m;
  33. }
  34. }
  35. else
  36. {
  37. ans=n1;
  38. for(int i=n1-1;i>=1;i--)
  39. {
  40. ans=(ans+1)%m*i%m;
  41. }
  42. }
  43. ans++;
  44. cout<<ans%m<<endl;
  45.  
  46. }
  47. }

  

hdu_3123_GCC的更多相关文章

随机推荐

  1. 机器学习kNN

    from numpy import * import operator def createDataSet(): group = array([[1.0, 1.1], [1.0, 1.0], [0, ...

  2. Scrapy框架之代理和cookie

    Cookie 是在 HTTP 协议下,服务器或脚本可以维护客户工作站上信息的一种方式.Cookie 是由 Web 服务器保存在用户浏览器(客户端)上的小文本文件,它可以包含有关用户的信息.无论何时用户 ...

  3. sort属性

    学习文章---链接 总结笔记 ①sort是Array.prototype的属性, ②如果不写入参数,则按照转换为的字符串的每个字符的unicode位点进行排序, ③如果传入一个比较函数sort(fun ...

  4. H5新特性-----type=file文件上传

    1.语法 单文件上传:<input type="file" id="file1"/> 多文件上传:<input type="file ...

  5. 【Android】2.0 Android开发环境的搭建——Eclipse

    1.0 eclipse,这可不算谷歌开发的,是IBM公司开发的,而且是Java语言写的 2.0 eclipse怎么下.百度“eclipse”,进入eclipse官网 然后,瞎几把下吧……,实在不行百度 ...

  6. JavaScript数组求和

    <script> function demo(){ var d=document.getElementsByTagName("input")[0].value.spli ...

  7. 用nodejs做一个svn密码修改页面

    linux上配置好svn服务后,管理修改密码还得去手工修改passwd这个文件,略麻烦,其实网上应该有配套的web管理修改界面程序.但我想自己用nodejs写一个,因为用node不用配置复杂的服务器. ...

  8. 【转】JavaScript 中值得注意的 for 循环

    在ECMAScript5(简称 ES5)中,有三种 for 循环,分别是: 简单for循环 for-in forEach 在2015年6月份发布的ECMAScript6(简称 ES6)中,新增了一种循 ...

  9. javascript面向对象的写法01

    类和对象 其他面向对象的语言类的语法是内置的,自然而然的事.javascript中有对象,但没有类的语法,类的实现需要模拟出来. 只需要把对象想成一个容器,里面存放一些属性或方法,把类想象成一个对象的 ...

  10. Network Request Failed

    在react native项目中,有时候调用接口会出现这样的错误提示:“Network Request Failed”. 一.模拟器上报“Network Request Failed”解决办法,也是官 ...