当前位置:网站首页>1103 integer factorization (30 points)
1103 integer factorization (30 points)
2022-07-03 04:55:00 【vs5】
The main idea of the topic : Count a number n, Divide k The number of p The sum of powers .
dfs: Preprocessing a^p <= n All of the a, Then start searching from the maximum number , Pay attention to pruning .
knapsack : In fact, the capacity is n The backpack can hold the maximum value ( The sum of all numbers is the largest )
Search code
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
const int N = 10001;
int n,k,p;
int v[N],s = 1,maxsum = -1;
vector<int>res(N),ans;
void dfs(int a,int sum,int cnt,int m)//sum Everyone and ,cnt frequency
{
if(cnt == k && (m != 0 || sum < maxsum )) return ;
if(cnt == k && m == 0)
{
if(sum > maxsum)
{
maxsum = sum;
ans = res;
}
return ;
}
for(int i = a; i > 0; i --) // Enumerate each bit from the high order
{
if(m - v[i] >= 0)
{
res[cnt] = i;// Save the answer
dfs(i,sum + i,cnt + 1,m - v[i]);
}
}
}
int main()
{
cin >> n >> k >> p;
while(pow(s,p) <= n) v[s] = pow(s ,p),s ++;
s --;
dfs(s,0,0,n);
if(maxsum == -1) puts("Impossible");
else
{
printf("%d = ",n);
for(int i = 0; i < k; i ++)
{
if(i != 0) printf(" + ");
printf("%d^%d",ans[i],p);
}
}
return 0;
}knapsack
#include <iostream>
#include <cmath>
#include <cstring>
using namespace std;
int f[25][410][410];
int n,k,p;
int v[410],w[410],val[410];
int main()
{
cin >> n >> k >> p;
int s = 0;//s Item
memset(f,-0x3f,sizeof f);
f[0][0][0] = 0;
while(pow(s,p) <= n)
{
v[s] = pow(s,p);// Volume
//w[s] = 1;// weight Don't write
val[s] = s;// value
s ++;
}
s --;
for(int i = 1; i <= s; i ++)
for(int j = 0; j <= n; j ++)
for(int z = 0; z <= k; z ++)
{
f[i][j][z] = f[i - 1][j][z];
if(j - v[i] >= 0 && z >= 1) f[i][j][z] = max(f[i][j][z],f[i][j - v[i]][z - 1] + val[i]);
}
if(f[s][n][k] < 0) puts("Impossible");
else
{
int flag = 0;
printf("%d = ",n);
for(int i = s,j = n,z = k; i ; i --)
{
while(f[i][j][z] == f[i][j - v[i]][z - 1] + val[i])
{
if(flag != 0) printf(" + ");
printf("%d^%d",i,p);
j -= v[i],z --;flag = 1;
}
}
}
return 0;
}边栏推荐
- General undergraduate college life pit avoidance Guide
- Leetcode simple problem delete an element to strictly increment the array
- Symbol of array element product of leetcode simple problem
- SSM framework integration
- [PCL self study: filtering] introduction and use of various filters in PCL (continuously updated)
- [research materials] 2021 annual report on mergers and acquisitions in the property management industry - Download attached
- Actual combat 8051 drives 8-bit nixie tube
- [research materials] 2021 China's game industry brand report - Download attached
- Review the old and know the new: Notes on Data Science
- JDBC database operation
猜你喜欢

2022-02-12 daily clock in: problem fine brush

Games101 Lesson 9 shading 3 Notes

Symbol of array element product of leetcode simple problem

关于开学的准备与专业认知

I stepped on a foundation pit today

并发操作-内存交互操作

【XSS绕过-防护策略】理解防护策略,更好的绕过

Network security textual research recommendation

Introduction to message queuing (MQ)

Leetcode simple question: check whether the string is an array prefix
随机推荐
2022-02-11 daily clock in: problem fine brush
LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
《牛客刷verilog》Part II Verilog进阶挑战
Source insight garbled code solution
Triangular rasterization
Caijing 365 stock internal reference: what's the mystery behind the good father-in-law paying back 50 million?
Blog building tool recommendation (text book delivery)
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
[XSS bypass - protection strategy] understand the protection strategy and better bypass
Leetcode simple question: the key with the longest key duration
The least operation of leetcode simple problem makes the array increment
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
The principle is simple, but I don't know how to use it? Understand "contemporaneous group model" in one article
Retirement plan fails, 64 year old programmer starts work again
Literature reading_ Research on the usefulness identification of tourism online comments based on semantic fusion of multimodal data (Chinese Literature)
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
Leetcode simple question: check whether two string arrays are equal
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
Unity tool Luban learning notes 1
Symbol of array element product of leetcode simple problem