• ZR1158


    ZR1158

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

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

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

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

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

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

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

    [egin{align} & 1 imes 2 ^0 2 imes2^0 dots M imes 2^0\ & 2 imes2^1 2 imes2^1 dots lfloorfrac{M}{2} floor imes2^1\ & dots end{align} ]

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

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

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

    之后注意

    我们设最终二分答案出的答案为(x),那么(x)一定是(k imes2^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
    ",l,r,mid,now);
    			if(now <= n) l = mid + 1,ans = mid,res = now;
    			else r = mid - 1;
    		}
    		printf("%lld
    ",geta(ans) + (n - res) / (ans + 1));
    	}
    	return 0;
    }
    
    
  • 相关阅读:
    Struts1、Struts2的线程安全问题
    java基础系列——线程池
    Memcached基础
    Git基本应用
    Angular UI框架 Ng-alain @delon的脚手架的生成开发模板
    .NET CORE 框架ABP的代码生成器(ABP Code Power Tools )使用说明文档
    角落的开发工具集之Vs(Visual Studio)2017插件推荐
    【52ABP实战教程】0.3-- 从github推送代码回vsts实现双向同步
    【52ABP实战教程】0.1-- Devops如何用VSTS持续集成到Github仓库!
    【52ABP实战教程】0.2-- VSTS中的账号迁移到东亚
  • 原文地址:https://www.cnblogs.com/wyxdrqc/p/11736050.html
Copyright © 2020-2023  润新知