HDU 5139 Formula (bestcoder Round #21)

Source: Internet
Author: User

FormulaTime limit:4000/2000 MS (java/others) Memory L imit:32768/32768 K (java/others)
Total submission (s): 306 Accepted Submission (s): 128


Problem Description F (N)=(∏ i = 1 n in? I + 1 )%1000000007
You were expected to write a program to calculate f (n) when a certain n was given.
Inputmulti test Cases (about 100000), every case contains an integer n-a single line.
Please process to the end of file.

[Technical specification]
1≤n≤10000000
Outputfor each n,output f (n) in a.
Sample Input
2100

Sample Output
2148277692

The recursive relationship is quite simple. official:
Find the Law f (1) =1f (2) =1*1*2= (1) * (1*2) =1!*2!f (3) =1*1*1*2*2*3= (1) * (1*2) * (1*2*3) =1!*2!*3! formula can be simplified to
     
      F  (N)=  ∏i=1n (N!)%M  OD
     , directly hit the table does not, will be ultra-memory, data can be processed offline. After a good order from small to large violence. clogc+10000000, C is the number of case.
My solution.
Use an array to save hundreds of results, and then use the number group to save the result of a whole hundred factorial. In this way, each query can be divided by 100 to find the corresponding whole hundred results, and then a maximum of 99 recursion can produce results.
Code:
#include <iostream> #include <cstdio> #include <algorithm>using namespace Std;const int mod= 1000000007;long long A[100010];long long B[100010];int main () {    long long help=1;    Long long ans0=1,ans1=1;    A[0]=1;    B[0]=1;    for (int i=1;i<=10000000;i++)    {        ans1= (ans0*help)%mod;        Help = help* (i+1)%mod;        if (i%100==0)        {            a[i/100]=ans1;            b[i/100]=help;        }        ans0=ans1;    }    int n;    while (~SCANF ("%d", &n))    {        int temp=n/100;       printf ("%i64d\n", A[temp]);        ANS0=ANS1=A[TEMP];        HELP=B[TEMP];        for (int i=temp*100+1;i<=n;i++)       {        ans1= (ans0*help)%mod;        Help = help* (i+1)%mod;        ans0=ans1;       }       printf ("%i64d\n", ans1);    }    return 0;}



HDU 5139 Formula (bestcoder Round #21)

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.