解法
就是要求[L,R]
里的每个整数中,有多少个整数的奇因子和偶因子个数之差不超过2.
- 对于奇数来说,它只可能有奇因子,那么需要奇因子个数少于2,那么只能是质数或1
- 对于偶数来说,假设,其中
n
是奇数,那么x
的因子有这几种情况:- 奇x偶: 那么应该是,其中
ab=n
,这时候不管a
和b
相不相等,这都是2个不同的因子,一奇一偶,抵消,所以奇因子个数与偶因子个数之差为0 - 偶x偶: 所以这个时候要求这部分的因子个数不能超过2,否则偶就比奇多了。这时候应该有,其中
ab=n
且i>0
- 0个因子:
m
不能拆分,也就是m==1
- 1个因子:,要求
n==1
且m==2
- 2个因子:【
n
为质数,且m==2
】或者【n=1
且m==3
】
- 0个因子:
- 奇x偶: 那么应该是,其中
#include <stdio.h>
#include <string>
#include <iostream>
#include <memory.h>
#include <stdlib.h>
#include <cmath>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#include <algorithm>
#include <functional>
#define MAXN 100010
#define NINF -100000
#define INF 65536
using namespace std;
typedef long long lld;
int L,R;
bool isZhiShu(int x) {
if (x==2||x==3) return true;
if (x%6!=1 && x%6!=5) return false;
int up = (int)floor(sqrt(x));
for (int d =3;d<=up;d += 2) {
if (x%d==0) return false;
}
return true;
}
bool able(int x) {
if (x==1) return true;
if (x%2!=0) return isZhiShu(x);
int even = 0;
while (x>0 and x%2==0) {
even++;
x = x>>1;
}
return even == 1 || (even == 3 && x==1) || (even == 2 && isZhiShu(x));
}
int solve() {
int ans = 0;
for(int i=L;i<=R;++i) {
// cout<<i<<" "<<able(i)<<endl;
if (able(i)) ans++;
}
return ans;
}
int main() {
// freopen("/Users/huangyuemei/CLionProjects/untitled/C-small-practice.in","r",stdin);
// freopen("/Users/huangyuemei/CLionProjects/untitled/my.out","w",stdout);
int t;
scanf("%d",&t);
for(auto round=1;round<=t;++round) {
scanf("%d%d",&L,&R);
printf("Case #%d: %d\n",round,solve());
}
}