hdu 3123 GCC(数学题)

题目链接: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

2009 Asia Wuhan Regional Contest Online

此题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

时间: 2024-10-10 17:25:25

hdu 3123 GCC(数学题)的相关文章

hdu 3123 GCC 阶乘

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3123 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 mat

HDU 3123 GCC

GCC Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 4204    Accepted Submission(s): 1384 Problem Description The GNU Compiler Collection (usually shortened to GCC) is a compiler system produc

hdu 3123

GCC Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 3754    Accepted Submission(s): 1216 Problem Description The GNU Compiler Collection (usually shortened to GCC) is a compiler system produce

hdu 5587 Array 数学题

Array Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=5587 Description Vicky is a magician who loves math. She has great power in copying and creating.One day she gets an array {1}. After that, every day she cop

HDU 1018(数学题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1018 Big Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 27548    Accepted Submission(s): 12526 Problem Description In many applications ve

HDU 6467 简单数学题 【递推公式 &amp;&amp; O(1)优化乘法】(广东工业大学第十四届程序设计竞赛)

传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6467 简单数学题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 308    Accepted Submission(s): 150 Problem Description 已知 F(n)=∑i=1n(i×∑j=inCij) 求 F(n) m

HDU 5019 简单数学题

这道题是说给定A和B,求第C大的公约数. 我们最长求的就是最大公约数了,也就是通常用的GCD算法.但是现在要求第C大的公约数,我们可以想见如果令第C大的公约数为x,最大公约数为g的话,那么x|g的,为什么呢? 我们可以直观的理解,最大公约数其实就是A和B分别进行素因子分解之后,能取到公共素因子乘起来得到的.而对于任意A.B的公约数,那么肯定包含了部分的最大公约数所包含的素因子,因此x|g. 于是要求第C大的公约数,只需要枚举g的因子就行了,我们知道求一个数的因子情况,是可以进行O(sqrt(n)

HDU 1722 Cake 数学题

#include<iostream> #include<stdio.h> #include<math.h> using namespace std; long long gcd(long long a,long long b) { return b==0?a:gcd(b,a%b); } int main() { long long a,b; while(cin>>a>>b)//这道题居然是多CASE= =,题目也不给说一声 cout<<

HDU 4279 Number(数学题,找规律)

题目大意: Here are two numbers A and B (0 < A <= B). If B cannot be divisible by A, and A and B are not co-prime numbers, we define A as a special number of B. For each x, f(x) equals to the amount of x's special numbers. For example, f(6)=1, because 6