武汉1006 GCC

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
 
我第一个看得题目是最后一个,但看不懂,悲剧啊,之后我重头看一边,看到1006这个题目时,感觉能看懂,就看下去了!
这个题目给的数据很大,但其实都是虚的,因为N很大根本用不上,但M也不小,所以循环只能执行一次,不然超时的,
我做这个题目时,因为没考虑全面,抱着试试的态度写下去的,但结果样例对了,于是太激动了,马上交了,结果WA,
第一次是输入我用WHILE(SCANF("%d",%t)!=EOF)了!本以为就这么错了 就稍微改了下又交了,结果又是WA,
后来看看题目,原来自己太鲁莽了,因为InPUT的要求是输入两个整数,我真当整数在输了,结果当然不可能对了,
因为n的范围到达10^100次;然后我采取字符串输入,不断修改之后终于认为能AC了。但是还是WA了,这下我真蒙了,
到底哪里不对呢。。。。在不断的考虑极端情况下,终于找了错误;那就是当N=0,M=1时 min=n;
所以输出就直接输出SUM(1)了;这当然错了!!还好,最终AC了,功过一半吧!!还耽搁了队友们好长的时间!
  (^.^)
我的代码

#include<iostream>
using namespace std;
__int64 f[1000002];
int main()
{
int m,t;
char n[101],ch;
__int64 i,sum;
scanf("%d",&t);
{
getchar();
while(t--)
{
sum=f[0]=1;

int k=0;
while((ch=getchar())!=' ')
n[k++]=ch;
n[k]='/0';
//puts(n);
scanf("%d",&m);getchar();
//printf("m=%d/n",m);

if( m == 1 ) {printf("0/n");continue;} 
int min;
if(k>=7) min=m;
else
{
int N=atoi(n);
//printf("N=%d/n",N);
if( N > m)
min = m;
else 
min = N;
}
for(i=1;i<=min;i++)
{
f[i]=(i*f[i-1])%m;
sum=(sum+f[i])%m;
}
printf("%I64d/n",sum);
}
}
return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值