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的更多相关文章
随机推荐
- jq on方法绑定多个事件
一.jquery为多个选择器绑定同一个事件 $("#start,#end").on("click",function(){ alert("The pa ...
- java参数传递之值传递
一 概述 1.什么是参数传递? 调用方法时向形参传递数据的过程叫做参数传递.在编程语言中有两种传递方式:值传递与引用传递.必须强调的是,这里提到的两种传递方式不是仅限于java使用到的传递方式,而是出 ...
- centOs升级
因为军佬放弃制作Centos7的网络重装包,又Centos7的安装引导和6有较大区别所以,选择曲线救国(技术不行,只能这样乱搞)前文:Centos6.9一键重装包https://ppx.ink/net ...
- List的设置值,跟变量的位置关系(变量范围的变化导致结果差别很大)
我们想要的结果是: [RegnTypeCharge: null,null,null,null,1,null,null,null,null,null,null,null,null,null,null,] ...
- ppt写作的注意事项
PPT推荐字体及大小: 宋体严谨,适合正文,显示最清晰 黑体庄重,适合标题,或者强调区 隶书楷体,艺术性强,不适合投影 如果通过文字排版突出重点:加粗.加大字号.变色 PPT文字太多怎么办? 1.抽象 ...
- (转)Android新的menu实现——ActionMode
Android的menu有多种实现方式,以前写过一篇Android中五种常用的menu(菜单),这里介绍一种新的menu实现方式:ActionMode.ActionMode是Android 3.0以后 ...
- keras 保存模型
转自:https://blog.csdn.net/u010159842/article/details/54407745,感谢分享! 我们不推荐使用pickle或cPickle来保存Keras模型 你 ...
- java中加密的方式概述
加密是用一种特殊的算法改变原有的数据,使未经授权的用户即使获得了已经加密的信息,但不知其解密的方法,仍然无法了解信息的内容. 大体上分为单向加密和双向加密,双向加密又可分为对称加密和非对称加密 ...
- Python中深浅拷贝 垃圾回收与 super继承(六)
1 python拷贝 深拷贝,浅拷贝 与引用三者的区别 import copy a = [1, 2, 3, 4, ['a', 'b']] #原始对象 b = a #赋值,传对象的引用 c = copy ...
- 探索grep命令
grep是linux的一款搜索工具,基本啥linux版本都有自带此工具.下面部分参数若没有说明,表示功力不够,理解不了. 选择匹配器 -E 正则表达式(相当于egrep命令) -F 将pattern当 ...