GCC

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 4473    Accepted Submission(s): 1472

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. 1
  2. 10 861017
 
Sample Output
  1. 593846
 
Source
2009 Asia Wuhan Regional Contest Online



好吧,0!是1,我服了,,,,,

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<algorithm>
  5. #define ll long long
  6. #define N 1000010
  7. using namespace std;
  8. char s[110];
  9. ll sum[N];
  10. ll change(char *s)
  11. {
  12. int l,i,ans=0;
  13. l=strlen(s);
  14. for(i=0;i<l;i++)
  15. ans=ans*10+(s[i]-'0');
  16. return ans;
  17. }
  18. int main()
  19. {
  20. int t;
  21. int n,m;
  22. int i,j,k,l;
  23. scanf("%d",&t);
  24. while(t--)
  25. {
  26. scanf("%s%d",s,&m);
  27. l=strlen(s);
  28. if(l<7)
  29. {
  30. n=change(s);
  31. if(n>m)
  32. n=m-1;
  33. sum[0]=1;
  34. int ans=0;
  35. for(i=1;i<=n;i++)
  36. {
  37. sum[i]=(sum[i-1]*i)%m;
  38. ans=(ans+sum[i])%m;
  39. }
  40. printf("%d\n",(ans+1)%m);
  41. }
  42. else
  43. {
  44. n=m-1;
  45. sum[0]=1;
  46. int ans=0;
  47. for(i=1;i<=n;i++)
  48. {
  49. sum[i]=(sum[i-1]*i)%m;
  50. ans=(ans+sum[i])%m;
  51. }
  52. printf("%d\n",(ans+1)%m);
  53. }
  54. }
  55. return 0;
  56. }

hdoj--3123--GCC(技巧阶乘取余)的更多相关文章

  1. hdu 3123 GCC (2009 Asia Wuhan Regional Contest Online)

    GCC Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  2. bjfu1238 卡特兰数取余

    题目就是指定n,求卡特兰数Ca(n)%m.求卡特兰数有递推公式.通项公式和近似公式三种,因为要取余,所以近似公式直接无法使用,递推公式我简单试了一下,TLE.所以只能从通项公式入手. Ca(n) = ...

  3. LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)

    链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...

  4. hud 3123 GCC

    题目 输入:n 和 mod 输出: Output the answer of (0! + 1! + 2! + 3! + 4! + ... + n!)%m. Constrains 0 < T &l ...

  5. float 对整形的取余运算

    取余是针对整形的,但是有时候一些特殊需求,我们需要 float 型对整形取下余数.比如,将角度化到 0- 360 范围内. 今天看到 lua 的实现方式: a % b == a - math.floo ...

  6. JS利用取余实现toggle多函数

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. salesforce 零基础学习(四十三)运算取余

    工作中遇到一个简单的小问题,判断两个数是否整除,如果不整除,获取相关的余数. 习惯java的我毫不犹豫的写下了代码 public Boolean isDivisibility(Integer divi ...

  8. PHP大数(浮点数)取余

    一般我们进行取余运算第一个想到的就是用百分号%,但当除数是个很大的数值,超出了int范围时,这样取余就不准确了. php大数(浮点数)取余函数 /** * php大数取余 * * @param int ...

  9. JAVA中取余(%)规则和介绍

    在java中%的含义为取余. java :a%b 数学公式a%b=a-(a/b)*b

随机推荐

  1. 【jQuery02】点击标题面板显示内容

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. CentOS7 安装 MySQL 5.7

    wget https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.25-linux-glibc2.12-x86_64.tar.gz yum -y ...

  3. [luogu] P4514 上帝造题的七分钟 (树状数组,二维差分)

    P4514 上帝造题的七分钟 题目背景 裸体就意味着身体. 题目描述 "第一分钟,X说,要有矩阵,于是便有了一个里面写满了0的n×m矩阵. 第二分钟,L说,要能修改,于是便有了将左上角为(a ...

  4. centos6.5安装配置nginx+php+mysql+httpsqs+ttserver+redis

    一.准备 1.nginx, http://nginx.org/download/nginx-1.5.12.tar.gz 2.pcre, ftp://ftp.csx.cam.ac.uk/pub/soft ...

  5. 洛谷 P1524 十字绣

    P1524 十字绣 题目背景 考古学家发现了一块布,布上做有针线活,叫做“十字绣”,即交替地在布的两面穿线. 题目描述 布是一个n*m的网格,线只能在网格的顶点处才能从布的一面穿到另一面.每一段线都覆 ...

  6. Codeforces Round #249 (Div. 2) (模拟)

    C. Cardiogram time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  7. (数据结构整理)NJUPT1054

    这一篇博客以一些OJ上的题目为载体,整理一下数据结构.会陆续的更新. .. 我们都知道,数据结构的灵活应用有时能让简化一些题目的解答. 一.栈的应用 1.NJUPT OJ 1054(回文串的推断) 回 ...

  8. STL源代码学习(vector篇)

    #include <concept_checks.h> #include<stl_allocate.h> /// The vector base class's constru ...

  9. 小贝_redis list类型学习

    redis  list类型 一.查看list类型的命令 二.list命令具体解释 一.查看list类型的命令 1.在终端数据 help @list 127.0.0.1:6379>help @li ...

  10. 英语影视台词---五、And Then There Were None

    英语影视台词---五.And Then There Were None 一.总结 一句话总结:求阿加莎·克里斯蒂小说<无人生还>的英文版<And Then There Were No ...