For even $$$n$$$, choosing the entire array in one operation will always turn it into a derangement. Therefore, the answer for even $$$n$$$ is just $$$n!$$$.
However, this does not work for odd $$$n$$$, because $$$\lceil \frac{n}{2} \rceil$$$ will fall into its corresponding index. Call this pivot value $$$v$$$. Notice that if $$$p_v = v$$$ and we can't move $$$v$$$ out of the $$$v^{th}$$$ index, then we can't turn $$$p$$$ into a derangement.
When does this happen? Well it must be the case that $$$p_v = v$$$ initially, all of the values to the left of $$$v$$$ are greater than $$$v$$$, and all of the values to the right of $$$v$$$ are less than $$$v$$$.
Why? Let's say one of the elements to the left of $$$v$$$ (at index $$$u$$$) is less than $$$v$$$. Then we can just apply the operation on $$$p_{u...v}$$$ and $$$v$$$ will be replaced with $$$u$$$ (or possibly a smaller element between the two indices). A similar argument can be made for the right side of $$$v$$$.
Now let's say the conditions are satisfied. We cannot move $$$p_v$$$ from $$$v$$$ because if we try to sort any subarray including index $$$v$$$, the order of the elements to the left of $$$v$$$ (or to the right of $$$v$$$) may change, but $$$v$$$ will stay in its position.
So now we have a condition where all $$$p$$$ satisfying it cannot be converted into some derangement. It turns out that all $$$p$$$ that don't satisfy the condition can always be turned into a derangement.
Convert the permutation into a simpler array $$$a$$$ where all elements less than $$$v$$$ are $$$0$$$'s, $$$v$$$ is $$$1$$$, and all elements greater than $$$v$$$ are $$$2$$$'s. The number of $$$0$$$'s is equal to the number of $$$2$$$'s. Now let's transform the operation into one that just swaps adjacent elements at indices $$$i, i + 1$$$ if and only if $$$a_i \lt a_{i + 1}$$$.
Notice that arrays of the form $$$[2, 2, \dots, 2, 0, 1, 0, 0, \dots 0]$$$ (form $$$1$$$) or $$$[2, 2, \dots, 2, 1, 2, 0, 0, \dots, 0]$$$ (form $$$2$$$) always represent derangements. For $$$n = 5$$$, these arrays would be $$$[2, 2, 0, 1, 0]$$$ and $$$[2, 1, 2, 0, 0]$$$, respectively.
The original $$$a$$$ won't be of the form $$$[2, 2, \dots, 2, 1, 0, 0, \dots, 0]$$$ (because such an array satisfies the condition). For the remaining arrays, we can either use the operation to push all of the $$$2$$$'s to the left and then position $$$1$$$ in its correct place to make form $$$1$$$, or we can use the operation to push all of the $$$0$$$'s to the right and then position $$$1$$$ in its correct place to make form $$$2$$$.
One of these procedures will certainly work because either the $$$1$$$ will come before a $$$2$$$ (meaning we can achieve form $$$2$$$) or the $$$1$$$ will come after a $$$0$$$ (meaning we can achieve form $$$1$$$).
So all permutations that don't satisfy the condition can be converted into some derangement. Therefore, the answer for odd $$$n$$$ is just $$$n! - (\lfloor \frac{n}{2} \rfloor!)^2$$$.
The problem is quite nice, but you could try proposing these problems for actual contests, you will even get paid for them ;-;
Thanks brosef, that would actually be pretty cool to have a problem in a contest like that. Hopefully another idea will strike me again.
I also have a very nice result, although it's very trivial. Try proving it:
"Let N be a positive integer. Suppose there exist two primes smaller than N, p1 and p2, such that p1 is the smallest prime smaller than and coprime to N and p2 is the second-smallest prime smaller than and coprime to N. Then N-p1 and N-p2 are always coprime with each other."
Nice statement, here is my attempt at a proof for it:
So let's say that $$$\gcd(N - p_1, N - p_2) = d$$$. By the Euclidean algorithm, this means that $$$\gcd(N - p_2, p_2 - p_1) = d$$$ or $$$d | p_2 - p_1$$$.
Okay, so we know that $$$p_2 - p_1 \lt p_2$$$ and $$$p_1 \nmid p_2 - p_1$$$. This means that, in order for $$$d \gt 1$$$ to hold, it must be the case that $$$d$$$ has some prime factor $$$ \lt p_2$$$ that isn't $$$p_1$$$.
We know that $$$d$$$ can't have a prime factor $$$ \lt p_1$$$ or else $$$d \nmid N - p_1$$$ and $$$d \nmid N - p_2$$$ because all prime factors below $$$p_1$$$ go into $$$N$$$. And $$$d$$$ can't have a prime factor between $$$p_1$$$ and $$$p_2$$$ or else that prime factor would also be coprime to $$$N$$$, meaning we'd have to move $$$p_2$$$ down. So I think $$$d = 1$$$.
Yeah. This is right.
Hey DeadMan69 can u please share some knowledge on "HOW" and "WHERE" can I share such problem ideas for actual contest say div2/3 .... Please if you would know or anyone seeing this message knows!
I'm not a 100% sure, but as per my knowledge, you will have to be good friends with some 2100+ rated guy, who is willing to host a contest, and offer him the problem, and then once the contest is done, the 2100+ (the one who organised) gets paid and ... . Other alternative is codechef, goforgold, iicpc, you might have seen their blogs even asking for problems and willing to pay.
Oh I see ... yeah I know about iicpc (I'm contributing there) but wanted to know how they request codeforces for hosting a contest! ... Thanks anyway bro
I am curious. How do u solve it?
there's a dropdown thing with the solution
Seems pretty fun, but do you think you'd need to take the results modulo M? Or else it might be harder due to BigNum implementations.
if it were a contest problem, probably yeah. I've never seen a problem like that not use modulo
Div. 2 F tbh
If the problem requires modulo M, then Div 2 A/B would work. If not, I agree (Bignum implementation).
It's not hard to copy-paste a bignum template, it's just a nuisance. This is why modulo p is the norm now
The constraints of this problem might make it harder to find a fast enough template for multiplication, but your point still stands :3
how n=2 gives 2 shouldn't it be 1 ohh got it no of operation can be 0
div2b i think, sloved it after writing a permutation
div.1D is not too high for the problem.
is there modulo also for the output, if not how can you give output
the formula is so simple so many people can guess it.
it's not on oeis, but discover that it is close to n! then you can find the pattern after check the different.