[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. jQuery 实现添加表格行,删除行,调用日期控件

    $(function () { getdatepicker(); getdatetimepicker(); }); $(document).on('click','#addTable',addTr); ...

  2. ES系列十三、Elasticsearch Suggester API(自动补全)

    1.概念 1.补全api主要分为四类 Term Suggester(纠错补全,输入错误的情况下补全正确的单词) Phrase Suggester(自动补全短语,输入一个单词补全整个短语) Comple ...

  3. eclipse自定义工具栏

    设置:1.Window2.Customize Perspective说明:Tool Bar Visibility定义菜单栏,Shortcuts定义右键new菜单

  4. 关于java中生产者消费者模式的理解

    在说生产者消费者模式之前,我觉得有必要理解一下 Obj.wait(),与Obj.notify()方法.wait()方法是指在持有对象锁的线程调用此方法时,会释放对象锁,同时休眠本线程.notify() ...

  5. python+selenium八:Alert弹窗

    此弹窗是浏览器自带的弹窗,不是html中的元素 from selenium import webdriverfrom selenium.webdriver.common.action_chains i ...

  6. python 全栈开发,Day117(popup,Model类的继承,crm业务开发)

    昨日内容回顾 第一部分:权限相关 1. 权限基本流程 用户登录成功后获取权限信息,将[权限和菜单]信息写入到session. 以后用户在来访问,在中间件中进行权限校验. 为了提升用户体验友好度,在后台 ...

  7. python 全栈开发,Day69(Django的视图层,Django的模板层)

    昨日内容回顾 相关命令: 1 创建项目 django-admin startproject 项目名称 2 创建应用 python manage.py startapp app名称 3 启动项目 pyt ...

  8. Entity Framework解决sql 条件拼接,完美解决 解决 不支持 LINQ 表达式节点类型“Invoke”【转】

    传统的操作数据库方式,筛选数据需要用StringBuilder拼接一大堆的WHERE子句. 在Entity Framework中,代码稍有不慎就会造成巨大性能消耗,如: using(var db=ne ...

  9. day8--socketserver作业

    fileno()文件描述符 handle_request()处理单个请求 server_forever(poll_interval=0.5)处理多个请求,poll_interval每0.5秒检测是否关 ...

  10. Codeforces 286B Shifting (看题解)

    Shifting 感觉这题被智力打击了.. 刚开始我想的是对于每个位置我们可以暴力找出最后的位置在哪里. 因为对于当前位置p, 在进行第x步操作时, 如果p % x == 1 则 p = p + x ...