题目链接 https://www.51nod.com/Challenge/Problem.html#!#problemId=2480
思路:多重背包的优化是二进制,所以这个题也可以用到二进制,通过加上一个数从而让这个到达区间更远
AC代码如下
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
const ll maxn = 1e5 + 5;
const ll mod = 1e9 + 7;
ll a[maxn];
int main()
{
ios::sync_with_stdio(0);
cin.tie(0),cout.tie(0);
ll m,n;
cin >> m;
for(int i = 1; i <= m; i ++)
{
cin >> a[i];
}
cin >> n;
ll ans = 0;
ll j = 1,i = 1;
while(j <= n)
{
if(i <= m && j >= a[i])j += a[i++];
else
{
j *= 2;
ans ++;
}
}
cout << ans << endl;
return 0;
}