Dados tres números enteros A, B y C , la tarea es contar el número de formas de dividir C en dos partes y sumar A y B de modo que A sea estrictamente mayor que B.
Ejemplos:
Entrada: A = 5, B = 3, C = 4
Salida: 3
Los posibles valores de A y B después de dividir C son:
A = 7, B = 5 donde C se divide en 2 y 2.
A = 8, B = 4 donde C se divide en 3 y 1.
A – 9, B = 3 donde C se divide en 4 y 0.
Entrada: A = 3, B = 5, C = 5
Salida: 2
Planteamiento: Al observar detenidamente, se forma la siguiente relación para este problema.
- Sume addA y addB a A y B respectivamente .
- Por lo tanto, addA + addB = C y debería satisfacer la desigualdad A + addA > B + addB .
- Ahora, ya que addB = C – addA y ponlo en la desigualdad:
A + addA > B + (C - addA) or, 2addA > C + B - A or, 2addA >= C + B - A + 1 or, addA >= (C + B - A + 1) / 2
- Dado que addA debe ser no negativo, addA = max(0, (C+ B – A + 1) / 2) .
- La división debe ser una división máxima, por lo que podemos reescribirla como addA = max(0, (C+ B – A + 2) / 2).
- Sea este valor igual a minAddA . Dado que todos los valores enteros addA de [minAddA, C] , satisface la relación A + addA > B + addB , por lo que el número requerido de formas es igual a max(0, C – minAddA + 1) .
A continuación se muestra la implementación del enfoque anterior:
C++
// C++ implementation of the above approach #include <bits/stdc++.h> using namespace std; // Function to count the number of ways to divide // C into two parts and add to A and B such // that A is strictly greater than B int countWays(int A, int B, int C) { // Minimum value added to A to satisfy // the given relation int minAddA = max(0, (C + B - A + 2) / 2); // Number of different values of A, i.e., // number of ways to divide C int count_ways = max(C - minAddA + 1, 0); return count_ways; } // Driver code int main() { int A = 3, B = 5, C = 5; cout << countWays(A, B, C); return 0; }
Java
// Java implementation of the above approach import java.util.*; class GFG{ // Function to count the number of ways to divide // C into two parts and add to A and B such // that A is strictly greater than B static int countWays(int A, int B, int C) { // Minimum value added to A to satisfy // the given relation int minAddA = Math.max(0, (C + B - A + 2) / 2); // Number of different values of A, i.e., // number of ways to divide C int count_ways = Math.max(C - minAddA + 1, 0); return count_ways; } // Driver code public static void main(String args[]) { int A = 3, B = 5, C = 5; System.out.println(countWays(A, B, C)); } } // This code is contributed by AbhiThakur
Python3
# Python3 implementation of the above approach # Function to count the number of ways to divide # C into two parts and add to A and B such # that A is strictly greater than B def countWays(A, B, C): # Minimum value added to A to satisfy # the given relation minAddA = max(0, (C + B - A + 2) // 2) # Number of different values of A, i.e., # number of ways to divide C count_ways = max(C - minAddA + 1, 0) return count_ways # Driver code A = 3 B = 5 C = 5 print(countWays(A, B, C)) # This code is contributed by shivanisingh
C#
// C# implementation of the above approach using System; class GFG { // Function to count the number of ways to divide // C into two parts and add to A and B such // that A is strictly greater than B static int countWays(int A, int B, int C) { // Minimum value added to A to satisfy // the given relation int minAddA = Math.Max(0, (C + B - A + 2) / 2); // Number of different values of A, i.e., // number of ways to divide C int count_ways = Math.Max(C - minAddA + 1, 0); return count_ways; } // Driver Code public static void Main(String[] args) { int A = 3, B = 5, C = 5; Console.Write(countWays(A, B, C)); } } // This code is contributed by shivanisinghss2110
Javascript
<script> // Javascript implementation of the above approach // Function to count the number of ways to divide // C into two parts and add to A and B such // that A is strictly greater than B function countWays(A, B, C) { // Minimum value added to A to satisfy // the given relation var minAddA = Math.max(0, parseInt((C + B - A + 2) / 2)); // Number of different values of A, i.e., // number of ways to divide C var count_ways = Math.max(C - minAddA + 1, 0); return count_ways; } // Driver code var A = 3, B = 5, C = 5; document.write( countWays(A, B, C)); // This code is contributed by rutvik_56. </script>
2
Tiempo Complejidad: O(1)
Espacio Auxiliar: O(1)
Publicación traducida automáticamente
Artículo escrito por rupesh_rao y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA