zoukankan      html  css  js  c++  java
  • PAT 1103 Integer Factorization

    The KP 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 KP 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 (≤), K (≤) and P (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
    

    where 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 1, or 1, 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 be larger than { , } if there exists 1 such that ai​​=bi​​ for i<L and aL​​>bL​​.

    If there is no solution, simple output Impossible.

    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

     1 #include<iostream>
     2 #include<cmath>
     3 #include<vector>
     4 using namespace std;
     5 vector<int> temp, path;
     6 int n, p, k, maxx=-1, t;
     7 bool flag=true;
     8 /*
     9   dfs的回溯中弹出和压栈的位置很重要
    10 */
    11 void dfs(int a, int b, int c){
    12     if(b==0 && c==0){
    13         int tempMax=0;
    14         for(int i=0; i<temp.size(); i++) tempMax+=temp[i];
    15         if(maxx<tempMax){
    16             maxx = tempMax;
    17             path = temp;
    18         }else if(maxx==tempMax && path<temp) path=temp;
    19         temp.pop_back();
    20         return;
    21     }else if(c<=0 || b<=0){
    22         temp.pop_back();
    23         return;
    24     }
    25     for(int i=a; i>=1; i--){
    26         if(b-c*pow(i, p)>0) break;  //解决超时的关键点
    27         if(b-pow(i, p)<0) {
    28             while(b-pow(i, p)<0 && i>=1) --i;
    29             temp.push_back(i);
    30             dfs(i, b-pow(i, p), c-1);
    31         }else{
    32             temp.push_back(i);
    33             dfs(i, b-pow(i, p), c-1);
    34         }
    35     }
    36     temp.pop_back();//弹出的位置在for循环之外,很重要;
    37 }
    38 int main(){
    39   scanf("%d%d%d", &n, &k, &p);
    40   t =pow(n, 1.0/p) > n/k+1 ? pow(n, 1.0/p) : (n/k+1);
    41   dfs(t, n, k);
    42   if(path.size()==0) cout<<"Impossible"<<endl;
    43   else {
    44     printf("%d = ", n);
    45     for(int i=0; i<path.size(); i++) {
    46       if(i!=0) printf(" + ");
    47       printf("%d^%d", path[i], p);
    48     }
    49   }
    50   return 0;
    51 }
  • 相关阅读:
    爬虫学习(五)——百度贴吧的爬取
    爬虫学习(四)——post请求爬取
    爬虫学习(三)——get请求参数解析
    爬虫学习(二)
    爬虫学习(一)
    第二阶段团队冲刺第三天
    第二阶段团队冲刺第二天
    第二阶段团队冲刺第一天
    第一阶段绩效评估
    Alpha版(内部测试版)发布
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9584952.html
Copyright © 2011-2022 走看看