The problem is quite straightward i.e to keep taking the n integer inputs and counting those which are divisible by 'k'.
So, the first solution that comes in everyone's mind is as given below (for those who are from C++ background).
#include<iostream>
using namespace std;
int main()
{
int n, k, t; cin>>n>>k;
int count=0; for(int i=0; i<n; i++)
{ cin>>t;
if (t%k==0) { count++;}
} cout<<count<<endl;
return 0;}And if you submit this solution to the CodeChef, it times out i.e the execution time of this program exceeds the given time
restriction of 8sec. The reason for this is that the basic I/O functions in C++ also called the stream objects become very
slow with the increase in input size. A stream is basically an object from which a program can extract data and also can insert
data. The default mechanism for a program to take input is through the keyboard and C++ defines the 'cin' stream to access
the data or the characters you type in the keyboard. We can understand this in the way that it is the responsibility of the 'cin' stream
object to handle the keyboard input and pass it to the program. Similarly, the monitor is the default output for a program and the
program accesses it using the 'cout' stream object.
In any contest problem like as those on CodeChef, SPOJ etc, you will be asked to read from the standard input and write to standard
output which turns out to be 'cout' and 'cin' in the case of C++. Coming back to the INTEST problem, if you see the size : n,k<10^7 and t<10^9.
The stream operations tend to be slow with such large size inputs and the solution times out.
Coming to the rescue are the C standard I/O operations which are very fast. It must be remembered that C++ is an extension
of C and you can do whatever you could in C in C++. You just have to include the appropriate library headers.
We can use the C standard I/O operations in C++ by including the cstdio library. The standard input function in C is scanf(). scanf reads
formatted data from the standard input. Formatted data implies that you specify the structure of the input. For example, if we want to read
two integers separated by space in one go, its format would be "%d %d" where %d stands for an integer. If we wish to read a string and an
integer separated by a space, the format would be "%s %d".
The basic syntax of using scanf() is as below:
int number; scanf("%d", &number);
Observe that we pass the reference to the number variable as the second parameter. The integer taken as input from the standard input
will be stored in the 'number' variable. You can read more about scanf here
http://www.cplusplus.com/reference/clibrary/cstdio/scanf/
So, our modified program becomes;
#include<iostream>#include<cstdio> //library containing the standard C I/O functions
using namespace std;
int main()
{
int n, k, t;
scanf("%d %d",&n,&k);//take the input and store them in n and k
int count=0;//initialize the counter variable
while(n--)
{
scanf("%d",&t)
if (t%k==0)//check if t is divisible by k
{ ++count;}
}
cout<<count<<endl;
return 0;
}
This program runs in 5.4 seconds.
So, you should keep in mind that whenever you encounter a problem which requires heavy I/O or large number
of I/O operations, you must use scanf and printf available in cstdio.
Please report if anywhere I have gone wrong or if the above solution can be optimised more.
Praveen