Count the permutations
Practice
3.7 (12 votes)
Advanced data structures
Combinatorics
Data structures
Fenwick tree
Medium
普通
Problem
59% Success 3015 Attempts 30 Points 2s Time Limit 256MB Memory 1024 KB Max Code
You are given two sequences \(A\) and \(B\) of length \(N\). Determine the number of different ways to permute the sequence \(A\) such that it is lexicographically smaller than the sequence \(B\).
A sequence \((X_1, X_2, \dots, X_K)\) is strictly lexicographically smaller than a sequence \((Y_1,Y_2, \dots, Y_K)\), if there exists an index \(t\) \((1 \le t \le K)\) such that:
\(1. \ X_t < Y_t\)
\(2. \ X_r = Y_r\) for all \(1 \le r < t\)
A permutation \(X\) of \(A\) is considered different from another permutation \(Y\) of \(A\), if there exists an index \(i \ (1 \le i \le N)\) such that \(X_i \neq Y_i\). For example:
- If \(A = (1,1,1)\), then there is only one different permutation of \(A\).
- If \(A = (1,1,2,2)\), then there are \(6\) different permutations of \(A\).
Input format
- First line: Integer \(N \ (1 \le N \le 10^5)\)
- Second line: \(N\) integers - \(A_1, A_2, \dots, A_N \ (1 \le A_i \le 2 \cdot 10^5)\)
- Third line: \(N\) integers - \(B_1, B_2, \dots, B_N \ (1 \le B_i \le 2 \cdot 10^5)\)
Output format
Print the remainder of the final answer when divided by \(10^9 + 7.\)
Submissions
Please login to view your submissions
Similar Problems
Points:30
29 votes
Tags:
AlgorithmsApprovedImplementationMediumOpenTrees
Points:30
59 votes
Tags:
ApprovedBit ManipulationData StructuresMediumOpenReadyString Manipulation
Points:30
2 votes
Tags:
Advanced Data StructuresData StructuresDivide-and-conquer algorithmFenwick TreeFenwick treeMediumTwo pointer
Editorial