ProgrammingVery hardCompetitive coding (CP)

What is printed?

What is printed?
#include <stdio.h>
int main(void) {
    unsigned long long r = 1, b = 7;
    int e = 222;
    while (e) {
        if (e & 1) r = r * b % 1000;
        b = b * b % 1000;
        e >>= 1;
    }
    printf("%llu", r);
}
  1. A. 343
  2. B. 7
  3. C. 1
  4. D. 49

Correct answer: D. 49

Binary exponentiation computes 7^222 mod 1000, and the result is 49. Every product is reduced before it is stored, so no intermediate value overflows.

More C Programming questions

Exams that ask this topic