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. LAMP环境搭建备忘 -- MariaDB 安装(三)

    因为 MySQL 的一些原因,在 Linux 平台上的开源数据库渐渐被 MariaDB 取代. MariaDB 安装命令如下图 安装成功后,接下来就启动这个数据库服务 我们还需要对数据库做一些初始化的 ...

  2. python3爬取全民K歌

    Python3爬取全民k歌 环境 python3.5 + requests 1.通过歌曲主页链接爬取 首先打开歌曲主页,打开开发者工具(F12). 选择Network,点击播放,会发现有一个请求返回的 ...

  3. caioj 1204 Catalan数(模板)

    题目中对卡特兰数的总结很不错 以下copy自题目 Catalan数列:1,1,2,5,14,42,(前面几个要背) 即 h(0)=1,h(1)=1,h(2)=2,h(3)=5...公式:h(n)=C( ...

  4. 题解 P3834 【【模板】可持久化线段树 1(主席树)】

    可持久化线段树的前置知识是权值线段树,但是你不学也没有太大的关系因为思想不是很难理解. 可持久化线段树支持历史记录查询,这是它赖以解题的方法. 在本题中思路是建立n颗线段树,然后对于每次询问,考虑其中 ...

  5. ZOJ 2601 Warehouse Keeper

    Warehouse Keeper Time Limit: 2000ms Memory Limit: 65536KB This problem will be judged on ZJU. Origin ...

  6. POJ 1161 Walls(最短路+枚举)

    POJ 1161 Walls(最短路+枚举) 题目背景 题目大意:题意是说有 n个小镇,他们两两之间可能存在一些墙(不是每两个都有),把整个二维平面分成多个区域,当然这些区域都是一些封闭的多边形(除了 ...

  7. linux安装oracleclient

    1.准备好所须要的安装包,http://www.oracle.com/technetwork/database/features/instant-client/index-097480.html这个网 ...

  8. hdu1978 How many ways

    How many ways Problem Description 这是一个简单的生存游戏,你控制一个机器人从一个棋盘的起始点(1,1)走到棋盘的终点(n,m).游戏的规则描述如下: 机器人一开始在棋 ...

  9. Android chromium 2

    Overview JNI (Java Native Interface) is the mechanism that enables Java code to call native function ...

  10. mac ssh报错处理

    总结一下 1.The authenticity of host '[192.168.1.100]:22 ([192.168.1.100]:22)' can't be established. RSA ...