当前位置:网站首页>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;
}边栏推荐
- [set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
- [SQL injection] joint query (the simplest injection method)
- Blog building tool recommendation (text book delivery)
- Market status and development prospect forecast of global button dropper industry in 2022
- The 19th Zhejiang I. barbecue
- Shuttle + alluxio accelerated memory shuffle take-off
- The reason why the entity class in the database is changed into hump naming
- LVS load balancing cluster of efficient multi-purpose cluster (NAT mode)
- Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
- I stepped on a foundation pit today
猜你喜欢

Career planning of counter attacking College Students

Source insight garbled code solution

2022-02-11 daily clock in: problem fine brush
![[XSS bypass - protection strategy] understand the protection strategy and better bypass](/img/72/d3e46a820796a48b458cd2d0a18f8f.png)
[XSS bypass - protection strategy] understand the protection strategy and better bypass

Online VR model display - 3D visual display solution

Shuttle + alluxio accelerated memory shuffle take-off

Oracle SQL table data loss

ZABBIX monitoring of lamp architecture (2): ZABBIX basic operation
![[research materials] 2021 China's game industry brand report - Download attached](/img/b7/a377b0b7c742078e2feb28ebfbca62.jpg)
[research materials] 2021 China's game industry brand report - Download attached

Truncated sentences of leetcode simple questions
随机推荐
Blog building tool recommendation (text book delivery)
sql语句模糊查询遇到的问题
Market status and development prospect forecast of global heat curing adhesive industry in 2022
RT thread flow notes I startup, schedule, thread
Number of 1 in binary (simple difficulty)
Preparation for school and professional cognition
Leetcode simple question: check whether two string arrays are equal
Notes | numpy-11 Array operation
Introduction to message queuing (MQ)
Games101 Lesson 9 shading 3 Notes
The usage of micro service project swagger aggregation document shows all micro service addresses in the form of swagger grouping
雇佣收银员(差分约束)
Handler understands the record
Shuttle + Alluxio 加速内存Shuffle起飞
Hire cashier (differential constraint)
Market status and development prospect prediction of global fermented plant protein industry in 2022
文献阅读_基于多模态数据语义融合的旅游在线评论有用性识别研究(中文文献)
Notes | numpy-08 Advanced index
[develop wechat applet local storage with uni app]
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution