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