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 12^2^ + 4^2^ + 2^2^ + 2^2^ + 1^2^, or 11^2^ + 6^2^ + 2^2^ + 2^2^ + 2^2^, 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 { a~1~, a~2~, ... a~K~ } is said to be larger than { b~1~, b~2~, ... b~K~ } if there exists 1<=L<=K such that a~i~=b~i~ for i<L and a~L~>b~L~

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
题意比较好懂,容易想到dfs,但是不能纯回溯,需要剪枝,或者说按照一个非递减的顺序去试每个值,而且判断结果时满足k项就更新,因为从第一层开始,往下dfs,每一层的初始试验值都大于等于上一层的正在试验值,如果遇到跟之前一次结果相同的,序列一定比之前大。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <vector>
using namespace std;
int n,k,p;
vector<int> temp,ans;
int pow_[];///把可能用到的p次方都算出来,要多算一个 这样循环结束 不至于死循环(如果算的pow_[j] 都小于n 那么最大的max(j) + 1对应pow_[j]初始为0
int pow(int t) {
int d = ;
for(int i = ;i < p;i ++) {
d *= t;
}
return d;
}
void dfs(int t,int s,int last) {///last是上一层正在尝试的 ,本次从last开始 达到非递减的目的
if(t >= k) {
if(!s) {
ans = temp;
}
return;
}
while(pow_[last] <= s) {///这里防止死循环 如果last = max(j) 保证pow_[last]能使循环结束
temp.push_back(last);
dfs(t + ,s - pow_[last],last);
temp.pop_back();
last ++;
}
}
int main() {
scanf("%d%d%d",&n,&k,&p);
int j = ,d = ;
while(d <= n) {
pow_[j ++] = d;
d = pow(j);
}
pow_[j] = d;///多算一次 不然判断会死循环
dfs(,n,);
if(ans.empty())printf("Impossible");
else {
printf("%d = %d^%d",n,ans[k - ],p);
for(int i = k - ;i >= ;i --) {
printf(" + %d^%d",ans[i],p);
}
}
}

1103 Integer Factorization (30)(30 分)的更多相关文章

  1. 【PAT】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 positi ...

  2. PAT 1103 Integer Factorization[难]

    1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...

  3. 1103 Integer Factorization (30)

    1103 Integer Factorization (30 分)   The K−P factorization of a positive integer N is to write N as t ...

  4. PAT甲级——1103 Integer Factorization (DFS)

    本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...

  5. PAT甲级1103. Integer Factorization

    PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...

  6. 【PAT甲级】1103 Integer Factorization (30 分)

    题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...

  7. 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 positi ...

  8. PAT A1103 Integer Factorization (30 分)——dfs,递归

    The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K positi ...

  9. PAT (Advanced Level) 1103. Integer Factorization (30)

    暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...

随机推荐

  1. Android源码下载之----内核需要另外下载

    用repo sync下载的android源码默认不包含kernel目录,需要自己另外下载. 下载命令:$ git clone https://android.googlesource.com/kern ...

  2. nodejs windows下安装运行

    node 官网下载地址http://nodejs.org/  下载自己对应的版本  ,我下的是windows版本 node-v4.1.1-x64.msi 然后 下一步 下一步 就完成安装了,非常简单, ...

  3. XmlDocument.selectNodes() and selectSingleNode()的xpath的学习资料

    Xpath网页: http://www.w3school.com.cn/xpath/xpath_syntax.asp XDocument.parse(string)类似于XmlDocument.loa ...

  4. GCD多线程在swift中的变化

    1.异步线程加载主线程刷新 DispatchQueue.global().async { // TODO:执行异步线程网络请求 DispatchQueue.main.async(execute: { ...

  5. php字符串操作: 去掉UTF-16的空格

    $s = json_encode($s); $s = str_replace('\u00a0','',$s); $s = str_replace('\u3000','',$s); $s = str_r ...

  6. 《编程导论(Java)&#183;1.1.2 颠倒的世界(柏拉图法则)》

    假设你读<编程导论(Java)·1.1.2 颠倒的世界(柏拉图法则)>感到无趣,请尝试评价这个段子. 3. Classes Classes drive me crazy. That mig ...

  7. java基础入门之数组循环初始化

    /* Name:数组循环化 Power by Stuart Date:2015-4-23 */public class ArrayTest02{ public static void main (St ...

  8. CONVERT_DATE_INPUT

    [转自http://www.cnblogs.com/VerySky/articles/2226862.html] 通过 Function Module 将外部日期转换为内部日期所属 Function ...

  9. mysql用户和权限管理(Linux系统下)

    在mysql自带的库中有一个mysql,这个库包含了太多的东西,其中有一张表user,这张表存储了所有的用户信息. mysql> select user,host,password from u ...

  10. persisted? vs new_record?

    https://joe11051105.gitbooks.io/you-need-to-know-about-ruby-on-rails/content/activerecord/persisted_ ...