📜  方程x ^ 2 + s(x)* x-n = 0的最小根,其中s(x)是根x的数字之和。

📅  最后修改于: 2021-05-04 14:26:11             🧑  作者: Mango

给您一个整数n,找到等式x的最小正整数根,如果找不到根,则打印-1。

等式:x ^ 2 + s(x)* x – n = 0

其中x,n是正整数,s(x)是该函数,等于十进制数字系统中数字x的位数之和。
1 <= N <= 10 ^ 18

例子:

Input: N = 110 
Output: 10 
Explanation: x = 10 is the minimum root. 
             As s(10) = 1 + 0 = 1 and 
             102 + 1*10 - 110=0.  

Input: N = 4
Output: -1 
Explanation: there are no roots of the 
equation possible

一个幼稚的方法是遍历X的所有可能值,并找出是否存在这样的根,但这将是不可能的,因为n的值非常大。

一种有效的方法如下
首先,让我们找出s(x)可能值的间隔。因此,x ^ 2 <= N并且N <= 10 ^ 18,x <=109。换句话说,对于每个相当大的解x,x的十进制长度都不会扩展10位。因此Smax = s(9999999999)= 10 * 9 = 90。
让我们蛮力s(x)的值(0 <= s(x)<= 90)。现在我们有了一个普通的平方方程。解决的办法是求解方程式,并检查s(x)的当前强力值等于解决方案的位数之和。如果解决方案存在并且等式成立,我们应该得到答案并存储尽可能少的根。

下面是上述方法的实现

C++
// CPP program to find smallest value of root
// of an equation under given constraints.
#include 
using namespace std;
  
// function to check if the sum of digits is
// equal to the summation assumed
bool check(long long a, long long b)
{
    long long int c = 0;
  
    // calculate the sum of digit
    while (a != 0) {
        c = c + a % 10;
        a = a / 10;
    }
  
    return (c == b);
}
  
// function to find the largest root possible.
long long root(long long n)
{
    bool found = 0;
    long long mx = 1e18;
  
    // iterate for all possible sum of digits.
    for (long long i = 0; i <= 90; i++) {
  
        // check if discriminent is a perfect square.
        long long s = i * i + 4 * n;
        long long sq = sqrt(s);
  
        // check if discriminent is a perfect square and
        // if it as perefect root of the equation
        if (sq * sq == s && check((sq - i) / 2, i)) {
            found = 1;
            mx = min(mx, (sq - i) / 2);
        }
    }
  
    // function returns answer
    if (found)
        return mx;
    else
        return -1;
}
  
// driver program to check the above function
int main()
{
    long long n = 110;
    cout << root(n);
}


Java
// Java program to find smallest value of root
// of an equation under given constraints.
  
class GFG{
// function to check if the sum of digits is
// equal to the summation assumed
static boolean check(long a, long b)
{
    long c = 0;
  
    // calculate the sum of digit
    while (a != 0) {
        c = c + a % 10;
        a = a / 10;
    }
  
    return (c == b);
}
  
// function to find the largest root possible.
static long root(long n)
{
    boolean found = false;
    long mx = (long)1E18;
  
    // iterate for all possible sum of digits.
    for (long i = 0; i <= 90; i++) {
  
        // check if discriminent is a perfect square.
        long s = i * i + 4 * n;
        long sq = (long)Math.sqrt(s);
  
        // check if discriminent is a perfect square and
        // if it as perefect root of the equation
        if (sq * sq == s && check((sq - i) / 2, i)) {
            found = true;
            mx = Math.min(mx, (sq - i) / 2);
        }
    }
  
    // function returns answer
    if (found)
        return mx;
    else
        return -1;
}
  
// driver program to check the above function
public static void main(String[] args)
{
    long n = 110;
    System.out.println(root(n));
}
}
// This code is contributed by mits


Python3
# Python3 program to find smallest 
# value of root of an equation
# under given constraints.
import math
  
# function to check if the sum 
# of digits is equal to the
# summation assumed
def check(a, b):
    c = 0;
  
    # calculate the
    # sum of digit
    while (a != 0):
        c = c + a % 10;
        a = int(a / 10);
  
    return True if(c == b) else False;
  
# function to find the
# largest root possible.
def root(n):
    found = False;
      
    # float(1E+18)
    mx = 1000000000000000001; 
  
    # iterate for all 
    # possible sum of digits.
    for i in range(91): 
  
        # check if discriminent
        # is a perfect square.
        s = i * i + 4 * n;
        sq = int(math.sqrt(s));
  
        # check if discriminent is 
        # a perfect square and
        # if it as perefect root 
        # of the equation
        if (sq * sq == s and 
            check(int((sq - i) / 2), i)): 
            found = True;
            mx = min(mx, int((sq-i) / 2));
  
    # function returns answer
    if (found):
        return mx;
    else:
        return -1;
  
# Driver Code
n = 110;
print(root(n));
  
# This code is contributed by mits


C#
//C# program to find smallest value of root 
// of an equation under given constraints. 
using System;
public class GFG{ 
    // function to check if the sum of digits is 
    // equal to the summation assumed 
    static bool check(long a, long b) 
    { 
        long c = 0; 
  
        // calculate the sum of digit 
        while (a != 0) { 
            c = c + a % 10; 
            a = a / 10; 
        } 
  
        return (c == b); 
    } 
  
    // function to find the largest root possible. 
    static long root(long n) 
    { 
        bool found = false; 
        long mx = (long)1E18; 
  
        // iterate for all possible sum of digits. 
        for (long i = 0; i <= 90; i++) { 
  
            // check if discriminent is a perfect square. 
            long s = i * i + 4 * n; 
            long sq = (long)Math.Sqrt(s); 
  
            // check if discriminent is a perfect square and 
            // if it as perefect root of the equation 
            if (sq * sq == s && check((sq - i) / 2, i)) { 
                found = true; 
                mx = Math.Min(mx, (sq - i) / 2); 
            } 
        } 
  
        // function returns answer 
        if (found) 
            return mx; 
        else
            return -1; 
    } 
  
    // driver program to check the above function 
    public static void Main() 
    { 
        long n = 110; 
        Console.Write(root(n)); 
    } 
} 
// This code is contributed by Raput-Ji


PHP


输出:

10