ZR1158

http://www.zhengruioi.com/contest/446/problem/1158

给定限制的问题大多数都是容斥或者二分,或者二分之后容斥

首先,这个问题的第一步我们还是比较容易地去转换的,发现每个物品选的次数一定是\(2^i - 1\)次,而下一次我们就是花费\(2^i\)的代价去把答案\(+1\)

第\(x\)物品第\(i\)次选择的代价就是\(x\times 2^{i - 1}\),

现在问题变成了一个最大的代价\(cost\)使得所有代价小于等于\(cost\)的物品全部选择之后代价和不超过\(n\)

这个东西就有二分性,所以,不能直接二分答案的题目尝试把问题转化后进行二分,所以,我们接下来问题变成了对于一个给定的代价\(M\),求所有代价小于等于\(M\)的物品的代价之和

之后我们发现所有小于等于\(M\)的代价是长成这个样子的

\[\begin{align}
& 1\times 2 ^0 \ \ \ \ \ 2\times2^0 \ \ \ \ \ \dots \ \ \ \ \ M \times 2^0\\\
& 2\times2^1 \ \ \ \ \ 2\times2^1 \ \ \ \ \ \dots \ \ \ \ \ \lfloor\frac{M}{2}\rfloor\times2^1\\
& \dots
\end{align}
\]

很明显,最多只会有log行,每一行都是一个等差数列

而很明显尾项不能超过\(M\),所以项数就会变成\(\lfloor\frac{M}{x}\rfloor\)

这样我们直接暴力枚举log行,每一行都用等差数列求和算一下总的贡献即可

之后注意

我们设最终二分答案出的答案为\(x\),那么\(x\)一定是\(k\times2^i - 1\)的形式

可能会出现\(k+1\)不能全部放下,但是可以放一部分的情况,这一部分我们可以通过除法最后计算贡献

  1. #include<cstdio>
  2. #include<iostream>
  3. #include<queue>
  4. #include<algorithm>
  5. #include<cstring>
  6. #include<cctype>
  7. #include<vector>
  8. #include<ctime>
  9. #include<cmath>
  10. #include<set>
  11. #include<map>
  12. #define LL long long
  13. #define pii pair<LL,LL>
  14. #define mk make_pair
  15. #define fi first
  16. #define se second
  17. using namespace std;
  18. int T;LL n;
  19. LL sum;
  20. inline LL check(LL x){
  21. sum = 0;
  22. for(LL p = 1;p <= x;p <<= 1) sum += (x / p * p + p) * (x / p) / 2;
  23. return sum;
  24. }
  25. inline LL geta(LL x){
  26. LL res = 0;
  27. for(LL p = 1;p <= x;p <<= 1) res += x / p;
  28. return res;
  29. }
  30. int main(){
  31. scanf("%d",&T);
  32. while(T--){
  33. scanf("%lld",&n);int ans = 0;LL res = 0;
  34. int l = 1,r = 2000000000;
  35. while(l <= r){
  36. int mid = (l + r) >> 1;
  37. LL now = check(mid);
  38. // printf("%d %d %d %lld\n",l,r,mid,now);
  39. if(now <= n) l = mid + 1,ans = mid,res = now;
  40. else r = mid - 1;
  41. }
  42. printf("%lld\n",geta(ans) + (n - res) / (ans + 1));
  43. }
  44. return 0;
  45. }

ZR1158的更多相关文章

随机推荐

  1. $(window).scrollTop() == $(document).height() - $(window).height()(底端)

    jQuery(window).height()代表了当前可见区域的大小,而jQuery(document).height()则代表了整个文档的高度,可视具体情况使用. 注意当浏览器窗口大小改变时(如最 ...

  2. 每天一个linux命令(1): which命令

    0.学习时间: 2014-05-15 which命令用来在PATH指定的路径中查找特定的文件, 并返回第一个找到的结果. 1. 命令格式:  which 文件名 2.命令功能 一般使用which命令来 ...

  3. Codeforces 414B

    题目链接 附上代码: #include<cstdio> #include<cstring> #include<bits/stdc++.h> #define mod ...

  4. C++:for范围循环特点--自我理解

    for(declaration : expression)statement for(xx-type i : P)....其一:for范围类型循环在循环前,可能会对p所在的队列里,对每一个对象进行一次 ...

  5. 中文乱码在java中URLEncoder.encode方法要调用两次解决

    中文乱码在java中URLEncoder.encode方法要调用两次解决 一.场景: 1.我在客户端要通过get方式调用服务器端的url,将中文参数做utf-8编码,需要在js中两次的进行编码,服务器 ...

  6. ssh 出错 Permission denied (publickey,password).

    将客户端的~/.ssh/know_hosts 文件删掉试试 ssh debug信息 ssh -vvv xxx@192.168.1.111

  7. android 数据存储----android短信发送器之文件的读写(手机+SD卡)

    本文实践知识点有有三: 1.布局文件,android布局有相对布局,线性布局,绝对布局,表格布局,标签布局等.各个布局能够嵌套的.本文的布局文件就是线性布局的嵌套 <LinearLayout x ...

  8. phpexecl

    <?phpnamespace Admin\Controller;use Think\Controller;class InoutController extends Controller { p ...

  9. PyTorch代码调试利器: 自动print每行代码的Tensor信息

    本文介绍一个用于 PyTorch 代码的实用工具 TorchSnooper.作者是TorchSnooper的作者,也是PyTorch开发者之一. GitHub 项目地址: https://github ...

  10. mysql 中 DATE_ADD(date,INTERVAL expr type)

    在Hiredmyway中: SELECT if(LENGTH(company_name) > 30,                   concat(SUBSTRING(company_nam ...