Lowbit Sum

2024-02-06 19:32
文章标签 sum lowbit

本文主要是介绍Lowbit Sum,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Lowbit Sum
Num : 6
Time Limit : 1000ms
Memory Limit : 65536K
description
long long ans = 0;
for(int i = 1; i <= n; i ++)
ans += lowbit(i)
lowbit(i)的意思是将i转化成二进制数之后,只保留最低位的1及其后面的0,截断前面的内容,然后再转成10进制数
比如lowbit(7),7的二进制位是111,lowbit(7) = 1
6 = 110(2),lowbit(6) = 2,同理lowbit(4) = 4,lowbit(12) = 4,lowbit(2) = 2,lowbit(8) = 8
每输入一个n,求ans
input
多组数据,每组数据一个n(1 <= n <= 10^9)
output
每组数据输出一行,对应的ans
sample_input
1
2
3
sample_output
1
3
4
hint
source

分析:
lowbit(x) 代表取将x转化成二进制数之后,只保留最低位的1及其后面的
因此结构只能 是1+0(len) len代表0的个数
因此得到的结构也就只能是1,2,4,8….. 这样的2的多少次幂。
我们可以思考一下 如果一个数x是4的倍数 那么他满足他的lowbit(x)>=4;
我们从大到小枚举2的多少次幂y,然后判断在[1,x]内有多少个数是这样的
数的倍数,很简单 直接用x/y,但是我们还要减去是y*2的倍数的个数。
我们这样做相当于统计装换成2进制以后从第一位一直到第i位为0的数的个数,
不减的话就会出现重复,

因此
设x转化成2进制以后的长度为n,
ans = (x/(1 << n))(1 << n) + (x/(1 << (n-1))-x/(1<< n))(1 << (n-1))+…….+(x/(1<<0)-x/(1<<1))*(1<<0);

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;typedef long long LL;const int maxn = 33;LL a[maxn];void init(){for(LL i = 0;i<maxn;i++)a[i]=1LL<<i;
}int main()
{init();LL x;while(~scanf("%lld",&x)){LL ans = 0;int pos=lower_bound(a,a+maxn,x)-a;//这个函数是,找到第一个大于等于x的数if(a[pos]>x) pos--;LL cnt1=0,cnt2;for(int i=pos;i>=0;i--){cnt2=x/a[i]-cnt1;ans+=a[i]*cnt2;//cout<<"a[i]*cnt2 "<<a[i]*cnt2<<endl;cnt1=x/a[i];}printf("%lld\n",ans);}return 0;
}

另一种方法:
这道题考查的是对树状数组lowbit的深入理解。首先题意并不难懂,直接暴力或者lowbit打表的话都行不通。

换种思路,把它当成数学题来做。可以先把1~15的lowbit值打出来

可以发现规律,奇数位的lowbit值都为1。如果我们下次把偶数位的lowbit值全部取出来,都除以2

你会发现这原来这可以递归。这样一直递归下去,就可以求解。
最后需要注意的是n分奇偶两种情况:
1.当n为奇数时,第一次n / 2后原来奇数的数量其实是少1的,所以最后要把1加回来;
2.当n为偶数时,第一次n / 2分成的两部分正好没有缺漏,所以最后不作处理。

给出公式res( n ) = 2 * res ( n / 2) + n / 2 + ( n & 1) // (n & 1)操作等价于(n % 2) ,作为判断n的奇偶用,奇数时加1,偶数加0

例如:当n == 5时,1~5对应的lowbit值分别为1,、2、1、4、1,
第一次分时,原来奇数的那部分明明有3个1,但被n / 2弄成了2,所以计算结束后要加回来;
同理,当n== 4时,因为第一次分时就没有缺漏,所以最后不处理。
我们为什么只考虑第一次呢,因为缺漏值可能出现在第一次,以后的分割不会造成缺漏。

#include <stdio.h>
#include <iostream>using namespace std;typedef long long ll;ll solve(int x){if(x==1) return 1;ll ans=2*solve(x/2)+x/2;if(x&1)ans++;return ans;
}int main()
{int n;while(scanf("%d",&n)!=EOF)printf("%lld\n",solve(n));return 0;
}

这篇关于Lowbit Sum的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/685325

相关文章

最大流=最小割=最小点权覆盖集=sum-最大点权独立集

二分图最小点覆盖和最大独立集都可以转化为最大匹配求解。 在这个基础上,把每个点赋予一个非负的权值,这两个问题就转化为:二分图最小点权覆盖和二分图最大点权独立集。   二分图最小点权覆盖     从x或者y集合中选取一些点,使这些点覆盖所有的边,并且选出来的点的权值尽可能小。 建模:     原二分图中的边(u,v)替换为容量为INF的有向边(u,v),设立源点s和汇点t

如何导入sun.misc.BASE64Encoder和sum.misc.BASE64Decoder

右击项目名--->Build Path--->Configure Build Path...--->java Build Path--->Access rules:1 rule defined,added to all librar...   --->Edit --->Add...

ACdream1154(lowbit的理解)

Lowbit Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticNext Problem 题目链接: http://acdream.info/problem?pid=1154 Problem Description

18. 4 Sum

题目: 解答: 与之前的三数之和的解法类似,也是先排序,然后不断剔除不可能的条件,最后两个参数,通过两头求和计算得出。 代码: class Solution {public:vector<vector<int>> fourSum(vector<int>& nums, int target) {vector<vector<int>> result;int len = nums.size

apt-get update更新源时,出现“Hash Sum mismatch”问题

转载自:apt-get update更新源时,出现“Hash Sum mismatch”问题 当使用apt-get update更新源时,出现下面“Hash Sum mismatch”的报错,具体如下: root@localhost:~# apt-get update ...... ...... W: Failed to fetch http://us.archive.ubuntu.com/ub

[LeetCode] 303. Range Sum Query - Immutable

题:https://leetcode.com/problems/range-sum-query-immutable/description/ 题目 Given an integer array nums, find the sum of the elements between indices i and j (i ≤ j), inclusive. Example: Given nums

[LeetCode] 64. Minimum Path Sum

题:https://leetcode.com/problems/minimum-path-sum/description/ 题目 Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes the sum of all numbers

[LeetCode] 404. Sum of Left Leaves

题:https://leetcode.com/problems/sum-of-left-leaves/description/ 题目 Find the sum of all left leaves in a given binary tree. Example: 3/ \9 20/ \15 7There are two left leaves in the binary t

数论 --- 费马小定理 + 快速幂 HDU 4704 Sum

Sum  Problem's Link:   http://acm.hdu.edu.cn/showproblem.php?pid=4704   Mean:  给定一个大整数N,求1到N中每个数的因式分解个数的总和。   analyse: N可达10^100000,只能用数学方法来做。 首先想到的是找规律。通过枚举小数据来找规律,发现其实answer=pow(2,n-1);

LeetCode - 40. Combination Sum II

40. Combination Sum II  Problem's Link  ---------------------------------------------------------------------------- Mean:  给你一个待选集合s和一个数n,选出所有相加之和为n的组合.(每个元素只能选一次) analyse: 递归求解. 在递归进入