Editorial for LCC '24 Contest 2 S4 - Cookies
Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
Subtask 1
Since , we can just simulate the cookies.
Time Complexity:
Subtask 2
Simulating for panels will TLE. Instead, we can work backwards and use a recursive approach:
Let be the number of cookies on panel . Notice that the only previous panels that contribute to the total sum are , , and . Therefore, , with for noninteger . It turns out that is fast enough to AC!
#include <stdio.h>
int f(long x) {
if (x < 2) {
return x;
} else {
return f(x / 2) + (x % 3 == 0 ? f(x / 3) : 0);
}
}
int main() {
long n;
scanf("%ld", &n);
printf("%d\n", f(n));
}
(note how is collapsible into .)
Time Complexity: , probably
Comments