给定正整数N(N≥2) ,任务是对范围[2,N]中的整数X进行计数,以使X可以使用以下操作将N转换为1:
- 如果N可被X整除,则将N的值更新为N / X。
- 否则,将N的值更新为N – X。
例子:
Input: N = 6
Output: 3
Explanation:
The following integers can be used to convert N to 1:
X = 2 => N = 6 -> N = 3 -> N = 1
X = 5 => N = 6 -> N = 1
X = 6 => N = 6 -> N = 1
Input: N = 48
Output: 4
天真的方法:这个问题的天真的方法是遍历从2到N的所有整数,并对可以将N转换为1的整数进行计数。
下面是上述方法的实现:
C++
// C++ program to count the numbers
// which can convert N to 1
// using the given operation
#include
using namespace std;
// Function to count the numbers
// which can convert N to 1
// using the given operation
int countValues(int n)
{
int answer = 0;
// Iterate through all the integers
for (int i = 2; i <= n; i++) {
int k = n;
// Check if N can be converted
// to 1
while (k >= i) {
if (k % i == 0)
k /= i;
else
k -= i;
}
// Incrementing the count if it can
// be converted
if (k == 1)
answer++;
}
return answer;
}
// Driver code
int main()
{
int N = 6;
cout << countValues(N);
return 0;
}
Java
// Java program to count the numbers
// which can convert N to 1
// using the given operation
import java.io.*;
import java.util.*;
class GFG{
// Function to count the numbers
// which can convert N to 1
// using the given operation
static int countValues(int n)
{
int answer = 0;
// Iterate through all the integers
for (int i = 2; i <= n; i++)
{
int k = n;
// Check if N can be converted
// to 1
while (k >= i)
{
if (k % i == 0)
k /= i;
else
k -= i;
}
// Incrementing the count if it can
// be converted
if (k == 1)
answer++;
}
return answer;
}
// Driver code
public static void main(String args[])
{
int N = 6;
System.out.print(countValues(N));
}
}
// This code is contributed by shivanisinghss2110
Python3
# Python3 program to count the numbers
# which can convert N to 1
# using the given operation
# Function to count the numbers
# which can convert N to 1
# using the given operation
def countValues(n):
answer = 0
# Iterate through all the integers
for i in range(2, n + 1, 1):
k = n
# Check if N can be converted
# to 1
while (k >= i):
if (k % i == 0):
k //= i
else:
k -= i
# Incrementing the count if it can
# be converted
if (k == 1):
answer += 1
return answer
# Driver code
if __name__ == '__main__':
N = 6
print(countValues(N))
# This code is contributed by Samarth
C#
// C# program to count the numbers
// which can convert N to 1
// using the given operation
using System;
class GFG{
// Function to count the numbers
// which can convert N to 1
// using the given operation
static int countValues(int n)
{
int answer = 0;
// Iterate through all the integers
for (int i = 2; i <= n; i++)
{
int k = n;
// Check if N can be converted
// to 1
while (k >= i)
{
if (k % i == 0)
k /= i;
else
k -= i;
}
// Incrementing the count if it can
// be converted
if (k == 1)
answer++;
}
return answer;
}
// Driver code
public static void Main()
{
int N = 6;
Console.Write(countValues(N));
}
}
// This code is contributed by Nidhi_biet
Javascript
C++
// C++ program to count the numbers
// which can convert N to 1
// using the given operation
#include
using namespace std;
// Function to count the numbers
// which can convert N to 1
// using the given operation
int countValues(int N)
{
vector div;
// Store all the divisors of N
for (int i = 2; i * i <= N; i++) {
// If i is a divisor
if (N % i == 0) {
div.push_back(i);
// If i is not equal to N / i
if (N != i * i) {
div.push_back(N / i);
}
}
}
int answer = 0;
// Iterate through all the divisors of
// N - 1 and count them in answer
for (int i = 1; i * i <= N - 1; i++) {
// Check if N - 1 is a divisor
// or not
if ((N - 1) % i == 0) {
if (i * i == N - 1)
answer++;
else
answer += 2;
}
}
// Iterate through all divisors and check
// for N mod d = 1 or (N-1) mod d = 0
for (auto d : div) {
int K = N;
while (K % d == 0)
K /= d;
if ((K - 1) % d == 0)
answer++;
}
return answer;
}
// Driver code
int main()
{
int N = 6;
cout << countValues(N);
return 0;
}
Java
// Java program to count the numbers
// which can convert N to 1
// using the given operation
import java.util.*;
class GFG{
// Function to count the numbers
// which can convert N to 1
// using the given operation
static int countValues(int N)
{
Vector div = new Vector<>();
// Store all the divisors of N
for(int i = 2; i * i <= N; i++)
{
// If i is a divisor
if (N % i == 0)
{
div.add(i);
// If i is not equal to N / i
if (N != i * i)
{
div.add(N / i);
}
}
}
int answer = 0;
// Iterate through all the divisors of
// N - 1 and count them in answer
for(int i = 1; i * i <= N - 1; i++)
{
// Check if N - 1 is a divisor
// or not
if ((N - 1) % i == 0)
{
if (i * i == N - 1)
answer++;
else
answer += 2;
}
}
// Iterate through all divisors and check
// for N mod d = 1 or (N-1) mod d = 0
for(int d : div)
{
int K = N;
while (K % d == 0)
K /= d;
if ((K - 1) % d == 0)
answer++;
}
return answer;
}
// Driver code
public static void main(String[] args)
{
int N = 6;
System.out.print(countValues(N));
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program to count the numbers
# which can convert N to 1
# using the given operation
# Function to count the numbers
# which can convert N to 1
# using the given operation
def countValues(N):
div = []
i = 2
# Store all the divisors of N
while ((i * i) <= N):
# If i is a divisor
if (N % i == 0):
div.append(i)
# If i is not equal to N / i
if (N != i * i):
div.append(N // i)
i += 1
answer = 0
i = 1
# Iterate through all the divisors of
# N - 1 and count them in answer
while((i * i) <= N - 1):
# Check if N - 1 is a divisor
# or not
if ((N - 1) % i == 0):
if (i * i == N - 1):
answer += 1
else:
answer += 2
i += 1
# Iterate through all divisors and check
# for N mod d = 1 or (N-1) mod d = 0
for d in div:
K = N
while (K % d == 0):
K //= d
if ((K - 1) % d == 0):
answer += 1
return answer
# Driver code
if __name__=="__main__":
N = 6
print(countValues(N))
# This code is contributed by rutvik_56
C#
// C# program to count the numbers
// which can convert N to 1
// using the given operation
using System;
using System.Collections.Generic;
class GFG{
// Function to count the numbers
// which can convert N to 1
// using the given operation
static int countValues(int N)
{
List div = new List();
// Store all the divisors of N
for(int i = 2; i * i <= N; i++)
{
// If i is a divisor
if (N % i == 0)
{
div.Add(i);
// If i is not equal to N / i
if (N != i * i)
{
div.Add(N / i);
}
}
}
int answer = 0;
// Iterate through all the divisors of
// N - 1 and count them in answer
for(int i = 1; i * i <= N - 1; i++)
{
// Check if N - 1 is a divisor
// or not
if ((N - 1) % i == 0)
{
if (i * i == N - 1)
answer++;
else
answer += 2;
}
}
// Iterate through all divisors and check
// for N mod d = 1 or (N-1) mod d = 0
foreach(int d in div)
{
int K = N;
while (K % d == 0)
K /= d;
if ((K - 1) % d == 0)
answer++;
}
return answer;
}
// Driver code
public static void Main(String[] args)
{
int N = 6;
Console.Write(countValues(N));
}
}
// This code is contributed by Amit Katiyar
输出:
3
时间复杂度: O(N) ,其中N是给定的数字。
高效的方法:想法是观察到,如果最初N不能被N整除,那么在整个减法过程中都将进行减法运算,因为每次减法N仍然不能被N整除。当N≤X时,这些运算也会停止, N的最终值将等于N mod X。
因此,对于从2到N的所有数字,只有两种可能的情况:
- 没有除法运算:对于所有这些数字,最终值将等于N modX。只有在N mod X = 1时,N才最终变为1。显然,对于X = N – 1,并且N的所有除数– 1,N mod X = 1成立。
- 除法运算不止一次发生:除法运算仅对N上的除数发生。对于N的每个除数,说d,执行除法直到N mod d!=0。如果最终N mod d = 1,则将其包括在否则就没有答案(使用从案例1派生的属性)。
下面是上述方法的实现:
C++
// C++ program to count the numbers
// which can convert N to 1
// using the given operation
#include
using namespace std;
// Function to count the numbers
// which can convert N to 1
// using the given operation
int countValues(int N)
{
vector div;
// Store all the divisors of N
for (int i = 2; i * i <= N; i++) {
// If i is a divisor
if (N % i == 0) {
div.push_back(i);
// If i is not equal to N / i
if (N != i * i) {
div.push_back(N / i);
}
}
}
int answer = 0;
// Iterate through all the divisors of
// N - 1 and count them in answer
for (int i = 1; i * i <= N - 1; i++) {
// Check if N - 1 is a divisor
// or not
if ((N - 1) % i == 0) {
if (i * i == N - 1)
answer++;
else
answer += 2;
}
}
// Iterate through all divisors and check
// for N mod d = 1 or (N-1) mod d = 0
for (auto d : div) {
int K = N;
while (K % d == 0)
K /= d;
if ((K - 1) % d == 0)
answer++;
}
return answer;
}
// Driver code
int main()
{
int N = 6;
cout << countValues(N);
return 0;
}
Java
// Java program to count the numbers
// which can convert N to 1
// using the given operation
import java.util.*;
class GFG{
// Function to count the numbers
// which can convert N to 1
// using the given operation
static int countValues(int N)
{
Vector div = new Vector<>();
// Store all the divisors of N
for(int i = 2; i * i <= N; i++)
{
// If i is a divisor
if (N % i == 0)
{
div.add(i);
// If i is not equal to N / i
if (N != i * i)
{
div.add(N / i);
}
}
}
int answer = 0;
// Iterate through all the divisors of
// N - 1 and count them in answer
for(int i = 1; i * i <= N - 1; i++)
{
// Check if N - 1 is a divisor
// or not
if ((N - 1) % i == 0)
{
if (i * i == N - 1)
answer++;
else
answer += 2;
}
}
// Iterate through all divisors and check
// for N mod d = 1 or (N-1) mod d = 0
for(int d : div)
{
int K = N;
while (K % d == 0)
K /= d;
if ((K - 1) % d == 0)
answer++;
}
return answer;
}
// Driver code
public static void main(String[] args)
{
int N = 6;
System.out.print(countValues(N));
}
}
// This code is contributed by gauravrajput1
Python3
# Python3 program to count the numbers
# which can convert N to 1
# using the given operation
# Function to count the numbers
# which can convert N to 1
# using the given operation
def countValues(N):
div = []
i = 2
# Store all the divisors of N
while ((i * i) <= N):
# If i is a divisor
if (N % i == 0):
div.append(i)
# If i is not equal to N / i
if (N != i * i):
div.append(N // i)
i += 1
answer = 0
i = 1
# Iterate through all the divisors of
# N - 1 and count them in answer
while((i * i) <= N - 1):
# Check if N - 1 is a divisor
# or not
if ((N - 1) % i == 0):
if (i * i == N - 1):
answer += 1
else:
answer += 2
i += 1
# Iterate through all divisors and check
# for N mod d = 1 or (N-1) mod d = 0
for d in div:
K = N
while (K % d == 0):
K //= d
if ((K - 1) % d == 0):
answer += 1
return answer
# Driver code
if __name__=="__main__":
N = 6
print(countValues(N))
# This code is contributed by rutvik_56
C#
// C# program to count the numbers
// which can convert N to 1
// using the given operation
using System;
using System.Collections.Generic;
class GFG{
// Function to count the numbers
// which can convert N to 1
// using the given operation
static int countValues(int N)
{
List div = new List();
// Store all the divisors of N
for(int i = 2; i * i <= N; i++)
{
// If i is a divisor
if (N % i == 0)
{
div.Add(i);
// If i is not equal to N / i
if (N != i * i)
{
div.Add(N / i);
}
}
}
int answer = 0;
// Iterate through all the divisors of
// N - 1 and count them in answer
for(int i = 1; i * i <= N - 1; i++)
{
// Check if N - 1 is a divisor
// or not
if ((N - 1) % i == 0)
{
if (i * i == N - 1)
answer++;
else
answer += 2;
}
}
// Iterate through all divisors and check
// for N mod d = 1 or (N-1) mod d = 0
foreach(int d in div)
{
int K = N;
while (K % d == 0)
K /= d;
if ((K - 1) % d == 0)
answer++;
}
return answer;
}
// Driver code
public static void Main(String[] args)
{
int N = 6;
Console.Write(countValues(N));
}
}
// This code is contributed by Amit Katiyar
输出:
3
时间复杂度:
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。