hdu 5139(离线处理+离散化下标)

本文针对一个特定的大数运算问题,提出了一种通过排序和离散化的优化方案来提高计算效率的方法。该方法能够有效地处理大量数据输入,并给出具体的C++实现代码。

Formula

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1204    Accepted Submission(s): 415


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] = 1n*2n-1*3n-2...*n ,这里的 F[n] 是可以通过一层循环就求解出来的,但是还是会超时。只能够将所有的询问保存下来,然后排个序,但是数字太大明显不能够作为下标,开个结构体记录下标,然后离散化下标,最后找到下标依次输出。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include <queue>
using namespace std;
typedef long long LL;
const LL mod = 1000000007;
struct Ask
{
    LL v;
    int ori;
} ask[200005];
LL a[200005];
int cmp(Ask a,Ask b){
    return a.v<b.v;
}
int main()
{
    int n,id=1;
    ask[0].v = ask[0].ori = 0;
    while(scanf("%d",&n)!=EOF)
    {
        ask[id].v = n;
        ask[id].ori = id;
        id++;
    }
    sort(ask+1,ask+id,cmp);
    for(int i=1;i<id;i++){
        a[ask[i].ori] = i;
    }
    LL cnt = 1,ans=1;
    for(int i=1; i<id; i++)
    {
        for(int j=ask[i-1].v+1; j<=ask[i].v; j++)
        {
            cnt = cnt*j%mod;
            ans = ans*cnt%mod;
        }
        a[ask[i].ori] = ans;
    }
    for(int i=1;i<id;i++){
        printf("%lld\n",a[i]);
    }
}

 

转载于:https://www.cnblogs.com/liyinggang/p/5667269.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值