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.

Input

Multi test cases (about 100000), every case contains an integer n in a single line.

Please process to the end of file.

[Technical Specification]

Output

For each n,output f(n) in a single line.

Sample Input

2
100

Sample Output

2
148277692

题意:给n,求

思路:直接求的话会TLE,打表会MLE,那么怎么办?分块?  也不行。无法平衡到一个不超过内存和时间的数据。

那么怎么办?  我们想,他会超时是因为什么?  因为算了很多次10000000这种大数字,那么我们能不能只算一次啊~

答案是肯定的。我们把所有的询问都存下来,然后排一个序,每次只在前面的基础上进行递推。最后按照输入顺序输出

这样,我们最多只需要算一遍1~10000000

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define mod 1000000007
#define N 100010
int f[N];
struct Node
{
    int v,id,ans;
}query[N];
long long now;
bool cmp(Node a,Node b)
{
    if(a.v==b.v) return a.id<b.id;
    return a.v<b.v;
}
bool cmp1(Node a,Node b)
{
    return a.id<b.id;
}
int solve(int l,int r,int t)
{
    int ans=t;
    for(int i=l+1;i<=r;i++)
    {
        now=now*i%mod;
        ans=now*ans%mod;
    }
    return ans;
}
int main()
{
    int n,cnt=0,t;
    while(~scanf("%d",&n))
    {
        query[cnt].id=cnt;
        query[cnt++].v=n;
    }
    sort(query,query+cnt,cmp);
    now=1;
    query[0].ans=solve(1,query[0].v,1);
    for(int i=1;i<cnt;i++)
        query[i].ans=solve(query[i-1].v,query[i].v,query[i-1].ans);
    sort(query,query+cnt,cmp1);
    for(int i=0;i<cnt;i++)
        printf("%d\n",query[i].ans);
    return 0;
}
时间: 2024-10-17 12:43:54

hdu 5139 Formula(离线处理)的相关文章

HDU 5139 Formula --离线处理

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

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

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

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 Formula 卡内存

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

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 2874 LCA离线算法

Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 4477    Accepted Submission(s): 1284 Problem Description After World War X, a lot of cities have been seriously damage

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)