My AC approach here -
I was just trying luck along with double sieve. Hence I used a variable named lucky. xD. In case I get AC this is reason behind it.
Approach -
Sieve to find S[i]
Maintain Counter Array.
Used Map. Although Set is more appropriate then Map
Brute force for 1<=a[i]<=lucky. With lucky<=100 Atmost 100 iterations.
For a[i]>lucky. One loop for 1<=i<=lucky and rest sieve. Atmost 200 iterations.
But still in TL .
Unfortunately it failed on last test case. And I made 15 submissions with different values of lucky with hope of getting AC.
Never use % until and unless it is absolutely necessary.
I repeat Never use % until and unless it is absolutely necessary.
It can take you from 100 pts to 20 pts And you will never realise reason behind it.
He added condition if(cnt[j]) then only computes[j]\%s[i]
Editorials soln can be optimised further by using set/map for outer loop. By in that case insertion complexity will be O(logn) And overall complexity will be again O(NlogN) .
I tried the way as mentioned in the editorial, but am getting TLE on the 1st, 2nd and last Test Cases. I find my and ediorialist’s codes almost similar but couldn’t figure out why I am getting TLE. Is the O(MAX^2) complexity in every precalculation is causing TLE.
Secondly, I tried to sieve only up to the maximum element in the input array. It decreased the time of execution but all tasks gave WA.(Some still gave TLE)
Time Complexity:- O(XlogX+N) per test case, where X=1000000
On Computing time, it is similar to 2*10^7
and there can be 100 test cases at maximum, when i am assuming N=10^4
then total time will be over >10^9 still no TLE according to editorial… why? please SomeOne Explain where I am wrong
Need Help !!! Since I am a beginner I don’t understand How Counting is performed to clear subtask 2. I try to do the dry run in my rough copy but was not able to solve due to the huge size of the array. Can u plz tell me where I am lacking and which concept should I study to understand this. You can also share some link.
I tried implementing the solution in the editorial in Java. Its giving me TLE. Can someone please tell me why?
Solution Link: CodeChef: Practical coding for everyone
I was so close to a full AC in this one. I didn’t realize we could precompute the good pairs and store them to speed up each test case. TLE on the absolute last test case
@aryanc403, think you. Hint: The answer is yes and instead of using only the linear sieve, we can use an additional O(N) loop again. Do you get some hint or more is required?