[CF403D]Beautiful Pairs of Numbers

题意:给定$n,k$,对于整数对序列$\left(a_1,b_1\right),\cdots,\left(a_k,b_k\right)$,如果$1\leq a_1\leq b_1\lt a_2\leq b_2\lt\cdots\lt a_k\leq b_k\leq n$且所有的$b_i-a_i$互不相同,则称这个序列是“美丽的”,求美丽的序列的个数

先转化一下,把每个数对$\left(a_i,b_i\right)$看作一个区间$\left[a_i,b_i\right]$,则题目要求的是$k$个长度不同的区间互不重叠地放置在$[1,n]$的方案数

记$f_{i,j}$表示$i$个长度不同的区间总长为$j$且按长度递增排列的方案数,则$f_{0,0}=1,f_{i,j}=f_{i,j-i}+f_{i-1,j-i}$(可以直接把原方案的每个区间长度$+1$,或者在这个基础上再增加一个长度为$1$的区间)

那么答案是$k!\sum\limits_{i=1}^n\binom{n-i+k}kf_{k,i}$

相当于是枚举所有区间的总长,对于总长为$i$的所有方案($f_{k,i}$),把区间塞到$[1,n]$后我们还有$n-i$的空隙,相当于计算有$k$个区间和$n-i$个空隙的排列数($\binom{n-i+k}k$),因为$f$计数的是按长度递增排列的方案数,所以最后乘上$k!$表示任意排列

以上所有的东西(组合数,$f$,答案)都可以预处理出来,然后$O(1)$回答询问即可

不是这样的我没有在刷水题

#include<stdio.h>
typedef long long ll;
const int mod=1000000007,N=1000,K=50;
int fac[1010],rfac[1010],f[60][1010],ans[1010][60];
int mul(int a,int b){return a*(ll)b%mod;}
int ad(int a,int b){return(a+b)%mod;}
int C(int n,int k){return mul(fac[n],mul(rfac[n-k],rfac[k]));}
int pow(int a,int b){
	int s=1;
	while(b){
		if(b&1)s=mul(s,a);
		a=mul(a,a);
		b>>=1;
	}
	return s;
}
int main(){
	int i,j,k,t,n;
	fac[0]=1;
	for(i=1;i<=N;i++)fac[i]=mul(fac[i-1],i);
	rfac[N]=pow(fac[N],mod-2);
	for(i=N;i>0;i--)rfac[i-1]=mul(rfac[i],i);
	f[0][0]=1;
	for(i=1;i<=K;i++){
		for(j=i*(i+1)/2;j<=N;j++)f[i][j]=ad(f[i][j-i],f[i-1][j-i]);
	}
	for(i=1;i<=N;i++){
		for(k=1;k<=K;k++){
			for(j=k*(k+1)/2;j<=i;j++){
				ans[i][k]=ad(ans[i][k],mul(C(i-j+k,k),f[k][j]));
			}
			ans[i][k]=mul(ans[i][k],fac[k]);
		}
	}
	scanf("%d",&t);
	while(t--){
		scanf("%d%d",&n,&k);
		if(k>K)
			puts("0");
		else
			printf("%d\n",ans[n][k]);
	}
}

原文地址:https://www.cnblogs.com/jefflyy/p/8604824.html

时间: 2024-07-30 12:51:41

[CF403D]Beautiful Pairs of Numbers的相关文章

【题解】CF#236(Div. 1) D-Beautiful Pairs of Numbers

这题还挺对胃口的哈哈~是喜欢的画风!回家路上一边听歌一边想到的解法,写出来记录一下…… 首先,由于 \(b_{k} < a_{k + 1}\) ,所以我们可以看作是在一个长度为 n 的序列上选择出 k 个不相交的区间使得这 k个区间的长度各不相同.那么我们可以先求出 \(f[i][j]\) 表示选择了 \(i\) 个区间,这 \(i\) 个区间的区间长度总和为 \(j\) 的方案数.然后,我们考虑用这些方案数与序列剩下的长度的划分的方案数共同构成答案.所以我们再求一个 \(g[i][j]\) 表

Street Numbers

 Street Numbers A computer programmer lives in a street with houses numbered consecutively (from 1) down one side of the street. Every evening she walks her dog by leaving her house and randomly turning left or right and walking to the end of the str

【HackerRank】Closest Numbers

Sorting is often useful as the first step in many different tasks. The most common task is to make finding things easier, but there are other uses also. Challenge Given a list of unsorted numbers, can you find the numbers that have the smallest absol

POJ 1320 Street Numbers 【佩尔方程】

任意门:http://poj.org/problem?id=1320 Street Numbers Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3181   Accepted: 1776 Description A computer programmer lives in a street with houses numbered consecutively (from 1) down one side of the

[C6] Andrew Ng - Convolutional Neural Networks

About this Course This course will teach you how to build convolutional neural networks and apply it to image data. Thanks to deep learning, computer vision is working far better than just two years ago, and this is enabling numerous exciting applica

UVa 133 The Dole Queue

 The Dole Queue  In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone i

The Dole Queue UVA - 133

In a serious attempt to downsize (reduce) the dole queue, The New National Green Labour Rhinoceros Party has decided on the following strategy. Every day all dole applicants will be placed in a large circle, facing inwards. Someone is arbitrarily cho

[SinGuLaRiTy] COCI 2011~2012 #2

[SinGuLaRiTy-1007] Copyright (c) SinGuLaRiTy 2017. All Rights Reserved. 测试题目 对于所有的题目:Time Limit:1s  |  Memory:256 MB 第一题:考试得分(score)[najboljih] [题目描述] 某单位举办了一次考试,考试有8道题,每道题的得分不一样.选手可以随便做,但最后只统计5道题的得分.现在给出选手每道题的得分,求出他最后的得分最大是多少? [输入] 8行,每行一个正整数X(0<=X<

HDU1695-GCD(数论-欧拉函数-容斥)

GCD Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 5454    Accepted Submission(s): 1957 Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD(x, y