Formula
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 996 Accepted Submission(s): 118
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) i n a single line.
Sample Input
2 100
Sample Output
2 148277692题解 也说了如果直接打表会MLE 需要数据的你离散化处理,以前我知道一种和数据本身大小无关,只和数据相对大小有关的离散化,比如400,880,25,69我们可以等价的用3 4 1 2 表示他们的相对大小关系,但是本体需要用到每个数据,不能这样做,所以 我们可以按照一定的规则,比如这里我每个10个数据记录一个元素,这样每次输入 我就用我记录过的这个数来计算行的数比如我们记下n=10时的阶乘a[10],还有F(n)即s[10],那我们计算n=13的时候就可以用10这个记录 a[10]*11*12*13算出13的阶乘了,同理F(13)也是
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define lson ((root<<1)+1)
#define rson ((root<<1)+2)
#define MID ((l+r)>>1)
typedef long long ll;
typedef pair<int,int> P;
#define For(i,t,n) for(int i=(t);i<(n);i++)
const int maxn=1000001;
const int base=1000;
const int inf=999999;
int mod=1000000007;
int a[maxn];//计算阶乘
int s[maxn];//计算答案
int main()
{
int n,m,i,j,k,t;
ll ans=1,rec=1;j=0;
for(i=1;i/10<maxn;i++)
{
rec=rec*i%mod;
ans=ans*rec%mod;
if(i/10>=j)//每隔10个数记录一次
{
a[j]=rec;
s[j++]=ans;
}
}
while(~scanf("%d",&n))
{
rec=a[n/10];//找到记录的元素
ans=s[n/10];
for(i=n/10*10+1;i<=n;i++)//用记录过的数来计算这个新的数
{
rec=rec*i%mod;
ans=ans*rec%mod;
}
printf("%lld\n",ans);
}
return 0;
}