gcc【数学几何】

GCC

Time Limit: 1000MS Memory limit: 65536K

题目描述

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. 

输入

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. 

0 < T <= 20
0 <= n < 10100 (without leading zero) 
0 < m < 1000000 

输出

Output the answer of (0! + 1! + 2! + 3! + 4! + ... + n!)%m. 

示例输入

1 
10 861017 

示例输出

593846

提示

 

来源

山东理工大学第三届ACM程序设计竞赛
代码:
 1 #include<iostream>
 2 #include<stdio.h>
 3 #include<string.h>
 4 #include<stdlib.h>
 5 #include<algorithm>
 6 using namespace std;
 7 int main()
 8 {
 9     int zong;
10     cin>>zong;
11     while(zong--)
12     {
13         char str[10004];
14         int mod,num;
15         cin>>str>>mod;
16         if(strlen(str)>=7)
17             num=1000000;
18         else sscanf(str,"%d",&num);
19         if(num>=mod)
20             num=mod;
21         long long int yushu1=1,i;
22         long long int yushu2=1;
23         for(i=1;i<=num;i++)
24         {
25             yushu1=(yushu1*i)%mod;
26             if(yushu1==0)break;
27             else
28             {
29                 yushu2+=yushu1;
30                 yushu2=yushu2%mod;
31             }
32         }
33          cout<<yushu2%mod<<endl;
34     }
35     return 0;
36 }
View Code

 

posted @ 2013-11-04 22:59  狂盗一枝梅  阅读(336)  评论(0编辑  收藏  举报