背包问题
f[i][v]=max{f[i-1][v],f[i-1][v-c[i]]+w[i]}
f[v]=max{f[v],f[v-c[i]]+w[i]}//表示前i件物品的取值至于前i-1件物品的状态值有关,可省略一维
目前主要是套公式。
伪码
for i=1… N
for v=0… V
f[v]=max{f[v], f[v-c[i]+w[i]};
完全背包 多重背包
f[i][j]只与f[i-1][j]和
f[i][j-w[i]]+v[i]有关。考虑
f[i][j]时,由于是从前往后
写,一维数组表示的f[j]
还没被写入,它表示的是
f[i-1][j],而f[j-w[i]]已经被写
入,它表示的是f[i][j-w[i]]
一维的f[v]=max{f[v],
f[v-w[i]]+v[i]}
表示的恰好是二维的
f[i][v]=max{f[i-1][v],f[i][v-w[i]]+v[i]}
星期天的比赛
A.Potion-making
You have an initially empty cauldron, and you want to brew a potion in it. The potion consists of two ingredients: magic essence and water. The potion you want to brew should contain exactly k % magic essence and (100−k) % water.
In one step, you can pour either one liter of magic essence or one liter of water into the cauldron. What is the minimum number of steps to brew a potion? You don’t care about the total volume of the potion, only about the ratio between magic essence and water in it.
#include<iostream>
using namespace std;
int main()
{
int t;
int k;
int sum;
cin>>t;
while(t--)
{
cin>>k;
sum=100;
while(k%2==0)
{ k=k/2;
if(sum%2!=0)
break;
sum=sum/2;
}
while(k%5==0)
{ k=k/5;
if(sum%5!=0)
break;
sum=sum/5;
}
cout<<sum<<endl;
}
return 0;
}