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的更多相关文章
随机推荐
- 机器学习kNN
from numpy import * import operator def createDataSet(): group = array([[1.0, 1.1], [1.0, 1.0], [0, ...
- Scrapy框架之代理和cookie
Cookie 是在 HTTP 协议下,服务器或脚本可以维护客户工作站上信息的一种方式.Cookie 是由 Web 服务器保存在用户浏览器(客户端)上的小文本文件,它可以包含有关用户的信息.无论何时用户 ...
- sort属性
学习文章---链接 总结笔记 ①sort是Array.prototype的属性, ②如果不写入参数,则按照转换为的字符串的每个字符的unicode位点进行排序, ③如果传入一个比较函数sort(fun ...
- H5新特性-----type=file文件上传
1.语法 单文件上传:<input type="file" id="file1"/> 多文件上传:<input type="file ...
- 【Android】2.0 Android开发环境的搭建——Eclipse
1.0 eclipse,这可不算谷歌开发的,是IBM公司开发的,而且是Java语言写的 2.0 eclipse怎么下.百度“eclipse”,进入eclipse官网 然后,瞎几把下吧……,实在不行百度 ...
- JavaScript数组求和
<script> function demo(){ var d=document.getElementsByTagName("input")[0].value.spli ...
- 用nodejs做一个svn密码修改页面
linux上配置好svn服务后,管理修改密码还得去手工修改passwd这个文件,略麻烦,其实网上应该有配套的web管理修改界面程序.但我想自己用nodejs写一个,因为用node不用配置复杂的服务器. ...
- 【转】JavaScript 中值得注意的 for 循环
在ECMAScript5(简称 ES5)中,有三种 for 循环,分别是: 简单for循环 for-in forEach 在2015年6月份发布的ECMAScript6(简称 ES6)中,新增了一种循 ...
- javascript面向对象的写法01
类和对象 其他面向对象的语言类的语法是内置的,自然而然的事.javascript中有对象,但没有类的语法,类的实现需要模拟出来. 只需要把对象想成一个容器,里面存放一些属性或方法,把类想象成一个对象的 ...
- Network Request Failed
在react native项目中,有时候调用接口会出现这样的错误提示:“Network Request Failed”. 一.模拟器上报“Network Request Failed”解决办法,也是官 ...