I solved problem B (permutation chain) of the last contest, and the same exact program using printf and scanf (stdio.h) went over the time limit by a fair chunk (exceeded the time limit by 2s for a single test),instead when using cout and cin (iostream) it passed all tests in 100ms.
Does anyone have any idea why this is happening?
It's not actually anything to do with scanf / printf (if you submit your scanf / printf code as c++ it works fine — https://codeforces.net/contest/1716/submission/167324397).
I've no idea why c is running so much slower, but someone else was reporting the same thing (https://codeforces.net/blog/entry/105611).
The reason is similar to the one mentioned in this blog.
In the blog it is mentioned that the patch was applied for the MinGW implementations for C++, but I am not sure if it was applied for C or not. Or perhaps they were thorough in applying their patches and Codeforces didn't update the version of MinGW they use for C.
Anyway, there is a simple way to fix this: add this line to the top of your submission, and it will revert to the faster implementation (at least as of now on Codeforces):
#define __USE_MINGW_ANSI_STDIO 0
. For example, 167339388 which has your code with this one extra line at the top gets AC.A word of caution though: don't rely on this behaviour on other systems, since it is an internal implementation macro and the behaviour is not guaranteed to be the same everywhere (and there were some discussions on deprecating this, not sure if it was deprecated or not).