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
题意:题目的意思很简单,就是让你求出f(n)=(∏i=1nin−i+1)%1000000007这个式子的结果,这里解释一下∏的意思是连乘,和连加那个符号差不多。
分析:首先可以看出这道题是需要找规律的,举例说明当n==3时,我们可以写出这个式子f(3)= 1^3*2^2*3^1 ==> f(3)= 1*1^2*2^2*3^1 ==>
f(3)= 1*1*2*1^1*2^1*3^1 ==> f(3)= 1*1*2*1*2*3,所以最终f(n) = 1!* 2!*3!*……*(n-1)!* n!。
#include<stdio.h> #include<string.h> #include<queue> #include<math.h> #include<stdlib.h> #include<algorithm> using namespace std; const int N=1e5+10; const int INF=0x3f3f3f3f; const int MOD=1e9+7; typedef long long LL; struct node { int n, index; }no[N]; LL F[N]; int cmp(node a, node b) { return a.n < b.n; } int main () { int n, k = 0, i, j; while (scanf("%d", &n) != EOF) { no[k].n = n; no[k].index = k; k++; } ///保存所有的n及其对应的位置 sort(no, no+k, cmp); LL fact, Fact; fact = Fact = 1; j = 1; for (i = 0; i < k; i++) { while (j <= no[i].n) { fact = fact*j%MOD; ///fact计算no[i].n的阶乘 Fact = Fact*fact%MOD; ///Fact计算1!*2!……*(no[i].n-1)!*(no[i].n)! j++; } F[no[i].index] = Fact; ///找到no[i].n对应的位置,保存Fact } for (i = 0; i < k; i++) printf("%lld ", F[i]); return 0; }