zoukankan      html  css  js  c++  java
  • HDU 6333 Harvest of Apples

    场上怎么都想不出来,看了标程想自闭。。。

    #include <algorithm>
    #include <iostream>
    #include <cstdio>
    #include <vector>
    #include <cmath>
    using namespace std;
    #define N 100005
    #define mod 1000000007
    struct query{
        int n,k,i;
    }Q[N];
    bool cmp(const query&a,const query&b){
        return a.n<b.n;
    }
    int reflect[N];///分块
    vector<query>lis[2000];
    int fac[N],inv[N];
    int quick(int a,int b){
        int odd = 1;
        while (b){
            if(b&1)odd = 1ll*odd*a%mod;
            a = 1ll*a*a%mod;
            b>>=1;
        }
        return odd;
    }
    const int mx = 100000;
    void deal(){
        fac[0] = 1;
        for(int i = 1 ; i <= mx ; ++i){
            fac[i] = 1ll*i*fac[i-1]%mod;
        }
        inv[mx] = quick(fac[mx],mod-2);
        for(int i = mx ; i ; --i){
            inv[i-1] = 1ll*i*inv[i]%mod;
        }
    }
    int res[N];
    int C(int n,int m){
        return 1ll*fac[n]*inv[m]%mod*inv[n-m]%mod;
    }
    int main() {
        deal();
        int cent = sqrt(mx);
        int cnt = 1;
        for (int i = 1; i <= mx; i += cent, ++cnt) {
            for (int j = i; j <= mx and j <= i + cent; ++j) {
                reflect[j] = cnt;
            }
        }
        int T;
        scanf("%d",&T);
        for(int i = 1 ; i <= T ; ++i){
            scanf("%d %d",&Q[i].n,&Q[i].k);
            Q[i].i = i;
            lis[reflect[Q[i].k]].push_back(Q[i]);
        }
        for(int i = 1 ; i <= cnt ; ++i)if(!lis[i].empty()){
            sort(lis[i].begin(),lis[i].end(),cmp);
            int val = 0,in = lis[i][0].n,ik = -1;
            for(auto &j : lis[i]){
                while (in<j.n)val = (0ll + val + val + mod - C(in++,ik))%mod;
                while (ik<j.k)val = (val + C(in,++ik))%mod;
                while (ik>j.k)val = (val + mod - C(in,ik--))%mod;
                res[j.i] = val;
            }
        }
        for(int i = 1 ; i <= T ; ++i){
            printf("%d
    ",res[i]);
        }
    }

    标程太秀了,顶礼膜拜。

  • 相关阅读:
    有继承的C++析构函数一定要用virtual
    CUDA vs2010配置
    lambda calculus(1)
    SICP练习1.6 1.16 解答
    用函数式来实现集合
    osx guile编译安装
    skiplist poj2892
    [转]理解 pkgconfig 工具
    专业术语解释
    【转】如何学习linux设备驱动
  • 原文地址:https://www.cnblogs.com/DevilInChina/p/9403578.html
Copyright © 2011-2022 走看看