PAT A1103 Integer Factorization Go語言題解及注意事項
TheK−Pfactorization of a positive integerNis to writeNas the sum of theP-th power ofKpositive integers. You are supposed to write a program to find theK−Pfactorization ofNfor any positive integersN,KandP.
Input Specification:
Each input file contains one test case which gives in a line the three positive integersN(≤),K(≤) andP(1). 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
wheren[i]
(i
= 1, ...,K
) is thei
-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 as1, or1, 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 {,} is said to belargerthan {,} if there exists1such thatai=bifori<LandaL>bL.
If there is no solution, simple outputImpossible
.
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
思路:
暴力求解100%會超時,這裡使用DFS進行求解,由題目要求,我們從最大的因子開始從大往小搜,在因子合相同時總是能先搜到最優解然後返回。
Solution(Golang):
package main import ( "fmt" ) var ( temp, fac []int ans [100]int maxsum int = 0 n, p, k int flag = false ) func pow(n, p int) int { a := n for i := 1; i < p; i++ { a *= n } return a } func init1() { fac = append(fac, 0) for i := 1; pow(i, p) <= n; i++ { fac = append(fac, pow(i, p)) } } func dfs(index, num, value, sumfac int) { if num > k || value > n { return } if num == k { if value == n && sumfac > maxsum { flag = true maxsum = sumfac for i, v := range temp { ans[i] = v } } return } temp = append(temp, index) //fmt.Println(index) dfs(index, num+1, value+fac[index], sumfac+index) temp = temp[0 : len(temp)-1] if index > 1 { dfs(index-1, num, value, sumfac) } } func main() { fmt.Scan(&n, &k, &p) init1() dfs(len(fac)-1, 0, 0, 0) //fmt.Println(ans) if !flag { fmt.Println("Impossible") } else { fmt.Printf("%d = %d^%d", n, ans[0], p) for i := 1; i < k; i++ { fmt.Printf(" + %d^%d", ans[i], p) } } return }
#include<bits/stdc++.h>
usingnamespacestd;
constintmaxn=10010; intn,x; stringa,b; structnode{ intleft,right; };
nodetree[20];
voidinvert(introot){ if(root==-1)return; invert(tree[root].left); invert(tree[root].right); swap(tree[root].left,tree[root].right); } vector<int>ans; voidlayer(introot){ queue<int>q; q.push(root); while(!q.empty()){ intt=q.front(); ans.push_back(t); q.pop(); if(tree[t].left!=-1)q.push(tree[t].left); if(tree[t].right!=-1)q.push(tree[t].right); } }
voidinorder(introot){ if(root==-1)return; inorder(tree[root].left); ans.push_back(root); inorder(tree[root].right); }
voidprintAns(){ for(inti=0;i<ans.size();++i){ cout<<ans[i]; if(i<ans.size()-1){ cout<<""; }else{ cout<<endl; } } }
intmain(){ cin>>n; boolisf[maxn]; fill(isf,isf+n,true); for(inti=0;i<n;++i){ cin>>a>>b; if(a[0]=='-'){ tree[i].left=-1; }else{ x=atoi(a.data()); tree[i].left=x; isf[x]=false; } if(b[0]=='-'){ tree[i].right=-1; }else{ x=atoi(b.data()); tree[i].right=x; isf[x]=false; } } for(inti=0;i<n;++i){ if(isf[i]){ x=i; break; } } invert(x); layer(x); printAns(); ans.clear(); inorder(x); printAns();
return0; }