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 1000000007
 7 #define N 100010
 8 using namespace std;
 9
10 ll n;
11 struct node
12 {
13     ll c,id,m;
14     bool operator <(const node &a)const
15     {
16         return id<a.id;
17     }
18 }p[N];
19 bool cmp1(node a,node b)
20 {
21     return a.c<b.c;
22 }
23
24 int main()
25 {
26     int t1=0;
27     while(scanf("%lld",&n)!=EOF)
28     {
29         p[t1].c=n;
30         p[t1].id=t1;
31         t1++;
32     }
33     sort(p,p+t1,cmp1);
34     ll s=1;
35     ll ans=1;
36     int cnt=0;
37     for(int x=1; x<=p[t1-1].c; x++)
38     {
39         s=s*x%mod;
40         ans=ans*s%mod;
41         while(cnt<t1&&p[cnt].c==x)
42         {
43             p[cnt].m=ans;
44             cnt++;
45         }
46     }
47     sort(p,p+t1);
48     for(int i=0; i<t1; i++)
49     {
50         printf("%lld\n",p[i].m);
51     }
52     return 0;
53 }

时间: 2024-12-20 03:30:51

hdu 5139 Formula的相关文章

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 (找规律+离线处理)

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 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 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&

Bestcoder #21&amp;&amp;hdoj 5139 Formula 【另类打表之分块】

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

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)

HDOJ 5139 Formula 离线

找规律 f(1)=1 f(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! 式子可以简化为 f(n)=∏i=1n(n!)%MOD,直接打表不行,会超内存,可以对数据进行离线处理.排好序之后从小到大暴力.ClogC+10000000 ,C为case数目. Formula Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (

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 i