21 / 25
Jan 2015

1) jeżeli dane są całkowite i wynik ma by całkowity, to obliczenia wykonujemy na liczbach całkowitych (powtarzam to już po raz tysięczny chyba smile)

2) wzór jest zły (niedużo[color=#FF4000], trzeba trochę poprawic, gdy liczymy na liczbach całkowitych[/color])

3) czy oglądałeś wyście programu dla danych testowych - z przekierowanym wejściem z pliku, lub np. na http://ideone.com8 ?

2 years later

Please stop using archaic code for the wrong reasons.

// never use endl instead of '\n' since endl forces a useless but expensive flush
#define endl '\n'

int main() {
    ios_base::sync_with_stdio(false);
    cin.tie(nullptr); // NULL if you're old fashioned.
}

Voila! cin/cout is faster than scanf/printf. But if you really need speed I/O, cin.read() and parse it yourself.

7 months later

Hi; I am trying to use std::cin.readsome() for very fast input. Unfortunately the solution is working on my terminal but not on some judging servers. The same solutions transated to fread() from cstdio worked perfectly…

namespace FI {
 const int L = 1 << 15 | 1;
 char buf[L], *front, *back;
 void nextChar(char &);
 template <typename T>void nextNumber(T &);
}

void FI::nextChar(char &c) {
 if(front == back) std::cin.readsome(buf, L), back = (front=buf) + std::cin.gcount();
 c = !std::cin.gcount() ? (char)EOF : *front++;
}

template<typename T>void FI::nextNumber(T &x) {
 char c; int f = 1;
 for(nextChar(c); c>'9'||c<'0'; nextChar(c)) if(c == '-') f = -1;
 for(x=0; c>='0'&& c<='9'; nextChar(c)) x = x*10+c-'0';
 x *= f;
}
2 months later

It is possible to make cin/cout almost as faster as scanf/printf by using this
ios_base::sync_with_stdio(false);cin.tie(NULL);
Using ‘\n’ instead of endl also makes output faster but beware of the differences between ‘\n’ and endl;these are not exactly similar,they do have differences.

The stream’s buffering will improve performance by reducing the number of OS write calls. You could increase the stream’s buffer size to improve performance.