[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 ...
随机推荐
- java并发编程系列二:原子操作/CAS
什么是原子操作 不可被中断的一个或者一系列操作 实现原子操作的方式 Java可以通过锁和循环CAS的方式实现原子操作 CAS( Compare And Swap ) 为什么要有CAS? Compar ...
- python用win32pdh模块查看进程信息
import win32pdh def get_processes(): win32pdh.EnumObjects(None, None, win32pdh.PERF_DETAIL_WIZARD) # ...
- GetStockObject 理解
原文地址:https://www.cnblogs.com/Clingingboy/archive/2013/04/13/3017952.html GetStockObject在图形编程中是常用API之 ...
- linux下混杂模式
混杂模式介绍: 混杂模式就是接收所有经过网卡的数据包,包括不是发给本机的包,默认情况下网卡只把发给本机的包(包括广播包)传递给上层程序,其它的包一律丢弃:简单的讲,混杂模式就是指网卡能接受所有通过它的 ...
- centos7和centos6.5环境rpm方式安装mysql5.7和mysql5.6详解
centos环境安装mysql5.7 其实不建议安装mysql5.7 语法和配置可能和以前的版本区别较大,多坑,慎入 1.yum方式安装(不推荐) a.安装mysql5.7 yum源 centos6: ...
- Android用户界面开发:TabHost
TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout.TabWidget就是每个tab的标签,FrameLayout则是tab内容.TabHost的二种实现方式:第 ...
- 各浏览器下使用 OBJECT 元素和 EMBED 元素嵌入 Flash 存在差异
标准参考 OBJECT 元素定义了一个嵌入的对象.其引入的初衷是取代 IMG 和 APPLET 元素.不过由于安全等各方面原因以及缺乏浏览器支持,这一初衷并未实现.浏览器的对象支持依赖于对象类型.然而 ...
- javascript 练习题目答案2
https://www.liaoxuefeng.com/wiki/001434446689867b27157e896e74d51a89c25cc8b43bdb3000/0014503724525055 ...
- 关于z-index的那些事儿
关于z-index的真正问题是,很少有人理解它到底是怎么用.其实它并不复杂,但是如果你从来没有花一定时间去看具体的z-index相关文档,那么你很可能会忽略一些重要的信息. 不相信我吗?好吧,看看你能 ...
- 微信支付支付宝支付生成二维码的方法(php生成二维码的三种方法)
如果图简单,可以用在线生成 http://pan.baidu.com/share/qrcode?w=150&h=150&url=http://www.xinzhenkj.com 最简单 ...