首页 > 其他 > 详细

hdu 3123 GCC(数学题)

时间:2014-08-16 19:51:01      阅读:536      评论:0      收藏:0      [点我收藏+]

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3123


GCC

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


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过于庞大,暴力是不可能的,所以必然会有一定的技巧在其中!

思路:当n大于m时 ,n的阶乘中必定包含因数m,所以取余后必定为0。


代码如下:

//#pragma warning (disable:4786)
#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <cstdlib>
#include <climits>
#include <ctype.h>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <deque>
#include <set>
#include <map>
#include <iostream>
#include <algorithm>
using namespace std;
const double eps = 1e-9;
//const double pi = atan(1.0)*4;
const double pi = 3.1415926535897932384626;
#define INF 1e18
//typedef long long LL;
typedef __int64 LL;
int main()
{
    int t;
    LL  m;
    char str[1017];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%I64d",str,&m);
        LL len = strlen(str);
        LL tmp = 1, tt = 0;
        for(int i = 0; i < len; i++)
        {
            tt = tt * 10 + str[i]-'0';
            if(tt >= m)
                break;
        }
        if(tt == 0)
        {
            printf("%I64d\n",1%m);
            continue;
        }
        if(tt == 1)
        {
            printf("%I64d\n",2%m);
            continue;
        }
        LL ans = 2;
        for(int i = 2; i <= m && i <= tt; i++)
        {
            tmp *= i;
            tmp %= m;
            ans += tmp;
            ans %= m;
        }
        printf("%I64d\n",ans);
    }
    return 0;
}


hdu 3123 GCC(数学题),布布扣,bubuko.com

hdu 3123 GCC(数学题)

原文:http://blog.csdn.net/u012860063/article/details/38615459

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!