Add the code flag to tell the forum you’re trying to write code:
[code] at the begining and [/code] at the end.
Here’s a formated version for better readbility:
#include <iostream>
using namespace std;
void main()
{
int t,a,m,n;
cin >> t;
for (int k = 0; k < t; k++)
{
cin >> m >> n;
if (m == 0 || m == 1 || m == 2)
cout << 2 << endl;
for (int i = m; i <= n; i++)
{
for (int l = 2; l < i; l++)
{
if (i % l == 0)
{
a = 0;
break;
}
else
a = 1;
}
if (a == 1)
cout << i << endl;
}
cout << endl;
}
}
If you’re gonna use cin and cout, use
ios_base::sync_with_stdio(false);
It disables the syncing of different stream methods, which slows down the stream processing and isn’t necessary in this kind of program.
Then i suggest you also replace your ‘endl’ by a simple newline: “\n”: endl forces a flush of the stream each time it is called, which is also unnecessary in this program.
Just using this should improve the speed, and might get you under the time limit.
If it doesn’t, then you’ll have to rethink your algorithm a bit, maybe try to improve the boundaries of your loops, figure out better ways to do certain things…
good luck =)