#include <iostream>
usingnamespace std;
int main()
{
unsignedint n, k, in, out = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> in;
out += (in % k == 0);
}
cout << out;
return 0;
}
I submitted the solution but it exceeded the time limit.
Here's my questions:
• If the input was "7 3" , would n equal 7 and k equal 3?
• Is there anyway that make the solution run faster, anything that can replace(can be used instead of) the for loop to make the solution run faster?
(I think the input assigned to n had to be a really big number so the for loop repeated so many times that the solution ran so slow)
P/s: Sorry for my English if it's hard to understand.
C++ streams are way slower than C ones. Do not use them when speed is critical and errors are unlikely. You can use scanf to read values in a loop. (you can leave other unchanged if you want, they do not use much time)
Sadly its effects implementation defined and as article states does not make much difference for VS.
no, they are not
Strange, I always thought that all those sentries constructing, checking states and more layers of indirection should impact perfomance. However graphs in your article shows that it is even faster than C output.
Do you have links to articles discussing this and maybe other tricks to streams?
#include <iostream>
usingnamespace std;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
unsignedint n, k, in, out = 0;
cin >> n >> k;
for (int i = 0; i < n; i++) {
cin >> in;
out += (in % k == 0);
}
cout << out;
return 0;
}