C. Alya and Permutation

  • 题目看上去过于复杂了,于是可以认定为找规律猜结论的“欺骗题”,通过类似数学归纳法的递推构造方案
点击查看代码
#include <bits/stdc++.h>
using namespace std;
int lowbit(int n)
{
	return n&(-n);
}
void solve(int n)
{
	if(n==5)
	{
		cout<<"2 1 3 4 5";
	}
	else if(n==6)
	{
		cout<<"1 2 4 6 5 3";
	}
	else if(n==lowbit(n))
	{
		solve(n-1);
		cout<<" "<<n;
	}
	else if(n%2==1)
	{
		solve(n-1);
		cout<<" "<<n;
	}
	else
	{
		cout<<n-1<<" "<<n<<" ";
		solve(n-2);
	}
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	int T;
	cin>>T;
	while(T--)
	{
		int n;
		cin>>n;
		if(n%2==1)
		{
			cout<<n<<"\n";
		}
		else
		{
			int m=n;
			while(m!=lowbit(m))
			{
				m-=lowbit(m);
			}
			cout<<2*m-1<<"\n";
		}
		solve(n);
		cout<<"\n";
	}
	return 0;
}
posted @ 2024-10-28 20:04  D06  阅读(12)  评论(0编辑  收藏  举报