PAT(甲级)1103
来源:互联网 发布:linux 修改目录结构 编辑:程序博客网 时间:2024/05/16 10:31
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;}
- PAT(甲级)1103
- PAT 甲级 1103
- PAT甲级1103
- PAT 甲级
- **浙大PAT甲级 1103 dfs+快速幂
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- PAT(甲级)1013
- Hi3516C工作记录
- spark streaming源码分析1 StreamingContext
- 【HTML】-案例-有序列表和无序列表
- PAT(甲级)1102
- 【Unity3D游戏开发学习笔记】(三)移山倒海—Terrain的建造
- PAT(甲级)1103
- 三步完成ssh免密码登录
- 18.Android 颜色工具类ColorUtil
- java判断字符串是否为空的方法总结
- 梯度下降法的简单理解
- hiho1227--The Cats' Feeding Spots
- MySQL中两个DateTime字段相减
- cc
- cvcv