当前位置:网站首页>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;
}边栏推荐
- 第十九届浙江省 I. Barbecue
- 论文阅读_中文医疗模型_ eHealth
- Problems encountered in fuzzy query of SQL statements
- First + only! Alibaba cloud's real-time computing version of Flink passed the stability test of big data products of the Institute of ICT
- Sdl2 + OpenGL glsl practice (Continued)
- Review the configuration of vscode to develop golang
- The simple problem of leetcode: dismantling bombs
- [Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
- [PHP vulnerability weak type] basic knowledge, PHP weak equality, error reporting and bypassing
- 【XSS绕过-防护策略】理解防护策略,更好的绕过
猜你喜欢

Thesis reading_ Chinese medical model_ eHealth

On typescript and grammar
![[luatos sensor] 1 light sensing bh1750](/img/70/07f29e072c0b8630f92ec837fc12d5.jpg)
[luatos sensor] 1 light sensing bh1750

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

Number of 1 in binary (simple difficulty)

The reason why the entity class in the database is changed into hump naming

Concurrent operation memory interaction

Esp32-c3 learning and testing WiFi (II. Wi Fi distribution - smart_config mode and BlueIf mode)

Prepare for 2022 and welcome the "golden three silver four". The "summary of Android intermediate and advanced interview questions in 2022" is fresh, so that your big factory interview can go smoothly
![[tools run SQL blind note]](/img/c3/86db4568b221d2423914990a88eec2.png)
[tools run SQL blind note]
随机推荐
Interface frequency limit access
Market status and development prospect prediction of the global fire hose industry in 2022
Preparation for school and professional cognition
C primre plus Chapter 10 question 6 inverted array
String matching: find a substring in a string
Huawei personally ended up developing 5g RF chips, breaking the monopoly of Japan and the United States
UiPath实战(08) - 选取器(Selector)
逆袭大学生的职业规划
Distinguish between releases and snapshots in nexus private library
Shell script Basics - basic grammar knowledge
@RequestMapping
Notes | numpy-11 Array operation
Blog building tool recommendation (text book delivery)
Thesis reading_ Tsinghua Ernie
2022-02-11 daily clock in: problem fine brush
Valentine's day limited withdrawal guide: for one in 200 million of you
Leetcode simple question: check whether the string is an array prefix
C language self-made Games: Sanzi (tic tac toe chess) intelligent chess supplement
C Primer Plus Chapter 10, question 14 3 × 5 array
Sdl2 + OpenGL glsl practice (Continued)