두 개의 정수가 주어지면 엘 그리고 아르 자형 , 작업은 범위에서 숫자의 개수를 계산하는 것입니다 [엘, 알] 정확히 가지고 5가지 뚜렷한 긍정적인 요소.
예:
입력: L = 1, R= 100
산출: 2
설명: [1, 100] 범위에서 정확히 5개의 고유 요소를 갖는 유일한 두 숫자는 16과 81입니다.
16의 약수는 {1, 2, 4, 8, 16}입니다.
81의 약수는 {1, 3, 9, 27, 81}입니다.자바 문자열 인덱스입력: L = 1, R= 100
산출: 2
순진한 접근 방식: 이 문제를 해결하는 가장 간단한 방법은 범위를 횡단하는 것입니다. [엘, 알] 그리고 모든 숫자에 대해 그 요소들을 세어보세요. 요인의 개수가 다음과 같은 경우 5 , 증분 카운트 1 .
시간 복잡도: (R – L) × ?N
보조 공간: 오(1)
효율적인 접근 방식: 위의 접근 방식을 최적화하려면 정확히 5개의 요소를 갖는 숫자에 대해 다음과 같은 관찰이 필요합니다.
숫자의 소인수분해를 p라고 하자1ㅏ1×p2ㅏ2× … ×pNㅏN.
따라서 이 숫자의 요소 수는 다음과 같이 쓸 수 있습니다.1+ 1)×(a2+ 1)× … ×(aN+ 1).
이 제품은 다음과 같아야 하기 때문에 5 (이것은 소수 ), 제품에는 1보다 큰 항이 하나만 있어야 합니다. 해당 항은 5와 같아야 합니다.
따라서 만약나+ 1 = 5
=> 아나= 4
문제를 해결하려면 아래 단계를 따르십시오.
- 필요한 개수는 p를 포함하는 범위에 있는 숫자의 개수입니다.4요인으로, 여기서 피 소수입니다.
- p를 효율적으로 계산하기 위해4넓은 범위의 경우( [1, 1018] ), 아이디어는 에라토스테네스의 체를 사용하여 다음까지의 모든 소수를 저장하는 것입니다. 104.5.
다음은 위의 접근 방식을 구현한 것입니다.
C++14
// C++ Program to implement> // the above approach> #include> using> namespace> std;> const> int> N = 2e5;> // Stores all prime numbers> // up to 2 * 10^5> vector<>long> long>>프라임;> // Function to generate all prime> // numbers up to 2 * 10 ^ 5 using> // Sieve of Eratosthenes> void> Sieve()> {> >prime.clear();> >vector<>bool>>p(N + 1,>true>);> >// Mark 0 and 1 as non-prime> >p[0] = p[1] =>false>;> >for> (>int> i = 2; i * i <= N; i++) {> >// If i is prime> >if> (p[i] ==>true>) {> >// Mark all its factors as non-prime> >for> (>int> j = i * i; j <= N; j += i) {> >p[j] =>false>;> >}> >}> >}> >for> (>int> i = 1; i // If current number is prime if (p[i]) { // Store the prime prime.push_back(1LL * pow(i, 4)); } } } // Function to count numbers in the // range [L, R] having exactly 5 factors void countNumbers(long long int L, long long int R) { // Stores the required count int Count = 0; for (int p : prime) { if (p>= L && p<= R) { Count++; } } cout << Count << endl; } // Driver Code int main() { long long L = 16, R = 85000; Sieve(); countNumbers(L, R); return 0; }> |
>
>
자바
// Java Program to implement> // the above approach> import> java.util.*;> class> GFG> {> >static> int> N =>200000>;> >// Stores all prime numbers> >// up to 2 * 10^5> >static> int> prime[] =>new> int> [>20000>];> >static> int> index =>0>;> >// Function to generate all prime> >// numbers up to 2 * 10 ^ 5 using> >// Sieve of Eratosthenes> >static> void> Sieve()> >{> >index =>0>;> >int> p[] =>new> int> [N +>1>];> >for>(>int> i =>0>; i <= N; i++)> >{> >p[i] =>1>;> >}> >// Mark 0 and 1 as non-prime> >p[>0>] = p[>1>] =>0>;> >for> (>int> i =>2>; i * i <= N; i++)> >{> >// If i is prime> >if> (p[i] ==>1>)> >{> >// Mark all its factors as non-prime> >for> (>int> j = i * i; j <= N; j += i)> >{> >p[j] =>0>;> >}> >}> >}> >for> (>int> i =>1>; i { // If current number is prime if (p[i] == 1) { // Store the prime prime[index++] = (int)(Math.pow(i, 4)); } } } // Function to count numbers in the // range [L, R] having exactly 5 factors static void countNumbers(int L,int R) { // Stores the required count int Count = 0; for(int i = 0; i { int p = prime[i]; if (p>= L && p<= R) { Count++; } } System.out.println(Count); } // Driver Code public static void main(String[] args) { int L = 16, R = 85000; Sieve(); countNumbers(L, R); } } // This code is contributed by amreshkumar3.> |
tkinter 프레임
>
>
파이썬3
# Python3 implementation of> # the above approach> N>=> 2> *> 100000> # Stores all prime numbers> # up to 2 * 10^5> prime>=> [>0>]>*> N> # Function to generate all prime> # numbers up to 2 * 10 ^ 5 using> # Sieve of Eratosthenes> def> Sieve() :> >p>=> [>True>]>*> (N>+> 1>)> ># Mark 0 and 1 as non-prime> >p[>0>]>=> p[>1>]>=> False> >i>=> 2> >while>(i>*> i <>=> N) :> ># If i is prime> >if> (p[i]>=>=> True>) :> ># Mark all its factors as non-prime> >for> j>in> range>(i>*> i, N, i):> >p[j]>=> False> >i>+>=> 1> >for> i>in> range>(N):> ># If current number is prime> >if> (p[i] !>=> False>) :> ># Store the prime> >prime.append(>pow>(i,>4>))> # Function to count numbers in the> # range [L, R] having exactly 5 factors> def> countNumbers(L, R) :> ># Stores the required count> >Count>=> 0> >for> p>in> prime :> >if> (p>>=> L>and> p <>=> R) :> >Count>+>=> 1> >print>(Count)> # Driver Code> L>=> 16> R>=> 85000> Sieve()> countNumbers(L, R)> # This code is contributed by code_hunt.> |
>
>
씨#
// C# Program to implement> // the above approach> using> System;> class> GFG> {> >static> int> N = 200000;> >// Stores all prime numbers> >// up to 2 * 10^5> >static> int> []prime =>new> int> [20000];> >static> int> index = 0;> >// Function to generate all prime> >// numbers up to 2 * 10 ^ 5 using> >// Sieve of Eratosthenes> >static> void> Sieve()> >{> >index = 0;> >int> []p =>new> int> [N + 1];> >for>(>int> i = 0; i <= N; i++)> >{> >p[i] = 1;> >}> >// Mark 0 and 1 as non-prime> >p[0] = p[1] = 0;> >for> (>int> i = 2; i * i <= N; i++)> >{> >// If i is prime> >if> (p[i] == 1)> >{> >// Mark all its factors as non-prime> >for> (>int> j = i * i; j <= N; j += i)> >{> >p[j] = 0;> >}> >}> >}> >for> (>int> i = 1; i { // If current number is prime if (p[i] == 1) { // Store the prime prime[index++] = (int)(Math.Pow(i, 4)); } } } // Function to count numbers in the // range [L, R] having exactly 5 factors static void countNumbers(int L,int R) { // Stores the required count int Count = 0; for(int i = 0; i { int p = prime[i]; if (p>= L && p<= R) { Count++; } } Console.WriteLine(Count); } // Driver Code public static void Main(String[] args) { int L = 16, R = 85000; Sieve(); countNumbers(L, R); } } // This code is contributed by shikhasingrajput> |
>
>
자바스크립트
> // javascript program of the above approach> let N = 200000;> > >// Stores all prime numbers> >// up to 2 * 10^5> >let prime =>new> Array(20000).fill(0);> >let index = 0;> > >// Function to generate all prime> >// numbers up to 2 * 10 ^ 5 using> >// Sieve of Eratosthenes> >function> Sieve()> >{> >index = 0;> >let p =>new> Array (N + 1).fill(0);> >for>(let i = 0; i <= N; i++)> >{> >p[i] = 1;> >}> > >// Mark 0 and 1 as non-prime> >p[0] = p[1] = 0;> >for> (let i = 2; i * i <= N; i++)> >{> > >// If i is prime> >if> (p[i] == 1)> >{> > >// Mark all its factors as non-prime> >for> (let j = i * i; j <= N; j += i)> >{> >p[j] = 0;> >}> >}> >}> >for> (let i = 1; i { // If current number is prime if (p[i] == 1) { // Store the prime prime[index++] = (Math.pow(i, 4)); } } } // Function to count numbers in the // range [L, R] having exactly 5 factors function countNumbers(L, R) { // Stores the required count let Count = 0; for(let i = 0; i { let p = prime[i]; if (p>= L && p<= R) { Count++; } } document.write(Count); } // Driver Code let L = 16, R = 85000; Sieve(); countNumbers(L, R);> |
>
>산출:
내 컴퓨터 화면 크기가 몇이야
7>
시간 복잡도: O(N * log(log(N)) )
보조 공간: 에)