GCC

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 3867    Accepted Submission(s): 1272

Problem Description
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
 
Input
The 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.
 
Output
Output 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
 
Source

昨天刚開始看到这道题。认为是一道大数题。開始看到其它人也在做这道题,可是他们都超时了,所以我认为应该就不是普通的大数题那么简单,后来整理了一下思路。发现了能够优化,假设n>=m的时候,后面的数对m取余得到的结果都是0。所以我们想到能够在这里进行优化。可是还是认为要用大数题去做,我就套了一个大数阶乘的模板。输入了100000的測试数据进行測试,发现几秒钟都得不出结果,我们就认为我们的思路有问题就放弃了没去做这道题了,后面看了一下别人的思路,发现我们的那种优化思想还是对的。仅仅只是不要依照那种大数阶乘的思路去做,我们直接一边算阶乘一边进行取余。这样就不会超时了;还是题目做的太少了。非常多知识都还不能灵活运用啊,这个题目应该还是能够解决的。

以下是代码:

#include <cstdio>
#include <cstring>
char s[120];
long long m,n,sum,ans;
int main()
{
int t,len;
scanf("%d",&t);
while(t--)
{
sum=ans=1;
scanf("%s%I64d",s,&m);
len=strlen(s);
/*if(m==1)//这里是考虑 n=0,m=1的那种情况。直接输出1,
{
printf("0\n");
continue;
}*/
if(len>7)
{
n=m-1; //当n>=m时,n!对m取余为0
}
else
{
n=0;
for(int i=0;i<len;i++)//把字符串转化为数字
n=n*10+s[i]-'0';
}
//求阶乘取余
for(int i=1;i<=n;i++)
{
sum=(sum*i)%m;//求阶乘取余
ans=(sum+ans)%m;//阶乘和取余
}
printf("%I64d\n",ans);//考虑到特殊情况,我们还能够直接再最后进行一次取余运算 ans%m;
}
}

hdu 3123 GCC (2009 Asia Wuhan Regional Contest Online)的更多相关文章

  1. HDU 3126 Nova [2009 Asia Wuhan Regional Contest Online]

    标题效果 有着n巫妖.m精灵.k木.他们都有自己的位置坐标表示.冷却时间,树有覆盖范围. 假设某个巫妖攻击精灵的路线(他俩之间的连线)经过树的覆盖范围,表示精灵被树挡住巫妖攻击不到.求巫妖杀死所有精灵 ...

  2. hdu oj 3127 WHUgirls(2009 Asia Wuhan Regional Contest Online)

    WHUgirls Time Limit: 3000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total ...

  3. hdu 3123 2009 Asia Wuhan Regional Contest Online

    以为有啥牛逼定理,没推出来,随便写写就A了----题非常水,可是wa了一次 n>=m  则n!==0 注意的一点,最后 看我的凝视 #include <cstdio> #includ ...

  4. hdu3231 (三重拓扑排序) 2009 Asia Wuhan Regional Contest Hosted by Wuhan University

    这道题算是我拓扑排序入门的收棺题了,卡了我好几天,期间分别犯了超时,内存溢出,理解WA,细节WA,格式WA…… 题目的意思大概是在一个三维坐标系中,有一大堆矩形,这些矩形的每条棱都与坐标轴平行. 这些 ...

  5. HDU 3269 P2P File Sharing System(模拟)(2009 Asia Ningbo Regional Contest)

    Problem Description Peer-to-peer(P2P) computing technology has been widely used on the Internet to e ...

  6. HDU 4433 locker 2012 Asia Tianjin Regional Contest 减少国家DP

    意甲冠军:给定的长度可达1000数的顺序,图像password像锁.可以上下滑动,同时会0-9周期. 每个操作.最多三个数字连续操作.现在给出的起始序列和靶序列,获得操作的最小数量,从起始序列与靶序列 ...

  7. HDU 5073 Galaxy 2014 Asia AnShan Regional Contest 规律题

    推公式 #include <cstdio> #include <cmath> #include <iomanip> #include <iostream> ...

  8. 2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest

    2017-2018 ACM-ICPC, Asia Tsukuba Regional Contest A Secret of Chocolate Poles 思路:暴力枚举黑巧克力的个数和厚黑巧克力的个 ...

  9. zoj 3659 Conquer a New Region The 2012 ACM-ICPC Asia Changchun Regional Contest

    Conquer a New Region Time Limit: 5 Seconds      Memory Limit: 32768 KB The wheel of the history roll ...

随机推荐

  1. ZooKeeper实践:(2)配置管理

    一. 前言     配置是每个程序不可或缺的一部分,配置有多重方式:xml.ini.property.database等等,从最初的单机环境到现在的分布式环境. 1. 以文件的格式存储配置,修改任何都 ...

  2. php读取xml中cdata部分方法

    本例使用php的simplexml:XML(eventtrackdata.xml'): <eventdata> <event> <date>2012.05.11&l ...

  3. C# 文件下载断点续传

    C# 文件下载断点续传的一个类 using System; using System.Collections.Generic; using System.Linq; using System.Text ...

  4. java内存溢出分析工具

    http://www.cnblogs.com/preftest/archive/2011/12/08/2281322.html java内存溢出分析工具:jmap使用实战 在一次解决系统tomcat老 ...

  5. Kafka/Metaq设计思想学习笔记

    http://my.oschina.net/geecoodeer/blog/194829

  6. curl之采集QQ空间留言

    目录 主要流程解析 注意事项 扩展 完整代码示例 采集效果一览 主要流程解析 首先,打开浏览器登录QQ空间并访问留言列表 由于QQ空间的链接是https,curl方式请求https链接需要突破http ...

  7. thinphp中auth认证方法使用

    一.获取Auth类1.ThinkPHP3.1.3完整版:http://www.thinkphp.cn/down/338.html2.OneThink1.0正式版:https://github.com/ ...

  8. Android IntentService

    IntentService简要分析 IntentService 继承自 android.app.Service.内部实现极其简单. 首先在 onCreate()中去开启了一个 HandlerThrea ...

  9. [leetcode tree]101. Symmetric Tree

    Given a binary tree, check whether it is a mirror of itself (ie, symmetric around its center). For e ...

  10. BZOJ3611 HEOI2014大工程

    先建虚树,然后统计答案. 对于这个两点间最大值和最小值的操作我参考了hzwer的代码. 建虚树时注意判自环 By:大奕哥 #include<bits/stdc++.h> using nam ...