给定一个数字N ,任务是检查N是否是一个可接受的数字。如果N是一个可接受的数字,则打印“是”,否则打印“否” 。
Amenable numbers are numbers if there exists a multiset of integers whose size, sum and product equal to the number.
For example, 8 is an Amenable Number because there is a multiset of integers {-1, -1, 1, 1, 1, 1, 2, 4} whose size, sum and product is 8.
例子:
Input: N = 8
Output: Yes
Explanation:
8 is an Amenable Number because there is a multiset of integers {-1, -1, 1, 1, 1, 1, 2, 4} whose size, sum and product is 8.
Input: N = 30
Output: No
Explanation:
30 is not an Amenable Number because there doesn’t exists a multiset of integers whose size, sum and product is 30.
方法:
前几个可允许编号是1、5、8、9、12、13、16、17、20、21 …..可以表示为4K或4K +1形式。
因此,任何形式为4K或4K +1的数字N都是可接受的数字。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to check if N
// is Amenable number
bool isAmenableNum(int N)
{
// Return true if N is of the form
// 4K or 4K + 1
return (N % 4 == 0
|| (N - 1) % 4 == 0);
}
// Driver Code
int main()
{
// Given Number
int n = 8;
// Function Call
if (isAmenableNum(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java program for the above approach
class GFG{
// Function to check if N
// is Amenable number
static boolean isAmenableNum(int N)
{
// Return true if N is of the form
// 4K or 4K + 1
return (N % 4 == 0 || (N - 1) % 4 == 0);
}
// Driver code
public static void main(String[] args)
{
int n = 8;
if (isAmenableNum(n))
{
System.out.println("Yes");
}
else
{
System.out.println("No");
}
}
}
// This code is contributed by shubham
Python3
# Python3 program for the above approach
import math
# Function to check if N
# is Amenable number
def isAmenableNum(N):
# Return true if N is of the
# form 4K or 4K + 1
return (N % 4 == 0 or
(N - 1) % 4 == 0);
# Driver code
# Given number
N = 8;
# Function call
if (isAmenableNum(N)):
print("Yes");
else:
print("No");
# This code is contributed by rock_cool
C#
// C# program for the above approach
using System;
class GFG{
// Function to check if N
// is Amenable number
static bool isAmenableNum(int N)
{
// Return true if N is of the form
// 4K or 4K + 1
return (N % 4 == 0 || (N - 1) % 4 == 0);
}
// Driver code
public static void Main(String[] args)
{
int n = 8;
if (isAmenableNum(n))
{
Console.WriteLine("Yes");
}
else
{
Console.WriteLine("No");
}
}
}
// This code is contributed by amal kumar choubey
Javascript
Yes
时间复杂度: O(1)