链接
题解
先数出来 n n n是第几个,然后就知道了我要求第几个,最后贪心从高位到低位填数字就行了
代码
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 110
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
ll c, f(1);
for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
for(;isdigit(c);c=getchar())x=x*10+c-0x30;
return f*x;
}
ll dig[maxn], c[maxn][maxn], ans[maxn], tot;
ll C(ll n, ll m)
{
if(n<0 or m<0 or m>n)return 0;
return c[n][m];
}
ll count()
{
ll ans=0, i, c[]={0,0};
rep(i,1,tot)c[dig[i]]++;
c[1]--;
for(i=tot-1;i;i--)
{
if(dig[i])ans+=C(c[0]-1+c[1],c[1]);
c[dig[i]]--;
}
return ans+1;
}
ll construct(ll res)
{
ll i, c[]={0,0};
rep(i,1,tot)c[dig[i]]++;
c[1]--;
if(C(c[0]+c[1],c[0])<res)return -1;
ans[tot]=1;
for(i=tot-1;i;i--)
{
ll t=C(c[0]-1+c[1],c[1]);
if(res>t)
{
res-=t;
ans[i]=1;
c[1]--;
}
else
{
ans[i]=0;
c[0]--;
}
}
return 0;
}
int main()
{
ll n, k, T, i, j, kase;
rep(i,0,maxn-1)c[i][0]=1;
rep(i,1,maxn-1)rep(j,1,i)c[i][j]=c[i-1][j-1]+c[i-1][j];
T=read();
rep(kase,1,T)
{
n=read(), k=read();
tot=0;
while(n)
{
dig[++tot]=n&1;
n>>=1;
}
ll t=count();
ll ret=construct(t+k);
printf("Case #%lld: ",kase);
if(ret==-1)printf("IMPOSSIBLE\n");
else
{
ll t=0;
rep(i,1,tot)t=t+(ans[i]<<i-1);
printf("%lld\n",t);
}
}
return 0;
}