HDU 5139 Formula(BestCoder Round #21)(阶乘、阶乘、阶乘)

Problem Description:

f(n)=(i=1nini+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]
1n10000000
 
Output:
For each n,output f(n) in a single line.
 
Sample Input:
2
100
 
Sample Output:
2
148277692

题意:题目的意思很简单,就是让你求出f(n)=(i=1nini+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\n", F[i]);

    return 0;
}

 

posted @ 2015-11-06 16:06  搁浅の记忆  阅读(180)  评论(0编辑  收藏  举报