Any case of shuffling of n cards can be described with a permutation of 1 to n. Thus there are totally n! cases of shuffling. Now suppose there are 5 cards, and a case of shuffle is <5, 3, 2, 1, 4>, then the shuffle will be:

Before shuffling:1, 2, 3, 4, 5
The 1st shuffle:5, 3, 2, 1, 4
The 2nd shuffle:4, 2, 3, 5, 1
The 3rd shuffle:1, 3, 2, 4, 5
The 4th shuffle:5, 2, 3, 1, 4
The 5th shuffle:4, 3, 2, 5, 1
The 6th shuffle:1, 2, 3, 4, 5(the same as it is in the beginning)

You'll find that after six shuffles, the cards' order returns the beginning. In fact, there is always a number m for any case of shuffling that the cards' order returns the beginning after m shuffles. Now your task is to find the shuffle with the largest m. If there is not only one, sort out the one with the smallest order.

Input

The first line of the input is an integer T which indicates the number of test cases. Each test case occupies a line, contains an integer n (1 ≤ n ≤ 100).

Output

Each test case takes a line, with an integer m in the head, following the case of shuffling.
 

Sample Input

2
1
5
Sample Output
1 1
6 2 1 4 5 3
求出大的循环长度的lcm
与游戏这道题相似,我们考虑把答案lcm分解成质因数的幂
$lcm=p_1^{a_1}*p_2^{a_2}*......$
显然有$p_1^{a_1}+p_2^{a_2}+......<=n$
不足n用1补齐
因为100内素数只有25个,所以搜索就行了
由于要字典序最小,所以我们要使循环数最小
那么我们使在lcm相同情况下,使得大的素数幂数更大就行了
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
int prime[],tot,maxlcm,a[],step[],n,cnt,s[];
bool vis[];
int qpow(int x,int y)
{
int res=;
while (y)
{
if (y&) res=res*x;
x=x*x;
y/=;
}
return res;
}
void dfs(int x,int re,int lcm)
{int i;
if (re<prime[x])
{
if (lcm>maxlcm)
{
memset(a,,sizeof(a));
for (i=;i<x;i++)
a[i]=step[i];
maxlcm=lcm;
}
return;
}
step[x]=;
if (x+<=tot)
dfs(x+,re,lcm);
for (step[x]=prime[x];step[x]<=re;step[x]*=prime[x])
if (x+<=tot)
dfs(x+,re-step[x],lcm*step[x]);
}
int main()
{int T,i,j,p;
cin>>T;
for (i=;i<=;i++)
{
if (vis[i]==)
{
tot++;
prime[tot]=i;
}
for (j=;j<=tot;j++)
{
if (i*prime[j]>) break;
vis[i*prime[j]]=;
if (i%prime[j]==) break;
}
}
while (T--)
{
cin>>n;
maxlcm=;
dfs(,n,);
cnt=;p=;
for (i=;i<=tot;i++)
{
if (a[i]) s[++cnt]=a[i],p+=a[i];
}
for (i=p+;i<=n;i++)
s[++cnt]=;
sort(s+,s+cnt+);
printf("%d",maxlcm);
int last=;
for (i=;i<=cnt;i++)
{
for (j=;j<=s[i]-;j++)
{
printf(" %d",last+j);
}
printf(" %d",last);
last=last+s[i];
}
cout<<endl;
}
}

最新文章

  1. 我的MYSQL学习心得(二) 数据类型宽度
  2. 了解了下spring boot,说一下看法
  3. 预习笔记 多态 --S2 4.3
  4. ZOJ Problem Set - 1002(DFS)
  5. 深入掌握JMS--转
  6. Linux编程基础——GDB(设置断点)(转:TianFang,cnblog: http://www.cnblogs.com/TianFang/archive/2013/01/20/2868889.html)
  7. ZigBee 技术简介
  8. android 管理Bitmap内存 - 开发文档翻译
  9. Effective Java 第三版——24. 优先考虑静态成员类
  10. MyBatis 传入参数之parameterType
  11. mysql My SQL获取某个表的列名
  12. QT槽函数处理线程
  13. 解开一个疑惑,为什么LVS开放的端口,使用netstat或ss命令,不能查找到其监听的端口呢?
  14. ORM some
  15. winfrom图片放大器
  16. [转](SQL Server) Convert a File from utf-8 to ANSI (such as Windows-1252)
  17. BZOJ4239 : 巴士走读
  18. CreateFile DeviceIoControl dwIoControlCode——应用程序与驱动程序通信
  19. bzoj 3928: [Cerc2014] Outer space invaders
  20. 【Java】SSM框架整合 附源码

热门文章

  1. Leetcode 2——Range Sum Query - Mutable(树状数组实现)
  2. 通过cmd窗口导入导出mysql数据库
  3. alpha冲刺第八天
  4. C语言第一次博客作业---顺序机构基础练习
  5. alpha-咸鱼冲刺day3-紫仪
  6. PTA題目的處理(三)
  7. 标准C++类std::string的内存共享和Copy-On-Write(写时拷贝)
  8. ios中录音功能的实现AudioSession的使用
  9. Codeforces 193 D. Two Segments
  10. 进军ABP第一天:ABP理论知识