Jsou dána dvě celá čísla L a R , úkolem je vypočítat počet čísel z rozsahu [L, R] mít přesně 5 různých pozitivních faktorů.
Příklady:
Vstup: L = 1, R = 100
Výstup: 2
Vysvětlení: Jediná dvě čísla v rozsahu [1, 100], která mají přesně 5 různých faktorů, jsou 16 a 81.
Faktory 16 jsou {1, 2, 4, 8, 16}.
Faktory 81 jsou {1, 3, 9, 27, 81}.java indexVstup: L = 1, R = 100
Výstup: 2
Naivní přístup: Nejjednodušší způsob, jak tento problém vyřešit, je překonat rozsah [L, R] a pro každé číslo spočítejte jeho faktory. Pokud se počet faktorů rovná 5 , zvýšit počet o 1 .
Časová náročnost: (R – L) × ?N
Pomocný prostor: O(1)
Efektivní přístup: Pro optimalizaci výše uvedeného přístupu je třeba provést následující pozorování týkající se čísel, která mají přesně 5 faktorů.
Nechť prvočíselný rozklad čísla je p1A1×p2A2× … ×pnAn.
Proto lze počet faktorů tohoto čísla zapsat jako (a1+ 1)×(a2+ 1)× …×(an+ 1).
Protože tento produkt se musí rovnat 5 (což je prvočíslo ), v produktu musí existovat pouze jeden výraz větší než 1. Tento termín se musí rovnat 5.
Pokud tedy ai+ 1 = 5
=> ai= 4
Při řešení problému postupujte podle následujících kroků:
- Požadovaný počet je počet čísel v rozsahu obsahujícím p4jako faktor, kde p je prvočíslo.
- Pro efektivní výpočet p4pro velký rozsah ( [1, 1018] ), myšlenkou je použít Sieve of Eratosthenes k uložení všech prvočísel až do 104.5.
Níže je uvedena implementace výše uvedeného přístupu:
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>>prvočíslo;> // 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; }> |
>
>
Jáva
// 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.> |
>
>
Python3
# 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.> |
nohy vs noha
>
>
C#
// 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
velikost písma latex
> // 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);> |
>
>Výstup:
7>
Časová náročnost: O(N * log(log(N)) )
Pomocný prostor: NA)