解法
就是要求[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());
}
}