Source code for submission s802

Fq.java

  1. import java.util.*;
  2. import java.io.*;
  3. import java.math.*;
  4.  
  5. class Fq
  6. {
  7.  
  8. public static char[] input;
  9. public static BigDecimal valid;
  10.  
  11. public static void main(String[] args) throws IOException {
  12.  
  13.  
  14. while(br.ready())
  15. {
  16. //System.out.println("////////////////////////");
  17. String line = br.readLine();
  18. input = line.toCharArray();
  19. int coins = input.length / 2;
  20. valid = new BigDecimal(0);
  21. f(0, 0, coins, coins);
  22. String v = valid.toString();
  23. String out = v;
  24.  
  25. if (v.length() > 6)
  26. {
  27. out = v.substring(v.length() - 6);
  28. }
  29. System.out.println(Integer.parseInt(out));
  30. }
  31. }
  32.  
  33. public static void f(int buff, int pos, int n5, int n10)
  34. {
  35. //System.out.println(buff + " " + pos + " " + n5 + " " + n10);
  36. if (buff < 0 || n5 < 0 || n10 < 0)
  37. {
  38. //System.out.println("fucked");
  39. return;
  40. }
  41. if (n5 == 0 && n10 == 0)
  42. {
  43. //System.out.println("valid");
  44. valid = valid.add(new BigDecimal(1));
  45. return;
  46. }
  47.  
  48. //System.out.println("pos" + pos + " " + input[pos]);
  49.  
  50. if (input[pos] == ')'){ // 10
  51. f(buff-1, pos+1, n5, n10 - 1);
  52. return;
  53. }
  54. if (input[pos] == '('){ // 5
  55. f(buff+1, pos+1, n5 - 1, n10);
  56. return;
  57. }
  58. f(buff-1, pos+1, n5, n10 - 1); // 10
  59. f(buff+1, pos+1, n5 - 1, n10); // 5
  60. }
  61.  
  62. }