[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的更多相关文章

  1. [LeetCode] Permutations II 全排列之二

    Given a collection of numbers that might contain duplicates, return all possible unique permutations ...

  2. [LeetCode] Permutations 全排列

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  3. Permutations

    Permutations Given a collection of distinct numbers, return all possible permutations. For example,[ ...

  4. 【leetcode】Permutations

    题目描述: Given a collection of numbers, return all possible permutations. For example, [1,2,3] have the ...

  5. Leetcode Permutations

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  6. 46. Permutations 回溯算法

    https://leetcode.com/problems/permutations/ 求数列的所有排列组合.思路很清晰,将后面每一个元素依次同第一个元素交换,然后递归求接下来的(n-1)个元素的全排 ...

  7. 【leetcode】Permutations (middle)

    Given a collection of numbers, return all possible permutations. For example,[1,2,3] have the follow ...

  8. LeetCode 【46. Permutations】

    Given a collection of distinct numbers, return all possible permutations. For example,[1,2,3] have t ...

  9. LeetCode:Permutations, Permutations II(求全排列)

    Permutations Given a collection of numbers, return all possible permutations. For example, [1,2,3] h ...

  10. leetcode总结:permutations, permutations II, next permutation, permutation sequence

    Next Permutation: Implement next permutation, which rearranges numbers into the lexicographically ne ...

随机推荐

  1. nodejs 使用http模块保存源码

    var xpath=require("xpath"); var fs=require("fs"); var dom = require('xmldom').DO ...

  2. Oracle数据库常用Sql语句大全

    一,数据控制语句 (DML) 部分 1.INSERT  (往数据表里插入记录的语句) INSERT INTO 表名(字段名1, 字段名2, ……) VALUES ( 值1, 值2, ……); INSE ...

  3. SQL定义变量

  4. jQuery性能优化指南

    总是从ID选择器开始继承在jQuery中最快的选择器是ID选择器,因为它直接来自于JavaScript的getElementById()方法. 例如有一段HTML代码:代码 <div id=&q ...

  5. 测试开发之前端——No4.HTML5中的事件属性

    HTML5的事件属性. 属性 值 描述 onafterprint script 在打印文档之后运行脚本 onbeforeprint script 在文档打印之前运行脚本 onbeforeonload ...

  6. LeetCode(47):全排列 II

    Medium! 题目描述: 给定一个可包含重复数字的序列,返回所有不重复的全排列. 示例: 输入: [1,1,2] 输出: [ [1,1,2], [1,2,1], [2,1,1] ] 解题思路: 这道 ...

  7. python 全栈开发,Day17(初识面向对象)

    一.引子 第一次参加工作,进入了一家游戏公司,公司需要开发一款游戏<人狗大战>一款游戏,首先得把角色和属性定下来. 角色有2个,分别是人和狗属性如下:人 :昵称.性别.血.攻击力狗 :名字 ...

  8. 步步为营-62-Excel的导入和导出

    说明:NPOI组件的使用 1 添加引用 2 代码 using System; using System.Collections.Generic; using System.ComponentModel ...

  9. 存储过程+Jquery+WebService实现三级联动:

    首先看一下数据库的设计:

  10. hdu 1879 有的边已存在 (MST)

    Sample Input31 2 1 0 //u v w 是否已建 1 3 2 02 3 4 031 2 1 01 3 2 02 3 4 131 2 1 01 3 2 12 3 4 10 Sample ...