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 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的部分不需要求,取模后等于0了嘛

#include <iostream>
#include <stdio.h>
#include <string>
#include <cstring>
#include <cmath>
#define N 1000009
using namespace std;

char s[N];
int m;

int main()
{
    int ca;

    scanf("%d",&ca);
    while(ca--)
    {
        int ff=0;
        scanf("%s%d",s,&m);

        int len=strlen(s);
        int num=0;

        if(len>=7)
        ff=m;
        else
        {
            for(int i=0;i<len;i++)
            {
                int a=s[i]-'0';
                num+=a;
                num*=10;
            }
            num/=10;

            if(num>=m)
            {
                ff=m-1;
            }
            else ff=num;
        }

        __int64 ans=0;
        __int64 t=1;

        for(int i=1;i<=ff;i++)
        {
            t*=i;
            t%=m;
            ans+=t;
            ans%=m;

        }
        ans=(ans+1)%m;//如果开始ans取1后再求mod的话会wa,但是最后加1再取mod就过了

        printf("%I64d\n",ans);

    }
    return 0;
}
时间: 2024-10-02 19:53:12

HDU 3123 GCC的相关文章

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 (u

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 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 2564 词组缩写

词组缩写 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 14487    Accepted Submission(s): 4705 Problem Description 定义:一个词组中每个单词的首字母的大写组合称为该词组的缩写.比如,C语言里常用的EOF就是end of file的缩写. Input 输入的第一行是一个整数T,表示一

HDU3123:GCC(同余模简单题)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3123 题意很简单,就是同余模的简单应用. 代码如下: #include <iostream> #include <algorithm> #include <stdio.h> #include <string.h> typedef __int64 ll; using namespace std; char a[10010]; int n,m; ll zan,sum;

hdu 5098 Smart Software Installer 拓扑排序or记忆化搜索

Smart Software Installer Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 416    Accepted Submission(s): 124 Problem Description The software installation is becoming more and more complex. An a

教你小小JAVA爬虫爬到HDU首页(只为学习)

记得以前刷hdu的时候总是发现有人能一分钟内提交很多次 而且还ac  感觉不可思议.后来百度搜了搜 原来是网络爬虫 带着这一届acm成员集训的时候有成员说hdu炸了  一直判断中  我就说是爬虫...然后就想了想 自己能不能写而且要用java写 结果一天没吃饭(因为感觉我能做出来)...直到晚上6点多 测试一个数据 A了  那种感觉   无法用言语表达...比吃了任何东西都快乐. 一直对自己的自学能力很有自信的  可怕的就是没有方向  四处乱撞  唉 给做题的人说声对不起  我知道这是不道德的行

HDU 1284 钱币兑换问题 母函数、DP

题目链接:HDU 1284 钱币兑换问题 钱币兑换问题 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5467    Accepted Submission(s): 3123 Problem Description 在一个国家仅有1分,2分,3分硬币,将钱N兑换成硬币有很多种兑法.请你编程序计算出共有多少种兑法. Input 每行只有

hdu 5084 前缀和预处理

http://acm.hdu.edu.cn/showproblem.php?pid=5084 给出矩阵M,求M*M矩阵的r行c列的数,每个查询跟前一个查询的结果有关. 观察该矩阵得知,令ans = M*M,则 ans[x][y] = (n-1-x行的每个值)*(n-1+y列的每个值),即: ans[x][y] = t[y] * t[2*n - 2 - x] +....+ t[y + n - 1]*t[n - 1 - x] 每一对的和为定值2*n-2-x-y,然后就是求每对i+j的前缀和(所有i+