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. Redis(二)CentOS7安装Redis4.0.10与集群搭建

    一 Redis单机安装 1 Redis下载安装 1.1 检查依赖环境(Redis是C语言开发,编译依赖gcc环境) [root@node21 redis-]$ gcc -v -bash: gcc: c ...

  2. USACO 6.4 The Primes

    The PrimesIOI'94 In the square below, each row, each column and the two diagonals can be read as a f ...

  3. iconfont 在项目中的简单使用

    font-class引用 font-class是unicode使用方式的一种变种,主要是解决unicode书写不直观,语意不明确的问题. 与unicode使用方式相比,具有如下特点: 兼容性良好,支持 ...

  4. poj1979 Red And Black(DFS)

    题目链接 http://poj.org/problem?id=1979 思路 floodfill问题,使用dfs解决 代码 #include <iostream> #include < ...

  5. MySQL性能优化(七·下)-- 锁机制 之 行锁

    一.行锁概念及特点 1.概念:给单独的一行记录加锁,主要应用于innodb表存储引擎 2.特点:在innodb存储引擎中应用比较多,支持事务.开销大.加锁慢:会出现死锁:锁的粒度小,并发情况下,产生锁 ...

  6. ref:详解MYSQL数据库密码的加密方式及破解方法

    ref:https://blog.csdn.net/paul123456789io/article/details/53081921 MySQL数据库用户密码跟其它数据库用户密码一样,在应用系统代码中 ...

  7. python脚本获取本机公网ip

    1.获取公网IP地址方式,访问:http://txt.go.sohu.com/ip/soip 2.python脚本实现: #!/usr/bin/python # -*- coding:utf8 -*- ...

  8. 机器学习之路:python线性回归分类器 LogisticRegression SGDClassifier 进行良恶性肿瘤分类预测

    使用python3 学习了线性回归的api 分别使用逻辑斯蒂回归  和   随机参数估计回归 对良恶性肿瘤进行预测 我把数据集下载到了本地,可以来我的git下载源代码和数据集:https://gith ...

  9. 浏览器在线查看pdf文件 pdf.js的使用教程

    谷歌浏览器可以直接在线查看pdf,而IE内核浏览器无法在线查看,默认是下载. 这里用到的是pdf.js,不仅支持IE内核浏览器,而且兼容手机查看pdf 官网地址:http://mozilla.gith ...

  10. BZOJ 4520 [Cqoi2016]K远点对(KD树)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=4520 [题目大意] 求K远点对距离 [题解] 修改估价函数为欧式上界估价,对每个点进行 ...