题目描述:

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, ..., a**n.

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) = a**l | a**l + 1 | ... | a**r.

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, ..., a**n (0 ≤ a**i ≤ 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

Copy

3
1 2 0

Output

Copy

4

Input

Copy

10
1 2 3 4 5 6 1 2 9 10

Output

Copy

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.

思路:

这道题刚开始觉得呀!肯定非常繁。但之后再想就有点想法了。想法来自于一个我之前想到的或的性质,就是一个数或另一个数结果是大于等于这两个数,也就是或起来只能增加或不变。然后又想到了题目让求几种可能的值。想到如果把所有数都或起来,那么得到的就是最大值。其次是每个数单独与自己或还是自己,于是有几个不同的数就可以在结果上先加上几。一看数的大小也就\(10^6\),那么或的最大个数也就是\(1111111\)个。可以用一个数组直接标记已获得了哪些值。最后就是想到二重循环,遍历每一种可能。但这样肯定超时啊。再想想。在胡思乱想想能不能用线性时间求解时想到了一个思路,是这样的:从头开始,进行累计或运算,每次把当前或的值与下一个或会得到的值做个比较,如果出现当前值等于下一次或的值时,我们可以说在这个位置及以前的累计值暂时做完了贡献,为什么是暂时呢?因为后面出现的数有可能还会改变这个值。

优化的思想与上面的原理相似,就是排除贡献做完的情况。我在计算或值时用两个变量,一个初始值是起点的值,另一个初始值为零。如果我在累计或值的时候发现两个或值竟然相等了,说明什么?说明起点的值完全没有起到作用对不对?它的贡献相对于后面是零,也就是说我用不用这个数对到两个值相等的后面位置没影响。那就可以跳出循环继续以下一个数为起点计算之后的或值。

网上关于这个优化的解释是数全为一后或它就是本身之类的,但我觉得好像是错的。因为我按照这种解释把这个优化变了一下形,把\(1111111\)之内二进制全为1的数预处理出来,累计或值过程中遇到这样的数就跳出循环,但在样例下得到的答案并不对。然后仔细想了想应该是上面那种解释。

代码:

#include <iostream>
#include <cstdio>
#define max_n 100005
using namespace std;
int n;
int a[max_n];
int cnt[1111112];
int cou = 0;
inline void read(int& x)
{
x=0;int f=0;char ch=getchar();
while('0'>ch||ch>'9'){if(ch=='-')f=1;ch=getchar();}
while('0'<=ch&&ch<='9'){x=10*x+ch-'0';ch=getchar();}
x=f?-x:x;
}
int main()
{
read(n);
for(int i = 0;i<n;i++)
{
read(a[i]);
if(cnt[a[i]]==0)
{
cnt[a[i]]=1;
cou++;
}
}
for(int i = 0;i<n;i++)
{
int last = a[i];
int prime = 0;
for(int j = i+1;j<n;j++)
{
last |= a[j];
if(cnt[last]==0)
{
cnt[last] = 1;
cou++;
}
prime |= a[j];
//cout << "last " << last << endl;
if(last==prime)
{
break;
}
}
}
printf("%d",cou);
return 0;
}

参考文章

关于优化的解释不对就不放了吧。

Codeforces G. The Brand New Function(枚举)的更多相关文章

  1. Codeforces243A The Brand New Function

    A. The Brand New Function time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  2. CodeForces 444C. DZY Loves Physics(枚举+水题)

    转载请注明出处:http://blog.csdn.net/u012860063/article/details/37509207 题目链接:http://codeforces.com/contest/ ...

  3. Codeforces H. Prime Gift(折半枚举二分)

    题目描述: Prime Gift time limit per test 3.5 seconds memory limit per test 256 megabytes input standard ...

  4. Codeforces Gym 100187M M. Heaviside Function two pointer

    M. Heaviside Function Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100187/ ...

  5. codeforces 678D D. Iterated Linear Function(水题)

    题目链接: D. Iterated Linear Function time limit per test 1 second memory limit per test 256 megabytes i ...

  6. Codeforces 626D Jerry's Protest(暴力枚举+概率)

    D. Jerry's Protest time limit per test:2 seconds memory limit per test:256 megabytes input:standard ...

  7. D. Diverse Garland Codeforces Round #535 (Div. 3) 暴力枚举+贪心

    D. Diverse Garland time limit per test 1 second memory limit per test 256 megabytes input standard i ...

  8. Codeforces gym101612 L.Little Difference(枚举+二分)

    传送:http://codeforces.com/gym/101612 题意:给定一个数n(<=1e18),将n分解为若干个数的成绩.要求这些数两两之间的差值不能大于1. 分析: 若n==2^k ...

  9. Codeforces 734C Anton and Making Potions(枚举+二分)

    题目链接:http://codeforces.com/problemset/problem/734/C 题目大意:要制作n个药,初始制作一个药的时间为x,魔力值为s,有两类咒语,第一类周瑜有m种,每种 ...

随机推荐

  1. ASP.NET Core 中的 Main 方法

    ASP.NET Core 中的 Main 方法 在 ASP.NET Core 项目中,我们有一个名为Program.cs的文件.在这个文件中,我们有一个public static void Main( ...

  2. 安装WIN7系统备忘录

    安装WIN7系统备忘录因为安装WIN7设置项太多,制作RAMOS如果忘了某项设置,终归是不方便,记录如下:1.用WINNTSETUP安装到VHD中,安装时优化选项中建议勾选关闭休眠和虚拟内存功能(假设 ...

  3. MySQL实战45讲学习笔记:第二十一讲

    一.引子 在上一篇文章中,我和你介绍了间隙锁和 next-key lock 的概念,但是并没有说明加锁规则.间隙锁的概念理解起来确实有点儿难,尤其在配合上行锁以后,很容易在判断是否会出现锁等待的问题上 ...

  4. 浅析RPO漏洞攻击原理

    RPO的全称为Relative Path Overwrite,也就是相对路径覆盖,利用客户端和服务端的差异,通过相对路径来引入我们想要的js或者css文件,从而实现某种攻击. 就目前来看此攻击方法依赖 ...

  5. javaSript 处理电脑和浏览器pc端缩放对页面的影响

    由于用户的个人习惯或其他原因,可能会调整到电脑上的缩放比例或者浏览器的缩放比例,如果访问页面会看起来比较不好看, 电脑上的缩放: 浏览器上的缩放: 解决方法: function detectZoom( ...

  6. 你真的了解strtotime('X month')吗

    经常会有人被strtotime结合-1 month, +1 month, next month的时候搞得很困惑, 然后就会觉得这个函数有点不那么靠谱, 动不动就出问题. 用的时候就会很慌… 今天是20 ...

  7. springcloud(六,多个服务提供者)

    spring cloud (一.服务注册demo_eureka) spring cloud (二.服务注册安全demo_eureka) spring cloud (三.服务提供者demo_provid ...

  8. 分布式数据库缓存系统Apache Ignite

    Apache Ignite内存数据组织是高性能的.集成化的以及分布式的内存平台,他可以实时地在大数据集中执行事务和计算,和传统的基于磁盘或者闪存的技术相比,性能有数量级的提升. 将数据存储在缓存中能够 ...

  9. c# mongodb时间类型字段保存时相差八个小时解决办法

    /// <summary> /// 添加时间 /// </summary> [BsonDateTimeOptions(Kind = DateTimeKind.Local)] p ...

  10. Unity Shader 2D水流效果

    水流的模拟主要运用了顶点变换和纹理动画的结合: 顶点变换中,利用正弦函数模拟河流的大致形态,例如波长,振幅等. 纹理动画中,将纹理坐标朝某一方向持续滚动以形成流动的效果. 脚本如下: Shader & ...