题目的意思是对于序列1,2,...,n。要你给出一种字典序最小的置换使得经过X次后变成最初状态,且要求最小的X最大。

通过理解置换的性质,问题可以等价于求x1,x2,..,xn 使得x1+x2+...+xk=n,且GLM(x1,x2,...,xn)最大。

这个就用dp来做,首先求出100内的所有素数记录为prime[1] 到 prime[25]。

状态:dp[i][j] 表示花费了i,且已经使用prime[1] 到 prime[j],的最大值。

转移方程:因为要求最大值,单纯的用素数的积并不能得到最大值,最大值得形式是prime[1]^s1*prime[2]^s2*...*prime[25]^s25

for(int i=;i<=cnt;i++)
{
long long tmp[];
for(int j=;j<=n;j++)
tmp[j]=dp[j];
for(int k=;mypow(saveprime[i],k)<=n;k++)
{
long long tmpnum=mypow(saveprime[i],k);
for(int j=tmpnum;j<=n;j++)
{
dp[j]=max(tmp[j-tmpnum]*tmpnum,dp[j]);
}
}
}
The shuffle Problem
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 1882   Accepted: 626

Description

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
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <stdlib.h>
#include <algorithm>
#include <iostream>
using namespace std; int saveprime[];
long long dp[];
int saveans[];
int mypow(int x,int y)
{
int sum=;
for(int i=;i<=y;i++)
sum*=x;
return sum;
} int main()
{
int cnt=;
for(int i=;i<=;i++)
{
int flag=;
for(int j=;j<i;j++)
{
if(i%j==)
{
flag=;
break;
}
}
if(flag==)
{
saveprime[++cnt]=i;
}
} int T;
cin>>T;
while(T--)
{
int n;
cin>>n;
for(int i=;i<=n;i++)
dp[i]=;
for(int i=;i<=cnt;i++)
{
long long tmp[];
for(int j=;j<=n;j++)
tmp[j]=dp[j];
for(int k=;mypow(saveprime[i],k)<=n;k++)
{
long long tmpnum=mypow(saveprime[i],k);
for(int j=tmpnum;j<=n;j++)
{
dp[j]=max(tmp[j-tmpnum]*tmpnum,dp[j]);
}
}
}
cout<<dp[n];
long long mx=dp[n];
int anscnt=;
int anssum=;
for(int i=;i<;i++)
saveans[i]=;
for(int i=;i<=cnt;i++)
{
int sign=;
while(mx%saveprime[i]==)
{
saveans[anscnt] *= saveprime[i];
mx /= saveprime[i];
sign=;
}
if(sign==)
{
anssum += saveans[ anscnt ];
anscnt++;
}
}
sort(saveans,saveans+anscnt); //printf("\n");
//for(int i=0;i<anscnt;i++)
//printf("%d ",saveans[i]);
//printf("\n");
for(int i=;i<=n-anssum;i++)
{
printf(" %d",i);
}
int pos=n-anssum;
for(int i=;i<anscnt;i++)
{
for(int j=;j<=saveans[i];j++)
printf(" %d",pos+j);
printf(" %d",pos+);
pos+=saveans[i];
}
printf("\n");
}
return ;
}

poj 3590(dp 置换)的更多相关文章

  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. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

  4. poj 1080 dp如同LCS问题

    题目链接:http://poj.org/problem?id=1080 #include<cstdio> #include<cstring> #include<algor ...

  5. poj 1609 dp

    题目链接:http://poj.org/problem?id=1609 #include <cstdio> #include <cstring> #include <io ...

  6. POJ 1037 DP

    题目链接: http://poj.org/problem?id=1037 分析: 很有分量的一道DP题!!! (参考于:http://blog.csdn.net/sj13051180/article/ ...

  7. poj3270 && poj 1026(置换问题)

    | 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...

  8. Jury Compromise POJ - 1015 dp (标答有误)背包思想

    题意:从 n个人里面找到m个人  每个人有两个值  d   p     满足在abs(sum(d)-sum(p)) 最小的前提下sum(d)+sum(p)最大 思路:dp[i][j]  i个人中  和 ...

  9. poj 1485 dp

    转自:http://www.cnblogs.com/kuangbin/archive/2011/11/12/2246407.html [题目大意] 一条公路上有n个旅馆,选出其中k个设置仓库,一个仓库 ...

随机推荐

  1. IIS支持伪静态(windows 2003)

    IIS配置支持伪静态 ISAPI Rewrite 第一:首先我们需要下载一个ISAPI_Rewrite,有精简版和完全版,一般精简版只能对服务器全局进行配置,而完整版可以对服务器上的各个网站进行伪静态 ...

  2. Amixer 控制声音

    amixer set Master XXXX 就可以直接控制主声卡属性 amixer set Master 20 #设置主声卡声音为 20 amixer set Master off #关闭主声卡(静 ...

  3. HAWQ技术解析(四) —— 启动停止

            前面已经完毕了HAWQ的安装部署,也了解了HAWQ的系统架构与主要组件,以下開始使用它. HAWQ作为Hadoop上的一个服务提供给用户,与其他全部服务一样.最主要的操作就是启动.停止 ...

  4. CHAPTER ONE LOAD-BALANCING

    1.1 Synopsis In this part, we will explain how to create a load-balancer withnginxfor a lot of OpenE ...

  5. Flume入门样例

    Flume 作为 cloudera 开发的实时日志收集系统,受到了业界的认可与广泛应用.Flume 初始的发行版本目前被统称为 Flume OG(original generation),属于 clo ...

  6. Lomboz插件的介绍 下载 安装 问题

    http://www.blogjava.net/javaandcc/articles/251334.html Lomboz是Eclipse的一个主要的开源插件(open-source plug-in) ...

  7. 查看电脑CPU核心数的方法

    查看电脑CPU核心数的方法: 方法一: 同时按下[Ctrl+Shift+Esc]组合快捷键打开任务管理器: 点击[性能]就可以看出是几核CPU了: 方法二: 在计算机图标上面点击右键,选择“管理”: ...

  8. Pycharm 安装scrapy

    因为scrapy需要依赖第三方的包,所以直接使用Pycharm安装Scrapy包无法安装成功.网上已经有很多使用cmd安装scrapy的优秀教程,此处不再介绍. 基于下图所示的结构之下向上即可完成sc ...

  9. [UIDevice currentDevice].model

    iPhone Simulator iPad Simulator iPod touch iPad iPhone  

  10. 深入理解Tomcat虚拟文件夹

        我们知道,Web站点中的内容(包含网页,图片,音频文件等)一般都存放在App的文件夹下.但随着站点内容的不断丰富,用户须要把不同层次的内容组织成站点的子文件夹. 我们通常的做法是在站点主文件夹 ...