hdu_3123_GCC
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
InputThe 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.
OutputOutput 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 唬人的题目,当x>=m, x! =0 mod m
我们就是在求小于m的 ∑x!
提取公因式化简得
1+(1*2)+(1*2*3)+.....+(1*2*3*....*x)=1*(1+2*(1+3*(.....x-1*(1+x))))
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int main()
{
char n[105];
int m,t;
scanf("%d",&t);
while(t--)
{
scanf("%s",n);
getchar();
scanf("%d",&m);
int s=strlen(n);
int n1=0;
for(int i=0;i<s;i++)
{
n1*=10;
n1+=n[i]-'0';
if(n1>=m)
break;
}
long long ans;
if(n1>=m)
{
ans=m-1;
for(int i=m-2;i>=1;i--)
{
ans=(ans+1)%m*i%m;
}
}
else
{
ans=n1;
for(int i=n1-1;i>=1;i--)
{
ans=(ans+1)%m*i%m;
}
}
ans++;
cout<<ans%m<<endl; }
}
hdu_3123_GCC的更多相关文章
随机推荐
- kafka自定义序列化器
<kafka权威指南> Customer.java public class Customer { private int customId; private String custome ...
- 重构指南 - 引入参数对象(Introduce Parameter Object)
当一个方法的参数超过3个以上,就可以考虑将参数封装成一个对象.将参数封装成对象后提高了代码的可读性,并且该参数对象也可以供多个方法调用,以后如果增加删除参数,方法本身不需要修改,只需要修改参数对象就可 ...
- DOM 和 BOM
DOM 和 BOM DOM: DOM= Document Object Model,文档对象模型,DOM可以以一种独立于平台和语言的方式访问和修改一个文档的内容和结构.换句话说,这是表示和处理一个H ...
- re模块——正则表达式操作
一.什么是正则? 正则就是用一些具有特殊含义的符号组合到一起(称为正则表达式)来描述字符或者字符串的方法.或者说:正则就是用来描述一类事物的规则.(在Python中)它内嵌在Python中,并通过 r ...
- Homebrew 的使用
安装 /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/ins ...
- Arcobject获得栅格影像的边界
一般的各种遥感影像都是采用某种地理或投影坐标的栅格影像,对于从事影像相关工作的人来说,得到现有影像的覆盖范围是确定研究内容,购买遥感影像的基础.怎么得到这个覆盖范围呢?当然我们可以在ArcGIS或ER ...
- git push & git pull 推送/拉取分支
git push与git pull是一对推送/拉取分支的git命令. git push 使用本地的对应分支来更新对应的远程分支. $ git push <远程主机名> <本地分支名& ...
- 大数据的正确用法你get到了吗?
Azure 镜像市场已于2016年9月21日正式上线,在这个统一的集成平台中,客户可以轻松地浏览.搜索和选择一系列来自第三方的应用和解决方案,并可以将其快速一键部署到 Azure 实例当中. 在移动为 ...
- Anaconda中spyder 安装tensorflow
关于Anaconda的安装就不介绍了,本文主要介绍spyder中安装 tensorflow.废话少说 直接重点: 1.安装好Anaconda之后,找到spyder图标 点击install,等待安装完成 ...
- WAKE-WIN10-SOFT-python配置
1,anaconda下载,安装 1,1 官网:https://www.continuum.io/downloads 下载:https://repo.continuum.io/archive/ win1 ...