当前位置:网站首页>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;
}
边栏推荐
- [XSS bypass - protection strategy] understand the protection strategy and better bypass
- Basic use of Metasploit penetration testing framework
- 《牛客刷verilog》Part II Verilog进阶挑战
- The least operation of leetcode simple problem makes the array increment
- Notes | numpy-07 Slice and index
- Market status and development prospects of the global autonomous marine glider industry in 2022
- Coordinatorlayout appbarrayout recyclerview item exposure buried point misalignment analysis
- Use Sqlalchemy module to obtain the table name and field name of the existing table in the database
- Market status and development prospect forecast of global button dropper industry in 2022
- data2vec! New milestone of unified mode
猜你喜欢
Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)
Leetcode simple question: check whether the string is an array prefix
Leetcode simple question: check whether two string arrays are equal
JDBC database operation
Number of uniform strings of leetcode simple problem
Truncated sentences of leetcode simple questions
The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
Concurrent operation memory interaction
Silent authorization login and registration of wechat applet
C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
随机推荐
First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
【SQL注入】联合查询(最简单的注入方法)
Leetcode simple question: the key with the longest key duration
Market status and development prospect prediction of global neutral silicone sealant industry in 2022
Blog building tool recommendation (text book delivery)
Sprintf formatter abnormal exit problem
[research materials] the fourth quarter report of the survey of Chinese small and micro entrepreneurs in 2021 - Download attached
The reason why the entity class in the database is changed into hump naming
Huawei personally ended up developing 5g RF chips, breaking the monopoly of Japan and the United States
Market status and development prospect prediction of global colorimetric cup cover industry in 2022
Leetcode simple question: check whether the string is an array prefix
Career planning of counter attacking College Students
[PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
The least operation of leetcode simple problem makes the array increment
The 19th Zhejiang I. barbecue
《牛客刷verilog》Part II Verilog进阶挑战
Flutter monitors volume to realize waveform visualization of audio
Market status and development prospects of the global autonomous marine glider industry in 2022
Market status and development prospects of the global automatic tea picker industry in 2022
[set theory] relation properties (reflexivity | reflexivity theorem | reflexivity | reflexivity theorem | example)