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. FAQ: c++ 函数名后添加 const void function1(int &id) const

    说明这个函数不能修改这个类的成员变量!只能操作自己的参数和内部的范围变量! 括号内的&id,&表示这个变量和C# in和out是一样的,算是一个reference,可以更改值,要想不更 ...

  2. linux第10天 msg消息队列

    cat /proc/sys/kernel/msgmax最大消息长度限制 cat /proc/sys/kernel/msgmnb消息队列总的字节数 cat /proc/sys/kernel/msgmni ...

  3. php 警告

    php.ini error_reporting = E_ALL & ~E_DEPRECATED & ~E_STRICT error_log = /var/log/php-fpm/php ...

  4. 解决Android抽屉被击穿问题

    1,创建一个抽屉DrawerLayout,在V4包下android.support.v4.widget.DrawerLayout,在要设置抽屉的布局中设置android:layout_gravity= ...

  5. 广告点击率 CTR预估中GBDT与LR融合方案

    http://www.cbdio.com/BigData/2015-08/27/content_3750170.htm 1.背景 CTR预估,广告点击率(Click-Through Rate Pred ...

  6. PAT乙级 1009. 说反话 (20)

    1009. 说反话 (20) 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 给定一句英语,要求你编写程序,将句中 ...

  7. MYSQL日期类型的加减更新使用INTERVAL 1 DAY

    例如:UPDATE teachingplan SET teachPlanBeginTime = teachPlanBeginTime +INTERVAL 1 DAY

  8. servlet、genericservlet、httpservlet之间的区别(转)

    当编写一个servlet时,必须直接或间接实现servlet接口,最可能实现的方法就是扩展javax.servlet.genericservlet或javax.servlet.http.httpser ...

  9. TI CC2541的中断优先级设置.

    实际看到的中断优先级设置是这样的:

  10. Array JSON

    Tool: Online jsonviewer JSON: JavaScript Object Notation. JSON is a syntax for storing and exchangin ...