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. spynner解析中文页面,应该显示中文字符的地方都是?的解决方案

    这个是底层的QtWebKit相关库里 用的是Qt的QString spynner在将QString转为Python的通用字符串时,没有考虑到中文编码这一块的问题. Python27\Lib\site- ...

  2. js上拉加载下拉刷新

    写在前边: 工作需要,使用ajax在原来的列表下边使用ajax请求后台数据,拼接在列表最下边,在github转了好久,发现了一个bug极多的js刷新插件,尝试了一个下午,就在快放弃的时候,发现下边有留 ...

  3. ArcMap没有工具条和菜单栏的解决方法

    问题: 在安装ArcGIS10后,打开竟然没有菜单栏和工具栏,安装10.1又有,卸了重装10还是没有 解决办法: 在ArcMap的状态栏上双击,在弹出的自定义对话框中,在Toolbars工具栏选择Ma ...

  4. 【Linux】动态链接函数库

    动静区别 1. gcc –c mylib.c –o mylib.o 2. gcc -shared -fPIC mylib.o -o libmylib.so 3. 将制作好的libmylib.so 复制 ...

  5. 《ArcGIS Runtime SDK for Android开发笔记》——问题集:Error:Error: File path too long on Windows, keep below 240 characters

    1.前言 在使用Android Studio开发环境时,经常会爆出以下错误,虽然具体细节内容各有不同,但是说明的都是同一个问题,在windows中使用过长的路径,超过240字符. Error:Erro ...

  6. 网络基础-IP、端口等

    首先来理解一下几个概念.      白帽子:有能力破坏电脑安全但不具恶意目的的黑客.白帽子一般有清楚的定义.道德规范并常常试图同企业合作去改善发现的安全弱点.         正义技术员. 灰帽子:对 ...

  7. javascript面向对象的写法01

    类和对象 其他面向对象的语言类的语法是内置的,自然而然的事.javascript中有对象,但没有类的语法,类的实现需要模拟出来. 只需要把对象想成一个容器,里面存放一些属性或方法,把类想象成一个对象的 ...

  8. 面向对象设计中private,public,protected的访问控制原则及静态代码块的初始化顺序

    第一:private, public, protected访问标号的访问范围. private:只能由          1.该类中的函数          2.其友元函数访问 不能被任何其他访问,该 ...

  9. 64位系统中连接Access数据库文件的一个问题

    近日在windows 7 64位系统中编译以前写的程序,发现在连接Access数据库时总是出现异常,提示“Microsoft.Jet.OLEDB.4.0”未在本机注册,同样的代码在32位的xp系统中却 ...

  10. 微软技术大会直播倒计时7天 | 地球上的IT咖们,正在慢慢向北京聚集

    去年,一句歌词火了:“我在北方的寒夜里,四季如春.”足以见得,北京的冬天,是可以把人冻蒙圈儿的. 然而有一群人,却正在慢慢靠近这样寒冷的北京,7 天后,他们将齐聚在北京. 他们来这里干什么?又是来见谁 ...