Uva 11384 Help is needed for Dexter

题意:给定正整数n,你的任务是用最少的操作次数把序列1,2,...,n中所有数都变成0。

每次操作可以从序列中选择一个或者多个整数,同时减去一个相同整数,求最小的操作次数。

题解:

我们可以先列举几个数

数   次数

1     1

2     2

3     2

4     3

5     3

6     3

7     3

8     4

发现了一定的规律,通过题意我们发现每次操作可以从序列中选择一个或者多个整数,不需要连续,这一条性质是非常有用的。

1 2 3 4 5 6 这一序列,我们可以将 4 5 6 同时减去 3 ->这样就变成了 1 2 3 可以将其与1 2 3重叠,及选择时与 1 2 3一样即可,这样f[6]=f[3]+1->f[n]=f[n/2]+1;

 1 #include<algorithm>
 2 #include<iostream>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<string>
 6 #include<cstdio>
 7 
 8 using namespace std;
 9 
10 int n;
11 
12 int main()
13 {
14     while (~scanf("%d",&n))
15     {
16         int ans=0;
17         while (n>0)
18         {
19             ans++;
20             n/=2;
21         }
22         printf("%d
",ans);
23     }
24 }
View Code
原文地址:https://www.cnblogs.com/fengzhiyuan/p/6985256.html