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

转载于:https://www.cnblogs.com/bolderic/p/7425078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值