给定一个整数N,任务是找到数字 N 的 Spt函数。
The spt function (smallest parts function) is a function in number theory that counts the sum of the number of smallest parts in each partition of a positive integer. It is related to the partition function.
For example Partitions of 4 are [1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3], [4]. 1 appears 4 times in the first, 1 twice in the second, 2 twice in the third, etc.; thus spt(4) = 4 + 2 + 2 + 1 + 1 = 10.
例子:
Input: N = 4
Output: 10
Explanation:
Partitions of 4 are [1, 1, 1, 1], [1, 1, 2], [2, 2], [1, 3], [4].
1 appears 4 times in the first, 1 twice in the second, 2 twice in the third, etc.
Thus, spt(4) = 4 + 2 + 2 + 1 + 1 = 10
Input: 5
Output: 14
方法:这个想法是考虑从 1 到 N 的每个整数,并将其添加到一个答案向量中,然后再次对减少总和的剩余元素进行递归。为了避免再次打印相同的表示,每个表示将按升序构造。如果达到N的表示,我们将找到答案向量中出现的最小元素的频率,并添加到 Spt 值变量中,最后打印 Spt 变量的值。
下面是上述方法的实现:
C++
// C++ implemetation to find the
// Spt Function to given number
#include
using namespace std;
// variable to store spt
// function of a number
int spt = 0;
// Function to add value
// of frquency of minimum element
// among all representations of n
void printVector(vector& arr)
{
int min_i = INT_MAX;
for (int i = 0; i < arr.size(); i++)
min_i = min(min_i, arr[i]);
// find the value of frquency
// of minimum element
int freq = count(arr.begin(),
arr.end(),
min_i);
// calculate spt
spt += freq;
}
// Recursive function to find
// different ways in which
// n can be written as a sum of
// at one or more positive integers
void findWays(vector& arr,
int i, int n)
{
// if sum becomes n,
// consider this representation
if (n == 0)
printVector(arr);
// start from previous element
// in the representation till n
for (int j = i; j <= n; j++) {
// include current element
// from representation
arr.push_back(j);
// call function again
// with reduced sum
findWays(arr, j, n - j);
// backtrack - remove current
// element from representation
arr.pop_back();
}
}
// Function to find
// the spt function
void spt_function(int n)
{
vector arr;
// Using recurrence find
// different ways in which
// n can be written as a sum of
// at 1 or more positive integers
findWays(arr, 1, n);
cout << spt;
}
// Driver Code
int main()
{
int N = 4;
spt_function(N);
return 0;
}
Java
// Java implemetation to find the
// Spt Function to given number
import java.util.*;
class GFG{
// Variable to store spt
// function of a number
static int spt = 0;
// Find the value of frquency
// of minimum element
static int count(Vector arr, int min_i)
{
int count = 0;
for(int i = 0; i < arr.size(); i++)
if(min_i == arr.get(i))
count++;
return count;
}
// Function to add value of
// frquency of minimum element
// among all representations of n
static void printVector(Vector arr)
{
int min_i = Integer.MAX_VALUE;
for(int i = 0; i < arr.size(); i++)
min_i = Math.min(min_i, arr.elementAt(i));
// Find the value of frquency
// of minimum element
int freq = count(arr, min_i);
// Calculate spt
spt += freq;
}
// Recursive function to find
// different ways in which
// n can be written as a sum of
// at one or more positive integers
static void findWays(Vector arr,
int i, int n)
{
// If sum becomes n, consider
// this representation
if (n == 0)
printVector(arr);
// Start from previous element
// in the representation till n
for(int j = i; j <= n; j++)
{
// Include current element
// from representation
arr.add(j);
// Call function again
// with reduced sum
findWays(arr, j, n - j);
// backtrack - remove current
// element from representation
arr.remove(arr.size() - 1);
}
}
// Function to find
// the spt function
static void spt_function(int n)
{
Vector arr = new Vector<>();
// Using recurrence find
// different ways in which
// n can be written as a sum of
// at 1 or more positive integers
findWays(arr, 1, n);
System.out.print(spt);
}
// Driver Code
public static void main(String[] args)
{
int N = 4;
spt_function(N);
}
}
// This code is contributed by amal kumar choubey
Python3
# Python3 implemetation to find the
# Spt Function to given number
import sys
# Variable to store spt
# function of a number
spt = 0
# Function to add value
# of frquency of minimum element
# among all representations of n
def printVector(arr):
global spt
min_i = sys.maxsize
for i in range(len(arr)):
min_i = min(min_i, arr[i])
# Find the value of frquency
# of minimum element
freq = arr.count(min_i)
# Calculate spt
spt += freq
# Recursive function to find
# different ways in which
# n can be written as a sum of
# at one or more positive integers
def findWays(arr, i, n):
# If sum becomes n,
# consider this representation
if (n == 0):
printVector(arr)
# Start from previous element
# in the representation till n
for j in range(i, n + 1):
# Include current element
# from representation
arr.append(j)
# Call function again
# with reduced sum
findWays(arr, j, n - j)
# Backtrack - remove current
# element from representation
del arr[-1]
# Function to find
# the spt function
def spt_function(n):
arr = []
# Using recurrence find
# different ways in which
# n can be written as a sum of
# at 1 or more positive integers
findWays(arr, 1, n)
print(spt)
# Driver Code
if __name__ == '__main__':
N = 4
spt_function(N)
# This code is contributed by mohit kumar 29
C#
// C# implemetation to find the
// Spt Function to given number
using System;
using System.Collections.Generic;
class GFG{
// Variable to store spt
// function of a number
static int spt = 0;
// Find the value of frquency
// of minimum element
static int count(List arr, int min_i)
{
int count = 0;
for(int i = 0; i < arr.Count; i++)
if (min_i == arr[i])
count++;
return count;
}
// Function to add value of
// frquency of minimum element
// among all representations of n
static void printList(List arr)
{
int min_i = int.MaxValue;
for(int i = 0; i < arr.Count; i++)
min_i = Math.Min(min_i, arr[i]);
// Find the value of frquency
// of minimum element
int freq = count(arr, min_i);
// Calculate spt
spt += freq;
}
// Recursive function to find
// different ways in which
// n can be written as a sum of
// at one or more positive integers
static void findWays(List arr,
int i, int n)
{
// If sum becomes n, consider
// this representation
if (n == 0)
printList(arr);
// Start from previous element
// in the representation till n
for(int j = i; j <= n; j++)
{
// Include current element
// from representation
arr.Add(j);
// Call function again
// with reduced sum
findWays(arr, j, n - j);
// backtrack - remove current
// element from representation
arr.RemoveAt(arr.Count - 1);
}
}
// Function to find
// the spt function
static void spt_function(int n)
{
List arr = new List();
// Using recurrence find
// different ways in which
// n can be written as a sum of
// at 1 or more positive integers
findWays(arr, 1, n);
Console.Write(spt);
}
// Driver Code
public static void Main(String[] args)
{
int N = 4;
spt_function(N);
}
}
// This code is contributed by amal kumar choubey
Javascript
10