给定2个整数L和R ,任务是找出[L,R]范围内的整数数量,以使它们可以完全被其Euler拉伸值整除。
例子:
Input: L = 2, R = 3
Output: 1
(2) = 2 => 2 %
(2) = 0
(3) = 2 => 3 %
(3) = 1
Hence 2 satisfies the condition.
Input: L = 12, R = 21
Output: 3
Only 12, 16 and 18 satisfy the condition.
方法:我们知道一个数字的欧拉上位函数如下:
重新排列条款,我们得到:
如果我们仔细研究RHS,我们会发现只有2和3是满足n%的素数
*** QuickLaTeX cannot compile formula:
*** Error message:
Error: Nothing to show, formula is empty
= 0 。这是因为对于素数p 1 = 2和p 2 = 3,p 1 – 1 = 1和p 2 – 1 = 2 。因此,当处于范围[L,R]时,仅需要计数2 p 3 q形式的数字,其中p> = 1且q> = 0 。
下面是上述方法的实现:
C++
*** QuickLaTeX cannot compile formula:
*** Error message:
Error: Nothing to show, formula is empty
Java
*** QuickLaTeX cannot compile formula:
*** Error message:
Error: Nothing to show, formula is empty
Python3
*** QuickLaTeX cannot compile formula:
*** Error message:
Error: Nothing to show, formula is empty
C#
*** QuickLaTeX cannot compile formula:
*** Error message:
Error: Nothing to show, formula is empty
PHP
// C++ implementation of the above approach.
#include
#define ll long long
using namespace std;
// Function to return a^n
ll power(ll a, ll n)
{
if (n == 0)
return 1;
ll p = power(a, n / 2);
p = p * p;
if (n & 1)
p = p * a;
return p;
}
// Function to return count of integers
// that satisfy n % phi(n) = 0
int countIntegers(ll l, ll r)
{
ll ans = 0, i = 1;
ll v = power(2, i);
while (v <= r) {
while (v <= r) {
if (v >= l)
ans++;
v = v * 3;
}
i++;
v = power(2, i);
}
if (l == 1)
ans++;
return ans;
}
// Driver Code
int main()
{
ll l = 12, r = 21;
cout << countIntegers(l, r);
return 0;
}
Javascript
// Java implementation of the above approach.
class GFG
{
// Function to return a^n
static long power(long a, long n)
{
if (n == 0)
return 1;
long p = power(a, n / 2);
p = p * p;
if (n%2== 1)
p = p * a;
return p;
}
// Function to return count of integers
// that satisfy n % phi(n) = 0
static int countIntegers(long l, long r)
{
long ans = 0, i = 1;
long v = power(2, i);
while (v <= r)
{
while (v <= r)
{
if (v >= l)
ans++;
v = v * 3;
}
i++;
v = power(2, i);
}
if (l == 1)
ans++;
return (int) ans;
}
// Driver Code
public static void main(String[] args)
{
long l = 12, r = 21;
System.out.println(countIntegers(l, r));
}
}
// This code contributed by Rajput-Ji
输出:
# Python3 implementation of the approach
# Function to return a^n
def power(a, n):
if n == 0:
return 1
p = power(a, n // 2)
p = p * p
if n & 1:
p = p * a
return p
# Function to return count of integers
# that satisfy n % phi(n) = 0
def countIntegers(l, r):
ans, i = 0, 1
v = power(2, i)
while v <= r:
while v <= r:
if v >= l:
ans += 1
v = v * 3
i += 1
v = power(2, i)
if l == 1:
ans += 1
return ans
# Driver Code
if __name__ == "__main__":
l, r = 12, 21
print(countIntegers(l, r))
# This code is contributed
# by Rituraj Jain
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。