Solving Blue Bridge Cup Programming Challenges with Java

Analysis of Pascal's Triangle Sequence Position

For a given integer N, the goal is to locate its first occurrence in the flattened one‑dimensional sequence of Pascal's triangle values, read row by row from left to right.
The approach uses the fact that early rows contain candidates. The computational limit is set to row index 44725, which corresponds to the highest row needed for the input range.
A dynamic array tracks incremental updates to the row values. For each row, elements are computed from right to left, avoiding an extra array copy. The running sum keeps count of total numbers processed up to the previous row.
If N equals 1, the position is directly 1. Otherwise, during each row update, the program checks whether the new value matches N. When found, the exact position is the prior count plus the column offset.
If no match occurs within the scanned range, the only possible match is N itself appearing as the second element of row N+1, so the result follows a triangular number formula.

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws Exception {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        long target = Long.parseLong(reader.readLine());
        final int ROWS = 44725;
        long[] rowVals = new long[ROWS];
        rowVals[0] = 1L;
        long posCounter = 1L;

        if (target == 1) {
            System.out.println(1);
            return;
        }

        for (int r = 1; r < ROWS; r++) {
            int half = r / 2;
            if (half < 1) half = 1;
            for (int c = r; c >= half; c--) {
                rowVals[c] = rowVals[c] + rowVals[c - 1];
                if (rowVals[c] == target) {
                    long result = posCounter + (r - c) + 1;
                    System.out.println(result);
                    return;
                }
            }
            posCounter += (r + 1);
        }

        long answer = ((1 + target) * target / 2) + 2;
        System.out.println(answer);
    }
}

Calculating Trailing Zeros in a Large Product

To determine how many trailing zeros exist in the product of 100 integers, factor decomposition offers an efficient path. A trailing zero is generated by a factor pair (2,5), so the limiting count is the minimum between total factor twos and total factor fives across all multiplied numbers. Each integer is processed by extracting all 2 and 5 divisors.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int cnt2 = 0, cnt5 = 0;
        
        while (scanner.hasNextInt()) {
            int val = scanner.nextInt();
            while (val % 2 == 0) {
                cnt2++;
                val /= 2;
            }
            while (val % 5 == 0) {
                cnt5++;
                val /= 5;
            }
        }
        scanner.close();
        
        int trailingZeros = cnt2 < cnt5 ? cnt2 : cnt5;
        System.out.println(trailingZeros);
    }
}

Note: The raw data can be pasted into standard input when running the code. Alternatively, the numbers can be hardcoded as an array literal for direct processing without input redirection.

Tags: java blue bridge cup Competitive Programming Pascal Triangle Trailing Zeros

Posted on Sat, 23 May 2026 23:11:42 +0000 by direction