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

    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 (≤), 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 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 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

    题意:

      给出一个数字,问这个数字能不能由k个不同数字(N[k])的p次方相加得到。如果存在多种可能,则输出N[k]之和最大的那个,如果仍然相等,则输出则输出下标相同时值大的那一个。

    思路:

      正确的解法是使用DFS + 剪枝。

      预处理: 将 i * i <= n 的所有可能存储到数组v[]中这里v中存储的数字是 i * i。

      深度优先搜索: DFS(int index, int tempSum, int tempK, int facSum)各参数代表的含义:

              index:代表预处理数组的下标; tempSum:代表当前已处理数字之和; tempK:代表已经处理的数字的个数; facSum:DFS调用到本层时所累加数字之和。

              递归跳出的条件是:index >= n, 代表已经遍历完了所有的可能。 当tempK == K的时候,if (tempSum == n && facSum > maxFaxSum) 说明结果需要进行更新。(ans = tempAns)

              ans用来表示结果的因子序列,tempAns用来表示当前遍历的因子序列。

    Code:

     1 #include <bits/stdc++.h>
     2 
     3 using namespace std;
     4 
     5 int n, k, p;
     6 vector<int> v, ans, tempAns;
     7 int maxFacSum = -1;
     8 
     9 void init() {
    10     int temp = 0, index = 1;
    11     while (temp <= n) {
    12         v.push_back(temp);
    13         temp = pow(index, p);
    14         index++;
    15     }
    16 }
    17 
    18 void DFS(int index, int tempSum, int tempK, int facSum) {
    19     if (tempK == k) {
    20         if (tempSum == n && facSum > maxFacSum) {
    21             ans = tempAns;
    22             maxFacSum = facSum;
    23         }
    24         return;
    25     }
    26     while (index >= 1) {
    27         if (tempSum + v[index] <= n) {
    28             tempAns[tempK] = index;
    29             DFS(index, tempSum + v[index], tempK + 1, facSum + index);
    30         }
    31         if (index == 1) return;
    32         index--;
    33     }
    34 }
    35 
    36 int main() {
    37     cin >> n >> k >> p;
    38     init();
    39     tempAns.resize(k);
    40     DFS(v.size() - 1, 0, 0, 0);
    41     if (maxFacSum == -1)
    42         cout << "Impossible" << endl;
    43     else {
    44         cout << n << " = ";
    45         for (int i = 0; i < ans.size(); ++i)
    46             if (i == 0)
    47                 cout << ans[i] << "^" << p;
    48             else
    49                 cout << " + " << ans[i] << "^" << p;
    50         cout << endl;
    51     }
    52     return 0;
    53 }

    看着别人的代码,自己一边想一边写总算是把这道题给解决了,而且提交也通过了。但是为什么样例的输出和代码得输出不一样呢?

    样例的输出是:

    169 = 6^2 + 6^2 + 6^2 + 6^2 + 5^2

    代码得输出是:

    169 = 7^2 + 6^2 + 6^2 + 5^2 + 5^2

    更神奇的是代码提交了之后还都通过了所有的样例??????????


    2020-07-12 22:05:00

    刚才又在我这台机器上(Linux)输出了一下发现结果有和测试样例一样了,有时间再到(Windows)上试一下,看一下结果是不是还是一样的。

    永远渴望,大智若愚(stay hungry, stay foolish)
  • 相关阅读:
    python处理yml
    awk命令笔记
    微信小程序wxml的数据传给js 点击事件 js获取view中的内容
    微信小程序js 字符串截取
    微信小程序 wx:if 多条件判断
    微信小程序 --- toast消息提示框
    微信小程序 点击事件获取到的 event.currentTarget.dataset.id 是空的 解决办法
    微信小程序页面跳转传参数
    微信小程序开发框架
    微信小程序获取当前时间
  • 原文地址:https://www.cnblogs.com/h-hkai/p/12794711.html
Copyright © 2011-2022 走看看