c++ – can anyone help me in this time exceeds error

Everything in this problem is known at compile time. So lets let the compiler solve this:

#include <stdio.h>
#include <cstddef>

consteval std::size_t solve() {
    int a=0;
    std::size_t count = 0;
    do {
        a = (a*10 +7) % 2003;
        ++count;
    } while (a != 0);
    return count;
}

int main() {
    printf("%zd\n", solve());
}

resulting in:

.LC0:
        .string "%zd\n"
main:
        sub     rsp, 8
        mov     esi, 1001
        mov     edi, OFFSET FLAT:.LC0
        xor     eax, eax
        call    printf
        xor     eax, eax
        add     rsp, 8
        ret

As may see this only calls printf with 1001 as argument.

Read more here: Source link