### oqfiqh's blog

By oqfiqh, history, 11 days ago,

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?

• 0

 » 11 days ago, # |   0 I think you will get your answer here:https://www.geeksforgeeks.org/cincout-vs-scanfprintf/https://www.geeksforgeeks.org/fast-io-for-competitive-programming/
•  » » 10 days ago, # ^ |   0 Thanks for the resources. What I find odd is that my program with scanf and printf was slower ( and by a substantial amount) than the one with cout and cin, when It should have been the opposite.
•  » » » 10 days ago, # ^ |   0 Sorry, I misunderstood your question. Can you share the source codes? Because I never faced anything like that before.
•  » » » » 10 days ago, # ^ |   0 This is the one with printf and scanf, and this the one with cout and cin. The only thing that changes between them is literally only the functions of input and output. Nevertheless, the first one exceeds test 3 with a mammoth 2 seconds. The second one only needs 140ms.
•  » » » » » 10 days ago, # ^ |   0 I submitted your previous code in C++17 and it got accepted. Link here
•  » » » » » » 10 days ago, # ^ |   0 Seems to be something buggy with the site. Will submit with C++17 from now on. Thanks for the help.
•  » » » 10 days ago, # ^ |   0 Also, can you mention the language you selected while submitting. I saw in some places, people are getting tle while submitting in GNU C11.
•  » » » » 10 days ago, # ^ | ← Rev. 3 →   0 I submitted with GNU C11 because I was using C, then since it didnt work I "translated" to C++ and submitted with C++ 17 . But program are identical, it literally changes the library included and the i/o functions.Edit: As confirmed by others, the problem was indeed the language selected for submitting.
 » 10 days ago, # |   +3 It's not actually anything to do with scanf / printf (if you submit your scanf / printf code as c++ it works fine — https://codeforces.com/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.com/blog/entry/105611).
•  » » 10 days ago, # ^ |   0 Ahh thanks, this thing bugged me so much, i couldnt understand what was going on.
 » 10 days ago, # |   +22 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).