HDU 6311 Harvest of Apples (組合數,莫隊)

來源:互聯網
上載者:User

標籤:turn   style   while   組合數   dea   mat   res   begin   cto   

場上怎麼都想不出來,看了標程想自閉。。。

#include <algorithm>#include <iostream>#include <cstdio>#include <vector>#include <cmath>using namespace std;#define N 100005#define mod 1000000007struct 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\n",res[i]);    }}

標程太秀了,頂禮膜拜。

HDU 6311 Harvest of Apples (組合數,莫隊)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.