GCC

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 3754    Accepted Submission(s):
1216

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!%p =0  那么 (n+1)!%p =0;
就这样。
 #include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef __int64 LL; void solve(int sum,int p)
{
int i;
LL ans = ,cur = % p ;
for(i=;i<=sum;i++)
{
ans = (ans * i)%p;
cur = (cur + ans)%p;
if(ans == )break;
}
printf("%I64d\n",cur);
}
int main()
{
int T;
int i,p;
char a[];
scanf("%d",&T);
while(T--)
{
scanf("%s%d",a,&p);
if(p==)
{
printf("0\n");
continue;
}
int sum = ;
for(i=;a[i]!='\0';i++)
{
sum=sum*+a[i]-'';
if(sum>=p)break;
}
solve(sum,p);
}
return ;
}

hdu 3123的更多相关文章

  1. hdu 3123 GCC 阶乘

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3123 The GNU Compiler Collection (usually shortened t ...

  2. hdu 3123 GCC

    这题分2种情况: 1) n>=m时,k!%m=0(k>=m),所以只需令n=m-1即可: 2) n<m时,正常情况处理即可. ;}

  3. hdu 3123 GCC (2009 Asia Wuhan Regional Contest Online)

    GCC Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Subm ...

  4. hdu 3123 2009 Asia Wuhan Regional Contest Online

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

  5. HDU 3091 - Necklace - [状压DP]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3091 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

  6. poj和hdu部分基础算法分类及难度排序

    最近想从头开始刷点基础些的题,正好有个网站有关于各大oj的题目分类(http://www.pythontip.com/acm/problemCategory),所以写了点脚本把hdu和poj的一些题目 ...

  7. HDU 1284 钱币兑换问题 母函数、DP

    题目链接:HDU 1284 钱币兑换问题 钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  8. HDOJ 2111. Saving HDU 贪心 结构体排序

    Saving HDU Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...

  9. 【HDU 3037】Saving Beans Lucas定理模板

    http://acm.hdu.edu.cn/showproblem.php?pid=3037 Lucas定理模板. 现在才写,noip滚粗前兆QAQ #include<cstdio> #i ...

随机推荐

  1. MySQL 中NULL和空值的区别 (转载 http://blog.sina.com.cn/s/blog_3f2a82610102v4dn.html)

    平时我们在使用MySQL的时候,对于MySQL中的NULL值和空值区别不能很好的理解.注意到NULL值是未知的,且占用空间,不走索引,DBA建议建表的时候最好设置字段是NOT NULL 来避免这种低效 ...

  2. Oracle游标整理二

    1.概念     游标是指向SQL处理的内存区的句柄或指针.当使用一个PL/SQL块来执行DML语句或只返回一行结果的SELECT语句时,系统将自动创建一个隐式游标.如果SQL语句返回多个结果,就必须 ...

  3. 夺命雷公狗---微信开发51----网页授权(oauth2.0)获取用户基本信息接口(1)

    如果用户在微信客户端访问第三方网页,公众号可以通过微信网页授权机制,来获取用户基本信息,从而实现业务逻辑. 一般我们用来“数据采集”,“市场调查”,“投票”,只要授权了第三方网页,微信用户无需注册就可 ...

  4. web负载均衡

    在有些时候进行扩展是显而易见的,比如下载服务由于带宽不足而必须进行的扩展,但是,另一些时候,很多人一看到站点性能不尽如人意,就马上实施负载均衡等扩展手段,真的需要这样做吗?当然这个问题也只有他们自己能 ...

  5. 【转】Delphi利用系统环境变量获取常用系统目录

    Delphi code //譬如 %WINDIR% 是表示系统目录的系统变量, 可以这样获取: var s: string; begin s := GetEnvironmentVariable('WI ...

  6. selenium启动PhantomJS错误

    from selenium import webdriverbrowser = webdriver.PhantomJS(executable_path="D:\PhantomJS\phant ...

  7. 数据库订正脚本性能优化两则:去除不必要的查询和批量插入SQL

    最近在做多数据库合并的脚本, 要将多个分数据库的表数据合并到一个主数据库中. 以下是我在编写数据订正脚本时犯过的错误, 记录以为鉴. 不必要的查询 请看以下语句: regiondb = db.Houy ...

  8. 怎样使用AutoLayOut为UIScrollView添加约束

    1.在ViewController中拖入1个UIScrollView,并为其添加约束 约束为上下左右四边与superview对齐 2.在scrollview中,拖入1个UIView,为了便于区分将其设 ...

  9. 【python cookbook】【数据结构与算法】15.根据字段将记录分组

    问题:想根据字典或者对象实例的某个特定的字典(比如日期)来分组迭代数据 解决方案:itertools.groupby()函数在对数据进行分组时特别有用(前提是先以目标字典进行排序) rows = [ ...

  10. c/c++ 常用的几个安全函数

    _stprintf_s  // 格式化字符串 _vsntprintf_s // 格式化 不定长参数用到