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;
}
}

POJ 3590 The shuffle Problem的更多相关文章

  1. poj 3590 The shuffle Problem——DP+置换

    题目:http://poj.org/problem?id=3590 bzoj 1025 的弱化版.大概一样的 dp . 输出方案的时候小的环靠前.不用担心 dp 时用 > 还是 >= 来转 ...

  2. POJ 3590 The shuffle Problem [置换群 DP]

    传送门 $1A$太爽了 从此$Candy?$完全理解了这种$DP$做法 和bzoj1025类似,不过是求最大的公倍数,并输出一个字典序最小的方案 依旧枚举质因子和次数,不足的划分成1 输出方案从循环长 ...

  3. POJ - 1978 Hanafuda Shuffle

    最初给牌编号时,编号的顺序是从下到上:洗牌时,认牌的顺序是从上到下.注意使用循环是尽量统一“i”的初始化值,都为“0”或者都为“1”,限界条件统一使用“<”或者“<=”. POJ - 19 ...

  4. POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)

    POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...

  5. poj 3590(dp 置换)

    题目的意思是对于序列1,2,...,n.要你给出一种字典序最小的置换使得经过X次后变成最初状态,且要求最小的X最大. 通过理解置换的性质,问题可以等价于求x1,x2,..,xn 使得x1+x2+... ...

  6. POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)

    POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...

  7. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  8. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  9. POJ 1152 An Easy Problem! (取模运算性质)

    题目链接:POJ 1152 An Easy Problem! 题意:求一个N进制的数R.保证R能被(N-1)整除时最小的N. 第一反应是暴力.N的大小0到62.发现当中将N进制话成10进制时,数据会溢 ...

随机推荐

  1. Alpha冲刺第十二天

    Alpha冲刺第十二天 站立式会议 项目进展 项目核心功能,如学生基本信息管理模块,学生信用信息模块,奖惩事务管理模块等等都已完成,测试工作大体结束. 问题困难 项目结束后对项目的阶段性总结缺乏一定的 ...

  2. 201621123050 《Java程序设计》第4周学习总结

    1. 本周学习总结 1.1 写出你认为本周学习中比较重要的知识点关键词 继承.抽象.多态 1.2 尝试使用思维导图将这些关键词组织起来.注:思维导图一般不需要出现过多的字. 1.3 可选:使用常规方法 ...

  3. string类的简洁版实现

    说是原创,差不多算是转载了,我也是看了好多大牛的写法,大牛的建议,自己加一总结,形成代码: 实现一个简洁版的string类,我觉得,下面的也够了:另外需要参见另外的写法: http://blog.cs ...

  4. ( 转 ) WebApiTestClient 的使用

    注意点:需要修改api路由规则,加上action: "api/{controller}/{action}/{id}" 1.如何引入组件 首先,我们需要定义一个API项目 然后通过N ...

  5. 算法题丨3Sum Closest

    描述 Given an array S of n integers, find three integers in S such that the sum is closest to a given ...

  6. 第5章 子网划分和CIDR

    第5章 子网划分和CIDR 划分网络 根据A类.B类或C类网络ID来识别网段具有一些局限性,主要是在网络级别之下不能对地址空间进行任何逻辑细分 如果一个IP是一个A类网络.数据报到达网关,然后传输到9 ...

  7. 【JavaScript中typeof、toString、instanceof、constructor与in】

    JavaScript中typeof.toString.instanceof.constructor与in JavaScript 是一种弱类型或者说动态语言.这意味着你不用提前声明变量的类型,在程序运行 ...

  8. angular2 学习笔记 ( Dynamic Component 动态组件)

    更新 2018-02-07 详细讲一下 TemplateRef 和 ViewContainerRef 的插入 refer : https://segmentfault.com/a/1190000008 ...

  9. LinkedHashMap就这么简单【源码剖析】

    前言 声明,本文用得是jdk1.8 前面已经讲了Collection的总览和剖析List集合以及散列表.Map集合.红黑树还有HashMap基础了: Collection总览 List集合就这么简单[ ...

  10. uva 11636 Hello World!

    https://vjudge.net/problem/UVA-11636 题意: 希望输出n条语句,但是并不会循环,所以只能复制粘贴,一条语句经过复制粘贴后可以变为2条,2条变成4条....每次可以只 ...