hdu 5139 数学+离线暴力求解

题目:

hdu 5139 数学+离线暴力求解_第1张图片

找规律可得,f(n) = 1!*2!*........*n!.

我们只要读入所有数据,排好序,离线递推处理即可

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define MAX 1007000
#define MOD 1000000007

using namespace std;

struct Node
{
    int value;
    int id;
    long long ans;
}num[MAX];

bool cmp1 ( const Node& a , const Node& b )
{
    return a.value < b.value;
}

bool cmp2 ( const Node& a , const Node& b )
{
    return a.id < b.id;
}

int main ( )
{
    int cnt = 0;
    while ( ~scanf ( "%d" , &num[cnt].value ) )
        num[cnt].id = cnt++;
    sort ( num , num + cnt , cmp1 );
    int index = 0;
    long long temp1 = 1 , temp2 = 1;
    for ( int i = 1 ; i <= 10000000 ; i++ )
    {
        temp1 = ( temp1 * (long long )(i) )%MOD ;
        temp2 = ( temp1 * temp2 )%MOD;
        while ( num[index].value == i ) num[index++].ans = temp2;        
    }
    sort ( num , num + cnt , cmp2 );
    for ( int i = 0 ; i < cnt ; i++ )
        printf ( "%I64d\n" , num[i].ans );
}


你可能感兴趣的:(C++,算法,数学,离线)