给定整数N ,任务是检查N是否为Zumkelle数
Zumkelle Number is a whose divisors can be partitioned into two sets with the same sum.
For example, 12 is a Zumkeller number because its divisors 1, 2, 3, 4, 6, 12, can be partitioned in the two sets {12, 2}, and {1, 3, 4, 6} with same sum 14.
例子:
Input: N = 12
Output: Yes
Explanation:
12’s’ divisors 1, 2, 3, 4, 6, 12, can be partitioned
in the two sets {12, 2}, and {1, 3, 4, 6} with same sum 14.
Input: N = 26
Output: No
方法:想法是将所有数字的因数存储在一个数组中,然后将数组划分为两个子集,以使两个子集中的元素之和相同。
相同的分区问题将在本文中详细说明:
分区问题DP-18
下面是上述方法的实现:
C++
// C++ Program to check if n
// is an Zumkelle number
#include
using namespace std;
// Function to store divisors of N
// in a vector
void storeDivisors(int n, vector& div)
{
// Find all divisors which divides 'num'
for (int i = 1; i <= sqrt(n); i++) {
// if 'i' is divisor of 'n'
if (n % i == 0) {
// if both divisors are same
// then store it once else store
// both divisors
if (i == (n / i))
div.push_back(i);
else {
div.push_back(i);
div.push_back(n / i);
}
}
}
}
// Returns true if vector can be partitioned
// in two subsets of equal sum, otherwise false
bool isPartitionPossible(vector& arr)
{
int n = arr.size();
int sum = 0;
int i, j;
// Calculate sum of all elements
for (i = 0; i < n; i++)
sum += arr[i];
if (sum % 2 != 0)
return false;
bool part[sum / 2 + 1][n + 1];
// initialize top row as true
for (i = 0; i <= n; i++)
part[0][i] = true;
// initialize leftmost column,
// except part[0][0], as 0
for (i = 1; i <= sum / 2; i++)
part[i][0] = false;
// Fill the partition table
// in bottom up manner
for (i = 1; i <= sum / 2; i++) {
for (j = 1; j <= n; j++) {
part[i][j] = part[i][j - 1];
if (i >= arr[j - 1])
part[i][j] = part[i][j] || part[i - arr[j - 1]][j - 1];
}
}
return part[sum / 2][n];
}
// Function to check if n
// is an Zumkelle number
bool isZumkelleNum(int N)
{
// vector to store all
// proper divisors of N
vector div;
storeDivisors(N, div);
return isPartitionPossible(div);
}
// Driver code
int main()
{
int n = 12;
if (isZumkelleNum(n))
cout << "Yes";
else
cout << "No";
return 0;
}
Java
// Java Program to check if n
// is an Zumkelle number
import java.util.*;
class GFG{
// Function to store divisors of N
// in a vector
static void storeDivisors(int n,
Vector div)
{
// Find all divisors which divides 'num'
for (int i = 1; i <= Math.sqrt(n); i++)
{
// if 'i' is divisor of 'n'
if (n % i == 0)
{
// if both divisors are same
// then store it once else store
// both divisors
if (i == (n / i))
div.add(i);
else
{
div.add(i);
div.add(n / i);
}
}
}
}
// Returns true if vector can be partitioned
// in two subsets of equal sum, otherwise false
static boolean isPartitionPossible
(Vector arr)
{
int n = arr.size();
int sum = 0;
int i, j;
// Calculate sum of all elements
for (i = 0; i < n; i++)
sum += arr.get(i);
if (sum % 2 != 0)
return false;
boolean [][]part = new boolean[sum / 2 + 1][n + 1];
// initialize top row as true
for (i = 0; i <= n; i++)
part[0][i] = true;
// initialize leftmost column,
// except part[0][0], as 0
for (i = 1; i <= sum / 2; i++)
part[i][0] = false;
// Fill the partition table
// in bottom up manner
for (i = 1; i <= sum / 2; i++) {
for (j = 1; j <= n; j++) {
part[i][j] = part[i][j - 1];
if (i >= arr.get(j - 1))
part[i][j] = part[i][j] ||
part[i - arr.get(j - 1)][j - 1];
}
}
return part[sum / 2][n];
}
// Function to check if n
// is an Zumkelle number
static boolean isZumkelleNum(int N)
{
// vector to store all
// proper divisors of N
Vector div = new Vector();
storeDivisors(N, div);
return isPartitionPossible(div);
}
// Driver code
public static void main(String[] args)
{
int n = 12;
if (isZumkelleNum(n))
System.out.print("Yes");
else
System.out.print("No");
}
}
// This code is contributed by Amit Katiyar
C#
// C# Program to check if n
// is an Zumkelle number
using System;
using System.Collections.Generic;
class GFG{
// Function to store divisors of N
// in a vector
static void storeDivisors(int n,
List div)
{
// Find all divisors which divides 'num'
for (int i = 1; i <= Math.Sqrt(n); i++)
{
// if 'i' is divisor of 'n'
if (n % i == 0)
{
// if both divisors are same
// then store it once else store
// both divisors
if (i == (n / i))
div.Add(i);
else
{
div.Add(i);
div.Add(n / i);
}
}
}
}
// Returns true if vector can be partitioned
// in two subsets of equal sum, otherwise false
static bool isPartitionPossible
(List arr)
{
int n = arr.Count;
int sum = 0;
int i, j;
// Calculate sum of all elements
for (i = 0; i < n; i++)
sum += arr[i];
if (sum % 2 != 0)
return false;
bool [,]part = new bool[sum / 2 + 1, n + 1];
// initialize top row as true
for (i = 0; i <= n; i++)
part[0, i] = true;
// initialize leftmost column,
// except part[0,0], as 0
for (i = 1; i <= sum / 2; i++)
part[i, 0] = false;
// Fill the partition table
// in bottom up manner
for (i = 1; i <= sum / 2; i++)
{
for (j = 1; j <= n; j++)
{
part[i, j] = part[i, j - 1];
if (i >= arr[j - 1])
part[i, j] = part[i, j] ||
part[i - arr[j - 1],
j - 1];
}
}
return part[sum / 2, n];
}
// Function to check if n
// is an Zumkelle number
static bool isZumkelleNum(int N)
{
// vector to store all
// proper divisors of N
List div = new List();
storeDivisors(N, div);
return isPartitionPossible(div);
}
// Driver code
public static void Main(String[] args)
{
int n = 12;
if (isZumkelleNum(n))
Console.Write("Yes");
else
Console.Write("No");
}
}
// This code is contributed by 29AjayKumar
输出:
是的
是的
参考: OEIS