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 }
  • 相关阅读:
    几个常见的在线评测系统,及我的点评
    信息学奥赛培训教材推荐
    致,青春
    文明小博客,管理员及网址列表
    NOIP2013,复赛及同步赛,报名及比赛,专题页面
    浅谈浏览器兼容性问题-(1)产生、看待与思考
    前端经典笔试题(腾讯前端,三栏布局)
    浅谈web语义化
    浅谈表现与数据分离
    浅谈w3c标准
  • 原文地址:https://www.cnblogs.com/huangxf/p/4381818.html
Copyright © 2011-2022 走看看