题解:CF1982E Number of k-good subarrays
设
问题是怎么算。除了内部的,还有一个端点在左边一个端点在右边的。官方题解是维护了三个值,很复杂。有没有直接算的方法?
我们发现,如果
直接记忆化搜索即可,复杂度是
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll mod = 1e9+7;
map<pair<ll,ll>,ll> mp;
ll sol(ll n,ll k){
if (k==0 || n==1){
return 1;
}
if (mp.count({n,k})){
return mp[{n,k}];
}
ll c=0;
while ((1ll<<c+1)<n){
c++;
}
ll l=sol(1ll<<c,k),r=sol(n-(1ll<<c),k-1);
ll ans=l+r;
if (c<=k){
ll s=min((1ll<<k)-1,n-(1ll<<c));
ans+=s%mod*((1ll<<c)%mod)%mod;
}
mp[{n,k}]=ans%mod;
return ans%mod;
}
void solve(){
ll n,k;
cin>>n>>k;
cout<<sol(n,k)<<"\n";
}
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while (t--){
solve();
}
return 0;
}