Enormous input test-practice problem(easy)

In the following code(using c++4.8.1), the time limit is being exceeded. How can I improve this code? Also why can’t I use the statement “endl;” after “cin>>num” in the for loop. Please answer my query ASAP.
#include
using namespace std;

int main()
{
long int n,k,num;
int i,count=0;
cin>>n>>k;
for(i=0;i<n;++i)
{
cin>>num;
if(num%k==0)
count++;
}
cout<<count;
return(0);
}

use scanf(printf) only…they are much faster than cin(cout).

3 Likes

The second most common cause of TLE is that your method of reading input and writing output is too slow. In Java, do not use a Scanner; use a BufferedReader instead. In C++, do not use cin/cout - use scanf and printf instead.

Try reading this from time to time: http://www.codechef.com/wiki/faq.

1 Like

Thanx mate, your suggestion worked!
Also, what about the “endl” part? It showed some function overloading error when I used “endl” after “cin>>num” in the for loop.

Thank you!

@recrnsettler use \n in scanf … it’s works as like endl…

for ex.- cin>>num>>endl;
scanf("%lld\n",&num);