• CodeForces


    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

    he 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 cin, cout streams or the %I64d specifier.

    Examples

    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) = 1, f(1, 2) = 3, f(1, 3) = 3, f(2, 2) = 2, f(2, 3) = 2, f(3, 3) = 0. There are exactly 4 distinct numbers among them: 0, 1, 2, 3.

    题解:

    这个题是真的迷。首先区间DP的话空间不够,枚举估计也要超时。当时真是想了各种方法都不行。之后一看别人的AC代码,WTFC!。两层for循环加一个剪枝就过去了。相当于是个O(NlogN)复杂度。这TM不就是测试数据水吗???真的烦。

    代码:

    #include <bits/stdc++.h>
    
    using namespace std;
    
    const int MAXN = 100005;
    
    int board[MAXN];
    
    set<int> S;
    
    int main(){
    	
    	int N;
    	while(scanf("%d",&N)!=EOF){
    		S.clear();
    		for(int i=0 ; i<N ; i++){
    			scanf("%d",&board[i]);
    		}
    		for(int i=0 ; i<N ; i++){
    			int a = board[i];
    			int b = 0;
    			S.insert(a);
    			for(int j=i+1 ; j<N ; j++){
    				a |= board[j]; 
    				b |= board[j];
    				S.insert(a);
    				if(a == b)break;//这里剪枝减掉了当已有数进行位或运算达到所有位都为1后的运算。
    				//如果a本身就等于0那么也直接减掉就行,不影响结果。 
    			}
    		}
    		printf("%d
    ",S.size());
    	}
    	
    	return 0;
    } 

  • 相关阅读:
    【翻译自mos文章】 11gR1版本号 asmcmd的新命令--cp、md_backup、md_restore
    Android实现ListView或GridView首行/尾行距离屏幕边缘距离
    iOS-为方便项目开发在pch加入一些经常使用宏定义
    [ACM] FZU 1686 神龙的难题 (DLX 反复覆盖)
    Cocos2d-x Touch事件处理机制
    在linux環境下安裝jprofiler_linux_8_0_2.sh
    QT5 Failed to load platform plugin &quot;windows&quot; 终极解决方式 命令行问题
    我们想要如何子的生活?
    javaEE mvc样例具体解释
    安装Kali Linux操作系统Kali Linux无线网络渗透
  • 原文地址:https://www.cnblogs.com/vocaloid01/p/9514114.html
Copyright © 2020-2023  润新知