HDU 3123 GCC Factorial

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3123

The GNU Compiler Collection (usually shortened to GCC) are a Compiler system produced by the GNU Project supporting VAR IOUs 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 4x3x2x1 = 24. (0! is defined as 1, which are a neutral element in multiplication, not multiplied by anything.)
We want you and help us with this formation: (0! + 1! + 2! + 3! + 4! + ... + n!) %mInputThe first line consists of an integer T, indicating the number of test cases.
Each test in a single consists of both integer n and M.OutputOutput The answer of (0! + 1! + 2! + 3! + 4! + ... + n!) %m.
constrains
0 < T <=
0 <= N < 10^100 (without leading zero)
0 < m < 1000000Test Instructions Description: Give n and M, calculate (0!+1!+2!+......+n!)%m. algorithm Analysis: Because the range of the mold m,m is not very large, even if n is large, think if n once greater than M, then (n!)%m is equal to 0, so there is no need to consider the time than M large.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <algorithm>7 #defineINF 0x7fffffff8 using namespacestd;9typedefLong LongLL;Ten  One LL n,m; A Charstr[111]; -  - intMain () the { -     intt; scanf ("%d",&t); -      while(t--) -     { +scanf"%s%i64d",str,&m); -         if(str[0]=='0') {printf ("%i64d\n", (LL)1%M);Continue; } +LL sum=0, len=strlen (str); A          for(LL i= len-7>=0? len-7:0; i<=len-1; i++) sum=sum*Ten+str[i]-'0'; atLL ans=1, a=1; -          for(LL i=1; i<=sum && i<=m; i++) -         { -a=a*i%m; -ans= (ans+a)%m; -         } inprintf"%i64d\n", ans%m); -     } to     return 0; +}

HDU 3123 GCC Factorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.