[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. 【vim】把当前文件转化为网页

    这会生成一个 HTML 文件来显示文本,并在分开的窗口显示源代码: :%TOhtml (译者注:原文是 :%Tohtml,但在我的电脑上是 :%TOhtml) 转载自:https://linux.cn ...

  2. Initialization of bean failed; nested exception is java.lang.IllegalArgumentException: error at ::0 inconsistent binding

    1.发生原因  springAOP 里面绑定参数出现错误  核对绑定参数的名称    核对 springAOP的版本 2.aop切面表达式写的有误

  3. 【 总结 】Tcp Keepalive 和 HTTP Keepalive 详解

    TCP Keepalive Tcp keepalive的起源          双方建立交互的连接,但是并不是一直存在数据交互,有些连接会在数据交互完毕后,主动释放连接,而有些不会,那么在长时间无数据 ...

  4. Oracle12c 性能优化攻略:攻略目录表

    注:本文来源于 [美] Sam Alapati ,   Darl Kuhn ,  Bill Padfield  著   朱浩波 翻译 <Oracle Database 12C 性能优化攻略> ...

  5. 传统DOM事件处理程序

    传统DOM事件处理程序与比HTML事件处理程序相比,优点:可以将HTML和JS脚本分离. 它的操作形式如下 : <body> <div>传统DOM事件处理程序与比HTML事件处 ...

  6. DDD模型领域WF到领域层(十五)

    实现超市的结算系统: 计算相应的优惠方式的接口 public interface ICompute { double GetResultTotalMoney(double TotalMoney); } ...

  7. centos6.5上安装配置telnet服务

    https://blog.csdn.net/wx5040257/article/details/78327915

  8. 【BZOJ3307】雨天的尾巴

    题解: win下的10mb和linux下的好像不是很一样 明天再看看 求lca用的离线求,注意bz数组开2*n 这道题的线段树合并还是很好想的 我们只要把操作差分一下就好了 时间复杂度nlogn的 写 ...

  9. day9线程和进程

    进程:qq要以一个整体的形式暴露给操作系统管理,里面包含对各种资源的调用,内存的对各种资源管理的集合,就可称之为进程. 线程:是操作系统最小的调度单位,是一串指令的集合. 进程:要操作CPU,必须要先 ...

  10. 关于C#中”扩展方法必须在非泛型静态类中定义“问题的解决

    问题描述: 在某个窗口下的编码中使用了以下扩展方法FindControl,以求根据字符串的值去操作控件(本文中的控件为Label控件)的属性. public static Control FindCo ...