给定三个整数N 、 X和Y 。任务是找到满足给定方程的N 个正整数。
- a 1 2 + a 2 2 + …. + a n 2 ≥ X
- a 1 + a 2 + …. + a n ≤ Y
如果不可能有这样的整数序列,则打印-1 。
例子:
Input: N = 3, X = 254, Y = 18
Output: 1 1 16
12 + 12 + 162 = 1 + 1 + 256 = 258 which is ≥ X
1 + 1 + 16 = 18 which is ≤ Y
Input: N = 2, X = 3, Y = 2
Output: -1
No such sequence exists.
方法:很容易看出,为了使平方和最大化,应该使除第一个数字以外的所有数字都等于1,并最大化第一个数字。记住这一点,我们只需要检查 y 的给定值是否足够大以满足所有 n 数都是正数的限制。如果 y 不是太小,那么我们只需要确保X ≤ 1 + 1 + … + (y – (n – 1)) 2 。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to find n positive integers
// that satisfy the given conditions
void findIntegers(int n, int x, int y)
{
// To store n positive integers
vector ans;
// Place N - 1 one's
for (int i = 0; i < n - 1; i++)
ans.push_back(1);
// If can not place (y - (n - 1))
// as the Nth integer
if (y - (n - 1) <= 0) {
cout << "-1";
return;
}
// Place Nth integer
ans.push_back(y - (n - 1));
// To store the sum of
// squares of N integers
int store = 0;
for (int i = 0; i < n; i++)
store += ans[i] * ans[i];
// If it is less than x
if (store < x) {
cout << "-1";
return;
}
// Print the required integers
for (int i = 0; i < n; i++)
cout << ans[i] << " ";
}
// Driver code
int main()
{
int n = 3, x = 254, y = 18;
findIntegers(n, x, y);
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
// Function to find n positive integers
// that satisfy the given conditions
static void findIntegers(int n, int x, int y)
{
// To store n positive integers
ArrayList ans = new ArrayList();
// Place N - 1 one's
for (int i = 0; i < n - 1; i++)
ans.add(1);
// If can not place (y - (n - 1))
// as the Nth integer
if (y - (n - 1) <= 0)
{
System.out.print("-1");
return;
}
// Place Nth integer
ans.add(y - (n - 1));
// To store the sum of
// squares of N integers
int store = 0;
for (int i = 0; i < n; i++)
store += ans.get(i) * ans.get(i);
// If it is less than x
if (store < x)
{
System.out.print("-1");
return;
}
// Print the required integers
for (int i = 0; i < n; i++)
System.out.print(ans.get(i)+" ");
}
// Driver code
public static void main (String[] args)
{
int n = 3, x = 254, y = 18;
findIntegers(n, x, y);
}
}
// This code is contributed by mits
Python3
# Python3 implementation of the approach
# Function to find n positive integers
# that satisfy the given conditions
def findIntegers(n, x, y):
# To store n positive integers
ans = []
# Place N - 1 one's
for i in range(n - 1):
ans.append(1)
# If can not place (y - (n - 1))
# as the Nth integer
if (y - (n - 1) <= 0):
print("-1", end = "")
return
# Place Nth integer
ans.append(y - (n - 1))
# To store the sum of
# squares of N integers
store = 0
for i in range(n):
store += ans[i] * ans[i]
# If it is less than x
if (store < x):
print("-1", end = "")
return;
# Print the required integers
for i in range(n):
print(ans[i], end = " ")
# Driver code
n, x, y = 3, 254, 18
findIntegers(n, x, y)
# This code is contributed by mohit kumar
C#
// C# implementation of the approach
using System;
using System.Collections;
class GFG
{
// Function to find n positive integers
// that satisfy the given conditions
static void findIntegers(int n, int x, int y)
{
// To store n positive integers
ArrayList ans = new ArrayList();
// Place N - 1 one's
for (int i = 0; i < n - 1; i++)
ans.Add(1);
// If can not place (y - (n - 1))
// as the Nth integer
if (y - (n - 1) <= 0)
{
Console.Write("-1");
return;
}
// Place Nth integer
ans.Add(y - (n - 1));
// To store the sum of
// squares of N integers
int store = 0;
for (int i = 0; i < n; i++)
store += (int)ans[i] *(int)ans[i];
// If it is less than x
if (store < x)
{
Console.Write("-1");
return;
}
// Print the required integers
for (int i = 0; i < n; i++)
Console.Write((int)ans[i]+" ");
}
// Driver code
static void Main()
{
int n = 3, x = 254, y = 18;
findIntegers(n, x, y);
}
}
// This code is contributed by mits
PHP
Javascript
输出:
1 1 16