HDU 3123 GCC 数学

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/TheWise_lzy/article/details/82993268

GCC

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

 

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

当n>m时,n!中一定包含m,所以n!%m==0,所以只要考虑到m-1的阶乘就可以了。

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
#include<set>
#include<queue>
#include<vector>
using namespace std;
#define inf 0x3f3f3f3f
#define LL long long
int main()
{
    int t;
	LL m,n,i,len;
    char a[105];
    scanf("%d",&t);
    while(t--)
    {
        scanf("%s%lld",a,&m);
        len=strlen(a);
        n=0;
        for(i=0;i<len;i++)
        {
            n=n*10+(a[i]-'0');
            if(n>=m)
                break;
        }
        LL k=1,sum=1;
        for(i=1;i<=m&&i<=n;i++)
        {
            k=(k*i)%m;
            sum=(sum+k)%m;
        }
        printf("%lld\n",sum%m);
    }
}

猜你喜欢

转载自blog.csdn.net/TheWise_lzy/article/details/82993268