题目
762.二进制表示中质数个计算置位
题目大意
给你两个整数 left
和 right
,在闭区间 [left, right]
范围内,统计并返回 计算置位位数为质数 的整数个数。
计算置位位数 就是二进制表示中 1
的个数。
- 例如,
21
的二进制表示10101
有3
个计算置位。
样例
示例 1:
输入:left = 6, right = 10
输出:4
解释:
6 -> 110 (2 个计算置位,2 是质数)
7 -> 111 (3 个计算置位,3 是质数)
9 -> 1001 (2 个计算置位,2 是质数)
10-> 1010 (2 个计算置位,2 是质数)
共计 4 个计算置位为质数的数字。
示例 2:
输入:left = 10, right = 15
输出:5
解释:
10 -> 1010 (2 个计算置位, 2 是质数)
11 -> 1011 (3 个计算置位, 3 是质数)
12 -> 1100 (2 个计算置位, 2 是质数)
13 -> 1101 (3 个计算置位, 3 是质数)
14 -> 1110 (3 个计算置位, 3 是质数)
15 -> 1111 (4 个计算置位, 4 不是质数)
共计 5 个计算置位为质数的数字。
数据规模
提示:
- 1 < = l e f t < = r i g h t < = 1 0 6 1 <= left <= right <= 10^6 1<=left<=right<=106
- 0 < = r i g h t − l e f t < = 1 0 4 0 <= right - left <= 10^4 0<=right−left<=104
思路
直接对x
进行分解,不断除
2
2
2取余数,统计
1
1
1的数量。然后对于t
(即统计出来的
1
1
1的数量)判断它是否是质数,尤其要注意
t
=
0
,
1
t=0,1
t=0,1的情况,可能需要特判(它们不是质数),如果是质数,那么ans
加
1
1
1,最后返回ans
。
代码
// short int long float double bool char string void
// array vector stack queue auto const operator
// class public private static friend extern
// sizeof new delete return cout cin memset malloc
// relloc size length memset malloc relloc size length
// for while if else switch case continue break system
// endl reverse sort swap substr begin end iterator
// namespace include define NULL nullptr exit equals
// index col row arr err left right ans res vec que sta
// state flag ch str max min default charray std
// maxn minn INT_MAX INT_MIN push_back insert
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
typedef pair<int, string>PIS;
const int maxn=3e4+50;//注意修改大小
long long read(){long long x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;}
ll qpow(ll x,ll q,ll Mod){ll ans=1;while(q){if(q&1)ans=ans*x%Mod;q>>=1;x=(x*x)%Mod;}return ans%Mod;}
class Solution {
public:
int countPrimeSetBits(int left, int right) {
int ans=0;
for(int i=left;i<=right;i++){
int x=i,t=0;
while(x){
t+=x%2;x/=2;
}
int sign=0;
for(int j=2;j<=sqrt(t);j++){
if(t%j==0){
sign=1;
break;
}
}
if(!sign&&t>1)ans++;
}
return ans;
}
};
补充
对于分解x
的过程,可以考虑直接使用函数__builtin_popcount
(直接统计x
二进制中
1
1
1的个数)
// short int long float double bool char string void
// array vector stack queue auto const operator
// class public private static friend extern
// sizeof new delete return cout cin memset malloc
// relloc size length memset malloc relloc size length
// for while if else switch case continue break system
// endl reverse sort swap substr begin end iterator
// namespace include define NULL nullptr exit equals
// index col row arr err left right ans res vec que sta
// state flag ch str max min default charray std
// maxn minn INT_MAX INT_MIN push_back insert
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int>PII;
typedef pair<int, string>PIS;
const int maxn=3e4+50;//注意修改大小
long long read(){long long x=0,f=1;char c=getchar();while(!isdigit(c)){if(c=='-') f=-1;c=getchar();}while(isdigit(c)){x=x*10+c-'0';c=getchar();}return x*f;}
ll qpow(ll x,ll q,ll Mod){ll ans=1;while(q){if(q&1)ans=ans*x%Mod;q>>=1;x=(x*x)%Mod;}return ans%Mod;}
class Solution {
public:
int countPrimeSetBits(int left, int right) {
int ans=0;
for(int i=left;i<=right;i++){
int x=i,t=0;
t=__builtin_popcount(x);
int sign=0;
for(int j=2;j<=sqrt(t);j++){
if(t%j==0){
sign=1;
break;
}
}
if(!sign&&t>1)ans++;
}
return ans;
}
};