Given a positive odd integer K and two positive integers low and high, determine how many integers between low and high contain exactly K divisors.
Input
The first line of the input contains a positive integer C (0
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
using namespace std;
typedef long long LL;
const int NICO = 100000 + 10;
int prime[NICO+1];
int T, k, ans[NICO];
LL low, high;
vector<int> vec[NICO];
void getPrim()
{
memset(prime, 0, sizeof(prime));
for(int i=2;i<NICO;i++)
{
if(!prime[i])
{
prime[++prime[0]] = i;
}
for(int j=1;(j<=prime[0])&&(prime[j]<(NICO/i));j++)
{
prime[prime[j]*i] = 1;
if(i%prime[j]==0) break;
}
}
}
void getFactor(int x)
{
int tmp = x;int ans = 1;
for(int i = 1; prime[i] * prime[i] <=tmp ;i++)
{
int cnt = 0;
if(tmp % prime[i] == 0)
{
while(tmp % prime[i] == 0)
{
cnt ++;
tmp /= prime[i];
}
}
if(cnt) ans *= (2*cnt + 1);
}
if(tmp!=1)
{
ans*=3;
}
vec[ans].push_back(x);//存的是约数个数为ans的完全平方数的平方根
}
int main()
{
getPrim();
for(int i=1;i<NICO;i++)
{
getFactor(i);
}
cin >> T;
while(T--)
{
cin >> k >> low >> high;
int pos1 = upper_bound(vec[k].begin(),vec[k].end(),(int)sqrt(high)) - vec[k].begin() - 1;//<=high
int pos2 = upper_bound(vec[k].begin(),vec[k].end(),(int)(sqrt(low)+1-1e-8)-1) - vec[k].begin() -1;//<=low-1
cout<<pos1-pos2<<endl;
//printf("%d\n", pos1-pos2);
}
}

本文介绍了一种算法,用于计算在给定范围内有多少个整数拥有确切数量的除数。通过预先计算质数和每个数的因数个数,可以有效地解决这一问题。
209

被折叠的 条评论
为什么被折叠?



