您的位置:首页 > 其它

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

2015-08-27 09:51 423 查看


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=1nin−i+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]

1≤n≤10000000

Output

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

Sample Input

2
100


Sample Output

2
148277692


Source

BestCoder Round #21

题目大意:给出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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: