GCC

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

Total Submission(s): 3993    Accepted Submission(s): 1304

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
求 n!%m=(n-1)!*n%m; 即f(n)=f(n-1)*n%m;
并且 (0!+1!+...n!)%m 假设n>m 那么以后的k!都能够被m整除。。忽略
然后 。。递推就能够了
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <list>
#define ll long long
using namespace std;
const int INF=1<<27;
const int maxn=1010;
int main()
{
int t;
char n[200];int m;
scanf("%d",&t);
while(t--)
{
scanf("%s %d",n,&m);
int tem;
if(strlen(n)>=7)
tem=999999;
else
sscanf(n,"%d",&tem);
tem=min(m,tem);
ll f1=1%m,fn,ans=f1;
for(int i=1;i<=tem;i++)
{
fn=f1*i%m;
ans+=fn;
f1=fn;
}
printf("%lld\n",ans%m);
}
return 0;
}

HDU 3123-GCC(递推)的更多相关文章

  1. HDU 4747 Mex 递推/线段树

    题目链接: acm.hdu.edu.cn/showproblem.php?pid=4747 Mex Time Limit: 15000/5000 MS (Java/Others)Memory Limi ...

  2. 致初学者(四):HDU 2044~2050 递推专项习题解

    所谓递推,是指从已知的初始条件出发,依据某种递推关系,逐次推出所要求的各中间结果及最后结果.其中初始条件或是问题本身已经给定,或是通过对问题的分析与化简后确定.关于递推的知识可以参阅本博客中随笔“递推 ...

  3. HDU 2604 Queuing(递推+矩阵)

    Queuing [题目链接]Queuing [题目类型]递推+矩阵 &题解: 这题想是早就想出来了,就坑在初始化那块,只把要用的初始化了没有把其他的赋值为0,调了3,4个小时 = = 本题是可 ...

  4. HDU - 2604 Queuing(递推式+矩阵快速幂)

    Queuing Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  5. hdu 1723 DP/递推

    题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacc ...

  6. hdu 1249 三角形 (递推)

    三角形 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submiss ...

  7. [hdu 2604] Queuing 递推 矩阵快速幂

    Problem Description Queues and Priority Queues are data structures which are known to most computer ...

  8. HDU 5366 dp 递推

    The mook jong Accepts: 506 Submissions: 1281 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65 ...

  9. hdu 3123 GCC 阶乘

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3123 The GNU Compiler Collection (usually shortened t ...

  10. HDU 4455 Substrings --递推+树状数组优化

    题意: 给一串数字,给q个查询,每次查询长度为w的所有子串中不同的数字个数之和为多少. 解法:先预处理出D[i]为: 每个值的左边和它相等的值的位置和它的位置的距离,如果左边没有与他相同的,设为n+8 ...

随机推荐

  1. memcache 安装教程

    memcached:官网http://memcached.org/ 说明:memcached本身没有Linux版本,网上的windows 版本都是个人开发的. memcached和memcache区别 ...

  2. 跨平台的C++应用和UI开发库 QT

    跨平台的C++应用和UI开发库 QT 运行环境: 授权方式:BSD 软件大小:M 下载量:3912 更新日期:2012-04-05 来源地址: 联系作者:Linux     Qt是诺基亚开发的一个跨平 ...

  3. 迎接 Windows Azure 和 DNN 挑战,几分钟内快速构建网站!

    编辑人员注释:本文章由高级商务策划师兼开发平台推广者 Neeti Gupta 撰写. 曾几何时,构建一个简单的网站需要耗费好几个月的时间.在过去,.NET 开发人员和设计社区的一些成员使用 DNN(以 ...

  4. 拓扑图弹力布局呈现Flickr图片搜索结果:智能创新

    十年前有值得分享的图片我都存在Flickr上,可惜yahoo收购了Flickr之后堕落​好多年,最近yahoo在梅姐带领下Flickr团队终于恢复了生机,个人免费存储空间扩充到了1T,界面用户体验也有 ...

  5. poj3006---素数筛法

    #include <stdio.h> #include <stdlib.h> ];//以后都用宏定义 MAX int main()//如要将包括1000000在内的打表,数组就 ...

  6. Linux学习之二十、循环

    原文地址:http://vbird.dic.ksu.edu.tw/linux_basic/0340bashshell-scripts_5.php 回圈 (loop) 除了 if...then...fi ...

  7. 任意给定一个正整数N,求一个最小的正整数M(M>1),使得N*M的十进制表示形式里只含有1和0。

    题目:任意给定一个正整数N,求一个最小的正整数M(M>1),使得N*M的十进制表示形式里只含有1和0. 解法一:暴力求解.从1开始查找M,然后判断M*N=X这个数字是否只含有0,1. 解法二:由 ...

  8. wikioi-1039-数的划分

    将整数n分成k份,且每份不能为空,任意两种划分方案不能相同(不考虑顺序). dp[i][j]:把数i分成k分的方案数 则:dp[i][j]=sum(dp[i-j][t])(t>=1&&a ...

  9. 关于Resharper的使用经验

    发现Resharper这东西真的有点累赘,重构也是,一开始用会有很多莫名其妙的提示.现在的项目用了Resharper,js是很方便,有定位功能,但连TypeScript的js都有了.

  10. 关于if (!cin)以及while (cin >> word)

    首先注意: 1.每一个输入(输出)对象就代表一个输入(输出)流: 2.输入(输出)对象中的流状态成员标记了输入(输出)流当前的状况,当eofbit.badbit.failbit三个标记位均为0时表示流 ...