In the problem STFM of FEB long challenge, my approach got AC for two sub tasks. It failed when value of x increased. After looking at the editorial, I tried it again with my approach with the mod trick(that factorial becomes zero). Now it is giving WA for all subtasks. I am unable to find my mistake. Can any one help please?

Second approach with WA for all sub task

First answer with AC for two sub task


Here is my approach. Since we have to calculate the values for different f§, if p1<p2, p2 will have same calculations as p1 plus some additional calculations. To reduce it, I sorted the values p1<=p2<=…<=pn.

So I calculated factorial part from 1 to pn(largest p) and whenever the value of iterator became equal to pi(less than pn), I added the value to result. Now, when the value of iterator became greater than mod, I stopped the calculations and stored the present factorial value. Now for all pi>=mod, f(pi) will be equal to f(mod-1). So I added this value to result for rest of the pi (i>=mod). This concludes the factorial part.

For the second part, i.e. the sum from 1 to pi (for all pi), I simply calculated it using Arithmetic progression formula and added to the result.


can you explain your approach?


Try including this:

#define nmod(num ,modValue) (modValue-((-num)%modValue))

Pass the values where there is a chance of getting negative values and you have to calculate the mod