ZVRK 函数

题目大意

求整数 aabb 的数的 lowbit 之和。

解题思路

枚举找规律。

#include <bits/stdc++.h>
using namespace std;

#define int long long

int a, b;

const int _ = 10000000;

int lowbit(int x)
{
	return x & -x;
}

int t_1[_], t_2[_],t_4[_], t_8[_], t_16[_], t_32[_], t_64[_], t_128[_];

int t_11, t_22, t_44, t_88, t_1616, t_3232, t_6464, t_128128;

signed main()
{
	freopen("666.out", "w", stdout);
//	scanf("%lld%lld", &a, &b);
	for(int i = 1; i <= 400; ++i)
	{
		int x = lowbit(i);
//		cout << x << " ";
		if(x == 1)
		{
			t_1[++t_11] = i;
		}
		if(x == 2)
		{
			t_2[++t_22] = i;
		}
		if(x == 4)
		{
			t_4[++t_44] = i;
		}
		if(x == 8)
		{
			t_8[++t_88] = i;
		}
		if(x == 16)
		{
			t_16[++t_1616] = i;
		}
		if(x == 32)
		{
			t_32[++t_3232] = i;
		}
		if(x == 64)
		{
			t_64[++t_6464] = i;
		}
		if(x == 128)
		{
			t_128[++t_128128] = i;
		}
	}
	cout << t_1[1];
	for(int i = 2; i <= t_11; ++i) cout << " " << t_1[i];
	cout << endl;
	cout << t_2[1];
	for(int i = 2; i <= t_22; ++i) cout << " " << t_2[i];
	cout << endl;
	cout << t_4[1];
	for(int i = 2; i <= t_44; ++i) cout << " " << t_4[i];
	cout << endl;
	cout << t_8[1];
	for(int i = 2; i <= t_88; ++i) cout << " " << t_8[i];
	cout << endl;
	cout << t_16[1];
	for(int i = 2; i <= t_1616; ++i) cout << " " << t_16[i];
	cout << endl;
	cout << t_32[1];
	for(int i = 2; i <= t_3232; ++i) cout << " " << t_32[i];
	cout << endl;
	cout << t_64[1];
	for(int i = 2; i <= t_6464; ++i) cout << " " << t_64[i];
	cout << endl;
	cout << t_128[1];
	for(int i = 2; i <= t_128128; ++i) cout << " " << t_128[i];
	cout << endl;
	cout << "1:" << t_11 << endl;
	cout << "2:" << t_22 << endl;
	cout << "4:" << t_44 << endl;
	cout << "8:" << t_88 << endl;
	cout << "16:" << t_1616 << endl;
	cout << "32:" << t_3232 << endl;
	cout << "64:" << t_6464 << endl;
	cout << "128:" << t_128128 << endl;
	return 0;
}

考虑差分。

sum(b)sum(a1)sum(b)-sum(a-1)

发现 2i2^i 的贡献为 (nn2i2^i 的倍数的个数 - nn2i+12^{i+1} 的倍数的个数) 2i*2^i

AC CODE

#include <bits/stdc++.h>

using namespace std;

#define int long long

int kkk(double d)
{
	if((double)d > (int)d)
	{
		return (int)d + 1;
	}
}
int query(int x)
{
	int ans = 0;
	for(int i = 1; i <= x; i *= 2)
	{
		ans += (x / i - x / (i * 2)) * i;
	}
	return ans;
}

int a, b;

signed main()
{
	scanf("%lld%lld", &a, &b);
	if(a > b) swap(a, b);
	printf("%lld\n", (long long)query(b) - query(a - 1));
}
posted @ 2021-09-25 13:15  蒟蒻orz  阅读(8)  评论(0编辑  收藏  举报  来源