1103. Integer Factorization (30)

递归算法,最后为了全部通过用了一些奇技淫巧。。。


时间限制
1200 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

The K-P factorization of a positive integer N is to write N as the sum of the P-th power of K positive integers. You are supposed to write a program to find the K-P factorization of N for any positive integers N, K and P.

Input Specification:

Each input file contains one test case which gives in a line the three positive integers N (<=400), K (<=N) and P (1<P<=7). The numbers in a line are separated by a space.

Output Specification:

For each case, if the solution exists, output in the format:

N = n1^P + ... nK^P

where ni (i=1, ... K) is the i-th factor. All the factors must be printed in non-increasing order.

Note: the solution may not be unique. For example, the 5-2 factorization of 169 has 9 solutions, such as 122 + 42 + 22 + 22 + 12, or 112+ 62 + 22 + 22 + 22, or more. You must output the one with the maximum sum of the factors. If there is a tie, the largest factor sequence must be chosen -- sequence { a1, a2, ... aK } is said to be larger than { b1, b2, ... bK } if there exists 1<=L<=K such that ai=bi for i<L and aL>bL

If there is no solution, simple output "Impossible".

Sample Input 1:
169 5 2
Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2
Sample Input 2:
169 167 3
Sample Output 2:
Impossible

 

  1. #include <iostream>
  2. #include<vector>
  3. #include <math.h>
  4. #include<stdio.h>
  5. #pragma warning(disable:4996)
  6. using namespace std;
  7. int n, p, k;
  8. vector<int> num,numtemp;
  9. void Calc(int i, int sum, int level,vector<int> temp) {
  10. sum += pow(i, k);
  11. temp.push_back(i);
  12. if (p - temp.size() > n - sum)
  13. return;
  14. if (sum > n)
  15. return;
  16. if (level == 0) {
  17. if (sum == n) {
  18. int flag = false;
  19. if (num.size() == 0)
  20. flag = true;
  21. else {
  22. for (int j = p-1; j >=0; j--) {
  23. if (num[j] > temp[j]) {
  24. flag = false;
  25. break;
  26. }
  27. else if (num[j] < temp[j]) {
  28. flag = true;
  29. break;
  30. }
  31. }
  32. }
  33. if (flag == true) {
  34. num.clear();
  35. for (int j = 0; j < p; j++)
  36. num.push_back(temp[j]);
  37. }
  38. }
  39. return;
  40. }
  41. else {
  42. for (int j = 1; j <= i; j++) {
  43. Calc(j, sum, level - 1, temp);
  44. }
  45. }
  46. }
  47. int main(void) {
  48. cin >> n >> p >> k;
  49. int breakpoint = pow(n, 1.0 / k);
  50. /*if (p > breakpoint) {
  51. if (n == p) {
  52. cout << n << " =";
  53. for (int i = 0; i < p; i++) {
  54. cout << " " << "1" << "^" << k;
  55. if (i != p - 1)
  56. cout << " +";
  57. }
  58. }
  59. else {
  60. cout << "Impossible";
  61. }
  62. return 0;
  63. }*/
  64. int cnt = 0;
  65. for (int i = 1; i <= breakpoint; i++) {
  66. if (cnt == 2)
  67. break;
  68. if (!num.empty())
  69. cnt++;//奇技淫巧。。。。
  70. numtemp.clear();
  71. Calc(i, 0, p-1, numtemp);
  72. }
  73. if (num.size() == 0) {
  74. cout << "Impossible";
  75. }
  76. else {
  77. cout << n << " =";
  78. for (int i = 0; i < num.size(); i++) {
  79. // cout << " " << num[i] << "^" << k;
  80. printf(" %d^%d", num[i], k);
  81. if (i != num.size() - 1)
  82. printf(" +");
  83. //cout << " +";
  84. }
  85. }
  86. return 0;
  87. }





posted @ 2015-12-06 12:39  白夜行zz  阅读(690)  评论(0编辑  收藏  举报