1103 Integer Factorization (30 分)

1103 Integer Factorization (30 分)
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 = n[1]^P + … n[K]^P
where n[i] (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

题目大意:
给定三个正整数N,K,P。N表示目标整数的大小,K表示可分解成的项数,P表示幂的大小。将N表示成K个整数的P次方的和,如果由多种方案,输出底数和最大的,如果仍有多种,选择序列的字典序最大的方案
变量定义:
1.由于要多次用到0到i的P次方,先将他们存储到weight数组中
2.设置vector temp存储每次遍历的临时序列,设置vector sequence存储最优的序列
3.DFS函数中传入四个参数
index:当前访问的数
tempK:已选中的数的个数
tempSum;已选中的数的P次方的和
factSum:已选中的数的和
算法分析:
运用深度优先搜索,注意剪枝
剪枝注意点:
1.剪去tempK=k,tempSum!=n的情况
2.从上界开始遍历,因为要输出字典序大的
DFS跳出的条件:
1.选取的数以达到K
2.选取的数未达到K,但再加入任意一个数的P次方,都会使得tempSum大于目标整数

AC代码:

#include<bits/stdc++.h>
using namespace std;
int n, k, p,maxFactor=-1;
vector<int> sequence,temp,weight;
void DFS(int index, int tempK,int tempSum,int factSum){
    
    
  if(tempK==k){
    
    //编号从0开始,此时代表已经有k个元素了
    if(factSum>maxFactor&&tempSum==n){
    
    
      //注意这里的条件判断,将tempK=k,tempSum!=n的剪枝了
      maxFactor = factSum;
      sequence = temp;
    }
    return;
  }
  while(index>=1){
    
    
    if(tempSum+weight[index]<=n){
    
    
      temp[tempK] = index;//这个的使用很精妙,不要用push压入元素,这样不仅减少了后面元素的弹出,还限制了元素的总个数
      DFS(index, tempK + 1, tempSum + weight[index], factSum + index);
    }
    if(index==1)
      return;
    index--;
  }
}
int main(){
    
    
  cin >> n >> k >> p;
  for (int i = 0;;i++){
    
    
    if(pow(i,p)>n){
    
    
      break;
    }
    weight.push_back((int)pow(i, p));//由于经常要用到0到i的p次方,先把他们存储起来,避免多次计算
  }
  temp.resize(k);
/*temp.resize(size)后用push_back,是在temp[size]处开始添加元素,所以resize与push_back不要连用。
resize后,直接通过下标索引的方式改变元素值。
对于大量数据的存储,使用reserve + push_back 的方式,或者resize + 下标索引 的方式*/
  DFS(weight.size()-1, 0,0,0);//因为最后一个条件要求输出底数字典序大的,所以从大数开始遍历
  if(maxFactor==-1)
    printf("Impossible");
  else{
    
    
    sequence.resize(k, 0);
    printf("%d = %d^%d", n, sequence[0], p);
    for (int i = 1; i < sequence.size();i++)
      printf(" + %d^%d", sequence[i], p);
  }
}

猜你喜欢

转载自blog.csdn.net/weixin_48954087/article/details/113849266