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



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

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#define ll long long
#define N 1000010
using namespace std;
char s[110];
ll sum[N];
ll change(char *s)
{
int l,i,ans=0;
l=strlen(s);
for(i=0;i<l;i++)
ans=ans*10+(s[i]-'0');
return ans;
}
int main()
{
int t;
int n,m;
int i,j,k,l;
scanf("%d",&t);
while(t--)
{
scanf("%s%d",s,&m);
l=strlen(s);
if(l<7)
{
n=change(s);
if(n>m)
n=m-1;
sum[0]=1;
int ans=0;
for(i=1;i<=n;i++)
{
sum[i]=(sum[i-1]*i)%m;
ans=(ans+sum[i])%m;
}
printf("%d\n",(ans+1)%m);
}
else
{
n=m-1;
sum[0]=1;
int ans=0;
for(i=1;i<=n;i++)
{
sum[i]=(sum[i-1]*i)%m;
ans=(ans+sum[i])%m;
}
printf("%d\n",(ans+1)%m);
}
}
return 0;
}

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. Linux 文件系统的层次化结构

    FHS,Filesystem Hierarchy Standard,文件系统层次化标准.这是一个推荐标准,可以从 http://www.pathname.com/fhs/ 获取. 本文不讨论 FHS, ...

  2. 紫书 习题 8-25 UVa 11175 (结论证明)(配图)

    看了这篇博客https://blog.csdn.net/u013520118/article/details/48032599 但是这篇里面没有写结论的证明, 我来证明一下. 首先结论是对于E图而言, ...

  3. 微信小程序踩坑记

    1:微信小程序之去掉横向滑动滚动条 /** 去除横向滚动条 */ ::-webkit-scrollbar { width: 0; height: 0; color: transparent; } 2: ...

  4. 洛谷 P1275 魔板

    P1275 魔板 题目描述 有这样一种魔板:它是一个长方形的面板,被划分成n行m列的n*m个方格.每个方格内有一个小灯泡,灯泡的状态有两种(亮或暗).我们可以通过若干操作使魔板从一个状态改变为另一个状 ...

  5. (cLion、RubyMine、PyCharm、WebStorm、PhpStorm、Appcode、Clion、Idea) 万能破解,获取自己的注冊码

    听说cLion的ide编写c/c++很的棒.今天下载了一个仅仅有30天的使用时间.作为程序猿破解它. 下载破解文件 | 点击下载 |password: 7biu 解压压缩包,然后打开命令行 cd 到解 ...

  6. [Webpack + React] Import CSS Modules with TypeScript and webpack

    If you try to use CSS Modules in TypeScript the same way you would use them in JavaScript, with webp ...

  7. C++primer书店程序

    #include <iostream> #include <string> #include <cassert> #include <algorithm> ...

  8. 捕捉到来自宇宙深空的神奇X-射线信号

    请看下图: 这是专门用于捕捉X-射线信号的航天望远镜,约有5吨重,执行轨道距离地面大约有5万多公里.6月24日,美国宇航局NASA宣布,这台航天望远镜从银河系深处捕捉到一种波长非常特殊的神奇X-射线信 ...

  9. Android学习路线(十二)Activity生命周期——启动一个Activity

    DEMO下载地址:http://download.csdn.net/detail/sweetvvck/7728735 不像其他的编程模式那样应用是通过main()函数启动的.Android系统通过调用 ...

  10. Python: PS 图像调整--黑白

    本文用Python 实现 PS 里的图像调整–黑白,PS 里的黑白并不是简单粗暴的将图像转为灰度图,而是做了非常精细的处理,具体的算法原理和效果图可以参考以前的博客: http://blog.csdn ...