武汉1006 GCC

来源:互联网 发布:数据分析师考试科目 编辑:程序博客网 时间:2024/04/30 13:25
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
110 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;
}

原创粉丝点击