POJ 3590 The shuffle Problem
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
#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的更多相关文章
- poj 3590 The shuffle Problem——DP+置换
题目:http://poj.org/problem?id=3590 bzoj 1025 的弱化版.大概一样的 dp . 输出方案的时候小的环靠前.不用担心 dp 时用 > 还是 >= 来转 ...
- POJ 3590 The shuffle Problem [置换群 DP]
传送门 $1A$太爽了 从此$Candy?$完全理解了这种$DP$做法 和bzoj1025类似,不过是求最大的公倍数,并输出一个字典序最小的方案 依旧枚举质因子和次数,不足的划分成1 输出方案从循环长 ...
- POJ - 1978 Hanafuda Shuffle
最初给牌编号时,编号的顺序是从下到上:洗牌时,认牌的顺序是从上到下.注意使用循环是尽量统一“i”的初始化值,都为“0”或者都为“1”,限界条件统一使用“<”或者“<=”. POJ - 19 ...
- POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询)
POJ.3468 A Simple Problem with Integers(线段树 区间更新 区间查询) 题意分析 注意一下懒惰标记,数据部分和更新时的数字都要是long long ,别的没什么大 ...
- poj 3590(dp 置换)
题目的意思是对于序列1,2,...,n.要你给出一种字典序最小的置换使得经过X次后变成最初状态,且要求最小的X最大. 通过理解置换的性质,问题可以等价于求x1,x2,..,xn 使得x1+x2+... ...
- POJ 3468.A Simple Problem with Integers-线段树(成段增减、区间查询求和)
POJ 3468.A Simple Problem with Integers 这个题就是成段的增减以及区间查询求和操作. 代码: #include<iostream> #include& ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
- 线段树(成段更新) POJ 3468 A Simple Problem with Integers
题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...
- POJ 1152 An Easy Problem! (取模运算性质)
题目链接:POJ 1152 An Easy Problem! 题意:求一个N进制的数R.保证R能被(N-1)整除时最小的N. 第一反应是暴力.N的大小0到62.发现当中将N进制话成10进制时,数据会溢 ...
随机推荐
- Leetcode 5——Median of Two Sorted Arrays
题目: There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the median of the ...
- 1013团队Beta冲刺day7
项目进展 李明皇 今天解决的进度 部分数据传递和使用逻辑测试 林翔 今天解决的进度 服务器端查看个人发布的action,修改已发布消息状态的action,仍在尝试使用第三方云存储功能保存图片 孙敏铭 ...
- 20162328蔡文琛week04
学号 20162328 <程序设计与数据结构>第4周学习总结 教材学习内容总结 本周学习了第四章和第七章,第四章中的内容已经有了初步定的掌握,布尔表达式的运用,是条件和循环语句的基础及数组 ...
- $.ajax 提交数据到后台.
//AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML -- (Extensible Markup Language 可扩展标记语言 ...
- php的格式化数字函数
php格式化数字:位数不足前面加0补足 php格式化数字:位数不足前面加0补足 感谢:http://www.cnblogs.com/xiaochaohuashengmi/archive/2011/12 ...
- Hazelcast分布式
一般的应用正式环境中都不止一台服务器(也就是说是集群的),那么如果只是简单的将数据预加载到内存,那么就会有数据不同步的现象. (更新了其中一台JVM,另一台JVM并不会收到通知从而保持数据同步). 这 ...
- dubbo的InvocationChain
个人觉得dubbo比较好的设计是:一个是Cooma微容器设计.另一个就是InvocationChain了 Cooma微容器是自己实现了一套SPI,方便了用户做扩展: InvocationChain类似 ...
- SiteMesh入门(1-1)SiteMesh是什么?
1.问题的提出 在开发Web 应用时,Web页面可能由不同的人参与开发,因此开发出来的界面通常千奇百怪.五花八门,风格难以保持一致. 为了统一界面的风格,Struts 框架提供了一个标签库Tiles ...
- byte在计算机中的存储方式--Double.byteValue()的输出结果思考
先举三个栗子: 1. public static void main(String[] args) { Double d = new Double(123.56); byte b = d.byteVa ...
- 实现GridControl的行单元格非顺序跳转
用GridControl控件添加数据的时候发现,有一些字段过多但是并不是每个字段都需要用户输入,每个单元格都回车跳转的时候不仅浪费时间,而且用户体验也不好,就需要单元格跳转的时候,不需要的字段可以隔过 ...