UVa 11986 - Save from Radiation (推理)

题意

N + 1个瓶子里有一瓶毒药,老鼠喝了就会死,现在要求用最少的老鼠判断出哪瓶是毒药

思路

把瓶子从000 ~ N(2进制)编号,每个瓶子都有唯一的二进制表示。

问题转化为确定毒药的二进制编号。

比如说有9个瓶子(8 + 1),编号为0000 ~ 1000,假设有毒的是1000,也就是第9个瓶子 
现在拿出第一位编号是0的瓶子,给第一只老鼠喝。 
拿出第二位编号是0的瓶子,给第二只老鼠。 
同上,一直到第四位。 
上面的过程是同时进行的。

显然前三只都不会死,最后一只不死。 
编号确定。

所以就是求N的二进制位数

代码

 
 
  1. #include <cstdio>
  2. #include <stack>
  3. #include <list>
  4. #include <set>
  5. #include <iostream>
  6. #include <string>
  7. #include <vector>
  8. #include <queue>
  9. #include <functional>
  10. #include <cstring>
  11. #include <iomanip>
  12. #include <algorithm>
  13. #include <cctype>
  14. #include <string>
  15. #include <map>
  16. #include <cmath>
  17. using namespace std;
  18. #define LL long long
  19. #define ULL unsigned long long
  20. #define SZ(x) (int)x.size()
  21. #define Lowbit(x) ((x) & (-x))
  22. #define MP(a, b) make_pair(a, b)
  23. #define MS(arr, num) memset(arr, num, sizeof(arr))
  24. #define PB push_back
  25. #define X first
  26. #define Y second
  27. #define ROP freopen("input.txt", "r", stdin);
  28. #define MID(a, b) (a + ((b - a) >> 1))
  29. #define LC rt << 1, l, mid
  30. #define RC rt << 1|1, mid + 1, r
  31. #define LRT rt << 1
  32. #define RRT rt << 1|1
  33. #define BitCount(x) __builtin_popcount(x)
  34. #define BitCountll(x) __builtin_popcountll(x)
  35. #define LeftPos(x) 32 - __builtin_clz(x) - 1
  36. #define LeftPosll(x) 64 - __builtin_clzll(x) - 1
  37. const double PI = acos(-1.0);
  38. const int INF = 0x3f3f3f3f;
  39. const double eps = 1e-8;
  40. const int MAXN = 1000 + 10;
  41. const int MOD = 1000007;
  42. const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
  43. int cases = 0;
  44. typedef pair<int, int> pii;
  45. typedef vector<int>::iterator viti;
  46. typedef vector<pii>::iterator vitii;
  47. int main()
  48. {
  49. LL n, T;
  50. cin >> T;
  51. while (T--)
  52. while (cin >> n)
  53. {
  54. if (n != 0) printf("Case %d: %d\n", ++cases, LeftPosll(n) + 1);
  55. else printf("Case %d: 0\n", ++cases);
  56. }
  57. return 0;
  58. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值