B. Emordnilap
B. Emordnilap
A permutation of length is an array consisting of distinct integers from to in arbitrary order. For example, is a permutation, but is not a permutation ( appears twice in the array), and is also not a permutation ( but there is in the array). There are different permutations of length .
Given a permutation of numbers, we create an array consisting of numbers, which is equal to concatenated with its reverse. We then define the beauty of as the number of inversions in .
The number of inversions in the array is the number of pairs of indices , such that and .
For example, for permutation , would be . The inversions in are and (assuming 1-based indexing). Hence, the beauty of is .
Your task is to find the sum of beauties of all permutations of size . Print the remainder we get when dividing this value by ().
Input
Each test contains multiple test cases. The first line contains the number of test cases (). The description of the test cases follows.
Each test case has only one line — the integer ().
It is guaranteed that the sum of over all test cases does not exceed .
Output
For each test case, print one integer — the sum of beauties of all permutations of size modulo ().
Example
input
3 1 2 100
output
0 4 389456655
Note
For the first test case of the example, is the only permutation. has inversions.
For the second test case of the example, the permutations are and . Their respective arrays are and , both of which have inversions.
解题思路
比赛的时候想不出正解,然后模拟了几个样例找规律,发现任意一个排列逆序对的数量都相同,均是,因为一共有种排列,因此答案就是。
参考官方题解的推导。考虑排列中的两个下标和(),在任意一个排列中他们形式一定是 ,然后分情况讨论:
- 。那么排列中对于而言就存在个逆序对。
- 。那么排列中对于而言就存在个逆序对。
因此对于任意一对下标都会存在两个逆序对,而一个排列中共有对这样的下标,因此一个排列中的逆序对个数就是。
由于有种不同的排列,故总的逆序对个数就是。
AC代码如下:
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long LL; 5 6 const int mod = 1e9 + 7; 7 8 void solve() { 9 int n; 10 scanf("%d", &n); 11 int ret = (n - 1ll) * n % mod; 12 for (int i = 1; i <= n; i++) { 13 ret = 1ll * ret * i % mod; 14 } 15 printf("%d\n", ret); 16 } 17 18 int main() { 19 int t; 20 scanf("%d", &t); 21 while (t--) { 22 solve(); 23 } 24 25 return 0; 26 }
参考资料
Codeforces Round #845 (Div. 2) and ByteRace 2023 Editorial:https://codeforces.com/blog/entry/111729
本文来自博客园,作者:onlyblues,转载请注明原文链接:https://www.cnblogs.com/onlyblues/p/17064291.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效