题目的表达式化简后就是1! ~ N! 的乘积 Mod 1000000007。
比赛的时候写的是把1~10000000的结果打表存数组里了,结果MLE了。。
由于题目数据组数为100000左右,所以可以把所有的输入存到结构体里面。首先按n的大小排序。
用两个变量进行计算,计算完毕后,再按输入顺序把结构体排序一下。输出即可。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <assert.h>
#include <time.h>
typedef long long LL;
const int mo = 1000000007;
using namespace std;
struct ak
{
int id;
int n;
int ans;
}s[100001];
int cmp1(const void *a, const void *b)
{
struct ak c = *(struct ak *)a;
struct ak d = *(struct ak *)b;
return c.n - d.n;
}
int cmp2(const void *a, const void *b)
{
struct ak c = *(struct ak *)a;
struct ak d = *(struct ak *)b;
return c.id - d.id;
}
int main()
{
#ifdef _Test
freopen("test0.in", "r", stdin);
freopen("test0.out", "w", stdout);
srand(time(NULL));
#endif
int n;
int cnt = 0;
while(~scanf("%d", &n))
{
s[cnt].id = cnt;
s[cnt++].n = n;
}
qsort(s, cnt, sizeof(s[0]), cmp1);
LL a = 1, b = 1;
int num = 0;
for(int i = 1; i <= 10000000; i++)
{
a = a * i % mo;
b = b * a % mo;
while(num < cnt && s[num].n == i)
{
s[num++].ans = b;
}
}
qsort(s, cnt, sizeof(s[0]), cmp2);
for(int i = 0; i < cnt; i++)
{
printf("%d\n", s[i].ans);
}
return 0;
}