【题目】
Formula
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42 Accepted Submission(s): 24
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.
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
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
【分析】
暴力出前6个即可得出规律,设ans[n]-ans[n-1] = a,则ans[n] = n*a[n-1]*ans[n-1];这样就能出结果了,可是n最大有10^7,开数组超内存,而一共只有10^5组数据,所以根据所有数据的n值从小到大排序,再同一算出即可。
【AC CODE】670ms
#include <cstdio>
#include <cstring>
#include <cctype>
#include <cmath>
#include <map>
//#include <unordered_map>
#include <queue>
#include <stack>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
typedef long long LL;
#define rep(i,a,n) for(int i = a; i < n; i++)
#define repe(i,a,n) for(int i = a; i <= n; i++)
#define per(i,n,a) for(int i = n; i >= a; i--)
#define clc(a,b) memset(a,b,sizeof(a))
const int INF = 0x3f3f3f3f, MAXN = 10000000+10, MOD = 1000000007, MAXT = 100000+10;
struct NODE{
int id,a;
NODE(int c = 0, int b = 0){
id = c, a = b;
}
bool operator<(NODE& t)const{
return a < t.a;
}
}p[MAXT];
map<int,LL> vis;
int main()
{
#ifdef SHY
freopen("e:\\1.txt", "r", stdin);
//freopen("e:\\out.txt", "w", stdout);
#endif
int t = 0;
int n;
while(~scanf("%d%*c", &n))
{
p[t] = NODE(t,n);
t++;
}
sort(p,p+t);
LL ans, lans = 1;
LL a,la = 1;
int cnt = 0;
repe(i,1,p[t-1].a)
{
ans = (i*(la)%MOD)*(LL)(lans)%MOD;
lans = ans;
a = i*la%MOD;
la = a;
while(p[cnt].a == i)
{
vis[p[cnt++].id] = ans;
}
}
rep(i,0,t)
printf("%I64d\n", vis[i]);
return 0;
}