# POJ 3590 The shuffle Problem 置换+DP

1382人阅读 评论(0)

1.求最小公倍数并不难，动态规划解决。

2.那么求得最小公倍数之后怎么保证置换排序最小呢？

n1+n2+n3+ni = n，所以p1^k1+p2^k2+···+pi^ki <= n。

3.用次用pi^ki个元素构成一个轮换，那么就能保证该置换T的阶最大。

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

#define N 110
#define lint __int64
lint dp[N][N], maxLcm[N];
lint factor[N], fnum;
int p[25] ={2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31,
37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};

inline lint gcd ( lint a, lint b )
{
lint c;
while ( b != 0 )
{
c = a % b;
a = b;
b = c;
}
return a;
}

void split ( int n )
{
int i, j, k, lcm;
memset(dp,0,sizeof(dp));
for ( i = 1; i <= n; i++ )
dp[i][1] = i;

for ( i = 2; i <= n; i++ )
for ( j = 2; j <= i; j++ )
for ( k = 1; k < i && i-k >= j-1; k++ )
{
lcm = dp[i-k][j-1] * k / gcd(dp[i-k][j-1], k);
if ( lcm > dp[i][j] ) dp[i][j] = lcm;
}

for ( i = 1; i <= n; i++ )
{
maxLcm[i] = 0;
for ( j = 1; j <= n; j++ )
if ( dp[i][j] >= maxLcm[i] )
maxLcm[i] = dp[i][j];
}
}

void split ( lint num )
{
fnum = 0;
for ( int i = 0; i < 25; i++ )
{
if ( num % p[i] ) continue;
factor[fnum] = 1;
while ( num % p[i] == 0 )
{
factor[fnum] *= p[i];
num /= p[i];
}
fnum++;
}
}

int main()
{
int t, n;
split(100);
scanf("%d",&t);
while ( t-- )
{
scanf("%d",&n);
split ( maxLcm[n] );
sort(factor,factor+fnum);

int i, j, k, tmp = 0;
for ( i = 0; i < fnum; i++ )
tmp += factor[i];

printf("%I64d",maxLcm[n]);
for ( i = 1; i <= n - tmp; i++ )
printf(" %d",i);

k = n - tmp;
for ( i = 0; i < fnum; i++ )
{
for ( j = 2; j <= factor[i]; j++ )
printf(" %d",k+j);
printf(" %d",k+1);
k += factor[i];
}
printf("\n");
}
return 0;
}


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：480763次
• 积分：8134
• 等级：
• 排名：第2474名
• 原创：342篇
• 转载：49篇
• 译文：0篇
• 评论：34条
评论排行
最新评论