hdu 4737 A Bit Fun

曾经有道题是说给出一个数组a0, a1 ... , an-1,一个数字m,设 f(i, j) = ai + ai+1 + ai+2 +  ... + aj ,求(i,j)使f(i,j) < m的数量

原理与这题一样,+ 换成 | ,需要换种数据结构,重载下运算符

#include <cstdio>
#include <iostream>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
// http://acm.hdu.edu.cn/showproblem.php?pid=4737
using namespace std;
const int MAXN = 100005;
const int NN = 31;
const int CN = NN*sizeof(int);
struct node
{
	int bt[31], nm;
	node (){ nm = 0; memset(bt, 0, CN); }
	node (int a)
	{
		node();
		nm = a;
		for (int i = 0; i< NN; ++i)
		{
			bt[i] = a & 0x1;
			a >>= 1;
		}
	}
	node operator + (const node &a)
	{
		node res;
		res.nm = nm | a.nm;
		for (int i = 0; i< NN; ++i)
		{
			res.bt[i] = bt[i] + a.bt[i];
		}
		return res;
	}
	node operator - (const node &a)
	{
		node res;
		for (int i = 0; i< NN; ++i)
		{
			res.bt[i] = bt[i] - a.bt[i];
		}
		for (int i = 0; i< NN; ++i)
		{
			if (res.bt[i])
			{
				res.nm |= 1<<i;
			}
		}
		return res;
	}
};
node da[MAXN];
int main() 
{
#ifndef ONLINE_JUDGE
	freopen("in.txt", "r", stdin);
#endif
	int t;
	scanf("%d", &t);
	for (int _ = 1; _ <= t; ++_)
	{
		printf("Case #%d: ", _);
		int n, m;
		scanf("%d%d", &n, &m);
		for (int i = 0; i< n; ++i)
		{
			int cc;
			scanf("%d", &cc);
			da[i] = node(cc);
		}
		node sum(0), tp;
		int res = 0;
		int i = 0, j =0;
		while (i < n)
		{
			while (j < n)
			{
				tp = sum + da[j];
				if (tp.nm >= m)
				{
					if (i == j)
					{
						j = ++i;
					}
					else
					{
						res += j-i;
						sum = sum - da[i++];
					}
					break;
				}
				else
				{
					++j;
					sum = tp;
				}	
			}
			if (j == n)
			{
				res += (n-i)*(n-i+1)/2;
				break;
			}
		}
		printf("%d\n", res);
	}
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值