# 14010 Jangan Makan Indomieku ## Brief You are tasked to output geometric series and arithmatic series on alternating lines For detailed explanation, visit [this link](https://acm.cs.nthu.edu.tw/problem/14010/). ## Idea Even if you have not a slightest idea of how to start, who was the one who ate TA Arithat's Indomie and caused you to be scratching your head with such a ridiculous question, or who the heck is TA Arithat and why is he giving you such a random ques-... uh... punishment, you should have this picture in your mind: ![](https://hackmd.io/_uploads/ry3NmNtZp.png) All every other steps are quite straightforward, but the problem is how to print the numbers in each line. Let us first ignore the spacing. What should we do if we want to print the numbers out? That's right: using another loop. You can start at 1, print out the number, and multiply or add by k, depending on the round you're on, then count down until you reach 1 again. The simplest way to do this is: ``` c= int knocks = 1; for(int i = 1; i < round; i++) { printf("%d ", knocks); if(round%2 == 0) { knocks += k; } else { knocks *= k; } } printf("%d ", knocks); for(int i = 1; i < round; i++) { if(round%2 == 0) { knocks -= k; } else { knocks /= k; } printf("%d ", knocks); } printf("\n"); ``` But there are two problem with this approach. First, this breaks when the number is too large. Look at the output when the input is 11 10: <font face="consolas"> 1 1 11 1 1 10 100 10 1 1 11 21 31 21 11 1 1 10 100 1000 10000 1000 100 10 1 1 11 21 31 41 51 41 31 21 11 1 1 10 100 1000 10000 100000 1000000 100000 10000 1000 100 10 1 1 11 21 31 41 51 61 71 61 51 41 31 21 11 1 1 10 100 1000 10000 100000 1000000 10000000 100000000 10000000 1000000 100000 10000 1000 100 10 1 1 11 21 31 41 51 61 71 81 91 81 71 61 51 41 31 21 11 1 1 10 100 1000 10000 100000 1000000 10000000 100000000 1000000000 <font color="#f00">1410065408 141006540 14100654 1410065 141006 14100 1410 141 14 1 0</font> </font> What you're seeing here is a result from having numbers out of range. Integers takes up 2 to 4 bytes of space (16-32 bits), so it could handle numbers up to 2^31 - 1. To solve this, you must use a number that can handle this. Since the problem explicitly states the number will be less than 2^63 - 1, `unsigned long long` works pretty decently. We can change `knocks` to type `unsigned long long` and `%d` to `%llu`, and now our code should be working fine. The second problem with the code is the spacing is wrong. Let us first discuss how to solve the problem in general, i.e. when the problem requires you to print out some numbers, separated by space but no space at the end. Suppose it look like this: ![](https://hackmd.io/_uploads/HysD_4Y-a.png) The first thing you might have in your mind is probably to split the output for each printf() to print like this: ![](https://hackmd.io/_uploads/SkjSYVYW6.png) and your code might look like: ```c= for(int i = 0; i < n; i++) { do_something(); if(is_the_last_number) printf("%d", some_number); //No space behind the last number else printf("%d ", some_number); } ``` This would work if you already know when the sequence will end, but what if you don't? Instead of thinking about when to append a space after a number, try thinking : ![](https://hackmd.io/_uploads/r1SwsVYba.png) and your code might look like: ```c= for(int i = 0; i < n; i++) { do_something(); if(is_the_first_number) { printf("%d", some_number); //No space before the first number is_the_first_number = 0; } else printf(" %d", some_number); } ``` The strong point of implementing it in this way is that it is scalable and can be extended to wider range of problem. It might look intimidating, but we can use the tertiary operator to make it simpler: ```c= for(int i = 0; i < n; i++) { do_something(); printf("%s%d", is_the_first_number? "": " ", some_number); is_the_first_number = 0; } ``` However, for this problem, we can simply use different spacing methods for ascending, peak, and descending part: ``` c= unsigned long long knocks = 1; //ASCENDING for(int i = 1; i < round; i++) { printf("%llu ", knocks); //Space at the end if(round%2 == 0) { knocks += k; } else { knocks *= k; } } //PEAK printf("%llu", knocks); //No space //DESCENDING for(int i = 1; i < round; i++) { if(round%2 == 0) { knocks -= k; } else { knocks /= k; } printf(" %llu", knocks); //Space before it. } printf("\n"); ``` With these ideas, you should be able to code the rest on your own now. ## Solution <details> <summary>Only click here when you're done implementing the algorithm or you have given up.</summary> I really hope it was not the latter. Anyway, here you go: ```c= #include <stdio.h> int main() { int n, k; scanf("%d%d", &n, &k); for(int round = 1; round <= n; round++) { unsigned long long knocks = 1; for(int i = 1; i <= round; i++) { printf("%llu ", knocks); if(round % 2 == 0) { knocks += k; } else { knocks *= k; } } printf("%llu", knocks); for(int i = 1; i <= round; i++) { if(round % 2 == 0) { knocks -= k; } else { knocks /= k; } printf(" %llu", knocks); } printf("\n"); } return 0; } ``` </details>