ZR1158
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\)的代价是长成这个样子的
& 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\)不能全部放下,但是可以放一部分的情况,这一部分我们可以通过除法最后计算贡献
#include<cstdio>
#include<iostream>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cctype>
#include<vector>
#include<ctime>
#include<cmath>
#include<set>
#include<map>
#define LL long long
#define pii pair<LL,LL>
#define mk make_pair
#define fi first
#define se second
using namespace std;
int T;LL n;
LL sum;
inline LL check(LL x){
sum = 0;
for(LL p = 1;p <= x;p <<= 1) sum += (x / p * p + p) * (x / p) / 2;
return sum;
}
inline LL geta(LL x){
LL res = 0;
for(LL p = 1;p <= x;p <<= 1) res += x / p;
return res;
}
int main(){
scanf("%d",&T);
while(T--){
scanf("%lld",&n);int ans = 0;LL res = 0;
int l = 1,r = 2000000000;
while(l <= r){
int mid = (l + r) >> 1;
LL now = check(mid);
// printf("%d %d %d %lld\n",l,r,mid,now);
if(now <= n) l = mid + 1,ans = mid,res = now;
else r = mid - 1;
}
printf("%lld\n",geta(ans) + (n - res) / (ans + 1));
}
return 0;
}
ZR1158的更多相关文章
随机推荐
- JAVA-WEB-错误之-'OPTION SQL_SELECT_LIMIT=DEFAULT'
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version ...
- Directx11教程37 纹理映射(7)
原文:Directx11教程37 纹理映射(7) 本章是在教程35.36的基础上来实现一个光照纹理结合的程序,就是把场景中旋转的cube加上纹理. lighttex.vs中顶点的结构现在 ...
- closest和parents方法区别
今天第一次看到closest方法,以前也从来没用过. 该方法从元素本身开始往上查找,返回最近的匹配的祖先元素. 1.closest查找开始于自身,parents开始于元素父级 2.closest向上查 ...
- docker-compose进入容器出现unable to find user root: no matching entries in passwd file
解决办法: 先docker-compose stop 容器,再docker-compose start 容器.虽然这样可以很快 解决问题,但并非长久之计.
- nodeJs学习-15 mysql中间件下载与使用、基本用法
下载mysql中间件(客户端):cnpm install mysql 链接数据库.查询示例: const mysql=require('mysql'); //1.连接 //createConnecti ...
- iOS开发周报-- 第一期
从Java转iOS第一个项目总结 http://www.cocoachina.com/ios/20150417/11595.html icon设计探讨:图标,文字,还是图标加文字? http://ww ...
- python 局部变量
- 通过反射拿到构造方法 Day25
package com.sxt.constructor; /* * 反射 * Class类拿到构造方法 */ import java.lang.reflect.Constructor; public ...
- 修改eclipse默认注释
windows-->preference-->Java-->Code Style-->Code Templates -->Comments :注释--> ... 关 ...
- init()方法必须使用super.init(config)的原因--Servlet
原 因: 一个servlet在它的init()方法中传递它的ServletConfig实例,在其他的方法中却不可以.当一个servlet在 init()方法外需要调用config对象时就会产生问题.使 ...