链接:https://ac.nowcoder.com/acm/contest/4381/C
来源:牛客网
题意
给你一块伪代码块让你求得对应循环的值
解题思路
根据题目意思编排递归函数。
我在这里曾使用了log2发现必定超时,而且使用了各种优化log2的方式发现都不尽人意,然后我看见大佬用了__lg能轻易通过,我不禁心痛了起来
#include "bits/stdc++.h"
using namespace std;
#define int unsigned long long
const int mod = 1000000007;
int dfs(int n, int m, int k)
{
int lim = 1;
lim=63;
while(!(n>>lim&1))--lim;
lim=(1ull<<lim);
int now = m + lim;
if (k == 1)
return now;
int lsum = lim;
if (k <= lsum)
return dfs(lim - 1, m, k - 1);
else
return dfs(n - lim, m + lim, k - lsum);
}
signed main()
{
int q;
cin >> q;
while (q--)
{
int n, m;
cin >> n >> m;
cout << dfs(n, 0, m) << endl;
}
}
#include <bits/stdc++.h>
#define ull unsigned long long
using namespace std;
ull n,m,f,t;
void build(ull n,ull base,ull l)
{
ull q=((ull)1<<(int)(__lg(n)));
if(l==m)
{
f=base+q;
return ;
}
else if(l+q>m)
build(q-1,base,l+1);
else
build(n-q,base+q,l+q);
}
int main()
{
scanf("%llu",&t);
while(t--)
{
f=0;
scanf("%llu%llu",&n,&m);
build(n,0ll,1ll);
printf("%llu\n",f);
}
return 0;
}