2 つの整数が与えられた場合 L そして R 、タスクは範囲から数値の数を計算することです。 [左、右] まさに 5 つの明確なポジティブ要素。
例:
入力: L = 1、R = 100
出力: 2
説明: [1, 100] の範囲内で、正確に 5 つの異なる因数を持つ数値は、16 と 81 の 2 つだけです。
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 ×p 2 ある 2 × … ×p n ある n 。
したがって、この数の因数の数は次のように書くことができます (a 1 + 1)×(a 2 + 1)× … ×(a n +1)。
この積は次と等しい必要があるため、 5 (これは 素数 )、積内には 1 より大きい項が 1 つだけ存在する必要があります。この項は 5 に等しくなければなりません。
したがって、もし 私 + 1 = 5
=> a 私 = 4
問題を解決するには、次の手順に従ってください。
- 必要なカウントは、p を含む範囲内の数値のカウントです。 4 要因として、 p は素数です。
- pを効率的に計算するには 4 広い範囲の場合 ( [1、10 18 】 )、アイデアは、エラトステネスのふるいを使用して、次の値までのすべての素数を保存することです。 10 4.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> >プライム;>> ジャワ // 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.> | 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 > // 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)) ) 補助スペース: の上)
|