Hdoj 5139 Formula offline

Source: Internet
Author: User

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.


Formula Time limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 714 Accepted Submission (s): 255


Problem Description F (N)=( ∏i=1n 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

Sourcebestcoder Round #21



#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include < Vector>using namespace Std;typedef Long Long int ll;const ll maxn=10001000;const ll Mod=1000000007;int a,id;struct QUE{ int x,id; LL ans;} Que[maxn];bool cmp1 (que a,que b) {return a.x<b.x;} BOOL Cmp2 (QUE A,que b) {return a.id<b.id;} LL Now,nowans; LL Nowjiecheng; ll Jiec (ll x) {for (int i=now+1;i<=x;i++) {nowjiecheng= (nowjiecheng*i)%mod;} Now=x;return Nowjiecheng;} int main () {while (scanf ("%d", &a)!=eof) {que[id].x=a; que[id].id=id;id++;} Sort (QUE,QUE+ID,CMP1); now=1,nowans=1,nowjiecheng=1;for (int i=0;i<id;i++) {if (Que[i].x==now) {Que[i].ans=nowans ; }else if (que[i].x>now) {LL temp=1;for (int j=now+1;j<=que[i].x;j++) {temp= (Temp*jiec (j))%mod;} Nowans= (Nowans*temp)%mod;que[i].ans=nowans;}} Sort (QUE,QUE+ID,CMP2); for (int i=0;i<id;i++) {Cout<<que[i].ans<<endl;} return 0;}



Hdoj 5139 Formula offline

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.