链接:https://codeforces.com/problemset/problem/1284/C

题意:定义一个framed segment,在区间[l,r]中,max值-min值 = r - l。求有1-n 组成的序列中,所有framed segment的个数%m

思路:组合数学推一个结论。例如假设1到n组成的序列中,求长度为k的framed segment,那么其一段序列的最大值 - 最小值 = k,例如n = 5,k = 3,这些framed segment 必定是 1 2 3 或者2 3 4 或者 3 4 5,可以观测到其长度为k的framed segment必定是连续的,可以把他们单独算一个整体,这样序列总体长度变为n - k + 1,内部长度为k,内部组合种类就是k!个,总体组合种类就是(n-k+1)!,长度为k的framed segment种类又是(123,234,345)n - k + 1种,所以长度为k的framed segment 最终答案就是(n-k+1)*(n-k+1)!*k!,预处理一下阶乘即可。

AC代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;
const int maxn = 2e5+5e4+;
ll fac[maxn];
int main(){
ll n,m;
cin>>n>>m;
fac[] = ;
for(int i = ;i<=n;i++){
fac[i] = (fac[i-]*i)%m;
}
ll ans = ;
for(int i = ;i<=n;i++){
ans +=((n+-i)*(fac[i])%m)*(fac[n+-i])%m;
ans = ans%m;
}
// 1 2 3
cout<<ans%m;
return ;
}

codeforces 1284C. New Year and Permutation(组合数学)的更多相关文章

  1. Codeforces 785 E. Anton and Permutation(分块,树状数组)

    Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...

  2. [Hello 2020] C. New Year and Permutation (组合数学)

    [Hello 2020] C. New Year and Permutation (组合数学) C. New Year and Permutation time limit per test 1 se ...

  3. Codeforces Global Round 7 C. Permutation Partitions(组合数学)

    题意: 给你 n 长全排列的一种情况,将其分为 k 份,取每份中的最大值相加,输出和的最大值和有多少种分法等于最大值. 思路: 取前 k 大值,储存下标,每两个 k 大值间有 vi+1 - vi 种分 ...

  4. Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学

    https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...

  5. CodeForces 691D:Swaps in Permutation(并查集)

    http://codeforces.com/contest/691/problem/D D. Swaps in Permutation   You are given a permutation of ...

  6. Codeforces 1264D - Beautiful Bracket Sequence(组合数学)

    Codeforces 题面传送门 & 洛谷题面传送门 首先对于这样的题目,我们应先考虑如何计算一个括号序列 \(s\) 的权值.一件非常显然的事情是,在深度最深的.是原括号序列的子序列的括号序 ...

  7. codeforces 677C C. Vanya and Label(组合数学+快速幂)

    题目链接: C. Vanya and Label time limit per test 1 second memory limit per test 256 megabytes input stan ...

  8. codeforces 676A A. Nicholas and Permutation(水题)

    题目链接: A. Nicholas and Permutation time limit per test 1 second memory limit per test 256 megabytes i ...

  9. Codeforces 612E - Square Root of Permutation

    E. Square Root of Permutation A permutation of length n is an array containing each integer from 1 t ...

随机推荐

  1. wxpython 简单例子:显示文本框的窗口显示鼠标位置

    简单例子来自教程: #!/bin/env python import wx class MyFrame(wx.Frame): def __init__(self): wx.Frame.__init__ ...

  2. 【终端使用】echo、">"覆盖、">>"追加、"|"管道

    1."echo"文字内容 "echo"命令,会在终端中显示参数指定的文字,通常会和 重定向 联合使用. 2.重定向 ">"和" ...

  3. NODEJS 使用 sqlite3 本地文件数据库

    npm install sqlite3 var sqlite3 = require('sqlite3').verbose();var db = new sqlite3.Database('WebFil ...

  4. css样式的兼容性

    浏览器                 前缀 IE和safari             -webkit- Chrome                -ms- Firefox            ...

  5. 关于form表单提交数据后不跳转页面+ajax接收返回值的处理

    1.前台的form表单建立,注意action.enctype的内容, 2.通过添加一个隐藏的iframe标签使form的target指向iframe来达到不跳转页面的效果,同时需要在js里获取ifra ...

  6. cf 989C

    构造一个网格图使得四种类型的联通分量分别有a,b,c,d 看图就知道应该如何去构造了 int gird[maxn][maxn]; int main(){ int a[4]; for(int i=0;i ...

  7. 关于Comparable和Comparator那些事

    在实际项目开发过程中,我们经常需要对某个对象或者某个集合中的元素进行排序,常用的两种方式是实现某个接口.常见的可以实现比较功能的接口有Comparable接口和 Comparator接口,那么这两个又 ...

  8. Maven修改test/rsource的output folder报错Test source folder 'src/test/java'... is not also used for main s

    eclipse新建maven项目时候,只出来三个文件夹,然后大都督手动添加了缺失的src/test/resource 的文件夹,最后想修改一下 Output folder的路径为 (原来是     d ...

  9. [51nod 1181] 质数中的质数 - 筛法

    如果一个质数,在质数列表中的编号也是质数,那么就称之为质数中的质数.例如:3 5分别是排第2和第3的质数,所以他们是质数中的质数.现在给出一个数N,求>=N的最小的质数中的质数是多少(可以考虑用 ...

  10. 原型链污染(Node.js污染,javasrcipt原型链污染的)

    学习链接: https://www.jianshu.com/p/6e623e9debe3 关于NJS  https://xz.aliyun.com/t/7184 相关题是 GYCTF  ez_expr ...