2021-10-01cfA. CQXYM Count Permutations涉及求逆元和费马小定理的讲解

A. CQXYM Count Permutations
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
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
inputCopy
4
1
2
9
91234
outputCopy
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,问这个长度*2的所有全排列中满足某个条件的个数,该条件是前一个数比后一个数小的个数之和>=n。
t组数据,t个n ,问每个n有多少个组合满足题意。
我们看到全排列是对称的,比方说n==2时,1 2 3 4是满足题意的,而4 3 2 1 自然是不满足的,因为如果正向满足,那么逆向一定就不满足。扩展到整个全排列,也就是一半满足,一半不满足。就用阶乘求出全排列的所有情况最后再/2即可。
但是,我们都知道(a−b)%c=(a%c−b%c)%c
( a + b ) % c = ( a % c + b % c ) % c
( a × b ) % c = ( a % c × b % c ) % c
都是满足的,而a与b的关系如果是除的话就不满足这个关系,因为除的时候有可能丢失信息,奇数偶数的相互影响…所以我们可以考虑求逆元。
这里就是乘法逆元,当然逆元还有更广义的定义,我们在这里只说乘法逆元。
这时候需要用到FLT(费马小定理):如果p是质数且a不是p的倍数,那么a^(p-1)≡1(mod p)
也可以写成a^(p-2)*a≡1(mod p)
我们这个题目不能/2但可以等价的乘以2的逆元,也就是乘以2(p-2), 2(p-2)可以用快速幂来求!
这道题目不就做完了吗!
当然,我么也可以巧妙地避开求2的逆元这件事,那就是阶乘从3开始不就好啦!
上代码

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#define int long long
#define mod 1000000007 
using namespace std; 
signed main()
{   int t,jilu ;
    cin>>jilu; 
    while(jilu)
	{jilu--; 
	cin >>t;
	t*=2; 
	int ans =1 ;
	while(t>=3)
	{
	ans=((ans%mod)*(t%mod))%mod; 
	t--; 
	}printf("%lld\n",ans);
	}
	return 0;  
}
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<stack>
#define int long long
#define mod 1000000007 
using namespace std; 
long long   power(long long  a, long long  x) {
	int  ans = 1;
	while(x) {
		if(x&1) ans = (ans * a) %mod;
		a = (a * a) %mod;
		x >>= 1;
	}
	return ans;
} 
long long  inv(long long  a) {
	return power(a, mod - 2);
}
signed main()
{   int t,jilu ;
    cin>>jilu; 
    while(jilu)
	{jilu--; 
	cin >>t;
	t*=2; 
	int ans =1 ;
	while(t>=2)
	{
	ans=((ans%mod)*(t%mod))%mod; 
	t--; 
	}int lo =inv(2); 
	ans=((ans%mod)*(lo%mod))%mod;
	printf("%lld\n",ans);
	}
	return 0;  
}

此外还有多种求逆元的知识,可以参考https://blog.csdn.net/qq_40861916/article/details/82928080,这位仁兄写的非常好了

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值