日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

杭电多校 Harvest of Apples 莫队

發(fā)布時間:2023/12/18 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 杭电多校 Harvest of Apples 莫队 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

問題 B: Harvest of Apples

時間限制:?1 Sec??內(nèi)存限制:?128 MB
提交:?78??解決:?35
[提交] [狀態(tài)] [討論版] [命題人:admin]

題目描述

There are n apples on a tree, numbered from 1 to n.
Count the number of ways to pick at most m apples.

?

輸入

The first line of the input contains an integer T (1≤T≤105) denoting the number of test cases.
Each test case consists of one line with two integers n,m (1≤m≤n≤105).

?

輸出

For each test case, print an integer representing the number of ways modulo 109+7.

?

樣例輸入

2 5 2 1000 500

?

樣例輸出

16 924129523

?

[提交][狀態(tài)]

官方題解

重點就是劃出來的公式,然后套莫隊即可

代碼:

#include <bits/stdc++.h> using namespace std; const int maxn=1e5+100; #define INF 0x3f3f3f; const int mod=1e9+7; typedef long long ll; struct node {ll n,m;int p; }s[maxn]; ll block; ll fac[maxn],inv[maxn]; ll ans[maxn]; ll noww; ll qpow(ll x,ll y){ll ret = 1;while(y){if(y&1) ret = ret * x % mod;x = x * x % mod;y >>= 1;}return ret; } void init(){fac[0] = 1;for(int i=1; i<maxn; i++) fac[i] = fac[i-1] * i % mod;inv[maxn-1] = qpow(fac[maxn-1],mod-2);for(int i=maxn-2; i>=0; i--) inv[i] = inv[i+1] * (i+1) % mod; } ll C(ll x,ll y) {return fac[x] * inv[y] %mod * inv[x-y] % mod; } bool cmp(node x,node y) {if(x.n / block == y.n / block) return x.m < y.m;return x.n / block < y.n / block; } int main() {int t;scanf("%d",&t);block = sqrt(maxn);init();for(int i=0; i<t; i++){scanf("%lld%lld",&s[i].n,&s[i].m);s[i].p = i;}sort(s,s + t,cmp);ll l = 1,r = 1;noww = 2;for(int i=0; i<t; i++){while(l < s[i].n){l++;noww = (2 * noww % mod - C(l-1,r) + mod) % mod;}while(l > s[i].n){noww = (noww + C(l-1,r)) % mod * inv[2] % mod;l--;}while(r < s[i].m){r++;noww = (noww + C(l,r)) % mod;}while(r > s[i].m){noww = (noww + mod - C(l,r)) % mod;r--;}ans[s[i].p] = noww;}for(int i=0; i<t; i++){printf("%lld\n",ans[i]);}return 0; }

剛剛學(xué)了莫隊,趕緊把原來兩個莫隊給補了

轉(zhuǎn)載于:https://www.cnblogs.com/renxiaomiao/p/9642664.html

總結(jié)

以上是生活随笔為你收集整理的杭电多校 Harvest of Apples 莫队的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。