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



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值