zoukankan      html  css  js  c++  java
  • 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 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
     
    题意描述:给出n和m,计算(0!+1!+2!+......+n!)%m。
    算法分析:由于是模m,m的范围不是很大,即使n很大,想想如果n一旦大于m,那么(n!)%m就等于0了,所以没有必要考虑比m大的时候了。
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<cstdlib>
     5 #include<cmath>
     6 #include<algorithm>
     7 #define inf 0x7fffffff
     8 using namespace std;
     9 typedef long long LL;
    10 
    11 LL n,m;
    12 char str[111];
    13 
    14 int main()
    15 {
    16     int t ;scanf("%d",&t) ;
    17     while (t--)
    18     {
    19         scanf("%s%I64d",str,&m);
    20         if (str[0]=='0') {printf("%I64d
    ",(LL)1%m);continue; }
    21         LL sum=0,len=strlen(str);
    22         for (LL i= len-7>=0 ? len-7 : 0 ;i<=len-1 ;i++) sum=sum*10+str[i]-'0';
    23         LL ans=1,a=1;
    24         for (LL i=1 ;i<=sum && i<=m ;i++)
    25         {
    26             a=a*i%m;
    27             ans=(ans+a)%m;
    28         }
    29         printf("%I64d
    ",ans%m);
    30     }
    31     return 0;
    32 }
  • 相关阅读:
    java获取指定目录中的文件列表
    windows下从命令行编译运行程序
    《操作系统》第3讲:“启动、中断、异常和系统调用”总结
    系统调用
    操作系统启动干啥了?
    基于LM2733升压电路设计
    Xftp远程连接出现“无法显示文件夹”的问题补充
    gVim 的安装以及配置
    ePx Server 服务端接口文档
    UE4C++(1)添加组件
  • 原文地址:https://www.cnblogs.com/huangxf/p/4381818.html
Copyright © 2011-2022 走看看