Hay 12 estaciones intermedias entre dos lugares A y B. ¿Encuentre el número de maneras en que se puede hacer que un tren se detenga en 4 de estas estaciones intermedias para que no haya dos estaciones de parada consecutivas?
Ejemplos –
Input : n = 12, s = 4 Output : 126 Input : n = 16, s = 5 Output : 792
Explicación 1:
Fije/elimine las cuatro paradas como puntos fijos y calcule de cuántas maneras se pueden insertar las otras estaciones entre ellas, si debe tener al menos una estación entre paradas.
A x x x x x x x x B
Entre estas 8 estaciones sin paradas tenemos 9 lugares y seleccionamos estos 9 lugares como parada entre estas 8 estaciones.
Por lo tanto, la respuesta debería ser = 126
Explicación 2:
si conoce combinaciones de bolas indistinguibles en cajas distinguibles, simplemente puede usar, . En esta pregunta, $n$es el número de estaciones y $p$es el número de estaciones en las que desea detenerse. Aquí las estaciones de parada son como bolas indistinguibles y las estaciones sin parada son como cajas distinguibles.
Por lo tanto, = = 126
Código –
C++
#include <stdio.h> int stopping_station(int, int); // function to calculate number // of ways of selecting 'p' non consecutive // stations out of 'n' stations int stopping_station(int p, int n) { int num = 1, dem = 1, s = p; // selecting 's' positions out of 'n-s+1' while (p != 1) { dem *= p; p--; } int t = n - s + 1; while (t != (n - 2 * s + 1)) { num *= t; t--; } if ((n - s + 1) >= s) printf("%d", num / dem); else // if conditions does not satisfy of combinatorics printf("not possible"); } // driver code int main() { // n is total number of stations // s is no. of stopping stations int n, s; // arguments of function are // number of stopping station // and total number of stations stopping_station(4, 12); }
Java
// Java code to calculate number // of ways of selecting 'p' non // consecutive stations out of // 'n' stations import java.io.*; import java.util.*; class GFG { public static int stopping_station(int p, int n) { int num = 1, dem = 1, s = p; // selecting 's' positions out of 'n-s+1' while (p != 1) { dem *= p; p--; } int t = n - s + 1; while (t != (n - 2 * s + 1)) { num *= t; t--; } if ((n - s + 1) >= s) System.out.print(num / dem); else // if conditions does not satisfy of combinatorics System.out.print("not possible"); return 0; } public static void main (String[] args) { // n is total number of stations // s is no. of stopping stations int n, s; // arguments of function are // number of stopping station // and total number of stations stopping_station(4, 12); } } // ""This code is contributed by Mohit Gupta_OMG ""
Python3
# Python code to calculate number # of ways of selecting 'p' non # consecutive stations out of # 'n' stations def stopping_station( p, n): num = 1 dem = 1 s = p # selecting 's' positions # out of 'n-s+1' while p != 1: dem *= p p-=1 t = n - s + 1 while t != (n-2 * s + 1): num *= t t-=1 if (n - s + 1) >= s: return int(num/dem) else: # if conditions does not # satisfy of combinatorics return -1 # driver code num = stopping_station(4, 12) if num != -1: print(num) else: print("Not Possible") # This code is contributed by "Abhishek Sharma 44"
C#
// C# code to calculate number // of ways of selecting 'p' non // consecutive stations out of // 'n' stations using System; class GFG { public static int stopping_station(int p, int n) { int num = 1, dem = 1, s = p; // selecting 's' positions // out of 'n-s+1' while (p != 1) { dem *= p; p--; } int t = n - s + 1; while (t != (n - 2 * s + 1)) { num *= t; t--; } if ((n - s + 1) >= s) Console.WriteLine(num / dem); // if conditions does not // satisfy of combinatorics else Console.WriteLine("Not possible"); return 0; } // Driver Code public static void Main(String []args) { // arguments of function are // number of stopping station // and total number of stations stopping_station(4, 12); } } // This code is contributed by vt_m.
PHP
<?php // PHP code for Number of stopping // station problem // function to calculate number // of ways of selecting 'p' non // consecutive stations out of // 'n' stations function stopping_station(int $p, int $n) { $num = 1; $dem = 1; $s = $p; // selecting 's' positions // out of 'n-s+1' while($p != 1) { $dem *= $p; $p--; } $t = $n - $s + 1; while($t != ($n - 2 * $s + 1)) { $num *= $t; $t--; } if (($n - $s + 1) >= $s) echo $num / $dem; else // if conditions does not // satisfy of combinatorics echo "not possible"; } // Driver Code // n is total number of stations // s is no. of stopping stations $n; $s; // arguments of function are // number of stopping station // and total number of stations stopping_station(4, 12); // This code is contributed by anuj_67. ?>
Javascript
<script> // Javascript code to calculate number // of ways of selecting 'p' non // consecutive stations out of // 'n' stations function stopping_station( p, n) { var num = 1, dem = 1, s = p; // selecting 's' positions // out of 'n-s+1' while (p != 1) { dem *= p; p--; } var t = n - s + 1; while (t != (n - 2 * s + 1)) { num *= t; t--; } if ((n - s + 1) >= s){ document.write(num / dem); } // if conditions does not // satisfy of combinatorics else{ document.write("Not possible"); } return 0; } // Driver Code // arguments of function are // number of stopping station // and total number of stations stopping_station(4, 12); </script>
Producción :
126
Publicación traducida automáticamente
Artículo escrito por Shivani Virmani y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA