HDU 5139 Formula(递推公式、离线处理)

Formula

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1094    Accepted Submission(s): 384


Problem Description
f(n)=(i=1nini+1)%1000000007
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]
1n10000000
 

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

Sample Input
  
  
2 100
 

Sample Output
  
  
2 148277692
 

Source
 

题目大意:给出n,求函数的值。


解题思路:递推公式:,由于数据比较大,打表会超内存,可以对数据进行离线处理。


代码如下:

#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <sstream>
#include <fstream>
#include <limits.h>
#include <ctime>
#define debug "output for debug\n"
#define pi (acos(-1.0))
#define eps (1e-6)
#define inf (1<<28)
#define sqr(x) (x) * (x)
#define mod 1000000007
using namespace std;
typedef long long ll;
struct node
{
    ll n;
    ll i;
    ll f;
}a[100005];
bool cmp(node a,node b)
{
    return a.n<b.n;
}
bool cmp1(node a,node b)
{
    return a.i<b.i;
}
int main()
{
    ll i,j,k,n;
    while(~scanf("%I64d",&n))
    {
        a[k].i=k;
        a[k++].n=n;
    }
    sort(a,a+k,cmp);
    ll fac=1,f=1;
    for(i=0,j=2;i<k;i++)
    {
        for(;j<=a[i].n;j++)
        {
            fac=fac*j%mod;
            f=f*fac%mod;
        }
        a[i].f=f;
    }
    sort(a,a+k,cmp1);
    for(i=0;i<k;i++)
        printf("%I64d\n",a[i].f);
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值