codeforces 1284C. New Year and Permutation(组合数学)
链接: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(组合数学)的更多相关文章
- Codeforces 785 E. Anton and Permutation(分块,树状数组)
Codeforces 785 E. Anton and Permutation 题目大意:给出n,q.n代表有一个元素从1到n的数组(对应索引1~n),q表示有q个查询.每次查询给出两个数l,r,要求 ...
- [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 ...
- Codeforces Global Round 7 C. Permutation Partitions(组合数学)
题意: 给你 n 长全排列的一种情况,将其分为 k 份,取每份中的最大值相加,输出和的最大值和有多少种分法等于最大值. 思路: 取前 k 大值,储存下标,每两个 k 大值间有 vi+1 - vi 种分 ...
- Codeforces - 1081C - Colorful Bricks - 简单dp - 组合数学
https://codeforces.com/problemset/problem/1081/C 这道题是不会的,我只会考虑 $k=0$ 和 $k=1$ 的情况. $k=0$ 就是全部同色, $k=1 ...
- CodeForces 691D:Swaps in Permutation(并查集)
http://codeforces.com/contest/691/problem/D D. Swaps in Permutation You are given a permutation of ...
- Codeforces 1264D - Beautiful Bracket Sequence(组合数学)
Codeforces 题面传送门 & 洛谷题面传送门 首先对于这样的题目,我们应先考虑如何计算一个括号序列 \(s\) 的权值.一件非常显然的事情是,在深度最深的.是原括号序列的子序列的括号序 ...
- codeforces 677C C. Vanya and Label(组合数学+快速幂)
题目链接: C. Vanya and Label time limit per test 1 second memory limit per test 256 megabytes input stan ...
- codeforces 676A A. Nicholas and Permutation(水题)
题目链接: A. Nicholas and Permutation time limit per test 1 second memory limit per test 256 megabytes i ...
- 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 ...
随机推荐
- ISE post-place&route仿真准备
ISE post-place&route仿真准备 使用目的:post-place&route仿真是综合后考虑门延时而进行的仿真.因为考虑到各个门的延时,所以可以发现行为仿真(behav ...
- 怎么在IDEA中给方法添加分割线?
方法中间分割不清晰 怎么在IDEA中给方法添加分割线呢? 效果如图 方法上有一条分割线,比较明了 按照下列顺序点击修改设置即可 File→Settings→Editor→General→Appeara ...
- laravel 解决 sql mode only_full_group_by
this is incompatible with sql_mode=only_full_group_by 先贴报错是这样的哦,sql 中使用到了 group by 然后这是mysql-5.7以上版本 ...
- 小程序onShow事件获取options方法
微信小程序 onShow() 事件 onShow() 事件不接受参数,因此无法获取页面 url 传递过来的参数,只有 onLoad() 事件可以. onShow(options){ console.l ...
- Tomcat 后台war部署上传shell
tomcat的后台登录的两个目录为: /admin /manager/html 如果版本过高,只有采用弱密码的方式进后台: 有些tomcat采用默认的用户名和密码(用户名:admin,密码:空): 或 ...
- react-native构建基本页面4---渲染电影列表
电影列表 import React, { Component } from 'react' import { View, Image, Text, ActivityIndicator, FlatLis ...
- ECMAScript基本语法——⑦js特殊的语法
定义方法 无法弹出因为是局部变量 可以弹出因为是全局变量 这样写可读性比上面的高
- 858. Prim算法求最小生成树(模板)
给定一个n个点m条边的无向图,图中可能存在重边和自环,边权可能为负数. 求最小生成树的树边权重之和,如果最小生成树不存在则输出impossible. 给定一张边带权的无向图G=(V, E),其中V表示 ...
- python开发第一篇:初识python
一. Python介绍 python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为AB ...
- Spring的代理模式(静态,JDK,CGLIB)
一.静态代理 1.定义业务接口 public interface Subject { void doSomeThing(); } 2.真实业务类实现接口 public class RealSu ...