题目的意思是对于序列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 ;
}

最新文章

  1. 小知识:C#可选参数的一个陷阱
  2. eap-peap/mschapv2
  3. Check list
  4. 使用RMAN对控制文件进行restore
  5. [Windows驱动]驱动包(Driver Packages)
  6. 基于python3的手机号生成脚本
  7. 黑马程序猿_7K面试题之交通灯系统
  8. 为什么用linear regression可以做classification
  9. (1)html初步--表格的使用
  10. 51nod1649- 齐头并进-最短路
  11. redHat linux 修改防火墙设置简略版
  12. Biorhythms(poj1006+中国剩余定理)
  13. python后端将svc文件数据读入数据库具体实现
  14. python 安装包制作
  15. centos7的ssh服务连接
  16. lightoj1038(数学期望dp)
  17. 全网最全的Windows下Anaconda2 / Anaconda3里Python语言实现定时发送微信消息给好友或群里(图文详解)
  18. android webview goback 跳过页面302自动跳转方法
  19. MVC中Controller与View之间的数据传递
  20. 圆点博士 陀螺仪和加速度计MPU6050的单位换算方法

热门文章

  1. Spring boot 数据源未配置异常
  2. 基于Spring开发的DUBBO服务接口测试
  3. node.js开发平台
  4. $ gulp watch 运行出错解决方法
  5. mongoDB 数据导出之mongoexport的用法
  6. Android自定义圆形进度条,完成类似LOFTER效果
  7. tony_LVS DR模式 RealServer 为 Windows客户端配置
  8. linux命令ulimit 系统限制你打开一些资源数
  9. Angular2升级到Angular4
  10. Atitit&#160;&#160;atiMail&#160;atiDns新特性&#160;v2&#160;&#160;q39