hdu 6983 / 2021“MINIEYE杯”中国大学生算法设计超级联赛(3)1011 Segment Tree with Pruning
https://acm.hdu.edu.cn/showproblem.php?pid=6983
节点代表的区间长度只有log种,记忆化搜索
#include<bits/stdc++.h> using namespace std; map<long long,long long>mp; long long dfs(long long n,long long k) { if(n<=k) return 1; if(mp.find(n)==mp.end()) mp[n]=dfs(n>>1,k)+dfs(n-(n>>1),k)+1; return mp[n]; } int main() { int T; long long n,k; scanf("%d",&T); while(T--) { scanf("%lld%lld",&n,&k); mp.clear(); printf("%lld\n",dfs(n,k)); } }