汉堡汉堡汉题解
2026-05-06 16:26:31
发布于:浙江
6阅读
0回复
0点赞
学生版:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t[51],p[51];
ll c(int n,ll x) {
if(n==0) {
return 1;
}
if(x==1) {
return 0;
}else if(x<=t[n-1]+1) {
return c(n-1,x-1);
}else if(x==t[n-1]+2) {
return p[n-1]+1;
}else if(x<=t[n-1]*2+2) {
return p[n-1]+1+c(n-1,x-(t[n-1]+2));
}else{
return p[n];
}
}
int main()
{
t[0]=1;
p[0]=1;
for(int i=1;i<=50;i++) {
t[i]=2*t[i-1]+3;
p[i]=2*p[i-1]+1;
}
int n;
ll x;
cin>>n>>x;
cout<<c(n,x)<<endl;
return 0;
}
老师版:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll t[51],p[51];
ll count(int n,ll x) {
if(n==0) {
return 1;
}
if(x==1) {
return 0;
}else if(x<=t[n-1]+1) {
return count(n-1,x-1);
}else if(x==t[n-1]+2) {
return p[n-1]+1;
}else if(x<=t[n-1]*2+2) {
return p[n-1]+1+count(n-1,x-(t[n-1]+2));
}else{
return p[n];
}
}
int main()
{
t[0]=1;
p[0]=1;
for(int i=1;i<=50;i++) {
t[i]=2*t[i-1]+3;
p[i]=2*p[i-1]+1;
}
int n;
ll x;
cin>>n>>x;
cout<<count(n,x)<<endl;
return 0;
}
这里空空如也







有帮助,赞一个