检查是否有任何有效的序列可以被M整除

检查是否有任何有效的序列可以被m整除

序列是对象的集合,在我们的例子中,它是整数的集合。任务是判断元素内使用加减运算符的序列是否能被 M 整除。

问题陈述

给定一个整数 M 和一个整数数组。仅使用元素之间的加法和减法检查是否存在其解可被 M 整除的有效序列。

示例 1

Input: M = 2, arr = {1, 2, 5}
Output: TRUE

解释 – 对于给定的数组,可能存在有效序列 {1 + 2 + 5} = {8},且可被 2 整除。

示例 2

Input: M = 4, arr = {1, 2}
Output: FALSE

解释 – 对于给定的数组,不可能存在其解可被 4 整除的序列。

方法 1:暴力方法

解决该问题的一个简单方法是使用递归函数查找数组的所有可能序列,然后检查是否有任何序列可被 M 整除。

伪代码

procedure divisible (M, arr[], index, sum, n)   if index == n      if sum is a multiple of M         ans = TRUE      end if      ans = false   end if   divisible(M, arr, index + 1, sum + arr[index], n) or divisible(M, arr, index + 1, sum - arr[index], n)end procedure

示例:C++ 实现

在下面的程序中,我们使用递归方法查找所有有效序列,然后检查是否有任何有效序列可被 M 整除。

#include using namespace std;// Recusive function to find if a valid sequence is divisible by M or notbool divisible(int M, int arr[], int index, int sum, int n){   // Cheking the divisiblilty by M when the array ends   if (index == n)  {      if (sum % M == 0){         return true;      }      return false;   }      // If either of addition or subtraction is true, return true   return divisible(M, arr, index + 1, sum + arr[index], n) || divisible(M, arr, index + 1, sum - arr[index], n);}int main(){   int M = 4, arr[2] = {1, 5};   if (divisible(M, arr, 0, 0, 2)){      cout << "TRUE";   }   else{      cout << " FALSE";   }   return 0;}

输出

TRUE

时间复杂度 – O(2^n) 由于使用了递归。

空间复杂度 – O(n) 由于递归堆栈空间。

方法 2:回溯

此方法类似于之前的强力递归方法,不同之处在于使用回溯,我们可以回溯搜索空间,以免走上我们知道不具有可被 M 整除的有效序列的路径。

伪代码

procedure divisible (M, arr[], index, sum, n)   if index == n      if sum is a multiple of M         ans = TRUE      end if      ans = false   end if   if divisible(M, arr, index + 1, sum + arr[index], n)      ans = true   end if   if divisible(M, arr, index + 1, sum - arr[index], n)      ans = true   end if   ans = falseend procedure

示例:C++ 实现

在下面的程序中,我们使用回溯来修剪搜索空间,以找到问题的解决方案。

#include using namespace std;// Function to find if a valid sequence is divisible by M or notbool divisible(int M, int arr[], int index, int sum, int n){   // Cheking the divisiblilty by M when the array ends   if (index == n){      if (sum % M == 0){         return true;      }      return false;   }      // Checking the divisibility of sum + arr[index]   if (divisible(M, arr, index + 1, sum + arr[index], n)){      return true;   }      // Checking the divisibility of sum - arr[index]   if (divisible(M, arr, index + 1, sum - arr[index], n)){      return true;   }   return false;}int main(){   int M = 4, arr[2] = {1, 5};   if (divisible(M, arr, 0, 0, 2)){      cout << "TRUE";   }   else{      cout << " FALSE";   }   return 0;}

输出

TRUE

时间复杂度 – 最坏情况下的时间复杂度为 O(2^n),但实际上由于搜索空间的修剪,它比暴力方法更好。

空间复杂度 – 由于递归堆栈空间而导致的 O(n)。

方法 3:贪婪方法

该问题的贪心解决方案是首先按升序对数组进行排序,然后在总和不超过 M 的情况下贪婪地应用加法函数。这种方法可能不会给出全局最优解,但会给出局部最优解。

伪代码

procedure divisible (M, arr[])   sum = 0   for i = 1 to end of arr      sum = sum + arr[i]   if sum is divisible by M      ans = true   end if   sort array arr[]   i = 0   j = last index of array   while i < j      if arr[j] - arr[i] is divisible by M         ans = true      end if      if sum % M == (sum - arr[j]) % M         sum = sum - arr[j]         j = j - 1      else         sum = sum - arr[i]         i = i + 1      end if   ans = falseend procedure

示例:C++ 实现

在下面的程序中,对数组进行排序以找到可被 M 整除的最佳局部子数组。

#include using namespace std;// Greedy function to find if a valid sequence is divisible by M or notbool divisible(int M, vector &arr){   int sum = 0;   for (int i = 0; i < arr.size(); i++) {      sum += arr[i];   }      // Checking if sumof all elements is divisible by M   if (sum % M == 0){      return true;   }      sort(arr.begin(), arr.end());   int i = 0, j = arr.size() - 1;   while (i < j){         // Checking if the difference between the largest and smallest element at a time in the array is divisible by M      if ((arr[j] - arr[i]) % M == 0){         return true;      }            // Removing either the largest or smallest element based on which does not affect the sum's divisibility      if (sum % M == (sum - arr[i]) % M){         sum -= arr[i];         i++;      }      else{         sum -= arr[j];         j--;      }   }   return false;}int main(){   int M = 4;   int array[2] = {1, 3};   vector arr(array, array + 2);   if (divisible(M, arr)){      cout << "TRUE";   }   else{      cout << " FALSE";   }   return 0;}

输出

TRUE

方法 4:动态规划

使用动态规划的概念,在此解决方案中我们存储评估的中间结果。我们将创建一个包含 N+1 行和 M 列的表,并且当我们不使用数组元素时,结果 % M == 0 的基本情况。然后迭代模 M 的所有可能余数,我们更新表格。

伪代码

procedure divisible (arr[], M , N)   dp[N+1][M] = false   dp[0][0] = true    for i = 1 to N      for i = j to M         mod = arr[ i- 1] % M         dp[i][j] = dp[i - 1][(j - mod + M) % M] or dp[i - 1][(j + mod) % M]   ans = dp[N][0]end procedure

示例:C++ 实现

在下面的程序中,我们将问题分解为子问题,然后解决它们。

#include using namespace std;// Function to find if a valid sequence is divisible by M or notbool divisible(int arr[], int M, int N){   // Creating the dp table of size N+1 * M   vector<vector > dp(N + 1, vector(M, false));      // Base case   dp[0][0] = true;      // For each element iterating over all possible remainders j modulo M   for (int i = 1; i <= N; i++){      for (int j = 0; j < M; j++){         int mod = arr[i - 1] % M;                  // Either exclude or include the current element in the table         dp[i][j] = dp[i - 1][(j - mod + M) % M] || dp[i - 1][(j + mod) % M];      }   }   return dp[N][0];}int main(){   int M = 4;   int arr[2] = {1, 3};   if (divisible(arr, M, 2)){      cout << "TRUE";   }   else{      cout << " FALSE";   }   return 0;}

输出

TRUE

结论

总而言之,为了找到可被 M 整除的有效序列,我们可以应用多种方法以及不同的关系和空间分析,范围从暴力情况下的 O(2^n) 到动态情况下的 O(NM)编程是最有效的方法。

以上就是检查是否有任何有效的序列可以被M整除的详细内容,更多请关注创想鸟其它相关文章!

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 chuangxiangniao@163.com 举报,一经查实,本站将立刻删除。
发布者:程序猿,转转请注明出处:https://www.chuangxiangniao.com/p/1444942.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2025年12月17日 22:02:11
下一篇 2025年12月13日 16:43:35

发表回复

登录后才能评论
关注微信