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


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;
 }






發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章