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

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的更多相关文章

随机推荐

  1. ACM-线段树

    http://blog.csdn.net/libin56842/article/details/8530197 基础可以看上面这篇文章 风格: maxn是题目给的最大区间,而节点数要开4倍,确切的说… ...

  2. stark——查看页面编辑删除按钮

    一.数据列表 设计查页面,主要展示两部分内容,表头部分和数据部分, 表头通过遍历list_display和默认要显示的编辑和删除字段. 1.数据构建 (1)service/stark.py,后台数据构 ...

  3. SharePoint 2013 - Client OM

    1. 向 executeQueryAsync 中传递参数可以使用以下两种方式,也可以参考这篇文章: var mySuccessCallBack = Function.createCallback(on ...

  4. JSP初学者1

    native2ascii.exe 是 Java 的一个文件转码工具,是将特殊各异的内容 转为 用指定的编码标准文体形式统一的表现出来,它通常位于 JDK_home\bin 目录下, 安装好 Java ...

  5. 文件上传fileupload文件接收

    form表单提交数据到servlet后,使用fileupload进行接收. fileupload 是由 apache 的 commons 组件提供的上传组件.它最主要的工作就是帮我们解析 reques ...

  6. 数据结构与算法分析java——线性表3 (LinkedList)

    1. LinkedList简介 LinkedList 是一个继承于AbstractSequentialList的双向链表.它也可以被当作堆栈.队列或双端队列进行操作.LinkedList 实现 Lis ...

  7. 通过describe命令学习Kubernetes的pod属性详解

    我们可以首先使用kubectl get pods命令得到pod列表,比如我们想研究pod nginx-storage-pod的明细: 使用命令kubectl describe pod nginx-st ...

  8. ubuntu 可以加速播放的播放器SMPlayer 16.4安装

    直接贴命令 sudo apt-add-repository ppa:rvm/smplayer sudo apt-get update sudo apt-get install smplayer smp ...

  9. 利物浦VS曼城,罗指导的先手与工程师的后手

      本想『标题党』一下的,『高速反击遭遇剧情反转,巴西人力挽狂澜绝处逢生!』这种好像看起来比较厉害的标题似乎在大战之后的第五天已显得不合适了. /不害臊    反正晚了,干脆写点能够引起讨论.并且在未 ...

  10. 设计模式——策略模式(Strategy Pattern)

    写在前面: 直接将书中的例子用来作为记录自己学习的成果,不知道这样好不好,如果给原作者带来什么不利的影响不妨告知一声,我及时删掉. UML图: 抽象策略:Strategy package com.cn ...