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. virtual box虚拟机在linux下设置共享文件夹

    使用的虚拟机版本是:VirtualBox-5.2.8-121009 使用的linux版本是:Ubuntu 12.04.5 LTS 和 Linux Mint 19 Tara 1. 安装增强功能包(Gue ...

  2. tree编译

    没有tree命令,就需要下载源代码 [root@fyc tree-1.7.0]#cd /opt/src [root@fyc tree-1.7.0]# wget ftp://mama.indstate. ...

  3. Vue异步组件Demo

    Vue异步组件Demo 在大型应用中,我们可能需要将应用拆分为多个小模块,按需从服务器下载.为了进一步简化,Vue.js 允许将组件定义为一个工厂函数,异步地解析组件的定义.Vue.js 只在组件需要 ...

  4. [MST] Use Volatile State and Lifecycle Methods to Manage Private State

    MST has a pretty unique feature: It allows you to capture private state on models, and manage this s ...

  5. MongoDB查询、索引和聚合

    初始化mongodb数据库 > use deng switched to db deng > db.createCollection("jingdong") #无參数 ...

  6. AppCan中标首都机场移动平台项目

    近日.正益无线AppCan依托东方航空.吉祥航空.国家电网.中化集团等大客户项目的丰富成功经验,凭借企业移动信息化建设的良好口碑.强大的移动化实施部署经验和高速响应的技术团队,在与多家国内外移动厂商比 ...

  7. 在Linux的终端中显示BMPString的内容

    在上一篇博文中,介绍了怎样在 Windows 的控制台界面下输出 BMPString 的内容,可是那里的方法在 Linux 下不适用.假设将那里的演示样例代码放到 Linux 下运行.输出的结果为乱码 ...

  8. OpenCASCADE直线与平面求交

    OpenCASCADE直线与平面求交 在<解析几何>相关的书中都给出了直线和平面的一般方程和参数方程.其中直线的一般方程有点向式形式的. 由于过空间一点可作且只能作一条直线平行于已知直线, ...

  9. 《鸟哥的Linux私房菜-基础学习篇(第三版)》(三)

    第2章 Linxu怎样学习         1. Linux当前的应用角色 当前的Linux常见的应用可略分为企业应用和个人应用双方面. 首先谈了企业环境的利用. 1)网络server. 2)关键任务 ...

  10. css3模糊图片

    image的css样式 filter: blur(10px); -webkit-filter: blur(10px); -moz-filter: blur(10px); -ms-filter: blu ...