题解:CF513B2 Permutations
zhangxiaohan007 · · 题解
思路
这道题其实如果想通了就不难,我们可以发现最小的那个数放在前面和后面那个序列算出来值都是一样的,就像
那该怎么做呢?我们可以这么想:就拿上面那个序列举例,算这个序列总的计算结果可以分解成把
剩下的就很简单了。因为它会按字典序排序,我们就把所有方案给分成
Code
估计这里很多人期待吧
#include<bits/stdc++.h>
#define ll long long
#define db double
using namespace std;
const int inf=2e9;
const db eps=1e-7;
int n,a[200005],totf,totb;
ll pot[70],k;
void dfs(int u,ll add)
{
if(u==n)
{
a[totf]=u;
return ;
}
if(pot[n-u-1]+add>=k)
{
a[totf]=u;
totf++;
dfs(u+1,add);
}
else
{
a[totb]=u;
totb--;
dfs(u+1,add+pot[n-u-1]);
}
}
void solve()
{
cin>>n>>k;
totf=1;
totb=n;
dfs(1,0);
//cerr<<"ok";
for(int i=1;i<=n;i++)cout<<a[i]<<" ";
cout<<"\n";
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
pot[0]=1;
for(int i=1;i<=52;i++)
{
pot[i]=pot[i-1]*2;
}
int t=1;
//cin>>t;
while(t--)
{
solve();
}
}
提交记录
十年OI一场空,不开LONG LONG 见祖宗