【思维、费马小定理】CQXYM Count Permutations

一、题面

题目链接:CQXYM Count Permutations

CQXYM is counting permutations length of 2n.

A permutation is an array consisting of n distinct integers from 1 to n in arbitrary order. For example, [2,3,1,5,4] is a permutation, but [1,2,2] is not a permutation (2 appears twice in the array) and [1,3,4] is also not a permutation (n=3 but there is 4 in the array).

A permutation p(length of 2n) will be counted only if the number of i satisfying pi<pi+1 is no less than n. For example:

Permutation [1,2,3,4] will count, because the number of such i that pi<pi+1 equals 3 (i=1, i=2, i=3).
Permutation [3,2,1,4] won't count, because the number of such i that pi<pi+1 equals 1 (i=3).
CQXYM wants you to help him to count the number of such permutations modulo 1000000007 (109+7).

In addition, modulo operation is to get the remainder. For example:

7mod3=1, because 7=3⋅2+1,
15mod4=3, because 15=4⋅3+3.
Input
The input consists of multiple test cases.

The first line contains an integer t(t≥1) — the number of test cases. The description of the test cases follows.

Only one line of each test case contains an integer n(1≤n≤105).

It is guaranteed that the sum of n over all test cases does not exceed 105
Output
For each test case, print the answer in a single line.

Example
input
4
1
2
9
91234

output
1
12
830455698
890287984
Note
n=1, there is only one permutation that satisfies the condition: [1,2].
In permutation [1,2], p1<p2, and there is one i=1 satisfy the condition. Since 1≥n, this permutation should be counted. In permutation [2,1], p1>p2. Because 0<n, this permutation should not be counted.

n=2, there are 12 permutations: [1,2,3,4],[1,2,4,3],[1,3,2,4],[1,3,4,2],[1,4,2,3],[2,1,3,4],[2,3,1,4],[2,3,4,1],[2,4,1,3],[3,1,2,4],[3,4,1,2],[4,1,2,3].

  • 题目大意:给你一个数字n,你拥有一个从1到2*n的数组,提问在数组的全排列中。ai<ai-1的数量不少于n的排列数量为多少?结果对109+7取模。

二、题目分析

  • 首先是思维部分,数组中正序对的数量如果最多则为{1、2、……、2n},最少为{2n、2n-1、……、1}。我们可以想到,正序对的数量小于n和不小于n的排列数量是相同的,那么其实我们只要求出全排列的数量除2就可以了。
  • 然后是费马小定理部分:由于我们最后要除以2但是在求排列的时候我们又要对1e9+7进行取模,我们还需要先把结果乘上2对1e9+7的逆元后再取模。逆元用费马小定理来求。

2对1e9+7的逆元:21e9+7-2
也可以
在这里插入图片描述
哪种更加轻松一眼就看得出来

三、代码分析

#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define INF 0x3f3f3f3f
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef  pair<int, int> PII;
const int N = 1e6 + 7, mod = 1e9 + 7;

int t;

void solve()
{
	cin >> t;
	while (t--)
	{
		int n;
		cin >> n;
		int ans = 1;
		for (int i = 1; i <= 2*n; i++)
		{
			ans *= i;
			ans %= mod;
		}
		cout <<( ans*(mod-mod/2) )%mod<< endl;
	}
}

signed main()
{
	solve();
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值