5139 找规律+离线处理

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5139

题意:

Formula

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


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
 

Source
 

Recommend
heyang   |   We have carefully selected several similar problems for you:   5141  5140  5138  5137  5136 
 思路: 这道题找出规律来比较简单,会发现
 f(1)=1;
 f(2)=2!
 f(3)=2!*3!
 f(4)=2!*3!*4!
...
然后我就开始打表处理,但是打表卡内存,我就想啊想,会不会有些别的什么数论公式来妙解这题了,事实证明我想多了;
看解题报告,发现可以离线处理(也就是将所有的测试组数据全都先存下来,然后一起处理,再整体输出即可)
以后如果再发现这种卡内存题,如果别的方法无解,就往离线处理这方面去想;
#include <iostream>
#include <stdio.h>
#include <algorithm>
const int N=1e5+100;
const int mod=1000000007;
using namespace std;
typedef long long ll;

struct node
{
 ll x;
 int id;
}a[N];
ll cnt[N];
bool cmp(node n1,node n2)
{
  return n1.x<n2.x;
}

int main()
{
  ll n;
  int ct=0;
  int maxnn=-9999;
  while(cin>>n)
  {
     a[ct].x=n;
     a[ct].id=ct;
     ct++;
     if(n>maxnn)maxnn=n;
  }
  sort(a,a+ct,cmp);
  ll dp=1,sum=1;
  int num=0;
  for(int i=1;i<=maxnn;i++)
  {
    dp=dp*i;
    if(dp>=mod)dp%=mod;
    sum=sum*dp;
    if(sum>=mod)sum%=mod;
    while(i==a[num].x)
    {
      cnt[a[num++].id]=sum;
    }
  }
  for(int i=0;i<ct;i++)
  {
    printf("%I64d\n",cnt[i]);
    getchar();
  }
  return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值