nowcode/84/E求值 set

本文介绍了一种算法,用于解决给定数组中所有可能的子数组进行位或操作后的不同结果数量问题。通过使用预处理集合和当前集合的方式,有效地避免了重复计算,最终输出不同的位或值总数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接: https://www.nowcoder.com/acm/contest/84/E
来源:牛客网

时间限制:C/C++ 1秒,其他语言2秒
空间限制:C/C++ 32768K,其他语言65536K
64bit IO Format: %lld

题目描述

给定n个数字a 1, a 2, ..., a n
定义f(l, r) = a l | a l+1| ... | a r
现在枚举(1 <= l <= r <= n),问不同的f值一共有多少个。

输入描述:

第一行一个整数n表示数组大小 (1 <= n <= 100,000);
第二行n个整数满足0 <= ai <= 1000,000。

输出描述:

输出一个整数表示不同的f值一共有多少个。
示例1

输入

3
1 2 0

输出

4
示例2

输入

10
1 2 3 4 5 6 1 2 9 10

输出

11


#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define all(x) begin(x),end(x)
ll rd(){
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
 
const int N=1e5+10;
int a[N];
 
int main(){
   //freopen("in.txt","r",stdin);
    int n=rd();
    for(int i=0;i<n;i++)
    a[i]=rd();
    set<int> pre;
    set<int> f;
    for(int i=0;i<n;i++){
        pre.insert(0);
        set<int> now;
        for(auto v:pre)now.insert(v|a[i]);
        for(auto v:now)f.insert(v);
        swap(pre,now);
    }
    cout<<f.size()<<endl;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值