题目链接:Formula
解题思路:这道题由于内存较小,如果使用打表就会出现MLE,所以这里采用的是离线算法,先把所有的输入存起来并排序,最后一同处理。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = 10000005;
const int mod = 1e9+7;
struct Node
{
int num,id;
bool operator < (const Node a)
{
return num < a.num;
}
}node[maxn];
int cnt,n,ans[maxn];
void solve()
{
__int64 sum = 1;
__int64 tmp = 1;
int st = 1;
for(int i = 0; i < cnt; i++)
{
for(int j = st; j <= node[i].num; j++)
{
tmp = tmp * sum % mod;
sum = sum * (j + 1) % mod;
}
st = node[i].num + 1;
ans[node[i].id] = (int)tmp;
}
for(int i = 0; i < cnt; i++)
printf("%d\n",ans[i]);
}
int main()
{
cnt = 0;
while(scanf("%d",&n)!=EOF)
{
node[cnt].num = n;
node[cnt].id = cnt++;
}
sort(node,node+cnt);
solve();
system("pause");
return 0;
}