Determine the Day of the Week After 20^22 Days (Lanqiao Cup 2022 Provincial Problem)

Problem Description Given that today is Saturday, determine which day of the week it will be after 20^22 days. Note that days are represented as 1 (Monday) through 7 (Sunday).

Approach This problem can be efficiently solved using modular arithmetic to avoid computing the extremely large value of 20^22 directly. Since the week cycles every 7 days, we only need to find the remainder when 20^22 is divided by 7. We then adjust this remainder based on the starting day (Saturday) to find the target day.

  1. Use Java's BigInteger class to compute 20 raised to the 22nd power, as this value exceeds the range of primitive integer types.
  2. Calculate the remainder of this result when divided by 7 using the mod operation.
  3. Adjust the remainder by adding the offset from Saturday (represented as 6 in the 1-7 numbering system) and wrap around if necesary to fit the required 1-7 day format.

Code Implementation

import java.math.BigInteger;

public class DayOfWeekCalculator {
    public static void main(String[] args) {
        BigInteger base = new BigInteger("20");
        BigInteger powerResult = base.pow(22);
        BigInteger divisor = new BigInteger("7");
        BigInteger remainder = powerResult.mod(divisor);
        
        int remValue = remainder.intValue();
        int targetDay = (remValue + 6) <=7 ? (remValue +6) : (remValue +6 -7);
        System.out.println(targetDay);
    }
}

Common BigInteger Methods

  1. add(BigInteger val): Returns a BigInteger representing the sum of this instance and the specified value.
  2. subtract(BigInteger val): Returns a BigInteger representing the difference between this instance and the specified value.
  3. multiply(BigInteger val): Returns a BigInteger representing the product of this instance and the specified value.
  4. divide(BigInteger val): Returns a BigInteger representing the quotient of this instance divided by the specified value.
  5. mod(BigInteger val): Returns a BigInteger representing the remainder when this instance is divided by the specified value.
  6. pow(int exponent): Returns a BigInteger representing this instance raised to the power of the specified exponent.
  7. gcd(BigInteger val): Returns the greatest common divisor of this instance and the specified value as a BigInteger.
  8. abs(): Returns a BigInteger representing the absolute value of this instance.
  9. negate(): Returns a BigInteger representing the negative of this instance.
  10. intValue(): Converts this BigInteger to an int value.
  11. longValue(): Converts this BigInteger to a long value.
  12. floatValue(): Converts this BigInteger to a float value.
  13. doubleValue(): Converts this BigInteger to a double value.

Common BigDecimal Methods

  1. add(BigDecimal augend): Returns a BigDecimal representing the sum of this instance and the specified augend.
  2. subtract(BigDecimal subtrahend): Returns a BigDecimal representing the difference between this instance and the specified subtrahend.
  3. multiply(BigDecimal multiplicand): Returns a BigDecimal representing the product of this instance and the specified multiplicand.
  4. divide(BigDecimal divisor): Returns a BigDecimal representing the quotient of this instance divided by the specified divisor. Throws an ArithmeticException if the division results in a non-terminating decimal.
  5. setScale(int newScale, RoundingMode roundingMode): Returns a BigDecimal with the specified number of decimal places, rounded according to the given rounding mode.
import java.math.BigDecimal;
import java.math.RoundingMode;

public class BigDecimalExample {
    public static void main(String[] args) {
        BigDecimal exampleDecimal = new BigDecimal("520.13141617");
        // Truncate to 7 decimal places
        System.out.println(exampleDecimal.setScale(7, RoundingMode.DOWN).doubleValue());
    }
}

Output: 520.1314161

  1. compareTo(BigDecimal val): Compares this BigDecimal numerically with the specified value, returning a negative integer, zero, or positive integer as this instance is less than, equal to, or greater than the specified value.
  2. equals(Object x): Returns true if and only if the specified Object is a BigDecimal with the same numeric value and scale as this instance.
  3. intValue(): Converts this BigDecimal to an int value.
  4. longValue(): Converts this BigDecimal to a long value.
  5. doubleValue(): Converts this BigDecimal to a double value.
  6. floatValue(): Converts this BigDecimal to a float value.

Tags: Lanqiao Cup Modular Arithmetic Java BigInteger Java Programming Competitive Programming

Posted on Sun, 31 May 2026 17:12:21 +0000 by dkphp2