Codeforces Round #150 (Div. 2) C. The Brand New Function

37 篇文章 0 订阅
8 篇文章 0 订阅


The Brand New Function
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Polycarpus has a sequence, consisting of n non-negative integers: a1, a2, ..., an.

Let's define function f(l, r) (l, r are integer, 1 ≤ l ≤ r ≤ n) for sequence a as an operation of bitwise OR of all the sequence elements with indexes from l to r. Formally: f(l, r) = al | al + 1 | ...  | ar.

Polycarpus took a piece of paper and wrote out the values of function f(l, r) for all l, r (l, r are integer, 1 ≤ l ≤ r ≤ n). Now he wants to know, how many distinct values he's got in the end.

Help Polycarpus, count the number of distinct values of function f(l, r) for the given sequence a.

Expression x | y means applying the operation of bitwise OR to numbers x and y. This operation exists in all modern programming languages, for example, in language C++ and Java it is marked as "|", in Pascal — as "or".

Input

The first line contains integer n (1 ≤ n ≤ 105) — the number of elements of sequence a. The second line contains n space-separated integers a1, a2, ..., an (0 ≤ ai ≤ 106) — the elements of sequence a.

Output

Print a single integer — the number of distinct values of function f(l, r) for the given sequence a.

Please, do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use cincout streams or the %I64dspecifier.

Sample test(s)
input
3
1 2 0
output
4
input
10
1 2 3 4 5 6 1 2 9 10
output
11
Note

In the first test case Polycarpus will have 6 numbers written on the paper: f(1, 1) = 1f(1, 2) = 3f(1, 3) = 3f(2, 2) = 2f(2, 3) = 2,f(3, 3) = 0. There are exactly 4 distinct numbers among them: 0, 1, 2, 3.

题意:

给你n个数,在1~n这个区间上,任意选择两个左右端点,求出这个子区间上的数连续或之后的结果,然后求一共有有多少个不同的数。前两天刚刚学会了STL中的set,现在就用到了,very very方便啊!

这里最主要的是优化,因为单纯的用set会超时。这里提到我们不得不说的或运算了。这道题我或运算都整了半天,首先如果f(l,r)==f(l+1,r),则对于任意r=<x<n,都有f(l,x)==f(l+1,x),所以直接可以继续,因为下一个循环也会出现这个数的,所以直接来一个break就可以了。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<set>
#include<algorithm>

using namespace std;
int a[100005],p[1000005];
set<int>ans;
set<int>::iterator iter;
 int main()
 {
  int n,i,j,k;
  cin>>n;
 for(i=0;i<n;i++)
 {
    cin>>a[i];
    ans.insert(a[i]);
 }
int s,e;
 for(i=0;i<n;i++)
  {
       s=a[i]; e=0;
      for(j=i+1;j<n;j++)
         {
             s=s|a[j]; e=e|a[j];
             //cout<<i<<" "<<j<<" "<<s<<"&&"<<endl;
             ans.insert(s);
          if(s==e) break;
         }
  }
  //for(iter=ans.begin();iter!=ans.end();iter++)
   // cout<<*iter<<"**"<<endl;
 cout<<ans.size();

 ans.clear();
     return 0;
 }






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值