hdoj--3123--GCC(技巧阶乘取余)
GCC
Total Submission(s): 4473 Accepted Submission(s): 1472
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
Each test on a single consists of two integer n and m.
Constrains
0 < T <= 20
0 <= n < 10^100 (without leading zero)
0 < m < 1000000
1
10 861017
593846
好吧,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(技巧阶乘取余)的更多相关文章
- 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 ...
- bjfu1238 卡特兰数取余
题目就是指定n,求卡特兰数Ca(n)%m.求卡特兰数有递推公式.通项公式和近似公式三种,因为要取余,所以近似公式直接无法使用,递推公式我简单试了一下,TLE.所以只能从通项公式入手. Ca(n) = ...
- LightOJ - 1282 - Leading and Trailing(数学技巧,快速幂取余)
链接: https://vjudge.net/problem/LightOJ-1282 题意: You are given two integers: n and k, your task is to ...
- hud 3123 GCC
题目 输入:n 和 mod 输出: Output the answer of (0! + 1! + 2! + 3! + 4! + ... + n!)%m. Constrains 0 < T &l ...
- float 对整形的取余运算
取余是针对整形的,但是有时候一些特殊需求,我们需要 float 型对整形取下余数.比如,将角度化到 0- 360 范围内. 今天看到 lua 的实现方式: a % b == a - math.floo ...
- JS利用取余实现toggle多函数
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- salesforce 零基础学习(四十三)运算取余
工作中遇到一个简单的小问题,判断两个数是否整除,如果不整除,获取相关的余数. 习惯java的我毫不犹豫的写下了代码 public Boolean isDivisibility(Integer divi ...
- PHP大数(浮点数)取余
一般我们进行取余运算第一个想到的就是用百分号%,但当除数是个很大的数值,超出了int范围时,这样取余就不准确了. php大数(浮点数)取余函数 /** * php大数取余 * * @param int ...
- JAVA中取余(%)规则和介绍
在java中%的含义为取余. java :a%b 数学公式a%b=a-(a/b)*b
随机推荐
- [NOIP2012提高组]疫情控制
题目:洛谷P1084.codevs1218.Vijos P1783. 题目大意:有一棵n个节点的,根为1的带权树和m支军队.每支军队可以在一个点上停下,那么从1开始就不能经过这个点了.现在有m支军队已 ...
- nginx开启gzip压缩后导致apk包下载不能正常安装
最后更新时间:2019/4/27 nginx一般都会开启gzip压缩,以提升传输性能. 配置如下: gzip on; gzip_comp_level 2; gzip_min_length 1k; gz ...
- layui框架下的摸索与学习
一.table表格内的查询 1.单个条件查询: 主要代码: <%-- Created by IntelliJ IDEA. User: Administrator Date: 2019/1/14 ...
- Ubuntu16.04 lnmp 环境搭建
Ubuntu16.04 lnmp 环境搭建 nginx 安装 sudo apt-add-repository ppa:nginx/stablesudo apt-add-repository ppa:o ...
- 常用的pdf工具
https://www.ilovepdf.com/zh-cn https://smallpdf.com/cn/compress-pdf https://www.pdf2go.com/zh/compre ...
- ZOJ 3365 Integer Numbers
Integer Numbers Time Limit: 1000ms Memory Limit: 32768KB This problem will be judged on ZJU. Origina ...
- iOS_第3方类库_側滑选项卡SlideSwitchView
终于效果: 用法: 1.在主控制器中创建一个[SlideSwitchView]的对象实例,并用成员变量记住,如_slideSwitchView,并加入到self.view 2.设置[_slideSwi ...
- hdu 1075 What Are You Talking About(map)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- poj_2187凸包,暴力解法
#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #i ...
- m_Orchestrate learning system---二十四、thinkphp里面的ajax如何使用
m_Orchestrate learning system---二十四.thinkphp里面的ajax如何使用 一.总结 一句话总结:其实ajax非常简单:前台要做的事情就是发送ajax请求过来,后台 ...