当前位置:网站首页>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;
}
边栏推荐
- The process of browser accessing the website
- stm32逆向入门
- SSM framework integration
- Number of 1 in binary (simple difficulty)
- Messy change of mouse style in win system
- Current market situation and development prospect prediction of global direct energy deposition 3D printer industry in 2022
- JDBC database operation
- Basic use of Metasploit penetration testing framework
- Market status and development prospect prediction of the global autonomous hybrid underwater glider industry in 2022
- Wechat applet waterfall flow and pull up to the bottom
猜你喜欢
Automatic voltage rise and fall 5-40v multi string super capacitor charging chip and solution
5-36v input automatic voltage rise and fall PD fast charging scheme drawing 30W low-cost chip
data2vec! New milestone of unified mode
并发操作-内存交互操作
Shuttle + alluxio accelerated memory shuffle take-off
Games101 Lesson 9 shading 3 Notes
[clock 223] [binary tree] [leetcode high frequency]: 102 Sequence traversal of binary tree
MediaTek 2023 IC written examination approved in advance (topic)
Source insight garbled code solution
Do you know UVs in modeling?
随机推荐
Market status and development prospects of the global autonomous marine glider industry in 2022
Leetcode simple question: check whether the array is sorted and rotated
[research materials] 2022q1 game preferred casual game distribution circular - Download attached
[set theory] binary relation (example of binary relation operation | example of inverse operation | example of composite operation | example of limiting operation | example of image operation)
[set theory] relational representation (relational matrix | examples of relational matrix | properties of relational matrix | operations of relational matrix | relational graph | examples of relationa
Number of uniform strings of leetcode simple problem
MediaTek 2023 IC written examination approved in advance (topic)
[Yu Yue education] basic reference materials of interchangeability and measurement technology of Zhongyuan Institute of Technology
Distinguish between releases and snapshots in nexus private library
Silent authorization login and registration of wechat applet
Keepalived热备与HAProxy
Sdl2 + OpenGL glsl practice (Continued)
The 19th Zhejiang I. barbecue
Sprintf formatter abnormal exit problem
Leetcode simple problem delete an element to strictly increment the array
雇佣收银员(差分约束)
Current market situation and development prospect forecast of global UV sensitive resin 3D printer industry in 2022
Market status and development prospect forecast of global button dropper industry in 2022
7. Integrated learning
Messy change of mouse style in win system