给定开始,结束和N个数字的数组。在每个步骤中,将start乘以数组中的任何数字,然后使用100000进行mod操作以获得新的开始。任务是找到从头开始即可达到最终目的的最小步骤。
例子:
Input: start = 3 end = 30 a[] = {2, 5, 7}
Output: 2
Step 1: 3*2 = 6 % 100000 = 6
Step 2: 6*5 = 30 % 100000 = 30
Input: start = 7 end = 66175 a[] = {3, 4, 65}
Output: 4
Step 1: 7*3 = 21 % 100000 = 21
Step 2: 21*3 = 6 % 100000 = 63
Step 3: 63*65 = 4095 % 100000 = 4095
Step 4: 4095*65 = 266175 % 100000 = 66175
方法:由于在上述问题中给出的模数为100000,因此最大状态数将为10 5 。可以使用简单的BFS检查所有状态。用-1初始化ans []数组,该数组指示尚未访问该状态。 ans [i]存储从开始到i所采取的步骤数。最初将开始推送到队列,然后应用BFS。弹出顶部元素,并检查它是否等于结尾,如果是,则打印ans [end]。如果该元素不等于最顶层的元素,则将顶层元素与数组中的每个元素相乘并执行mod操作。如果先前未访问过乘法元素状态,则将其推入队列。通过ans [top_element] + 1初始化ans [pushed_element]。一旦访问了所有状态,并且无法通过执行所有可能的乘法来达到该状态,则打印-1。
下面是上述方法的实现:
C++
// C++ program to find the minimum steps
// to reach end from start by performing
// multiplications and mod operations with array elements
#include
using namespace std;
// Function that returns the minimum operations
int minimumMulitplications(int start, int end, int a[], int n)
{
// array which stores the minimum steps
// to reach i from start
int ans[100001];
// -1 indicated the state has not been visited
memset(ans, -1, sizeof(ans));
int mod = 100000;
// queue to store all possible states
queue q;
// initially push the start
q.push(start % mod);
// to reach start we require 0 steps
ans[start] = 0;
// till all states are visited
while (!q.empty()) {
// get the topmost element in the queue
int top = q.front();
// pop the topmost element
q.pop();
// if the topmost element is end
if (top == end)
return ans[end];
// perform multiplication with all array elements
for (int i = 0; i < n; i++) {
int pushed = top * a[i];
pushed = pushed % mod;
// if not visited, then push it to queue
if (ans[pushed] == -1) {
ans[pushed] = ans[top] + 1;
q.push(pushed);
}
}
}
return -1;
}
// Driver Code
int main()
{
int start = 7, end = 66175;
int a[] = { 3, 4, 65 };
int n = sizeof(a) / sizeof(a[0]);
// Calling function
cout << minimumMulitplications(start, end, a, n);
return 0;
}
Java
// Java program to find the minimum steps
// to reach end from start by performing
// multiplications and mod operations with array elements
import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;
class GFG {
// Function that returns the minimum operations
static int minimumMulitplications(int start, int end, int a[], int n) {
// array which stores the minimum steps
// to reach i from start
int ans[] = new int[100001];
// -1 indicated the state has not been visited
Arrays.fill(ans, -1);
int mod = 100000;
// queue to store all possible states
Queue q = new LinkedList<>();
// initially push the start
q.add(start % mod);
// to reach start we require 0 steps
ans[start] = 0;
// till all states are visited
while (!q.isEmpty()) {
// get the topmost element in the queue
int top = q.peek();
// pop the topmost element
q.remove();
// if the topmost element is end
if (top == end) {
return ans[end];
}
// perform multiplication with all array elements
for (int i = 0; i < n; i++) {
int pushed = top * a[i];
pushed = pushed % mod;
// if not visited, then push it to queue
if (ans[pushed] == -1) {
ans[pushed] = ans[top] + 1;
q.add(pushed);
}
}
}
return -1;
}
// Driver Code
public static void main(String args[]) {
int start = 7, end = 66175;
int a[] = {3, 4, 65};
int n = a.length;
// Calling function
System.out.println(minimumMulitplications(start, end, a, n));
}
}
// This code is contributed by PrinciRaj19992
Python3
# Python3 program to find the minimum steps
# to reach end from start by performing
# multiplications and mod operations with
# array elements
from collections import deque
# Function that returns the minimum operations
def minimumMulitplications(start, end, a, n):
# array which stores the minimum
# steps to reach i from start
ans = [-1 for i in range(100001)]
# -1 indicated the state has
# not been visited
mod = 100000
q = deque()
# queue to store all possible states
# initially push the start
q.append(start % mod)
# to reach start we require 0 steps
ans[start] = 0
# till all states are visited
while (len(q) > 0):
# get the topmost element in the
# queue, pop the topmost element
top = q.popleft()
# if the topmost element is end
if (top == end):
return ans[end]
# perform multiplication with
# all array elements
for i in range(n):
pushed = top * a[i]
pushed = pushed % mod
# if not visited, then push it to queue
if (ans[pushed] == -1):
ans[pushed] = ans[top] + 1
q.append(pushed)
return -1
# Driver Code
start = 7
end = 66175
a = [3, 4, 65]
n = len(a)
# Calling function
print(minimumMulitplications(start, end, a, n))
# This code is contributed by mohit kumar
C#
// C# program to find the minimum steps
// to reach end from start by performing
// multiplications and mod operations with array elements
using System;
using System.Collections.Generic;
class GFG
{
// Function that returns the minimum operations
static int minimumMulitplications(int start, int end,
int []a, int n)
{
// array which stores the minimum steps
// to reach i from start
int []ans = new int[100001];
// -1 indicated the state has not been visited
for(int i = 0; i < ans.Length; i++)
ans[i] = -1;
int mod = 100000;
// queue to store all possible states
Queue q = new Queue();
// initially push the start
q.Enqueue(start % mod);
// to reach start we require 0 steps
ans[start] = 0;
// till all states are visited
while (q.Count != 0)
{
// get the topmost element in the queue
int top = q.Peek();
// pop the topmost element
q.Dequeue();
// if the topmost element is end
if (top == end)
{
return ans[end];
}
// perform multiplication with all array elements
for (int i = 0; i < n; i++)
{
int pushed = top * a[i];
pushed = pushed % mod;
// if not visited, then push it to queue
if (ans[pushed] == -1)
{
ans[pushed] = ans[top] + 1;
q.Enqueue(pushed);
}
}
}
return -1;
}
// Driver Code
public static void Main(String []args)
{
int start = 7, end = 66175;
int []a = {3, 4, 65};
int n = a.Length;
// Calling function
Console.WriteLine(minimumMulitplications(start, end, a, n));
}
}
/* This code contributed by PrinciRaj1992 */
4
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。