zoukankan      html  css  js  c++  java
  • 2018 Multi-University Training Contest 4 B Harvest of Apples 莫队算法

    Problem B. Harvest of Apples

    Problem Description
    There are n apples on a tree, numbered from 1 to n.
    Count the number of ways to pick at most m apples.
     
    Input
    The first line of the input contains an integer T (1T105) denoting the number of test cases.
    Each test case consists of one line with two integers n,m (1mn105).
     
    Output
    For each test case, print an integer representing the number of ways modulo 109+7.
     
    Sample Input
    2 5 2 1000 500
     
    Sample Output
    16 924129523
     
    Source

    题解: 

     1 #include <bits/stdc++.h>
     2 #define ll long long
     3 using namespace std;
     4 const ll mod = 1e9+7;
     5 const int N = 1e5+10;
     6 struct mo{
     7     int n, k,id;
     8 }q[N];
     9 ll Be[N], fac[N], inv[N], res[N], unit, t;
    10 vector<mo> lst[N];
    11 ll pow_mod(ll x, ll n){  
    12     ll res=1;  
    13     while(n>0){  
    14         if(n&1)res=res*x%mod;  
    15         x=x*x%mod;  
    16         n>>=1;  
    17     }  
    18     return res;  
    19 }
    20 bool cmp(mo a, mo b) {
    21     return a.n < b.n;
    22 }
    23 ll C(int a, int b) {
    24     return fac[a] * inv[b] % mod * inv[a-b] % mod;
    25 }
    26 int main() {
    27     int mx = 100000; unit = sqrt(mx);
    28     fac[0] = 1;for(int i = 1; i <= mx; i ++) fac[i] = fac[i-1] * i %mod, Be[i] = i/unit + 1;
    29     inv[mx] = pow_mod(fac[mx], mod-2); for(int i = mx-1; i >= 0; i --) inv[i] = inv[i+1] *(i+1) % mod;
    30     scanf("%lld", &t);
    31     for(int i = 1; i <= t; i ++) {
    32         scanf("%d%d",&q[i].n,&q[i].k), q[i].id = i;
    33         lst[Be[q[i].k]].push_back(q[i]);
    34     }
    35     for(int i = 1; i <= mx; i ++) {
    36         if(lst[i].size()) {
    37             sort(lst[i].begin(),lst[i].end(), cmp);
    38             ll val = 0, in = lst[i][0].n, ik = -1;
    39             for(auto e : lst[i]) {
    40                 while(in < e.n) val = (val + val + mod - C(in++, ik)) % mod;
    41                 while(ik < e.k) val = (val + C(in, ++ik)) % mod;
    42                 while(ik > e.k) val = (val + mod - C(in, ik--)) % mod;
    43                 res[e.id] = val;                
    44             }
    45         }
    46     }
    47     for(int i = 1; i <= t; i ++) printf("%lld
    ",res[i]);
    48     return 0;
    49 }
  • 相关阅读:
    [转]使用Composer管理PHP依赖关系
    Php环境下载(PHPNow)安装
    精美的 ( Android, iPhone, iPad ) 手机界面设计素材和线框图设计工具
    八款强大的jQuery图片滑块动画插件
    JavaScript prototype.js提升JavaScript开发效率
    JS Message 网页消息提醒
    Vis.js图表插件
    动态算法学习
    GPS功能:百度路书自定义【轨迹回放】
    CSS美化页面滚动条
  • 原文地址:https://www.cnblogs.com/xingkongyihao/p/9407727.html
Copyright © 2011-2022 走看看