题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3123

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
 
题意描述:给出n和m,计算(0!+1!+2!+......+n!)%m。
算法分析:由于是模m,m的范围不是很大,即使n很大,想想如果n一旦大于m,那么(n!)%m就等于0了,所以没有必要考虑比m大的时候了。
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#define inf 0x7fffffff
using namespace std;
typedef long long LL; LL n,m;
char str[]; int main()
{
int t ;scanf("%d",&t) ;
while (t--)
{
scanf("%s%I64d",str,&m);
if (str[]=='') {printf("%I64d\n",(LL)%m);continue; }
LL sum=,len=strlen(str);
for (LL i= len->= ? len- : ;i<=len- ;i++) sum=sum*+str[i]-'';
LL ans=,a=;
for (LL i= ;i<=sum && i<=m ;i++)
{
a=a*i%m;
ans=(ans+a)%m;
}
printf("%I64d\n",ans%m);
}
return ;
}

hdu 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. hdu 3123 GCC

    这题分2种情况: 1) n>=m时,k!%m=0(k>=m),所以只需令n=m-1即可: 2) n<m时,正常情况处理即可. ;}

  3. hdu 3123

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

  4. HDOJ(HDU) 2212 DFS(阶乘相关、)

    Problem Description A DFS(digital factorial sum) number is found by summing the factorial of every d ...

  5. hud 3123 GCC

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

  6. HDU 1042 大数阶乘

    B - 2 Time Limit:5000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Statu ...

  7. hdu 3123 2009 Asia Wuhan Regional Contest Online

    以为有啥牛逼定理,没推出来,随便写写就A了----题非常水,可是wa了一次 n>=m  则n!==0 注意的一点,最后 看我的凝视 #include <cstdio> #includ ...

  8. HDU 1018Big Number(大数的阶乘的位数,利用公式)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big Number Time Limit: 2000/1000 MS (Java/Others) ...

  9. HDU 1018 Big Number (阶乘位数)

    题意: 给一个数n,返回该数的阶乘结果是一个多少位(十进制位)的整数. 思路: 用对数log来实现. 举个例子 一个三位数n 满足102 <= n < 103: 那么它的位数w 满足 w ...

随机推荐

  1. 设置DataGridView 显示自己添加编辑的列名,不动态显示数据库本身

    设置DataGridView 显示自己添加编辑的列名,不动态显示数据库本身. 方法: (1)界面操作,把DataGridView控件拖放在窗体中,就看到DataGridView控件的右上角有个小三角, ...

  2. JS模块化工具requirejs教程(一):初识requirejs

    随着网站功能逐渐丰富,网页中的js也变得越来越复杂和臃肿,原有通过script标签来导入一个个的js文件这种方式已经不能满足现在互联网开发模式,我们需要团队协作.模块复用.单元测试等等一系列复杂的需求 ...

  3. jquery ajax跨域请求详解

    本文章来给大家详细jquery中的ajax跨域请求, 在JQuery对于Ajax的跨域请求有两类解决方案,不过都是只支持get方式.分别是JQuery的jquery.ajax jsonp格式和jque ...

  4. 平衡二叉树(AVL)

    性质:它是一棵空树或它的左右两个子树的高度差的绝对值不超过1,并且左右两个子树都是一棵平衡二叉树. 定义:平衡二叉树或为空树,或为如下性质的二叉排序树: (1)左右子树深度之差的绝对值不超过1; (2 ...

  5. C#之事件初步

    上文简述了委托,所谓的简述,只是说了一下如何使用委托,既然有了委托的基础,便可以稍微一探事件的机制. 事件,实际上是委托类型,事件处理函数如下: public delegate void MyHand ...

  6. virtualenv python虚拟环境搭建

    python virtualenv.py flask

  7. Oracle Study Note : Tablespace and Data Files

    1.how to create a tablespace that employs the most common features create tablespace tb_name #create ...

  8. Redhat 6环境下安装Oracle 12c的方法

    Step 1: 要在Linux上安装Oracle,需要在安装Oracle之前安装好相应的软件包,在不同操作系统环境下,对软件包的要求各不相同.具体对应的软件包,见官网文档:https://docs.o ...

  9. Linux下mysql自动备份

    #!/bin/bashDATE=`date +%Y-%m-%d-%H:%M -d -3minute`USER=rootPASSWORD=mayboBACKUP_DIR='/home/mysqlbak/ ...

  10. hdu 4715 Difference Between Primes

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=4715 Difference Between Primes Description All you kn ...