HDU 5139

题目意思是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 }
时间: 2024-08-10 01:59:03

HDU 5139的相关文章

hdu 5139 Formula

http://acm.hdu.edu.cn/showproblem.php?pid=5139 思路:这道题要先找规律,f(n)=n!*(n-1)!*(n-2)!.....1!;  不能直接打表,而是离线处理,一次性处理出来. 1 #include <cstdio> 2 #include <cstring> 3 #include <map> 4 #include <algorithm> 5 #define ll long long 6 #define mod

hdu 5139 Formula(离线处理)

Formula Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1200    Accepted Submission(s): 415 Problem Description You are expected to write a program to calculate f(n) when a certain n is given.

HDU 5139 Formula --离线处理

题意就不说了,求公式. 解法: 稍加推导能够得出 : f(n) = n! * f(n-1) , 即其实是求: ∏(n!)  ,盲目地存下来是不行的,这时候看见条件: 数据组数 <= 100000, 那么我们可以离线做,先把他们存下来,然后再从小到大扫一边, 也就是最多10000000次乘法的复杂度.然后离线输出即可. 代码: #include <iostream> #include <cstdio> #include <cstring> #include <

HDU 5139 Formula 卡内存

题目就是求这个 n达到10^7,测试数据组数为10^5 为了防止TLE,一开始把每个n对应的值先求出来,但发现竟然开不了10^7的数组(MLE),然后就意识到这是第一道卡内存的题目... 只能离线做,把每个n从小到大排序,然后从小到大依次求,然后把结果存下来,最后排回去输出. #include<cstdio> #include<cstring> #include<cmath> #include<iostream> #include<algorithm&

hdu 5139 Formula (找规律+离线处理)

Formula Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 206    Accepted Submission(s): 83 Problem Description f(n)=(∏i=1nin−i+1)%1000000007You are expected to write a program to calculate f(n) w

HDU 5139数据离线处理

此题可以找到规律f(n) = 1! * 2! *...*n!, 如果直接打表的话,由于n比较大(10000000),所以会超内存,这时候就要用到离线处理数据,就是先把数据存起来,到最后在暴力一遍求解就行了,代码如下 代码一(超内存): 1 #include <stdio.h> 2 3 const long long mod = 1000000007; 4 const int N = 10000007; 5 long long a[N]; 6 7 int main() 8 { 9 a[0] =

hdu 5139(离线处理+离散化下标)

Formula Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1204    Accepted Submission(s): 415 Problem Description f(n)=(∏i=1nin−i+1)%1000000007You are expected to write a program to calculate f(n)

hdu 5139 Formula(BestCoder Round #21)

Formula                                                             Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 306    Accepted Submission(s): 128 Problem Description f(n)=(∏i=1nin?i+1)%100

hdu 5139 (离线处理)

题意: f(n)=(∏i=1nin?i+1)%1000000007 You are expected to write a program to calculate f(n) when a certain n is given. 思路: 写出前几项,就很容易得出递推式.但是因为n的数据范围是1~10000000,而内存给的小 ,所以并不能直接打表(MLE) 采用离线处理--先读完,再一次性输出. 中间采取了一点小技巧,先把所有读入的输入按照n的大小排序,使得处理答案时的时间复杂度为线性的,不然会