给定三个正整数N 、 M和X ,任务是通过在N的右侧附加X位来生成一个数字,以便该数字可以被M整除。如果存在多个解决方案,则打印其中任何一个。否则,打印-1 。
例子:
Input: N = 10, M = 5, X = 4
Output: 105555
Explanation: One of possible values of N (= 10) by appending X(= 4) digits on the right side of N is 105555, which is divisible by M (= 5).
Input: N = 4, M = 50, X = 2
Output: 400
方法:我们的想法是对N的右侧追加X位数通过来自区间[0,9]尝试所有可能的数字和上的N,检查右侧附加X数字后,如果数字是整除M或不是。如果发现是真的,则打印数字。否则,打印-1 。以下是递推关系:
请按照以下步骤解决问题:
- 使用上面的递推关系,通过在N的右侧附加X位来检查数字N是否可以被M整除。如果发现为真,则通过附加X位来打印N的值。
- 否则,打印-1 。
下面是上述方法的实现:
C++
// C++ program to implement
// the above approach
#include
using namespace std;
// Function to check if the value of N by
// appending X digits on right side of N
// is divisible by M or not
bool isDiv(int N, int X, int M, int& res)
{
// Base Case
if (X == 0) {
// If N is divisible
// by M
if (N % M == 0) {
// Update res
res = N;
return true;
}
return false;
}
// Iterate over the range [0, 9]
for (int i = 0; i <= 9; i++) {
// If N is divisible by M by
// appending X digits
if (isDiv(N * 10 + i, X - 1, M, res)) {
return true;
}
}
}
// Driver Code
int main()
{
int N = 4, M = 50, X = 2;
// Stores the number by appending
// X digits on the right side of N
int res = -1;
isDiv(N, X, M, res);
cout << res;
}
Java
// Java program for the above approach
import java.io.*;
import java.util.*;
class GFG
{
// Function to check if the value of N by
// appending X digits on right side of N
// is divisible by M or not
static int isDiv(int N, int X, int M, int res)
{
// Base Case
if (X == 0)
{
// If N is divisible
// by M
if (N % M == 0)
{
// Update res
res = N;
return res;
}
return res;
}
// Iterate over the range [0, 9]
for (int i = 0; i < 9; i++)
{
// If N is divisible by M by
// appending X digits
int temp = isDiv(N * 10 + i, X - 1, M, res);
if (temp != -1)
{
return temp;
}
}
return res;
}
// Driver code
public static void main(String[] args)
throws java.lang.Exception
{
int N = 4, M = 50, X = 2;
// Stores the number by appending
// X digits on the right side of N
int res = -1;
res = isDiv(N, X, M, res);
System.out.println(res);
}
}
// This code is contributed by 18bhupenderyadav18.
Python3
# Python3 program to implement
# the above approach
# Function to check if the value of N by
# appending X digits on right side of N
# is divisible by M or not
# global variable to store result
res = -1
def isDiv(N, X, M):
# Base case
if(X == 0):
# If N is divisible
# by M
if(N % M == 0):
global res
res = N
return True
return False
# Iterate over the range [0, 9]
for i in range(10):
# if N is Divisible by M upon appending X digits
if(isDiv(N*10+i, X-1, M)):
return True
# Driver Code
if __name__ == "__main__":
N, M, X = 4, 50, 2
if(isDiv(N, X, M)):
print(res)
else:
print("-1")
C#
// C# program to implement
// the above approach
using System;
class GFG
{
// Function to check if the value of N by
// appending X digits on right side of N
// is divisible by M or not
static int isDiv(int N, int X, int M, int res)
{
// Base Case
if (X == 0)
{
// If N is divisible
// by M
if (N % M == 0)
{
// Update res
res = N;
return res;
}
return res;
}
// Iterate over the range [0, 9]
for (int i = 0; i < 9; i++)
{
// If N is divisible by M by
// appending X digits
int temp = isDiv(N * 10 + i, X - 1, M, res);
if (temp != -1)
{
return temp;
}
}
return res;
}
// Driver code
public static void Main()
{
int N = 4, M = 50, X = 2;
// Stores the number by appending
// X digits on the right side of N
int res = -1;
res = isDiv(N, X, M, res);
Console.WriteLine(res);
}
}
// This code is contributed by sanjoy_62
Javascript
输出
400
时间复杂度: O(10 X )
辅助空间: O(1)