SPOJ NDIV - n-divisors
题目链接:http://www.spoj.com/problems/NDIV/
题目大意:求a~b(包括a,b)区间内有多少个数的因子恰好有K个。1 <= a, b <=10^9 0 <= b - a <= 10^4 1 <= n <= 100
解题思路:筛法的一个非常巧妙的运用。当我们考虑数x的因子的时候,只需要考虑sqrt(x)之前的每个整数即可,所有的因子个数是计算得到的sum[x] * 2或者如果x是平方数的话为sum[x] * 2 - 1。
枚举2~sqrt(b)的每个整数i,对a~b中的i的倍数x来说,若i*i <= x那么x对应的素因子个数sum[x]++.
然后遍历数组,计算真正的sum,判断是否为k即可。
代码:
1 const int maxn = 1e5 + 5; 2 int n, a, b; 3 int sum[maxn]; 4 5 void solve(){ 6 memset(sum, 0, sizeof(sum)); 7 for(int i = 2; i <= sqrt(b); i++){ 8 int st = (a / i + (a % i == 0? 0: 1)) * i, ed = b; 9 for(int j = st; j <= ed; j += i) 10 if(i * i <= j && i * i > 0) sum[j - a]++; 11 } 12 int ans = 0; 13 for(int i = 0; i <= b - a; i++){ 14 sum[i]++; 15 int x = sqrt(a + i); 16 sum[i] *= 2; 17 if(x * x == a + i) sum[i]--; 18 if(sum[i] == n) ans++; 19 } 20 printf("%d\n", ans); 21 } 22 23 int main(){ 24 scanf("%d %d %d", &a, &b, &n); 25 solve(); 26 }
题目:
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