Pat a problem-1103. Integer factorization-(DFS)

Source: Internet
Author: User

The problem is not bad ~.

Test instructions: Given n, K, P, which can be decomposed into n = n1^p + ... the form of nk^p, if possible, the largest division of the output sum (NI), if sum, the output sequence of the larger one. Otherwise the output impossible.

DFS enumeration, in order to prevent timeouts, this is to pre-store the value of i^p starting at 1 in the factor array until i^p>n. Then Dfs depth first search, equivalent to the problem step by step decomposition, even if the first factor is N1, then we have to determine whether n-n1^p, k-1 is feasible. The sum sum of the current factor is stored at the same time, the sum is the largest, and the index of the factor that was added last, because the factor is from large to small output, so the latter one cannot be greater than the previous factor.

#include <iostream>#include<cstdio>#include<algorithm>#include<string.h>#include<cmath>using namespacestd;/*I can actually store the i^p<n I in advance.*/Const intmaxn=405;intRES[MAXN];intANS[MAXN];intFACTOR[MAXN];intfidx=0;intmaxsum=0;BOOLflag=false;intn,k,p;/*num is the current sum of CNT as there are still several i^p items, that is, the current ksum is the sum of the factors, because the index of the last factor to be taken and the maximum is, because to ensure that the factor from large to small output, so the index of the post-DFS factor in factor cannot be greater than the previous */voidDfsintNumintCntintSumintLast ) {    if(num==0&&cnt==0){        if(sum>maxsum) {Flag=true;  for(intI=1; i<=k;i++) Ans[i]=Res[i]; Maxsum=sum; }        return; }    Else if(cnt==0)        return;  for(intI=min (fidx-1, last); i>=0; i--){        intleft=num-Factor[i]; if(left>=cnt-1) {res[cnt]=i+1; DFS (left,cnt-1, sum+i+1, i); }    }}intMain () {scanf (" %d%d%d",&n,&k,&p); inttmp=1; Fidx=0; //pre-stored i^p<=n i     while(tmp<=N) {Factor[fidx]=tmp; Fidx++; TMP=pow (fidx+1, p); }    intCnt=0; intlast=fidx-1; DFS (N,k,0, last); if(flag) {printf ("%d =", N);  for(inti=k;i>=2; i--) {printf ("%d^%d +", ans[i],p); } printf ("%d^%d", ans[1],p); }    Else{printf ("Impossible"); }    return 0;}
View Code

Pat a problem-1103. Integer factorization-(DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.