1103 Integer Factorization (30)(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 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 分)的更多相关文章
- 【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 ...
- PAT 1103 Integer Factorization[难]
1103 Integer Factorization(30 分) The K−P factorization of a positive integer N is to write N as the ...
- 1103 Integer Factorization (30)
1103 Integer Factorization (30 分) The K−P factorization of a positive integer N is to write N as t ...
- PAT甲级——1103 Integer Factorization (DFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...
- PAT甲级1103. Integer Factorization
PAT甲级1103. Integer Factorization 题意: 正整数N的K-P分解是将N写入K个正整数的P次幂的和.你应该写一个程序来找到任何正整数N,K和P的N的K-P分解. 输入规格: ...
- 【PAT甲级】1103 Integer Factorization (30 分)
题意: 输入三个正整数N,K,P(N<=400,K<=N,2<=P<=7),降序输出由K个正整数的P次方和为N的等式,否则输出"Impossible". / ...
- 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 ...
- 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 ...
- PAT (Advanced Level) 1103. Integer Factorization (30)
暴力搜索. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #i ...
随机推荐
- linux系统之间基于密钥对免输入密码登陆
通常,我们登陆到一台linux主机是需要输入密码,这样可以验证登陆者的身份. 这篇随笔里面我打算记录一下配置基于ssh密钥对登陆系统.在配置之前先和大家说明一下我遇到过的问题:刚接触到linux时候首 ...
- poj3708(公式化简+大数进制装换+线性同余方程组)
刚看到这个题目,有点被吓到,毕竟自己这么弱. 分析了很久,然后发现m,k都可以唯一的用d进制表示.也就是用一个ai,和很多个bi唯一构成. 这点就是解题的关键了. 之后可以发现每次调用函数f(x),相 ...
- sudo npm install -g cnpm --registry=https://registry.npm.taobao.org
- Jenkins maven仓库地址 和 手动修改maven 版本
① Jenkins maven仓库地址,一般情况会在:/root/.m2/repository/* ② 手动修改maven 版本,Apache 下载指定的maven版本,然后解压后copy到指定目录即 ...
- [原创]css设置禁止中文换行
white-space: nowrap; 如有需要还可以设置word-break,word-wrap配合.
- Ajax之基础总结
一.Ajax 简介 Ajax 由 HTML.JavaScript技术.DHTML 和 DOM 组成,这一杰出的方法可以将笨拙的 Web 界面转化成交互性的 Ajax 应用程序.在详细探讨 Ajax 是 ...
- [置顶] 2013_CSUST暑假训练总结
2013-7-19 shu 新生训练赛:母函数[转换成了背包做的] shuacm 题目:http://acm.hdu.edu.cn/diy/contest_show.php?cid=20083总结:h ...
- swift 一句代码补全tableView分割线
1.swift实现分割线补全 swift一个大进步,只要设置tableView.separatorInset = UIEdgeInsets.zero即可补全分割线, 2.OC实现分割线补全 而在OC中 ...
- Frobenius Norm
http://mathworld.wolfram.com/FrobeniusNorm.html
- 使用memcache进行账号验证服务
适用环境是需要频繁进行账号和请求合法性验证的地方 大致思路: 1.登陆时,服务器端接收一个账号和密码,还可以再加上用户的ip等信息通过md5等加密算法计算出一个定长的字符串作为用来验证的token 2 ...