HDU 3123-GCC(递推)

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;
}
时间: 2024-10-16 23:25:12

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

hdu 3123 GCC(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3123 GCC Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 3808    Accepted Submission(s): 1234 Problem Description The GNU Compiler Collection (u

HDU 2013(递推&amp;递归_D题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2013 ----------------------------------------------------------------------------------- 题意:每天吃掉一半再多一个,给出第几天吃到只剩一个,求开始时的数量. 思路:递推.按照每天的处理方式反向处理一下,最终得到结果. 代码: #include<cstdio> #include<cstring> #in

HDU 2045(递推&amp;递归_B题)解题报告

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2045 ----------------------------------------------------------------------------------- 题意:3种颜色,方格涂色,从左到右,最后一个方格颜色不能和第一个方格颜色相等,相邻颜色不能相同. 思路:最开始思路想简单了,以为第一个方格3种颜色,第二个方格到倒数第二个方格都是2种选择,最后一个方格一种选择.但是,这种递推需要

hdu 2067(递推或卡特兰数【待补充】)

//解法一:递推#include<iostream> using namespace std; long long d[36][36]; int main() { for(int i=1;i<=35;i++) { d[0][i]=1; } for(int i=1;i<=35;i++) for(int j=i;j<=35;j++) { if(i==j) d[i][j]=d[i-1][j]; else d[i][j]=d[i-1][j]+d[i][j-1]; } int n; i

hdu 1249 三角形 (递推)

三角形 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4390    Accepted Submission(s): 2957 Problem Description 用N个三角形最多可以把平面分成几个区域? Input 输入数据的第一行是一个正整数T(1<=T<=10000),表示测试数据的数量.然后是T组测试数据,每组测试数据只

HDU 2604 Queuing (递推+矩阵快速幂)

[题目链接]:click here~~ [题目大意]: n个人排队,f表示女,m表示男,包含子串'fmf'和'fff'的序列为O队列,否则为E队列,有多少个序列为E队列. [思路]: 用f(n)表示n个人满足条件的结果,那么如果最后一个人是m的话,那么前n-1个满足条件即可,就是f(n-1): 如果最后一个是f那么这个还无法推出结果,那么往前再考虑一位:那么后三位可能是:mmf, fmf, mff, fff,其中fff和fmf不满足题意所以我们不考虑,但是如果是 mmf的话那么前n-3可以找满足

hdu 1723 DP/递推

题意:有一队人(人数 ≥ 1),开头一个人要将消息传到末尾一个人那里,规定每次最多可以向后传n个人,问共有多少种传达方式. 这道题我刚拿到手没有想过 DP ,我觉得这样传消息其实很像 Fibonacci 所举的例子:一个人每次能够跨一或二阶台阶,问到 n 阶台阶有几种跨法.理念是一样的,只不过跨得台阶数可能会变而已.根据 Fibonacci 数列类比过来,每次最多能传 m 人,则 A [ i ] = A [ i - m ] + A [ i - m + 1 ] +  …… + A [ i - 1

hdu 5366 简单递推

记f[i]为在长度是i的格子上面至少放一个木桩的方法数.考虑第i个格子,有放和不放两种情况. 1.如果第i个格子放了一个木桩,则i - 1和i - 2格子上面不能放木桩,方案数为:f[i - 3] + 1 2.如果第i个格子没有放木桩,则方案数为:f[i - 1] 然后递推即可. 1 #include <iostream> 2 using namespace std; 3 4 typedef long long ll; 5 const int N = 61; 6 ll f[N]; 7 8 vo

HDU 5366 dp 递推

The mook jong Accepts: 506 Submissions: 1281 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) 问题描述 ZJiaQ为了强身健体,决定通过木人桩练习武术.ZJiaQ希望把木人桩摆在自家的那个由1*1的地砖铺成的1*n的院子里.由于ZJiaQ是个强迫症,所以他要把一个木人桩正好摆在一个地砖上,由于木人桩手比较长,所以两个木人桩之间地砖必须大于等

hdu 3123 GCC 阶乘

题目链接: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 mat