题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5139
题意:
Formula
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 400 Accepted Submission(s): 164
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
Recommend
思路: 这道题找出规律来比较简单,会发现
f(1)=1;
f(2)=2!
f(3)=2!*3!
f(4)=2!*3!*4!
...
然后我就开始打表处理,但是打表卡内存,我就想啊想,会不会有些别的什么数论公式来妙解这题了,事实证明我想多了;
看解题报告,发现可以离线处理(也就是将所有的测试组数据全都先存下来,然后一起处理,再整体输出即可)
以后如果再发现这种卡内存题,如果别的方法无解,就往离线处理这方面去想;
#include <iostream>
#include <stdio.h>
#include <algorithm>
const int N=1e5+100;
const int mod=1000000007;
using namespace std;
typedef long long ll;
struct node
{
ll x;
int id;
}a[N];
ll cnt[N];
bool cmp(node n1,node n2)
{
return n1.x<n2.x;
}
int main()
{
ll n;
int ct=0;
int maxnn=-9999;
while(cin>>n)
{
a[ct].x=n;
a[ct].id=ct;
ct++;
if(n>maxnn)maxnn=n;
}
sort(a,a+ct,cmp);
ll dp=1,sum=1;
int num=0;
for(int i=1;i<=maxnn;i++)
{
dp=dp*i;
if(dp>=mod)dp%=mod;
sum=sum*dp;
if(sum>=mod)sum%=mod;
while(i==a[num].x)
{
cnt[a[num++].id]=sum;
}
}
for(int i=0;i<ct;i++)
{
printf("%I64d\n",cnt[i]);
getchar();
}
return 0;
}