题意:将一个数拆成若干数的和使得它们的最小公倍数最大

思路:一个数x可以拆成p1k1 + p2k2 + ... + pnkn形式,其中pi是质数或1。对于最小公倍数最大的情况,一定可以表示成这种形式。令dp[i][j]表示考虑前j个质数来构成i的最大公倍数,那么可以得到如下转移方程:

dp[i][j]=max{dp[i][j-1],dp[i-k][j-1]*k},k=prime[j]t<=i

用滚动数组计算dp数组,同时用一个数组来存具体方案,在dp值更新时更新具体方案。

  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
  7. 7
  8. 8
  9. 9
  10. 10
  11. 11
  12. 12
  13. 13
  14. 14
  15. 15
  16. 16
  17. 17
  18. 18
  19. 19
  20. 20
  21. 21
  22. 22
  23. 23
  24. 24
  25. 25
  26. 26
  27. 27
  28. 28
  29. 29
  30. 30
  31. 31
  32. 32
  33. 33
  34. 34
  35. 35
  36. 36
  37. 37
  38. 38
  39. 39
  40. 40
  41. 41
  42. 42
  43. 43
  44. 44
  45. 45
  46. 46
  47. 47
  48. 48
  49. 49
  50. 50
  51. 51
  52. 52
  53. 53
  54. 54
  55. 55
  56. 56
  57. 57
  58. 58
  59. 59
  60. 60
  61. 61
  62. 62
  63. 63
  64. 64
  65. 65
  66. 66
  67. 67
  68. 68
  69. 69
  70. 70
  71. 71
  72. 72
  73. 73
  74. 74
  75. 75
  76. 76
  77. 77
  78. 78
  79. 79
  80. 80
  81. 81
  82. 82
  83. 83
  84. 84
  85. 85
  86. 86
  87. 87
  88. 88
  89. 89
  90. 90
  91. 91
  92. 92
  93. 93
  94. 94
  95. 95
  96. 96
  97. 97
  98. 98
  99. 99
  100. 100
  101. 101
  102. 102
  103. 103
  104. 104
  105. 105
  106. 106
  107. 107
  108. 108
  109. 109
  110. 110
  111. 111
  112. 112
  113. 113
  114. 114
  115. 115
  1. #pragma comment(linker, "/STACK:10240000")
  2. #include <map>
  3. #include <set>
  4. #include <cmath>
  5. #include <ctime>
  6. #include <deque>
  7. #include <queue>
  8. #include <stack>
  9. #include <vector>
  10. #include <cstdio>
  11. #include <string>
  12. #include <cstdlib>
  13. #include <cstring>
  14. #include <iostream>
  15. #include <algorithm>
  16.  
  17. using namespace std;
  18.  
  19. #define X first
  20. #define Y second
  21. #define pb push_back
  22. #define mp make_pair
  23. #define all(a) (a).begin(), (a).end()
  24. #define fillchar(a, x) memset(a, x, sizeof(a))
  25. #define copy(a, b) memcpy(a, b, sizeof(a))
  26.  
  27. typedef long long ll;
  28. typedef pair<int, int> pii;
  29. typedef unsigned long long ull;
  30.  
  31. #ifndef ONLINE_JUDGE
  32. void RI(vector<int>&a,int n){a.resize(n);for(int i=;i<n;i++)scanf("%d",&a[i]);}
  33. void RI(){}void RI(int&X){scanf("%d",&X);}template<typename...R>
  34. void RI(int&f,R&...r){RI(f);RI(r...);}void RI(int*p,int*q){int d=p<q?:-;
  35. while(p!=q){scanf("%d",p);p+=d;}}void print(){cout<<endl;}template<typename T>
  36. void print(const T t){cout<<t<<endl;}template<typename F,typename...R>
  37. void print(const F f,const R...r){cout<<f<<", ";print(r...);}template<typename T>
  38. void print(T*p, T*q){int d=p<q?:-;while(p!=q){cout<<*p<<", ";p+=d;}cout<<endl;}
  39. #endif
  40. template<typename T>bool umax(T&a, const T&b){return b<=a?false:(a=b,true);}
  41. template<typename T>bool umin(T&a, const T&b){return b>=a?false:(a=b,true);}
  42.  
  43. const double PI = acos(-1.0);
  44. const int INF = 1e9 + ;
  45. const double EPS = 1e-12;
  46.  
  47. /* -------------------------------------------------------------------------------- */
  48.  
  49. const int maxn = 1e4 + ;
  50.  
  51. vector<int> prime;
  52. vector<int> rst[maxn];
  53. int extra[maxn];
  54. bool vis[maxn];
  55. double dp[maxn];
  56.  
  57. void getprimelist() {
  58. for (int i = ; i < maxn; i ++) {
  59. if (!vis[i]) {
  60. prime.pb(i);
  61. for (ll j = (ll)i * i; j < maxn; j += i) {
  62. vis[j] = true;
  63. }
  64. }
  65. }
  66. }
  67.  
  68. void initdp() {
  69. for (int i = ; i < maxn; i ++) dp[i] = 1.0;
  70. for (int j = ; j < prime.size(); j ++) {
  71. for (int i = maxn - ; i; i --) {
  72. for (ll k = prime[j]; k <= i; k *= prime[j]) {
  73. if (umax(dp[i], dp[i - k] * k)) {
  74. rst[i] = rst[i - k];
  75. rst[i].pb(k);
  76. }
  77. }
  78. }
  79. }
  80. for (int i = ; i < maxn; i ++) {
  81. sort(all(rst[i]));
  82. extra[i] = i;
  83. for (int j = ; j < rst[i].size(); j ++) {
  84. extra[i] -= rst[i][j];
  85. }
  86. }
  87. }
  88.  
  89. void work(int n) {
  90. vector<int> ans;
  91. for (int i = ; i < extra[n]; i ++) ans.pb();
  92. for (int i = ; i < rst[n].size(); i ++) ans.pb(rst[n][i]);
  93. int start = ;
  94. for (int i = ; i < ans.size(); i ++) {
  95. for (int j = start + ; j < start + ans[i]; j ++) printf("%d ", j);
  96. printf("%d%c", start, i == ans.size() - ? '\n' : ' ');
  97. start += ans[i];
  98. }
  99. }
  100.  
  101. int main() {
  102. #ifndef ONLINE_JUDGE
  103. freopen("in.txt", "r", stdin);
  104. //freopen("out.txt", "w", stdout);
  105. #endif // ONLINE_JUDGE
  106. getprimelist();
  107. initdp();
  108. int T, n;
  109. cin >> T;
  110. while (T --) {
  111. cin >> n;
  112. work(n);
  113. }
  114. return ;
  115. }

[hdu4713 Permutation]DP的更多相关文章

  1. HDU 4345 Permutation dp

    Permutation Problem Description There is an arrangement of N numbers and a permutation relation that ...

  2. HDU6446 Tree and Permutation(树上DP)

    传送门:点我 Tree and Permutation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (J ...

  3. hdu 3664 Permutation Counting(水DP)

    Permutation Counting Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  4. HDU 3811 Permutation 状压dp

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3811 Permutation Time Limit: 6000/3000 MS (Java/Othe ...

  5. HDU3811 Permutation —— 状压DP

    题目链接:https://vjudge.net/problem/HDU-3811 Permutation Time Limit: 6000/3000 MS (Java/Others)    Memor ...

  6. HDU - 3664 Permutation Counting 排列规律dp

    Permutation Counting Given a permutation a1, a2, … aN of {1, 2, …, N}, we define its E-value as the ...

  7. HDU 4917 Permutation(拓扑排序 + 状压DP + 组合数)

    题目链接 Permutation 题目大意:给出n,和m个关系,每个关系为ai必须排在bi的前面,求符合要求的n的全排列的个数. 数据规模为n <= 40,m <= 20. 直接状压DP空 ...

  8. hdu3664 Permutation Counting(dp)

    hdu3664 Permutation Counting 题目传送门 题意: 在一个序列中,如果有k个数满足a[i]>i:那么这个序列的E值为k,问你 在n的全排列中,有多少个排列是恰好是E值为 ...

  9. HDU 3664 Permutation Counting (DP)

    题意:给一个 n,求在 n 的所有排列中,恰好有 k 个数a[i] > i 的个数. 析:很明显是DP,搞了好久才搞出来,觉得自己DP,实在是太low了,思路是这样的. dp[i][j]表示 i ...

随机推荐

  1. 谁说 Vim 不好用?送你一个五彩斑斓的编辑器!

    相信大家在使用各种各样强大的 IDE 写代码时都会注意到,代码中各种类型的关键字会用独特的颜色标记出来,然后形成一套语法高亮规则.这样不仅美观,而且方便代码的阅读. 而在上古神器 Vim 中,我们通常 ...

  2. Unity 游戏框架搭建 2019 (二十九) 方法所在类命名问题诞生的原因

    我们在整理阶段解决了一些意外的问题.但是这些问题仅仅只是被解决而已,我们并没有去思考过这些问题是为什么产生的?以及在以后我们如何去避免这些问题的产生? 方法所在类的命名问题,最后我们通过方法分类解决了 ...

  3. vue2.x学习笔记(十九)

    接着前面的内容:https://www.cnblogs.com/yanggb/p/12631022.html. 程序化的事件侦听器 在前面的学习中,我们已经知道了[$emit]全局属性的用法,它可以被 ...

  4. jmeter元件的执行顺序

    元件的执行顺序 在同一作用域范围内,test plan中的元件按照以下顺序执行:1) Config Elements--配置元件2) Pre-porcessors --前置处理器3) Timer-定时 ...

  5. C++获取char值

    直接获取内存地址,不需要定义指针类型的方法,(当然也就不需要释放了)USES_CONVERSION;    if (myFun1)    {        CString _input;       ...

  6. Spring5:IOC注解

    使用注解须知: 1:导入约束:导入context的命名空间 2:配置注解的支持:<context:annotation-config/> <?xml version="1. ...

  7. Android | 教你如何快速集成机器学习能力

    背景   继上篇博文说了如何快速集成扫码以后 我又上官网去了解了一下其他的功能,其中机器学习服务是当下比较火的,而且还是免费的.就赶紧点进去学习一下.看看能够快速实现哪些功能. 链接在这里:https ...

  8. 【Python可视化】使用Pyecharts进行奥运会可视化分析~

    项目全部代码 & 数据集都可以访问我的KLab --[Pyecharts]奥运会数据集可视化分析-获取,点击Fork即可- 受疫情影响,2020东京奥运会将延期至2021年举行: 虽然延期,但 ...

  9. 2019-2020-1 20199328《Linux内核原理与分析》第六周作业

    使用gdb跟踪分析一个系统调用内核函数 首先我们删除本身的menu目录,并从github上克隆一个menu,并进行编译 编译过程 现在找到test.c文件,加入上个实验中做的getPid()方法 利用 ...

  10. Spring Boot filter

    在Spring Boot中自定义filter 本文我们将会讲解如何在Spring Boot中自定义filter并指定执行顺序. 定义Filter很简单,我们只需要实现Filter接口即可,同时我们可指 ...