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 = 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 2Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2Sample Input 2:
169 167 3Sample Output 2:
Impossible
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;
const int SIZE = 404;
int a[SIZE];
int pow(int base,int exp){
int res =1;
while(exp){
if(exp & 0x1)
res *= base;
base *= base;
exp >>= 0x1;
}
return res;
}
int init_a(const int &n,const int &p){
a[0] = 0;
for(int i=1;i<=n;i++){
int tmp = pow(i,p);
if(tmp <= n)
a[i] = tmp;
else
return i-1;
}
return n;
}
vector<int> res,vtmp;
int numbers=0;
void display(){
vector<int>::iterator iter = vtmp.begin();
while(iter != vtmp.end())
cout <<*iter++ <<' ';
cout <<endl;
}
/*
void find_factors(int n,int k,int p,int start,int end,int sum,int cnt){
bool flag = true;
numbers++;
for(;start<=end && flag;start++){
if(sum == n && cnt == k)
res = vtmp;
else if(sum < n && cnt < k){
int tmp = a[start];
vtmp.push_back(start);
find_factors(n,k,p,start,end,sum+tmp,cnt+1);
vtmp.pop_back();
}else if(sum > n)
flag = false;
}
}
*/
void find_factors(int n,int k,int p,int start,int end,int sum,int cnt){
bool flag = true;
// numbers++;
for(;start<=end && flag;start++){
int tmpsum = sum +a[start];
vtmp.push_back(start);
if(tmpsum == n && cnt+1 == k)
res = vtmp;
else if(tmpsum < n && cnt+1 < k){
find_factors(n,k,p,start,end,tmpsum,cnt+1);
}else if(tmpsum > n)
flag = false;
vtmp.pop_back();
}
}
//*/
int main()
{
int N,K,P;
scanf("%d%d%d",&N,&K,&P);
int end = init_a(N,P);
find_factors(N,K,P,1,end,0,0);
if(!res.empty()){
printf("%d = %d^%d",N,res[K-1],P);
for(int i=K-2;i>=0;i--){
printf(" + %d^%d",res[i],P);
}
printf("\n");
}else
printf("Impossible\n");
// cout <<numbers <<endl;
return 0;
}
下面的版本代码运行的更快,但有一个bug始终找不出来,望高手指出:
<pre name="code" class="cpp">#include <cstdio>
#include <iostream>
#include <algorithm>
#include <vector>
//where is the bug???
///
using namespace std;
const int SIZE = 404;
int a[SIZE];
int pow(int base,int exp){
int res =1;
while(exp){
if(exp & 0x1)
res *= base;
base *= base;
exp >>= 0x1;
}
return res;
}
int init_a(const int &n,const int &p){
a[0] = 0;
for(int i=1;i<=n;i++){
int tmp = pow(i,p);
if(tmp <= n)
a[i] = tmp;
else
return i-1;
}
return n;
}
vector<int> res,vtmp;
int sum=0,vsum=0;
void display(){
vector<int>::iterator iter = vtmp.begin();
while(iter != vtmp.end())
cout <<*iter++ <<' ';
cout <<endl;
}
int numbers = 0;
//bug version/
void find_factors(int n,int k,int p,int start){
numbers++;
if(k ==0 && n == 0){
res = vtmp;
// display();
}else if(k > 0 && n > 0){
for(int i= start;i>=1;i--){
int tmp = a[i];
vtmp.push_back(i);
find_factors(n-tmp,k-1,p,i);
vtmp.pop_back();
}
}
}
/*
void find_factors(int n,int k,int p,int start){
// numbers++;
for(int i= start;i>=1;i--){
int newn = n - a[i];
vtmp.push_back(i);
if(newn == 0 && k == 1)
res = vtmp;
else if(newn >0 && k > 1)
find_factors(newn,k-1,p,i);
vtmp.pop_back();
}
}
*/
int main()
{
int N,K,P;
scanf("%d%d%d",&N,&K,&P);
int start = init_a(N,P);
find_factors(N,K,P,start);
if(!res.empty()){
printf("%d = %d^%d",N,res[0],P);
for(int i=1;i<K;i++){
printf(" + %d^%d",res[i],P);
}
printf("\n");
}else
printf("Impossible\n");
// cout << numbers <<endl;
return 0;
}