给定一个正整数N ,任务是生成一个数组,使每个元素的欧拉Totient函数之和等于N。
例子:
Input: N = 6
Output: 3 8
Explanation:
The total number of integers i, from 1 to 3 such that GCD(i, 3) = 1 is 2.
The total number of integers i, from 1 to 6 such that GCD(i, 6) = 1 is 4.
After the above operations, the required array whose sum is 6 is {3, 8}.
Input: N = 12
Output: 1 12 2 6 3 4
方法:可以根据欧拉Totient函数的除数和性质来解决给定问题,即
- 数量为N <的Euler函数是给出GCD(I,N)为1,一个数N可以被表示为N的所有的除数的Euler函数值的总和从1到N的整数的数。
- 因此,该想法是找到给定数N的除数作为结果数组。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to construct the array such
// the sum of values of Euler Totient
// functions of all array elements is N
void constructArray(int N)
{
// Stores the resutlant array
vector ans;
// Find divisors in sqrt(N)
for (int i = 1; i * i <= N; i++) {
// If N is divisible by i
if (N % i == 0) {
// Push the current divisor
ans.push_back(i);
// If N is not a
// perfect square
if (N != (i * i)) {
// Push the second divisor
ans.push_back(N / i);
}
}
}
// Print the resultant array
for (auto it : ans) {
cout << it << " ";
}
}
// Driver Code
int main()
{
int N = 12;
// Function Call
constructArray(N);
return 0;
}
Java
// Java program for the above approach
import java.util.*;
class GFG{
// Function to construct the array such
// the sum of values of Euler Totient
// functions of all array elements is N
static void constructArray(int N)
{
// Stores the resutlant array
ArrayList ans = new ArrayList();
// Find divisors in sqrt(N)
for(int i = 1; i * i <= N; i++)
{
// If N is divisible by i
if (N % i == 0)
{
// Push the current divisor
ans.add(i);
// If N is not a
// perfect square
if (N != (i * i))
{
// Push the second divisor
ans.add(N / i);
}
}
}
// Print the resultant array
for(int it : ans)
{
System.out.print(it + " ");
}
}
// Driver Code
public static void main(String[] args)
{
int N = 12;
// Function Call
constructArray(N);
}
}
// This code is contributed by splevel62
Python3
# Python3 program for the above approach
from math import sqrt
# Function to construct the array such
# the sum of values of Euler Totient
# functions of all array elements is N
def constructArray(N):
# Stores the resutlant array
ans = []
# Find divisors in sqrt(N)
for i in range(1, int(sqrt(N)) + 1, 1):
# If N is divisible by i
if (N % i == 0):
# Push the current divisor
ans.append(i)
# If N is not a
# perfect square
if (N != (i * i)):
# Push the second divisor
ans.append(N / i)
# Print the resultant array
for it in ans:
print(int(it), end = " ")
# Driver Code
if __name__ == '__main__':
N = 12
# Function Call
constructArray(N)
# This code is contributed by ipg2016107
C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to construct the array such
// the sum of values of Euler Totient
// functions of all array elements is N
static void constructArray(int N)
{
// Stores the resutlant array
List ans = new List();
// Find divisors in sqrt(N)
for(int i = 1; i * i <= N; i++)
{
// If N is divisible by i
if (N % i == 0)
{
// Push the current divisor
ans.Add(i);
// If N is not a
// perfect square
if (N != (i * i))
{
// Push the second divisor
ans.Add(N / i);
}
}
}
// Print the resultant array
foreach(int it in ans)
{
Console.Write(it + " ");
}
}
// Driver Code
public static void Main()
{
int N = 12;
// Function Call
constructArray(N);
}
}
// This code is contributed by ukasp
输出:
1 12 2 6 3 4
时间复杂度: O(√N)
辅助空间: O(N)