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. Oracle EBS-SQL (PO-3):检查期间手工下达的采购订单记录数.sql

    SELECT DECODE(pda.req_distribution_id,'','手工','自动创建') 下达方式, --pda.req_distribution_id                ...

  2. matlab绘制函数

    >> x1=linspace(,*pi,); x2=linspace(,*pi,); x3=linspace(,*pi,); y1=sin(x1); y2=+sin(x2); y3=+si ...

  3. android PopupWindow显示位置

    PopupWindow的显示及位置设置 window.showAtLocation(parent, Gravity.RIGHT | Gravity.BOTTOM, 10,10); 第一个参数指定Pop ...

  4. overload的一点思考

    仅参数类型不同的重载方法,使用过程的一个困惑: 有没有必要使用instanceof方法? package overload.special; public class OverLoadTest { p ...

  5. Xlint以及Java Lint 选项

    Java Lint 选项 Java 编译器的选项包括所谓的标准选项和非标准选项.标准选项是指在当前版本的开发环境中支持,且在未来版本中也将被支持的选项.常用的标准选项比如 -classpath 以及 ...

  6. poj3085

    #include <stdio.h> #include <stdlib.h> int main() { ; scanf("%d",&n); whil ...

  7. Spring、AOP详解

    如何配置AOP查看:Spring.Hello AOP 1.对于拦截规则@Pointcut的介绍: @Pointcut("execution (* cn.raffaello.service.. ...

  8. Android AsyncHttpClient

    Android Asynchronous Http Client A Callback-Based Http Client Library for Android   Tweet Downloadve ...

  9. dropDownList之"请选择",同时设置默认选项

    dropDownList.Items.Insert(0, new ListItem("--请选择--", "-1"));dropDownList.Selecte ...

  10. 集合与lamdba

    List<, , , , }; List<)); /* foreach (int k in iList2) { Console.WriteLine(k.ToString()); } * * ...