AND
链接:https://ac.nowcoder.com/acm/contest/88269/E
来源:牛客网
题目描述
xiao hua 非常喜欢质数,ta想要知道在x和y之间有多少质数,并且这些质数形成的数组有多少个子区间AND和为0。子区间 [ l , r ] ( 1 ≤ l ≤ r ≤ n ) A N D [l,r](1≤l≤r≤n)AND [l,r](1≤l≤r≤n)AND 和为 0,即a[l]至 a[r]中所有数进行AND操作得到的结果为0。
与(AND)运算,运算规则:全一为一,有零为零。即只有两个操作数对应的二进制位都为1时,结果才为1,其他情况均为0(也可以说,只要有0,结果就为0)。
输入描述:
第一行给出一个数t之后 t 行,每行会给出两个正整数 x和y。
1
≤
t
≤
150
1 ≤ t ≤ 150
1≤t≤150
2
≤
x
≤
y
≤
100000000
2 ≤ x ≤ y≤ 100 000 000
2≤x≤y≤100000000
输出描述:
每行输出两个整数a,b
a是x和y区间内的所有质数的数目
b是x和y区间内的所有质数形成的数组中有多少子区间and和为0
输入示例1
3
2 3
2 4
2 5
输出示例1
2 0
2 0
3 1
说明
第一个的质数有2,3
第二个的质数有2,3
第三个的质数有2,3,5,并且2,3,5的and和为0
代码内容
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <sstream>//整型转字符串
// #include <stack>//栈
// #include <deque>//堆/优先队列
// #include <queue>//队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll N=1e8;
ll pri[N],cnt;
bool vis[N];
//线性筛
void prim()
{
for(ll i=2;i<=N;i++)
{
if(!vis[i]) pri[cnt++]=i;
for(ll j=0;pri[j]*i<=N;j++)
{
vis[pri[j]*i]=1;
if(i%pri[j]==0) break;
}
}
}
int main()
{
ll t;
cin>>t;
prim();
while(t--)
{
ll a,b;
cin>>a>>b;
ll ans=upper_bound(pri,pri+cnt,b)-lower_bound(pri,pri+cnt,a);
cout<<ans<<" ";
if(a>2) cout<<0<<endl;
else
{
if(b>=5) cout<<ans-2<<endl;
else cout<<0<<endl;
}
}
return 0;
}