HDU 5139 Formula --离线处理

题意就不说了,求公式。

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

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#define Mod 1000000007
#define ll long long
using namespace std;
#define N 100002

ll ans[N];
struct node {
    int n,ind;
}a[N];

int cmp(node ka,node kb) { return ka.n < kb.n; }

int main()
{
    int tot = 0,i,n;
    while(scanf("%d",&n)!=EOF)
    {
        a[++tot].n = n;
        a[tot].ind = tot;
    }
    sort(a+1,a+tot+1,cmp);
    ll fac = 1, f = 1;
    int j = 1;
    for(i=1;i<=tot;i++)
    {
        while(j <= a[i].n)
        {
            fac = fac*j%Mod;
            f = f*fac%Mod;
            j++;
        }
        ans[a[i].ind] = f;
    }
    for(i=1;i<=tot;i++)
        cout<<ans[i]<<endl;
    return 0;
}

时间: 2024-12-13 09:22:06

HDU 5139 Formula --离线处理的相关文章

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(离线处理)

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

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)