public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int T = sc.nextInt();

    long d3 = 3, d5 = 5, d15 = 15;
    long n3 = 0, n5 = 0, n15 = 0;
    if ((1 <= T) && (T <= 100000)) {
      for (int j = 0; j < T; j++) {
        long sum3 = 0, sum5 = 0, sum15 = 0, sum = 0;
        long l3, l5, l15 = 0;

        int N = sc.nextInt();
        if ((1 <= N) && (N <= 1000000000)) {
          N = N - 1;
          l3 = N % 3;
          l3 = N - l3;
          n3 = (l3 - 3) / 3 + 1;
          sum3 = (3 * ((n3) + 1) * (n3)) / 2;

          l5 = N % 5;
          l5 = N - l5;
          n5 = (l5 - 5) / 5 + 1;
          sum5 = (5 * ((n5) + 1) * (n5)) / 2;

          l15 = N % 15;
          l15 = N - l15;
          n15 = (l15 - 15) / 15 + 1;
          sum15 = (15 * ((n15) + 1) * (n15)) / 2;

          sum = sum3 + sum5 - sum15;
          System.out.println(sum);
        }
      }
    }
  }
  public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner sc = new Scanner(System.in);
    String[][] numArray = new String[6][];
    int largestSum = 0;
    for (int i = 0; i < 6; i++) {
      numArray[i] = sc.nextLine().split(" ");
    }

    for (int i = 0; i <= 3; i++) {
      for (int j = 0; j <= 3; j++) {
        int sum =
            Integer.parseInt(numArray[i][j])
                + Integer.parseInt(numArray[i][j + 1])
                + Integer.parseInt(numArray[i][j + 2])
                + Integer.parseInt(numArray[i + 1][j + 1])
                + Integer.parseInt(numArray[i + 2][j])
                + Integer.parseInt(numArray[i + 2][j + 1])
                + Integer.parseInt(numArray[i + 2][j + 2]);
        // System.out.println(sum);
        if (i == 0 && j == 0) {
          largestSum = sum;
        } else {
          if (sum > largestSum) {
            largestSum = sum;
          }
        }
      }
    }

    System.out.println(largestSum);
  }
 public void init() {
   Scanner scan = new Scanner(System.in);
   count = scan.nextInt();
   x0 = scan.nextLong();
   y0 = scan.nextLong();
   int result = 0;
   boolean special = false;
   for (int i = 0; i < count; i++) {
     long tempx = scan.nextLong();
     long tempy = scan.nextLong();
     if (tempx == x0 && tempy == y0) {
       special = true;
       continue;
     }
     boolean isDuplicate = false;
     for (int j = 0; j < result; j++) {
       long x1 = xList.get(j);
       long y1 = yList.get(j);
       if ((x1 - x0) * (tempy - y0) == (y1 - y0) * (tempx - x0)) {
         isDuplicate = true;
         break;
       }
     }
     if (!isDuplicate) {
       xList.add(tempx);
       yList.add(tempy);
       result++;
     }
   }
   if (special && result == 0) result = 1;
   System.out.println(result);
   scan.close();
 }
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int arr[][] = new int[6][6];
    int max = -10000; // lowest possible value is -9*6=-54
    int sum;
    for (int arr_i = 0; arr_i < 6; arr_i++) {
      for (int arr_j = 0; arr_j < 6; arr_j++) {
        arr[arr_i][arr_j] = in.nextInt();
      }
    }

    for (int i = 0; i < 4; i++) {
      for (int j = 0; j < 4; j++) {
        sum =
            arr[i][j]
                + arr[i][j + 1]
                + arr[i][j + 2]
                + arr[i + 1][j + 1]
                + arr[i + 2][j]
                + arr[i + 2][j + 1]
                + arr[i + 2][j + 2];
        max = Math.max(sum, max);
      }
    }
    System.out.println(max);
  }
Exemple #5
0
  public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner in = new Scanner(System.in);

    int N = in.nextInt();

    Node[] nodes = new Node[N];

    for (int i = 0; i < N; i++) {
      nodes[i] = new Node(i + 1);
    }

    for (int i = 0; i < N; i++) {
      int a = in.nextInt();
      int b = in.nextInt();

      Node n = nodes[i];

      if (a != -1) n.left = nodes[a - 1];
      if (b != -1) n.right = nodes[b - 1];
    }

    int T = in.nextInt();

    for (int i = 0; i < T; i++) {
      int K = in.nextInt();

      swap(nodes[0], K, 1);

      Inorder(nodes[0]);
      System.out.println("");
    }
  }
Exemple #6
0
  private InstanceList readFile() throws IOException {

    String NL = System.getProperty("line.separator");
    Scanner scanner = new Scanner(new FileInputStream(fileName), encoding);

    ArrayList<Pipe> pipeList = new ArrayList<Pipe>();
    pipeList.add(new CharSequence2TokenSequence(Pattern.compile("\\p{L}\\p{L}+")));
    pipeList.add(new TokenSequence2FeatureSequence());

    InstanceList testing = new InstanceList(new SerialPipes(pipeList));

    try {
      while (scanner.hasNextLine()) {

        String text = scanner.nextLine();
        text = text.replaceAll("\\x0d", "");

        Pattern patten = Pattern.compile("^(.*?),(.*?),(.*)$");
        Matcher matcher = patten.matcher(text);

        if (matcher.find()) {
          docIds.add(matcher.group(1));
          testing.addThruPipe(new Instance(matcher.group(3), null, "test instance", null));
        }
      }
    } finally {
      scanner.close();
    }

    return testing;
  }
Exemple #7
0
  public static void main(String[] args) throws PatternSyntaxException {
    Scanner in = new Scanner(System.in);
    System.out.println("Enter pattern: ");
    String patternString = in.nextLine();

    Pattern pattern = Pattern.compile(patternString);

    while (true) {
      System.out.println("Enter string to match: ");
      String input = in.nextLine();
      if (input == null || input.equals("")) return;
      Matcher matcher = pattern.matcher(input);
      if (matcher.matches()) {
        System.out.println("Match");
        int g = matcher.groupCount();
        if (g > 0) {
          for (int i = 0; i < input.length(); i++) {
            // Print any empty groups
            for (int j = 1; j <= g; j++)
              if (i == matcher.start(j) && i == matcher.end(j)) System.out.print("()");
            // Print ( for non-empty groups starting here
            for (int j = 1; j <= g; j++)
              if (i == matcher.start(j) && i != matcher.end(j)) System.out.print('(');
            System.out.print(input.charAt(i));
            // Print ) for non-empty groups ending here
            for (int j = 1; j <= g; j++)
              if (i + 1 != matcher.start(j) && i + 1 == matcher.end(j)) System.out.print(')');
          }
          System.out.println();
        }
      } else System.out.println("No match");
    }
  }
  public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */

    try {

      Scanner sc = new Scanner(new File("/home/santosh/Desktop/testData"));

      int testCases = sc.nextInt();
      int i = 0;
      ArrayList<ArrayList<Integer>> inputArraysList = new ArrayList<ArrayList<Integer>>();
      ArrayList<Integer> list;
      while (i++ < testCases) {

        int n = sc.nextInt();
        int k = 0;
        list = new ArrayList<Integer>();
        while (k < n) {
          list.add(sc.nextInt());
          k++;
        }
        inputArraysList.add(list);
      }

      // System.out.println(inputArraysList.size());
      for (ArrayList<Integer> arr : inputArraysList) {
        countPairs(arr.toArray(new Integer[arr.size()]));
      }

    } catch (Exception e) {

    }
  }
Exemple #9
0
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int arr[][] = new int[6][6];
    for (int i = 0; i < 6; i++) {
      for (int j = 0; j < 6; j++) {
        arr[i][j] = in.nextInt();
      }
    }

    List sum = new ArrayList();
    for (int i = 1; i < 5; i++) {
      for (int j = 1; j < 5; j++) {
        int temp =
            arr[i][j]
                + arr[i - 1][j - 1]
                + arr[i - 1][j]
                + arr[i - 1][j + 1]
                + arr[i + 1][j - 1]
                + arr[i + 1][j]
                + arr[i + 1][j + 1];
        sum.add(temp);
      }
    }

    System.out.println(Collections.max(sum));
  }
  public void init() {
    Scanner scan = new Scanner(System.in);
    n = scan.nextInt();
    m = scan.nextInt();
    for (int i = 0; i < n; i++) {
      String input = scan.next();
      record.add(input);
    }

    for (int i = 0; i < m; i++) {
      String input = scan.next();
      char[] charArray = input.toCharArray();
      boolean isValid = false;
      for (int j = 0; j < charArray.length; j++) {
        char c = charArray[j];
        for (char d = 'a'; d <= 'c'; d = (char) (d + 1)) {
          if (d == c) continue;
          charArray[j] = d;
          if (record.contains(new String(charArray))) {
            isValid = true;
            break;
          }
        }
        charArray[j] = c;
        if (isValid) break;
      }
      if (isValid) System.out.println("YES");
      else System.out.println("NO");
    }
    scan.close();
  }
 public static void main(String[] args) {
   Scanner inp = new Scanner(System.in);
   int n = inp.nextInt();
   String[] store = new String[n];
   for (int i = 0; i < n; i++) store[i] = inp.next();
   int[] cnt = new int[n];
   Arrays.fill(cnt, 0);
   String str = inp.next();
   for (int j = 0; j < n; j++) {
     int l1 = store[j].length();
     for (int k = 0; k <= (str.length() - l1); k++) {
       if (str.substring(k, k + l1).equals(store[j])) {
         cnt[j] = cnt[j] + 1;
       }
     }
   }
   int y = 0;
   for (int m = 0; m < n; m++) {
     y = Math.max(y, cnt[m]);
   }
   System.out.println(y);
   for (int h = 0; h < n; h++) {
     if (cnt[h] == y) System.out.println(store[h]);
   }
 }
 public static void main(String[] args) {
   /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   Scanner in = new Scanner(System.in);
   int cases = in.nextInt();
   for (int i = 0; i < cases; i++) {
     System.out.println(digits(in.nextInt()));
   }
 }
Exemple #13
0
  public static void main(String[] args) {
    /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
    Scanner scan = new Scanner(System.in);
    BigInteger bigInteger1 = scan.nextBigInteger();
    BigInteger bigInteger2 = scan.nextBigInteger();

    System.out.println(bigInteger1.add(bigInteger2));
    System.out.println(bigInteger1.multiply(bigInteger2));
  }
 public static void main(String[] args) {
   /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   Scanner in = new Scanner(System.in);
   int numberOfTestCases = in.nextInt();
   for (int i = 0; i < numberOfTestCases; i++) {
     int n = in.nextInt();
     System.out.println((n * (n - 1)) / 2);
   }
 }
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int t = in.nextInt();
   for (int a0 = 0; a0 < t; a0++) {
     int n = in.nextInt();
     int treeHeight = getHeightOfTree(n);
     System.out.println(treeHeight);
   }
 }
 public static void main(String[] args) {
   Scanner s = new Scanner(System.in);
   BigInteger a = new BigInteger(s.next());
   BigInteger b = new BigInteger(s.next());
   BigInteger ad = a.add(b);
   BigInteger mu = a.multiply(b);
   System.out.println(ad);
   System.out.println(mu);
 }
Exemple #17
0
 public static void main(String[] args) {
   Scanner in = new Scanner(System.in);
   int m;
   m = in.nextInt();
   String grid[] = new String[m];
   for (int i = 0; i < m; i++) {
     grid[i] = in.next();
   }
   displayPathtoPrincess(m, grid);
 }
Exemple #18
0
  public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    int n = sc.nextInt();
    String ans = "";
    if (n % 2 == 1) ans = "Weird";
    else if (n >= 2 && n <= 5) ans = "Not Weird";
    else if (n <= 20) ans = "Weird";
    else ans = "Not Weird";
    System.out.println(ans);
  }
 public static void main(String[] args) {
   /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   Scanner scan = new Scanner(System.in);
   long T = scan.nextLong();
   for (long i = 0; i < T; i++) {
     long N = scan.nextLong();
     long diff = 0;
     diff = wholeSum(N) - indiSum(N);
     System.out.println(diff);
   }
 }
 public static void main(String[] args) {
   /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   Scanner sc = new Scanner(System.in);
   int mapSize = Integer.parseInt(sc.nextLine());
   char[][] map = new char[mapSize][];
   for (int i = 0; i < mapSize; i++) {
     String numbersString = sc.nextLine();
     map[i] = numbersString.toCharArray();
   }
   printMapWithCavities(map, mapSize);
 }
 public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   int k = sc.nextInt();
   while (k > 0) {
     int a = sc.nextInt();
     int b = sc.nextInt();
     int n = sc.nextInt();
     printer(a, b, n);
     k--;
   }
 }
 public static void main(String[] args) {
   Scanner sc = new Scanner(System.in);
   int a = sc.nextInt();
   if (a == 2) {
     System.out.println("Bob wins, by winning 2 game(s) and tying 1 game(s)");
     System.out.println("Alice and Bob tie, each winning 0 game(s) and tying 1 game(s)");
   } else {
     System.out.println("Alice wins, by winning 2 game(s) and tying 1 game(s)");
     System.out.println("Alice and Bob tie, each winning 0 game(s) and tying 1 game(s)");
   }
 }
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int N = in.nextInt();
    int arr[] = new int[N];
    for (int i = 0; i < N; i++) {
      arr[i] = in.nextInt();
    }

    for (int i = N - 1; i >= 0; i--) {
      System.out.print(arr[i] + " ");
    }
  }
  public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int T = Integer.parseInt(in.nextLine());

    for (int a = 0; a < T; a++) {

      String PAN = in.nextLine();
      System.out.println(isValidPAN(PAN));
    }

    in.close();
  }
Exemple #25
0
  public static void main(String[] args) {
    BigInteger sum = BigInteger.ZERO;
    Scanner in = new Scanner(System.in);
    int n = in.nextInt();
    int arr[] = new int[n];
    for (int arr_i = 0; arr_i < n; arr_i++) {
      arr[arr_i] = in.nextInt();

      sum = sum.add(BigInteger.valueOf(arr[arr_i]));
    }
    System.out.println(sum);
  }
Exemple #26
0
 public static void main(String[] args) {
   while (scan.hasNextDouble()) {
     double t1 = scan.nextDouble();
     double t2 = scan.nextDouble();
     int i1, i2;
     for (i1 = 0; i1 < s1.length; i1++) {
       if (t1 < s1[i1]) break;
     }
     for (i2 = 0; i2 < s2.length; i2++) {
       if (t2 < s2[i2]) break;
     }
     System.out.println(r[Math.max(i1, i2)]);
   }
 }
Exemple #27
0
 public static void main(String[] args) {
   /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
   Scanner in = new Scanner(System.in);
   String input = "";
   int n = 1;
   String output = "";
   while (in.hasNextLine()) {
     if (in.hasNext()) {
       output += n + " " + in.nextLine() + "\n";
       n++;
     }
   }
   System.out.println(output);
 }
  public static void main(String[] args) {
    long start = System.currentTimeMillis();
    Scanner input = new Scanner(System.in);
    int numberOfTestCases = input.nextInt();
    ArrayList<Integer> order = new ArrayList<Integer>(numberOfTestCases);
    int previousKey = -1;
    int previousValue = 0;
    int cycleNumber = 0;

    Map<Integer, Integer> testCases = new TreeMap<Integer, Integer>();

    for (int i = 0; i < numberOfTestCases; i++) {
      int numberOfCycles = input.nextInt();
      testCases.put(numberOfCycles, 1);
      order.add(numberOfCycles);
    }

    for (Map.Entry<Integer, Integer> entry : testCases.entrySet()) {
      int numberOfCycles;
      int initialHeight;

      if (previousKey == -1) {
        numberOfCycles = entry.getKey();
        initialHeight = entry.getValue();
      } else {
        numberOfCycles = entry.getKey() - previousKey;
        initialHeight = previousValue;
      }

      for (int i = 0; i < numberOfCycles; i++) {
        if (cycleNumber % 2 == 0) {
          initialHeight *= 2;
        } else {
          initialHeight += 1;
        }
        cycleNumber++;
      }

      entry.setValue(initialHeight);
      previousKey = entry.getKey();
      previousValue = initialHeight;
    }

    for (Integer element : order) {
      System.out.println(testCases.get(element));
    }

    long elapsed = System.currentTimeMillis() - start;
    System.out.println("time: " + elapsed);
  }
Exemple #29
0
  public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int N = sc.nextInt();
    int[] a = new int[N];
    for (int i = 0; i < N; i++) {
      a[i] = sc.nextInt();
    }

    Difference D = new Difference(a);

    D.computeDifference();

    System.out.print(D.maximumDifference);
  }
  public static void main(String[] args) {
    try {
      BufferedReader br = new BufferedReader(new FileReader(args[0]));
      int tIndex = 0;
      int pIndex = 0;

      // This will probably change soon (name and implementation)
      NgramParser tnp = new NgramParser(args[1]);

      ArrayList<String> triplet = tnp.getTriplet();
      ArrayList<String> polarity = tnp.getPolarity();

      FileWriter sw = new FileWriter(args[2]);
      String line = null;

      while (((line = br.readLine()) != null)
          && (tIndex < triplet.size())
          && (pIndex < polarity.size())) {
        if (line.matches("^[\\d]*:")) {
          // System.out.println(line);
          sw.write(line + "\n");
        } else {
          Scanner sc = new Scanner(line);
          String trip = sc.findInLine(Pattern.compile("[a-zA-Z]+#[a-z]+[#]?[0-9]*"));
          // if (trip != null && trip.equals(triplet.get(tIndex))) {
          System.out.println(trip);
          if (trip != null && !trip.toLowerCase().contains("no#cl")) {
            // System.out.println(triplet.get(tIndex) + ":" +polarity.get(pIndex));
            String pol = polarity.get(pIndex);
            sw.write(line + " " + pol + "\n");
            sc.close();
            tIndex++;
            pIndex++;
          } else {
            String pol = "neg";
            sw.write("no#a#1" + " " + pol + "\n");
            sc.close();
          }
        }

        // sw.flush();
      }

      sw.close();

    } catch (IOException e) {
      e.printStackTrace();
    }
  }