题目链接:http://www.spoj.com/problems/NDIV/en/
NDIV - n-divisors
We all know about prime numbers, prime number is a natural number greater than 1 that has no positive divisors other than 1 and itself.
We can Classify the numbers by its number of divisors, as n-divisors-numbers, for example number 1 is 1-divisor number, number 4 is 3-divisors-number... etc.
Note: All prime numbers are 2-divisors numbers.
Example:
8 is a 4-divisors-number [1, 2, 4, 8].
Input
Three integers a, b, n.
Output
Print single line the number of n-divisors numbers between a and b inclusive.
Example
Input: 1 7 2 Output: 4
Constraints
1 <= a, b <=10^9
0 <= b - a <= 10^4
1 <= n <= 100
题意:求[a,b]区间因子个数为n的个数
解析:区间比较大,咱可以打个素数表,然后复杂度就降下来了,k = 2^x1 + 3*x2 + 5*x3 + 7*x4 + ...... k因子个数 = (x1+1)*(x2+1)*(x3+1)*.......
代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<map>
#include<cmath>
#include<string>
#define N 100009
using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9 + 7;
typedef long long LL;
int ispri[N], pri[N], cnt;
void init()
{
cnt = 0;
memset(ispri, 0, sizeof(ispri));
ispri[0] = ispri[1] = 1;
for(int i = 2; i <= 100000; i++)
{
if(ispri[i]) continue;
pri[cnt++] = i;
for(int j = i * 2; j <= 100000; j += i) ispri[j] = 1;
}
}
int main()
{
int a, b, n;
int ans = 0;
init();
scanf("%d%d%d", &a, &b, &n);
for(int i = a; i <= b; i++)
{
int k = i, sum = 1, num = 0;
for(int j = 0; j < cnt; j++)
{
num = 0;
while(k % pri[j] == 0)
{
num++;
k /= pri[j];
}
sum *= num + 1;
if(sum > n) break;
if(k == 0 || pri[j] > k) break;
}
if(k && k != 1) sum *= 2;
if(sum == n) ans++;
}
printf("%d\n", ans);
return 0;
}