poj 3590(dp 置换)
题目的意思是对于序列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]);
}
}
}
| 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 置换)的更多相关文章
- 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 输出方案从循环长 ...
- hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)
题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...
- poj 1080 dp如同LCS问题
题目链接:http://poj.org/problem?id=1080 #include<cstdio> #include<cstring> #include<algor ...
- poj 1609 dp
题目链接:http://poj.org/problem?id=1609 #include <cstdio> #include <cstring> #include <io ...
- POJ 1037 DP
题目链接: http://poj.org/problem?id=1037 分析: 很有分量的一道DP题!!! (参考于:http://blog.csdn.net/sj13051180/article/ ...
- poj3270 && poj 1026(置换问题)
| 1 2 3 4 5 6 | | 3 6 5 1 4 2 | 在一个置换下,x1->x2,x2->x3,...,xn->x1, 每一个置换都可以唯一的分解为若干个不交的循环 如上面 ...
- Jury Compromise POJ - 1015 dp (标答有误)背包思想
题意:从 n个人里面找到m个人 每个人有两个值 d p 满足在abs(sum(d)-sum(p)) 最小的前提下sum(d)+sum(p)最大 思路:dp[i][j] i个人中 和 ...
- poj 1485 dp
转自:http://www.cnblogs.com/kuangbin/archive/2011/11/12/2246407.html [题目大意] 一条公路上有n个旅馆,选出其中k个设置仓库,一个仓库 ...
随机推荐
- Java8 对多个异步任务进行流水线操作(笔记)
现在我们要对商店商品进行折扣服务.每个折扣代码对应不同的折扣率,使用一个枚举变量Discount.Code来实现这一想法,具体代码如下所示. 以枚举类型定义的折扣代码 /** * 折扣服务api * ...
- Android自己定义button实现长按功能
Android自己定义button实现长按功能 通过自己定义BUTTON,写一个LongTouchBtn类,在按下的时候运行onTouchEvent事件,通过这个事件使用回调函数来实现长按功能! XM ...
- Git——版本管理工具(一)
Git 是一个分布式版本控制工具,它的作者 Linus Torvalds 是这样给我们介绍 Git —— The stupid content tracker(傻瓜式的内容跟踪器) 1. Git 背 ...
- CSS/JavaScript hacks,browserhacks使用
1.网址 http://browserhacks.com/ 2.使用 (1)JavaScript Hacks 浏览器js判断 (2)条件注释hack (3)Media Query Hacks 媒体查询 ...
- 機器學習基石(Machine Learning Foundations) 机器学习基石 作业四 Q13-20 MATLAB实现
大家好,我是Mac Jiang,今天和大家分享Coursera-NTU-機器學習基石(Machine Learning Foundations)-作业四 Q13-20的MATLAB实现. 曾经的代码都 ...
- UINavigationController(二)
在使用UINavigtionController的时候,常常会弄不清哪些属性是UINavigationController的,哪些属性是UIViewController的.并且在更改导航栏和工具栏的样 ...
- springboot学习(八) 使用jpa访问数据库
1.添加maven依赖 <dependency> <groupId>mysql</groupId> <artifactId>mysql-connecto ...
- 【原创】菜鸟版Android 笔记2- Activity
1. Activity介绍 Acitivity在安卓开发中非常重要,他很像Java桌面开发中的JFrame,在MVC模式中属于Controller,一般一个应用程序通常由多个松耦合关系的activit ...
- GitBlit (1)-- 在linux 安装 GitBlit 并运行
Git是一款注重速度.数据完整性.分布式支持和非线性工作流的分布式版本控制工具.Git最初由Linus Torvalds在2005年为Linux内核开发而设计,如今已经成为被广泛接受的版本控制系统. ...
- Eclipse中关于JRE System Library、Web App Libraries的疑惑
当我们在Eclipse中建立java的web工程时,会产生JRE System Library和Referenced Libraries,Web App Libraries不生成,下面会 简要说明一下 ...