[CC-PERMUTE]Just Some Permutations 3
[CC-PERMUTE]Just Some Permutations 3
题目大意:
\(T(T\le10^5)\)次询问,每次询问有多少长度为\(n(n\le10^6)\)的排列,满足任意相邻两个数的和不超过\(m\)。
思路:
找规律。
首先打表出来是这样的:
1: 1
2: 0 0 2
3: 0 0 0 2 6
4: 0 0 0 0 4 12 24
5: 0 0 0 0 0 4 36 72 120
6: 0 0 0 0 0 0 8 72 288 480 720
7: 0 0 0 0 0 0 0 8 216 864 2400 3600 5040
8: 0 0 0 0 0 0 0 0 16 432 3456 9600 21600 30240 40320
9: 0 0 0 0 0 0 0 0 0 16 1296 10368 48000 108000 211680 282240 362880
10: 0 0 0 0 0 0 0 0 0 0 32 2592 41472 192000 648000 1270080 2257920 2903040 3628800
把左边的\(0\)去掉,就是:
2:0 2
3:0 2 6
4:0 4 12 24
5:0 4 36 72 120
6:0 8 72 288 480 720
7:0 8 216 864 2400 3600 5040
8:0 16 432 3456 9600 21600 30240 40320
9:0 16 1296 10368 48000 108000 211680 282240 362880
10:0 32 2592 41472 192000 648000 1270080 2257920 2903040 3628800
发现最上面一层斜线就是阶乘,往下就是不停乘\(m,m-1\)。
源代码:
#include<cstdio>
#include<cctype>
#include<algorithm>
inline int getint() {
register char ch;
while(!isdigit(ch=getchar()));
register int x=ch^'0';
while(isdigit(ch=getchar())) x=(((x<<2)+x)<<1)+(ch^'0');
return x;
}
typedef long long int64;
const int N=1e6+1,mod=1e9+7;
int p[N],fac[N];
inline int power(int a,int k) {
int ret=1;
for(;k;k>>=1) {
if(k&1) ret=(int64)ret*a%mod;
a=(int64)a*a%mod;
}
return ret;
}
int main() {
for(register int i=fac[0]=1;i<N;i++) {
fac[i]=(int64)fac[i-1]*i%mod;
}
for(register int T=getint();T;T--) {
int n=getint(),m=getint();
if(n==1) {
puts("1");
continue;
}
if(m>=n*2-1) {
printf("%d\n",fac[n]);
continue;
}
if(m<n+1) {
puts("0");
continue;
}
m-=n-1;
n-=m;
int ans=fac[m];
ans=(int64)ans*power((int64)m*(m-1)%mod,n/2)%mod;
if(n&1) ans=(int64)ans*(m-1)%mod;
printf("%d\n",ans);
}
return 0;
}
[CC-PERMUTE]Just Some Permutations 3的更多相关文章
- [LeetCode] Permutations II 全排列之二
Given a collection of numbers that might contain duplicates, return all possible unique permutations ...
- [LeetCode] Permutations 全排列
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- Permutations
Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...
- 【leetcode】Permutations
题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...
- Leetcode Permutations
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- 46. Permutations 回溯算法
https://leetcode.com/problems/permutations/ 求数列的所有排列组合.思路很清晰,将后面每一个元素依次同第一个元素交换,然后递归求接下来的(n-1)个元素的全排 ...
- 【leetcode】Permutations (middle)
Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...
- LeetCode 【46. Permutations】
Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...
- LeetCode:Permutations, Permutations II(求全排列)
Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...
- leetcode总结:permutations, permutations II, next permutation, permutation sequence
Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...
随机推荐
- 在分析nginx日志时常用命令总结【转】
1. 利用grep ,wc命令统计某个请求或字符串出现的次数 比如统计GET /app/kevinContent接口在某天的调用次数,则可以使用如下命令: [root@Fastdfs_storage_ ...
- 使用nginx实现浏览器跨域请求
跨域访问问题, 相信很多人都遇到过, 并且都用不同的办法去解决过. 方法有很多种, 不一一叙述了. 这里主要使用nginx反向代理来解决跨域问题. 啥是跨域? 假如你是百度开发人员, 在百度页面去请求 ...
- centos7使用haproxy1.7.5实现反向代理负载均衡实战
使用haproxy实现反向代理负载均衡实战环境准备:两台虚拟机 # yum install -y gcc glibc gcc-c++ make screen tree lrzsz node1源码编译安 ...
- WampServer配置
本机php环境搭建教程:windows环境下wampserver的配置教程--超级详细 对于初做PHP网站的朋友来说,第一步肯定是希望在自己电脑是搭建PHP环境,省去空间和上传的麻烦!但搭建环境也不是 ...
- selenium和pyquery抓取异步加载数据
安装selenium和pyquery 打开命令行输入: pip install selenium pip install pyquery chromedriver的下载地址如下: http://chr ...
- citySelect省市区jQuery联动插件
参考地址:http://blog.csdn.net/qq_33556185/article/details/50704446 参考地址:http://www.lanrenzhijia.com/jque ...
- Idea xml 粘贴文本保持原有格式
setting->Editor->Code Style->XML 在右边的面板中,单击第二个 “Other” 的页签,勾选“Keep white spaces”,重启idea.
- python for dl
算是python的简明教程吧,总结的不错: https://zhuanlan.zhihu.com/p/24162430 python for opencv: https://zhuanlan.zhih ...
- java多线程快速入门(十五)
使用violate关键字解决了变量的可见性问题(volatile让多线程刷新falg的值) package com.cppdy; class MyThread11 extends Thread { / ...
- 搭建ssh框架项目(二)
一.创建dao层 (1)创建接口ICommonDao.java package com.cppdy.ssh.dao; public interface ICommonDao<T> { pu ...