码迷,mamicode.com
首页 > 其他好文 > 详细

HDU 5139

时间:2014-12-16 20:58:22      阅读:175      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   os   sp   for   on   div   

题目意思是f(n)=1!*2!*...*n!

求f(n);

如果直接开个10^7大的数组,会直接超内存

而时间是2s,所以用时间换内存

就开了两个10^6的数组,n[i]表示(i*10)!,f[i]表示f(i*10);

(注意运算过程就爆int)

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4 const int N=1000002;
 5 const int mod=1000000007;
 6 int n[N],f[N];
 7 void getnum(){
 8     n[0]=f[0]=1;
 9     for(int i=1;i<N;i++){
10         int tem=n[i-1];
11         f[i]=f[i-1];
12         for(int j=(i-1)*10+1;j<=i*10;j++){
13             tem=(long long) tem*j%mod;
14             f[i]=(long long) f[i]*tem%mod;
15         }
16         n[i]=tem;
17     }
18 }
19 int main()
20 {    getnum();
21     int a;
22     while(scanf("%d",&a)!=EOF){
23         if(a%10==0)printf("%d\n",f[a/10]);
24         else {
25             int i,k,j=a%10;
26             i=k=a/10;
27             int tem=n[i],sum=f[i];
28             for(i=i*10+1;i<=a;i++){
29                 tem=(long long) tem*i%mod;
30                 sum=(long long) sum*tem%mod;
31             }
32             printf("%d\n",sum);
33         }
34     }
35     return 0;
36 }

 

HDU 5139

标签:style   blog   io   color   os   sp   for   on   div   

原文地址:http://www.cnblogs.com/Mr-Xu-JH/p/4167933.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!