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; }
}

  

最新文章

  1. Windows 10家庭版共享打印机
  2. EF框架step by step(5)—处理实体简单属性
  3. 在Asp.Net MVC中使用ModelBinding构造Array、List、Collection以及Dictionary
  4. win7共享文件夹给局域网
  5. ThinkPHP 分页实现
  6. ruby-gem
  7. rhel5.8-LAMP环境搭建
  8. SQL 存储过程 触发器 事务
  9. 既然函数也是对象,那么为什么this不指向普通函数?
  10. Kubernetes 新时代的宠儿
  11. Mybatis框架基础支持层——解析器模块(2)
  12. MyBatis笔记----多表关联查询两种方式实现
  13. [Hive_3] Hive 建表指定分隔符
  14. Codeforces.612E.Square Root of Permutation(构造)
  15. 下划线字符串camel
  16. 四则运算coding
  17. centos下httpd-2.4的编译安装
  18. C语言中:static与extern对变量和函数的作用
  19. JS 词法作用域 p2
  20. 值得收藏的十二条Jquery随身笔记

热门文章

  1. SQL:Example Uses of the SUBSTRING String Function
  2. drupal对数据库操作
  3. 关于 “VMware Workstation 不可恢复错误- (vcpu-0)”
  4. 安装adobe,路径My Pictures或卷无效。请重新输入。
  5. 安全隐患,你对X-XSS-Protection头部字段理解可能有误
  6. python3乱码问题:接口返回数据中文乱码问题解决
  7. 泛型约束-swift
  8. 【转】JS gzip加密字符串并base64输出
  9. java随机数Reandom(简单介绍)
  10. 【洛谷P1118】数字三角形