BestCoder Round #21 02 Formula题解(规律+离散化)

                
                                                                                 BestCoder Round #21 02 Formula
            
官方题解:
找规律
f(1)=1
f(2)=1*1*2=(1)*(1*2)=1!*2!
f(3)=1*1*1*2*2*3=(1)*(1*2)*(1*2*3)=1!*2!*3!

式子可以简化为 
   
   
    
    f(n)=i=1n(n!)%MOD
   
   ,直接打表不行,会超内存,可以对数据进行离线处理。
排好序之后从小到大暴力。
#include<stdio.h>
#include<math.h>
#include<string.h>
#include<stdlib.h>
#include<vector>
#include<algorithm>
using namespace std;
#define N 1000005
#define ll __int64
const int mod=1000000007;
struct node
{
    int x,id,s;
}a[N];
bool cmp1(node a,node b)
{
    return a.x<b.x;
}
bool cmp2(node a,node b)
{
    return a.id<b.id;
}
int main()
{
    int m=0,i,j,n;
    ll s,s2,s3;
    while(~scanf("%d",&n))
    {
        a[m].id=m;
        a[m++].x=n;
    }
    sort(a,a+m,cmp1);
    s2=s3=1;
    s=1;
    for(i=0,j=2; i<m; i++)
    {
        for(; j<=a[i].x; j++)
        {
            s=s*j;
            if(s>=mod)
                s%=mod;
            s2=(s*s2)%mod;
            s3=s2;
        }
        a[i].s=s2;
    }
    sort(a,a+m,cmp2);
    for(i=0;i<m;i++)
        printf("%d\n",a[i].s);
    return 0;
}
管方题解代码参考自http://www.cnblogs.com/walker11/p/4148947.html
==========================================分=割=线====================================================
我的做法:
离线预处理出0 10 100 1000...i的i!值,然后从n/10+1->n开始推。。。
/* BC code 
*   by qiu
*   12.6
*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N=10000010;
const long long MOD=1000000007;
long long a[1000010], b[1000010];
int main()
{
           long long sum=1, ans=1;
           a[0]=1,b[0]=1;
           for(int i=1; i<=10000005; i++){
                sum = (sum%MOD*i%MOD)%MOD;
                ans = (ans%MOD*sum%MOD)%MOD;
                if(i%10==0){
                     b[i/10]=sum;
                     a[i/10]=ans;
                }
             }
           int n;
    while(scanf("%d", &n)!=EOF){
          ans=a[n/10];
          sum=b[n/10];
          printf("%d\n", n/10*10+1);
       for(int i=n/10*10+1; i<=n; i++){
               sum = (sum%MOD*i%MOD)%MOD;
               ans = (ans%MOD*sum%MOD)%MOD;
        }
        printf("%lld\n", ans);
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值