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的更多相关文章
随机推荐
- 微服务学习笔记二:Eureka服务注册发现
Eureka服务注册发现 服务发现:云端负载均衡,一个基于 REST 的服务,用于定位服务,以实现云端的负载均衡和中间层服务器的故障转移. 1. Service Discovery: Eureka S ...
- javascript对HTML字符转义与反转义
1.背景:在项目中,经常遇到一些字符需要进行转义后才能显示到界面上,如“&”,在界面中显示的是“&”,在html中书写“&”,显示在界面的中的依然是“&”. 这时候,就 ...
- JPA 使用 Specification 复杂查询和 Criteria 查询
转自:https://blog.wuwii.com/jpa-specification.html 前言 JPA 给我们提供了基础的 CURD 的功能,并且用起来也是特别的方便,基本都是一行代码完成各种 ...
- 开通cnblogs博客
开通博客,准备记录学习和开发过程
- 关于scheduleAtFixedRate方法与scheduleWithFixedDelay的使用
一.scheduleAtFixedRate方法 该方法是ScheduledExecutorService中的方法,用来实现周期性执行给定的任务,public ScheduledFuture<?& ...
- sql字段合并与分组聚合
http://blog.csdn.net/cuixianlong/article/details/74024846 1 字段合并 原始数据如下:表名为Employee ID FirstName Las ...
- 3.GlusterFS 企业分布式存储的搭建
3.1 硬件要求 一般选择 2U 机型,磁盘 SATA 盘 4TB,如果 IO 要求比较高,可以采购 SSD 固态硬盘.为了充分保证系统的稳定性和性能,要求所有 glusterfs 服务器硬件配置尽量 ...
- 如何在ubuntu上安装virtualbox的driver module vboxdrv
干净的ubuntu安装完毕之后是没有vboxdrv这个driver module的. 新建一个folder jerry_virtualbox: 使用wget下载virtualbox安装包:https: ...
- ABAP宏的调试
我们都知道高级语言宏一般是无法调试的.但是ABAP的宏例外. 比如我写了下面一段宏,名为insert_table. 执行这段代码,调试器会在第23行停下来. ABAP调试器里有个工具可以用于宏的调试, ...
- Tinkoff Challenge - Final Round (ABC)
A题:从两个保安中间那钞票 #include <bits/stdc++.h> using namespace std; int main() { int a,b,c; scanf(&quo ...