Fork me on GitHub

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;
}
View Code

 

posted @ 2014-12-06 22:10  whatbeg  阅读(231)  评论(0编辑  收藏  举报