码迷,mamicode.com
首页 > 其他好文 > 详细

HDU5139 Formula (找规律+离线处理)

时间:2014-12-11 12:23:43      阅读:96      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   for   

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=5139


分析:

直接暴力求很明显会超时,因此我们想到了打表但是把表打下来发现超内存了,由于数据有从小到大递推的关系,

可以对数据进行离线处理,排好序之后从小到大暴力即可


代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <algorithm>
using namespace std;

typedef long long LL;

const int mod = 1000000007;

const int maxn = 100000;

struct nod{
    int id,n;
    bool operator <(const struct nod &tmp) const{
        if(n==tmp.n)
            return id < tmp.id;
        return n<tmp.n;
    }
}a[maxn];

LL ans[maxn];

int main()
{
    int cnt=0,n;
    //freopen("out.txt","w",stdout);
    while(~scanf("%d",&n)){
        a[cnt].id = cnt;
        a[cnt++].n = n;
    }
    sort(a,a+cnt);
    LL tmp = 1, tans = 1,j = 1;
    for(int i = 0;i < cnt; i++){
        for(;j <= a[i].n;j++){
            tmp = tmp*j%mod;
            tans = tans*tmp%mod;
        }
        ans[a[i].id]=tans;
    }
    for(int i=0;i<cnt;i++)
        printf("%lld\n",ans[i]);
}


HDU5139 Formula (找规律+离线处理)

标签:style   blog   http   io   ar   color   os   sp   for   

原文地址:http://blog.csdn.net/bigbigship/article/details/41864121

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!