1103. Integer Factorization
題目的大體意思就是,給出N,k,p三個數字
找出滿足N=n_1^P+…n_k^P的所有n的值,並且在所有方法中找到n_1+…+n_k最大的值,如果大小相同,就選擇字典序最大的一組陣列。
這道題肯定是要通過搜尋去尋找最合適的組合,明顯是dfs(有點貪心的意思在裡面)
首先,先將index^p
#include<iostream>
#include<cstdio>
#include<vector>
#include<cmath>
using namespace std;
vector<int> tmpAns,Ans,data;
int n,k,p,maxfac=-1;
int myindex=1;
void init()
{
int tmp=0;
while(tmp<=n)
{
data.push_back(tmp);
tmp=pow(myindex,p);
myindex++;
}
}
void dfs(int index,int tmpNum,int tmpK,int fac)
{
if(tmpNum==n && tmpK==k)
{
if(fac>maxfac)
{
Ans=tmpAns;
maxfac=fac;
}
return ;
}
if(tmpNum>n || tmpK>k) return;
if(index>0)
{
tmpAns.push_back(index);
dfs(index,tmpNum+data[index],tmpK+1,fac+index);
tmpAns.pop_back();
dfs(index-1,tmpNum,tmpK,fac);
}
}
int main()
{
scanf("%d %d %d",&n,&k,&p);
init();
dfs(data.size()-1 ,0,0,0);
if(maxfac==-1) printf("Impossible\n");
else
{
printf("%d = ",n);
for(int i=0;i<Ans.size();++i)
{
if(i==0) printf("%d^%d",Ans[i],p);
else printf(" + %d^%d",Ans[i],p);
}
}
return 0;
}
相關推薦
PAT 1103 Integer Factorization[難]
感覺 psu problem amp text fix HERE ogr 是不是 1103 Integer Factorization(30 分) The K?P factorization of a positive integer N is to write N
1103 Integer Factorization
clu 序列 flow i++ integer csu n) emp lib 題意:給出一個正整數N,以及k,p,要求把N分解成k個因式,即N=n1^p + n2^p + ... + nk^p。要求n1,n2,...按降序排列,若有多個解,則選擇n1+n2+...+nk最大
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
1103 Integer Factorization(PAT 甲等 C++實現)
1103 Integer Factorization (30 point(s)) The K−P factorization of a positive integer N is to write N as the sum of the P-th power of K po
1103. Integer Factorization
題目的大體意思就是,給出N,k,p三個數字 找出滿足N=n_1^P+…n_k^P的所有n的值,並且在所有方法中找到n_1+…+n_k最大的值,如果大小相同,就選擇字典序最大的一組陣列。 這道題肯
dfs | PAT-A | 1103 Integer Factorization
https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 要求尋找一個數列n1 n2 … nk 構造一棵子集數, 初始化後得到max_nk, 相當於從1~max_nk這max_nk個數中,
1103 Integer Factorization(30 分)
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 i
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 f
1103 Integer Factorization (DFS)
text sample number and form separate ups power ges The K?P factorization of a positive integer N is to write N as the sum of the P-th pow
PAT甲級1103 Integer Factorization【dfs】【剪枝】
printf cto pin ger iostream 思路 ems cstring 等於 題目:https://pintia.cn/problem-sets/994805342720868352/problems/994805364711604224 題意: 給定一個
leetcode題解 || Roman to Integer問題
leet only top xiv har convert 擁有 pro think problem: Given a roman numeral, convert it to an integer. Input is guaranteed to be within
Matrix Factorization, Algorithms, Applications, and Avaliable packages
con spam member bing struct ado sign aries nio 來源:http://www.cvchina.info/2011/09/05/matrix-factorization-jungle/ 美帝的有心人士收集了市面上的矩陣
Integer和new Integer
數據 com tool lba pbo intval keyword 註意 etc Java code ? 1 2 3 4 5 6 7 8 9 10 public static void main(String[] args) { Integer
Java中Integer和int比較大小出現的錯誤
最好 裏的 pan 轉換 als 範圍 urn 返回 錯誤 Java在某一處維護著一個常量池,(我記得)在小於128的範圍內,直接用 1 Integer i = 100; 2 int j = 100; 3 return i == j;//true 這裏返回的是true.
LeetCode--Reverse Integer
iss com ack ostream 題目 width 報告 pull stack //#include <iostream> #include <cmath> #include <stack> //using namespace
String,Integer,int類型之間的相互轉換
封裝 指定 進行 基本 三種 pan 數字 拆箱 方法 String, Integer, int 三種類型之間可以兩兩進行轉換 1. 基本數據類型到包裝數據類型的轉換 int -> Integer (兩種方法) Integer it1 = new Intege
“無效數字” ;java.lang.Integer cannot be cast to java.lang.String
現在 類型 bsp 字段 ann 不難 esc ger ring 今天頁面上突然查詢不出數據,大致的sql語句是 select xx ,xxx from table a where a.lrmb in ( 6101060033, 6101
int和Integer有什麽區別?
機制 microsoft val family jdk1 經歷 ont .com () 可參考:http://www.cnblogs.com/liuling/archive/2013/05/05/intAndInteger.html 從Java 5開始引入了自動裝箱/拆
Find one unique integer
amp bits return osi ret spa algorithm hub content https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/bl
【LeetCode-面試算法經典-Java實現】【008-String to Integer (atoi) (字符串轉成整數)】
pre except tco ecif hid pan format 說明 elf 【008-String to Integer (atoi) (字符串轉成整數)】 【LeetCode-面試算法經典-Java實現】【全部題目文件夾索引】 原題