codility: Sieve of Eratosthenes (CountNonDivisible, CountSemiprimes)

CountSemiprimes.

The prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.

The semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers. The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.

You are given two non-empty zero-indexed arrays P and Q, each consisting of M integers. These arrays represent queries about the number of semiprimes within specified ranges.

Query K requires you to find the number of semiprimes within the range (P[K], Q[K]), where 1 ≤ P[K] ≤ Q[K] ≤ N.

For example, consider an integer N = 26 and arrays P, Q such that:

 

    P[0] = 1    Q[0] = 26
    P[1] = 4    Q[1] = 10
    P[2] = 16   Q[2] = 20

The number of semiprimes within each of these ranges is as follows:

  • (1, 26) is 10,
  • (4, 10) is 4,
  • (16, 20) is 0.

Write a function:

vector<int> solution(int N, vector<int> &P, vector<int> &Q);

that, given an integer N and two non-empty zero-indexed arrays P and Q consisting of M integers, returns an array consisting of M elements specifying the consecutive answers to all the queries.

For example, given an integer N = 26 and arrays P, Q such that:

 

    P[0] = 1    Q[0] = 26
    P[1] = 4    Q[1] = 10
    P[2] = 16   Q[2] = 20

the function should return the values [10, 4, 0], as explained above.

Assume that:

  • N is an integer within the range [1..50,000];
  • M is an integer within the range [1..30,000];
  • each element of array P is an integer within the range [1..N];
  • each element of array Q is an integer within the range [1..N];
  • P[i] ≤ Q[i].

Complexity:

  • expected worst-case time complexity is O(N*log(log(N))+M);
  • expected worst-case space complexity is O(N+M), beyond input storage (not counting the storage required for input arguments).

The prime is a positive integer X that has exactly two distinct divisors: 1 and X. The first few prime integers are 2, 3, 5, 7, 11 and 13.

The semiprime is a natural number that is the product of two (not necessarily distinct) prime numbers. The first few semiprimes are 4, 6, 9, 10, 14, 15, 21, 22, 25, 26.

You are given two non-empty zero-indexed arrays P and Q, each consisting of M integers. These arrays represent queries about the number of semiprimes within specified ranges.

Query K requires you to find the number of semiprimes within the range (P[K], Q[K]), where 1 ≤ P[K] ≤ Q[K] ≤ N.

For example, consider an integer N = 26 and arrays P, Q such that:

 

    P[0] = 1    Q[0] = 26
    P[1] = 4    Q[1] = 10
    P[2] = 16   Q[2] = 20

The number of semiprimes within each of these ranges is as follows:

  • (1, 26) is 10,
  • (4, 10) is 4,
  • (16, 20) is 0.

Write a function:

vector<int> solution(int N, vector<int> &P, vector<int> &Q);

that, given an integer N and two non-empty zero-indexed arrays P and Q consisting of M integers, returns an array consisting of M elements specifying the consecutive answers to all the queries.

For example, given an integer N = 26 and arrays P, Q such that:

 

    P[0] = 1    Q[0] = 26
    P[1] = 4    Q[1] = 10
    P[2] = 16   Q[2] = 20

the function should return the values [10, 4, 0], as explained above.

Assume that:

  • N is an integer within the range [1..50,000];
  • M is an integer within the range [1..30,000];
  • each element of array P is an integer within the range [1..N];
  • each element of array Q is an integer within the range [1..N];
  • P[i] ≤ Q[i].

Complexity:

  • expected worst-case time complexity is O(N*log(log(N))+M);
  • expected worst-case space complexity is O(N+M), beyond input storage (not counting the storage required for input arguments).

这个很好办。首先可以用个大小为N+1的数组buf存一下从1到N的这些数每一个数的最小的素数因子,如果本身是素数那就计为0.比如buf[2]=0, buf[4]=2, buf[9]=3这样。然后通过判断buf[i/buf[i]]是否等于0就知道i是不是两个素数的乘积了。

// you can also use includes, for example:
#include <algorithm>
vector<int> solution(int N, vector<int> &P, vector<int> &Q) {
    // write your code in C++98
    vector<int> buf(N+1,0);
    for(int i=2;i*i<=N;i++) {
        if(buf[i]!=0) {
            continue;
        }
        int tmp = i*i;
        while(tmp<=N) {
            if(buf[tmp]==0) {
                buf[tmp]=i;
            }
            tmp+=i;
        }
    }
    vector<int> primeCount(buf);
    for(int i=4;i<=N;i++) {
        if(buf[i]==0) {
            primeCount[i] = primeCount[i-1];
            continue;
        }
        if(buf[i/buf[i]]==0) {
            primeCount[i] = primeCount[i-1]+1;
        }else {
            primeCount[i] = primeCount[i-1];
        }
    }
    vector<int> res;
    for(int i=0;i<P.size();i++) {
        res.push_back(primeCount[Q[i]]-primeCount[P[i]-1]);
    }
    return res;
}

 

然后CountNonDivisible:

You are given a non-empty zero-indexed array A consisting of N integers.

For each number A[i] such that 0 ≤ i < N, we want to count the number of elements of the array that are not the divisors of A[i]. We say that these elements are non-divisors.

For example, consider integer N = 5 and array A such that:

 

    A[0] = 3
    A[1] = 1
    A[2] = 2
    A[3] = 3
    A[4] = 6

For the following elements:

  • A[0] = 3, the non-divisors are: 2, 6,
  • A[1] = 1, the non-divisors are: 3, 2, 3, 6,
  • A[2] = 2, the non-divisors are: 3, 3, 6,
  • A[3] = 3, the non-divisors are: 2, 6,
  • A[6] = 6, there aren't any non-divisors.

Assume that the following declarations are given:

struct Results {
  int * C;
  int L;
};

Write a function:

struct Results solution(int A[], int N);

that, given a non-empty zero-indexed array A consisting of N integers, returns a sequence of integers representing the numbers of non-divisors.

The sequence should be returned as:

  • a structure Results (in C), or
  • a vector of integers (in C++), or
  • a record Results (in Pascal), or
  • an array of integers (in any other programming language).

For example, given:

 

    A[0] = 3
    A[1] = 1
    A[2] = 2
    A[3] = 3
    A[4] = 6

the function should return [2, 4, 3, 2, 0], as explained above.

Assume that:

  • N is an integer within the range [1..50,000];
  • each element of array A is an integer within the range [1..2 * N].

Complexity:

  • expected worst-case time complexity is O(N*log(N));
  • expected worst-case space complexity is O(N), beyond input storage (not counting the storage required for input arguments).

还没想到合适的解法。。。先空着吧。。。

 

 

转载于:https://www.cnblogs.com/parapax/p/3648482.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值