fork download
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. public class Test
  6. {
  7. static int[] minValues;
  8. static int[] maxValues;
  9. static int length;
  10. static int sum;
  11.  
  12. static List<int> current = new List<int>();
  13.  
  14. private static void Do(int index, int currentSum)
  15. {
  16. if (currentSum > sum) return;
  17.  
  18. if (index == length)
  19. {
  20. if (currentSum == sum)
  21. {
  22. Output(current);
  23. }
  24. return;
  25. }
  26.  
  27. for (int i = minValues[index]; i <= maxValues[index]; i++)
  28. {
  29. current.Add(i);
  30. Do(index + 1, currentSum + i);
  31. current.RemoveAt(current.Count() - 1);
  32. }
  33. }
  34.  
  35. private static void Output<T>(IEnumerable<T> list)
  36. {
  37. foreach (var item in list)
  38. Console.Write("{0} ", item);
  39. Console.WriteLine();
  40. }
  41.  
  42. public static void Main()
  43. {
  44. var values = Console.ReadLine().Split(' ');
  45. length = int.Parse(values[0]);
  46. sum = int.Parse(values[1]);
  47.  
  48. minValues = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  49. maxValues = Console.ReadLine().Split(' ').Select(int.Parse).ToArray();
  50.  
  51. Do(0, 0);
  52. }
  53. }
Success #stdin #stdout 0.06s 24136KB
stdin
3 10
1 2 3
4 4 6
stdout
1 3 6 
1 4 5 
2 2 6 
2 3 5 
2 4 4 
3 2 5 
3 3 4 
3 4 3 
4 2 4 
4 3 3