Formula
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 517 Accepted Submission(s): 198
点击打开题目链接
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
BestCoder Round #21
题意:求f(n)的值,
由于N非常大,直接打表会MLE(当时就是打表做的。。。直接贡献MLE);
应该就数据进行离线处理,就是先把数据存起来,然后集中处理数据;
输入完数据后排序O(ClogC);
然后再暴力O(N);
总的时间复杂度O(ClogC+N);
还好不会超时;
代码:
题意:求f(n)的值,
![This is the rendered form of the equation. You can not edit this directly. Right click will give you the option to save the image, and in most browsers you can drag the image onto your desktop or another program.](https://i-blog.csdnimg.cn/blog_migrate/2421e091ec0735380c87fcd9036d60e6.gif)
由于N非常大,直接打表会MLE(当时就是打表做的。。。直接贡献MLE);
应该就数据进行离线处理,就是先把数据存起来,然后集中处理数据;
输入完数据后排序O(ClogC);
然后再暴力O(N);
总的时间复杂度O(ClogC+N);
还好不会超时;
代码:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <vector>
#include <stdlib.h>
#include <string>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <set>
#define inf 0x3f3f3f3f
#define eps 1e-5
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define N 100010
#define mod 1000000007
using namespace std;
struct node
{
int number,order,ans;
} a[N];
int cmp(const void *a,const void *b)
{
node *p1=(node *)a,*p2=(node *)b;
return p1->number-p2->number;
}
int cmp2(const void *a,const void *b)
{
node *p1=(node *)a,*p2=(node *)b;
return p1->order-p2->order;
}
int main()
{
/// freopen("in.txt","r",stdin);
/// freopen("stdout1002.txt","w",stdout);
int n,i,num=0,k,number1;
while(~scanf("%d",&number1))
{
a[num].number=number1;
a[num].order=num;
num++;
}
qsort(a,num,sizeof(a[0]),cmp);
long long answer=1,pre=1,pren=1;
for(i=1,k=0; i<=a[num-1].number; i++)
{
pren=(pren*i)%mod;
answer=(pre*pren)%mod;
pre=answer;
while(a[k].number==i)///重复元素消除
{
a[k].ans=answer;
k++;
}
}
qsort(a,num,sizeof(a[0]),cmp2);
for(i=0;i<num;i++)
{
printf("%d\n",a[i].ans);
}
return 0;
}