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?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]^Pwhere?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 2Sample Output 1:
169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2Sample Input 2:
169 167 3Sample Output 2:
Impossible我的思路:
1. 用 n / k ,然后開根號p 得到最大的因子, 接著進行dfs, 從最大的因子開始,填因子序列,直到到k個為止,寫出的代碼有點問題(畢竟我是搜索渣渣),但是參考了柳神的代碼,感覺思路可以,?重新寫一寫!
補:
24分,錯了2個點
#include <iostream> #include <cmath> #include <vector> #include <algorithm> using namespace std;int n, k, p; vector<int> temp , ans; int maxFacSum = -1; //記錄全局最大因子和double round(double r) // 四舍五入求最大因子 {return (r > 0.0) ? floor(r + 0.5) : ceil(r - 0.5); }void dfs(int index,int maxFac,int nowSum,int sum) //index代表因子序列位置,maxFac目前最大的因子,nowSum表示目前的和,sum表當前因子和 {if (index == k) //找到結果{if (nowSum == n && sum > maxFac){maxFacSum = sum;ans = temp;} return;}for (int i = maxFac; i > 0; i--) //從最大因子開始枚舉{if (nowSum + pow(i*1.0, p) <= n) //如果 和<n 才可以填{temp[index] = i; //在index位置填上該因子dfs(index+1, i, nowSum + pow(i*1.0, p), sum+i); //遞歸填index+1位置 } if(index == k - 1) return;}}int main() {scanf("%d %d %d", &n, &k, &p);int maxFactor = (int)round(sqrt(round(n / (k*1.0))));temp.resize(k);dfs(0,maxFactor,0,0); //要填0號位置,最大因子是maxFactor,當前和為n,因子和為0if (maxFacSum == -1){printf("Impossible");return 0;}printf("%d =", n);for (int i = 0; i < ans.size(); i++){if (i == ans.size() - 1){printf(" %d^%d",ans[i], p);return 0;}printf(" %d^%d +", ans[i], p);} }?
參考代碼:
#include <iostream> #include <cmath> #include <vector> using namespace std;int n, k, p; vector<int> v, temp, ans; int maxFacSum = -1;void init() //此舉的目的是保存i的p次方,知道其 <= nint temp = 0, index = 1;while(temp <= n){v.push_back(temp);temp = pow(index*1.0, p); //這個地方不能寫成pow(index,p),在vs2010里面編譯的時候無法匹配列表,正確是double pow(double r,int p)index++;} }void dfs(int index,int tempSum,int nowK,int facSum) //index代表因子,tempSum是當前因為的p次冪的和,nowK是當前的因子個數,facSum是當前的因子和 {if (nowK == k) //選了k個因子{if (tempSum == n && facSum > maxFacSum) //和正好是n 并且當前因子和 > 全局因子和,進行更新{ans = temp;maxFacSum = facSum;}return;}while (index >= 1) {if (tempSum + v[index] <= n) //只有目前和+要選的因子的p次冪的和<n時,才進行選擇{temp[nowK] = index;dfs(index,tempSum+v[index], nowK+1, facSum+index);}if (index == 1) return;index--;}}int main() {scanf("%d %d %d", &n, &k, &p);init();temp.resize(k);dfs(v.size()-1, 0, 0, 0); //從最大開始,才能降序輸出if (maxFacSum == -1) {printf("Impossible");return 0;}printf("%d = ", n);for (int i = 0; i < ans.size(); i++) {if (i != 0) printf(" + ");printf("%d^%d", ans[i], p);}return 0; }?
?
總結
以上是生活随笔為你收集整理的PAT甲级 -- 1103 Integer Factorization (30 分)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: PAT甲级 -- 1148 Werewo
- 下一篇: PAT甲级 -- 1079 Total