【思维、费马小定理】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
91234output
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;
}
【思维、费马小定理】CQXYM Count Permutations相关推荐
- E Groundhog Chasing Death(2020牛客暑期多校训练营(第九场))(思维+费马小定理+质因子分解)
E Groundhog Chasing Death(2020牛客暑期多校训练营(第九场))(思维+费马小定理+质因子分解) 链接:https://ac.nowcoder.com/acm/contest ...
- 快速幂-Jxc军训 (费马小定理+求概率)(2021-08-17)
Jxc军训 题目 在文某路学车中学高一新生军训中,Jxc正站在太阳下站着军姿,对于这样的酷热的阳光,Jxc 表示非常不爽. Jxc将天空看做一个nn的矩阵,此时天上有m朵云,这些云会随机分布在m个不同 ...
- c语言生成两位随机素数算法,[算法]费马小定理求质数的算法之Miller-Rabin算法,C语言实现 | 李大仁博客...
今天讲点比较高级的算法,目的也很简单,求质数,但是应用一种新的算法Miller-Rabin算法,这是一种利用了概率和费马小定理的算法设计,有点玄乎吧,其实本人也是刚接触这种算法,这是一种纯数学的解法, ...
- 欧拉定理 费马小定理
前言 学基础数论的时候看过证明,然而很快就忘了,最近在学习高深一点的数论,于是再复习一下欧拉定理和费马小定理. 欧拉定理 内容 若正整数 \(a,n\) 互质,则 \(a^{\varphi(n)}\e ...
- 「欧拉定理」学习笔记(费马小定理)
欧拉定理:对于互质的两个正整数$a, n$,满足$a^{φ(n)} ≡ 1\ (mod\ n)$ 证明: 设集合$S$包含所有$n$以内与$n$互质的数,共有$φ(n)$个:$$S = \{ x_1 ...
- 【ACM】杭电OJ 4704 Sum (隔板原理+组合数求和公式+费马小定理+快速幂)
http://acm.hdu.edu.cn/showproblem.php?pid=4704 1.隔板原理 1~N有N个元素,每个元素代表一个1.分成K个数,即在(N-1)个空挡里放置(K-1)块隔板 ...
- 子段乘积(逆元费马小定理)+线段树做法
题解:一开始做这个题的时候想过尺取法,但是因为没有逆元的知识,不知道该如何不断删除左端元素.其实这题并不难想,设l,r为两端开始都置为1,当长度小于k的时候不断乘右端元素并取余,当长度等于k时删除左端 ...
- luogu P2613 【模板】有理数取余(费马小定理,乘法逆元)
整理的算法模板合集: ACM模板 目录 题目传送门 题目传送门 相当于是一个高精的费马小定理求乘法逆元.虽然数据达到了101000110^{10001}1010001,但是我们可以使用快读然后一直模m ...
- 关于素数常用结论--威尔逊定理、欧拉定理、费马小定理、米勒罗宾算法
再需要判定的数比较大时,用枚举法肯定不行的,但目前数学界也没有任何一种又快又准确的判定素数的方法,并且也证明了素数不存在任何一种通项表达式.但作为初等数论中最大的一部分内容,数学家们对素数性质进行了大 ...
- 【BZOJ1951】【SDOI2010】古代猪文 Lucas定理、中国剩余定理、exgcd、费马小定理
Description "在那山的那边海的那边有一群小肥猪.他们活泼又聪明,他们调皮又灵敏.他们自由自在生活在那绿色的大草坪,他们善良勇敢相互都关心--" --选自猪王国民歌 很久 ...
最新文章
- 虚拟机VMware操作系统安装
- 【Linux】开源分布式存储系统:GlusterFS
- TensorRT(5)-INT8校准原理
- 第八十三期:这些高性能负载均衡架构知识点,90%的人分不清!
- python三引号的作用及用法
- 让fedora满足你的日常办公和影音
- 【调试工具】之VIM快捷键
- 使用DBATools PowerShell修复SQL Server中的孤立用户
- 省市区三级联动数据库
- Mac下GitHub安装及使用教程
- 苹果App Store应用程序的下载链接地址
- Elasticsearch实现内容精确匹配查询
- Ubuntu 14.04 卸载搜狗拼音输入法及后续问题解决
- gentoo 安装笔记
- 鹿晗关晓彤公布恋情阿里云服务器救场 这位微博程序员新婚还加班
- 不要去打扰别人的幸福
- GitLab-CI持续集成(CI)的介绍与运行机制
- 关于Context的理解(转)
- 在官方网站获得JMeter工具的步骤
- Python利用Scrapy爬取智联招聘和前程无忧的招聘数据
热门文章
- 终端安全求生指南(三)--脆弱性管理
- j$(function() j$(document).ready 区别
- PHP 命令行之-F (--process-file) 对每个输入行都执行 PHP 文件 (PHP 5 新加)
- SQL Server常用的字符串/日期/系统函数
- 构建可扩展的思科互联网络---多区域OSPF
- 关于 myeclipse 里面没有 add hibernate capabilities 问题解决方法
- java语言标识符的声明规范
- java设计模式-State模式
- python 对目录下文件过滤删除
- Summary on deep learning framework --- Torch7