BestCoder Round #21 1002 Formula

Formula


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


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) i n a single line.
 
Sample Input
  
  
2 100
 
Sample Output
  
  
2 148277692
题解 也说了如果直接打表会MLE 需要数据的你离散化处理,以前我知道一种和数据本身大小无关,只和数据相对大小有关的离散化,比如400,880,25,69
我们可以等价的用3 4 1 2 表示他们的相对大小关系,
但是本体需要用到每个数据,不能这样做,所以 我们可以按照一定的规则,比如这里我每个10个数据记录一个元素,这样每次输入 我就用我记录过的这个数来计算行的数
比如我们记下n=10时的阶乘a[10],还有F(n)即s[10],那我们计算n=13的时候就可以用10这个记录 a[10]*11*12*13算出13的阶乘了,同理F(13)也是
#include <iostream>
#include <algorithm>
#include <string>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include<queue>
#include<stack>
#include<map>
#include<set>
using namespace std;
#define lson ((root<<1)+1)
#define rson ((root<<1)+2)
#define MID ((l+r)>>1)
typedef long long ll;
typedef pair<int,int> P;
#define For(i,t,n) for(int i=(t);i<(n);i++)
const int maxn=1000001;
const int base=1000;
const int inf=999999;
int mod=1000000007;
int a[maxn];//计算阶乘
int s[maxn];//计算答案
int main()
{
    int n,m,i,j,k,t;
    ll ans=1,rec=1;j=0;
    for(i=1;i/10<maxn;i++)
    {
        rec=rec*i%mod;
        ans=ans*rec%mod;
        if(i/10>=j)//每隔10个数记录一次
        {
            a[j]=rec;
            s[j++]=ans;
        }
    }

    while(~scanf("%d",&n))
    {
        rec=a[n/10];//找到记录的元素
        ans=s[n/10];
        for(i=n/10*10+1;i<=n;i++)//用记录过的数来计算这个新的数
        {
            rec=rec*i%mod;
            ans=ans*rec%mod;
        }
        printf("%lld\n",ans);
    }
    return 0;
}








评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值