123gjweq2's blog

By 123gjweq2, 10 months ago, In English

I thought of this problem a while back, but I'm not sure if it has been created before. Anyway, I think that it is a pretty fun (possibly easier) problem, but I might be a bit biased. I'd appreciate it if you could give it a try and rate it. Also, maybe someone can find a simpler proof for it.

Problem statement:

A derangement of length $$$n$$$ is a permutation $$$p$$$ where $$$p_i \ne i$$$ for all $$$1 \le i \le n$$$.

You are given an integer $$$n$$$. You have to count the number of permutations $$$p$$$ of length $$$n$$$ that can be converted into a derangement using the following operation some (possibly $$$0$$$) number of times:

  • Choose any two integers $$$l, r$$$ such that $$$1 \le l \lt r \le n$$$ and sort the subarray $$$p_{l...r}$$$ in descending order.

For example, $$$p = [1, 2]$$$ would be counted for $$$n = 2$$$ as you can pick $$$l = 1,\,r = 2$$$ and transform the permutation into $$$[2, 1]$$$.

Constraints: $$$10$$$ testcases per test, each consisting of a single integer $$$n$$$ $$$(1 \le n \le 2 \cdot 10^5)$$$.

Examples:

$$$n = 1$$$ gives $$$0$$$, as $$$[1]$$$ cannot be converted into a derangement.

$$$n = 2$$$ gives $$$2$$$, as each permutation can be converted into a derangement.

$$$n = 3$$$ gives $$$5$$$. The only permutation that cannot be converted is $$$[3, 2, 1]$$$.

Solution

Vote:

Good problem

Mid problem

Bad problem

  • Vote: I like it
  • +59
  • Vote: I do not like it

»
10 months ago, hide # |
 
Vote: I like it +20 Vote: I do not like it

The problem is quite nice, but you could try proposing these problems for actual contests, you will even get paid for them ;-;

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it +5 Vote: I do not like it

    Thanks brosef, that would actually be pretty cool to have a problem in a contest like that. Hopefully another idea will strike me again.

    • »
      »
      »
      10 months ago, hide # ^ |
       
      Vote: I like it +10 Vote: I do not like it

      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."

      • »
        »
        »
        »
        10 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        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$$$.

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    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!

    • »
      »
      »
      10 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      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.

      • »
        »
        »
        »
        10 months ago, hide # ^ |
         
        Vote: I like it +5 Vote: I do not like it

        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

»
10 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

I am curious. How do u solve it?

»
10 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

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.

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    if it were a contest problem, probably yeah. I've never seen a problem like that not use modulo

»
10 months ago, hide # |
 
Vote: I like it -9 Vote: I do not like it

Div. 2 F tbh

  • »
    »
    10 months ago, hide # ^ |
     
    Vote: I like it -32 Vote: I do not like it

    If the problem requires modulo M, then Div 2 A/B would work. If not, I agree (Bignum implementation).

    • »
      »
      »
      10 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      It's not hard to copy-paste a bignum template, it's just a nuisance. This is why modulo p is the norm now

      • »
        »
        »
        »
        10 months ago, hide # ^ |
         
        Vote: I like it 0 Vote: I do not like it

        The constraints of this problem might make it harder to find a fast enough template for multiplication, but your point still stands :3

»
10 months ago, hide # |
Rev. 3  
Vote: I like it +1 Vote: I do not like it

how n=2 gives 2 shouldn't it be 1 ohh got it no of operation can be 0

»
10 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

div2b i think, sloved it after writing a permutation

»
10 months ago, hide # |
 
Vote: I like it +1 Vote: I do not like it

div.1D is not too high for the problem.

»
10 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

is there modulo also for the output, if not how can you give output

»
10 months ago, hide # |
 
Vote: I like it +3 Vote: I do not like it

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.