DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

Unusual Permutation

Back to problem

Editorial

Let m=r−l+1m=r-l+1m=r−l+1. The value kkk must appear inside the interval, and the other m−1m-1m−1 interval values must be chosen from 111 through k−1k-1k−1.

There are (k−1m−1)\binom{k-1}{m-1}(m−1k−1​) choices for these values, m!m!m! orders inside the interval, and (N−m)!(N-m)!( orders outside it. Thus the answer is

(k−1m−1)m!(N−m)!.\binom{k-1}{m-1}m!(N-m)!.(m−1k−1​)m!(N−m)!.

Precomputing factorials and inverse factorials gives total time complexity O(∑N)O(\sum N)O(∑N).

Solution written by GPT5

N
−
m)!