枚举最多数字的出现次数$k$, 考虑其他数字的分配情况.

对至少$x$种数出现$\ge k$次的方案容斥, 有

$\sum (-1)^x\binom{m-1}{x}\binom{n-(x+1)k+m-2}{m-2}$.

暴力枚举$k$和$x$, 复杂度是$O(nlogn)$

#include <iostream>
#include <sstream>
#include <algorithm>
#include <cstdio>
#include <math.h>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <string.h>
#include <bitset>
#define REP(i,a,n) for(int i=a;i<=n;++i)
#define PER(i,a,n) for(int i=n;i>=a;--i)
#define hr putchar(10)
#define pb push_back
#define lc (o<<1)
#define rc (lc|1)
#define mid ((l+r)>>1)
#define ls lc,l,mid
#define rs rc,mid+1,r
#define x first
#define y second
#define io std::ios::sync_with_stdio(false)
#define endl '\n'
#define DB(a) ({REP(__i,1,n) cout<<a[__i]<<' ';hr;})
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
const int P = 1e9+7, P2 = 998244353, INF = 0x3f3f3f3f;
ll gcd(ll a,ll b) {return b?gcd(b,a%b):a;}
ll qpow(ll a,ll n) {ll r=1%P;for (a%=P;n;a=a*a%P,n>>=1)if(n&1)r=r*a%P;return r;}
ll inv(ll x){return x<=1?1:inv(P%x)*(P-P/x)%P;}
inline int rd() {int x=0;char p=getchar();while(p<'0'||p>'9')p=getchar();while(p>='0'&&p<='9')x=x*10+p-'0',p=getchar();return x;}
//head const int N = 1e6+10;
int n, m;
ll fac[N], ifac[N];
ll C(int n, int m) {
return fac[n]*ifac[n-m]%P*ifac[m]%P;
}
int main() {
fac[0]=ifac[0]=1;
REP(i,1,N-1) fac[i]=fac[i-1]*i%P,ifac[i]=inv(fac[i]);
int t;
scanf("%d", &t);
REP(i,1,t) {
scanf("%d%d", &n, &m);
if (m==1) {puts("1");continue;}
int ans = 0;
REP(k,1,n) REP(x,0,m-1) {
ll p = n-(ll)(x+1)*k+m-2;
if (p<m-2) break;
int ret = C(p,m-2)*C(m-1,x)%P;
if (x&1) (ans-=ret)%=P;
else (ans+=ret)%=P;
}
ans = (ll)ans*m%P;
if (ans<0) ans+=P;
printf("%d\n", ans);
}
}

51nod 1251 Fox序列的数量 (容斥)的更多相关文章

  1. 【51nod】1251 Fox序列的数量

    题解 容斥题 我们枚举出现次数最多的数出现了K次 然后我们需要计算的序列是所有数字出现个数都不超过K - 1次 我们枚举不合法的数字的数目j,说明这个排列里除了我们固定出现K次的数至少有j个数是不合法 ...

  2. 【51nod 1251】 Fox序列的数量(以及带限制插板法讲解)

    为什么网上没有篇详细的题解[雾 可能各位聚聚觉得这道题太简单了吧 /kk 题意 首先题目是求满足条件的序列个数,条件为:出现次数最多的数仅有一个 分析 感谢 刚睡醒的 JZ姐姐在咱写题解忽然陷入自闭的 ...

  3. 牛客挑战赛 39 牛牛与序列 隔板法 容斥 dp

    LINK:牛牛与序列 (牛客div1的E题怎么这么水... 还没D难. 定义一个序列合法 当且仅当存在一个位置i满足 $a_i>a_,a_j<a_$且对于所有的位置i,$1 \leq a_ ...

  4. 51nod 1514 美妙的序列 分治NTT + 容斥

    Code: #include<bits/stdc++.h> #define ll long long #define mod 998244353 #define maxn 400000 # ...

  5. hdu 5792(树状数组,容斥) World is Exploding

    hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...

  6. 51nod部分容斥题解

    51nod1434 区间LCM 跟容斥没有关系.首先可以确定的一个结论是:对于任意正整数,有1*2*...*n | (k+1)*(k+2)*...*(k+n).因为这就是$C_{n+k}^{k}$. ...

  7. Trees in a Wood. UVA 10214 欧拉函数或者容斥定理 给定a,b求 |x|<=a, |y|<=b这个范围内的所有整点不包括原点都种一棵树。求出你站在原点向四周看到的树的数量/总的树的数量的值。

    /** 题目:Trees in a Wood. UVA 10214 链接:https://vjudge.net/problem/UVA-10214 题意:给定a,b求 |x|<=a, |y|&l ...

  8. 51Nod 1486 大大走格子 —— 容斥

    题目:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1486 对于每个点,求出从起点到它,不经过其他障碍点的方案数: 求一 ...

  9. 洛谷 P2634 [国家集训队]聪聪可可-树分治(点分治,容斥版) +读入挂+手动O2优化吸点氧才过。。。-树上路径为3的倍数的路径数量

    P2634 [国家集训队]聪聪可可 题目描述 聪聪和可可是兄弟俩,他们俩经常为了一些琐事打起来,例如家中只剩下最后一根冰棍而两人都想吃.两个人都想玩儿电脑(可是他们家只有一台电脑)……遇到这种问题,一 ...

随机推荐

  1. su: Authentication failure 的解决方法

    su命令不能切换root,提示su: Authentication failure 原因是 没有设置过 root密码 sudo passwd root 会提示输入密码 重新输入密码 下次再su的时候只 ...

  2. postgres的数据库备份和恢复

    备份和恢复 一条命令就可以解决很简单: 这是备份的命令: pg_dump -h 127/0.0.1 -U postgres databasename > databasename.bak 指令解 ...

  3. Qt第三方库libvlc-qt——ubuntu上编译、安装,测试

      cmake 3.0编译安装(最低版本要求): sudo apt-get install ncurses-dev sudo apt-get install build-essential 下载cma ...

  4. Laravel find in set排序

    做项目遇到个需求,需要对结果集中的数据进行指定规则的顺序排列.例如,用户状态有四种: 0=>未激活:1=>正常:2=>禁用:3=>软删除 现在的需求是,我要按照:正常-> ...

  5. -webkit-scrollbar 的使用,滚动条的隐藏

    滚动条的隐藏 -webkit-scrollbar 是一个伪类选择器 设置滚动条的样式 例如滚动条的隐藏 元素::-webkit-scrollbar{ width:0; }

  6. LC 889. Construct Binary Tree from Preorder and Postorder Traversal

    Return any binary tree that matches the given preorder and postorder traversals. Values in the trave ...

  7. ssh 的一个坑

    概述 今天我碰到 fabric 和 ssh 的一个坑,记录下来,供以后开发时参考,相信对其他人也有用. ssh 今天用 ssh 登录远程服务器用不了 npm,查了下,发现原因是: ssh登录时不会加载 ...

  8. 在单元测试中处理Debug.Assert的好方法

    在单元测试项目的配置文件中配置好, <?xml version="1.0" encoding="utf-8"?> <configuration ...

  9. Git命令与介绍

    一. Git[命令与介绍] 1. 作用 可以用于个的项目版本控制和管理 目前多用于团队间的多人协作项目开发 2. 安装 l 安装包路径 l 安装 后的路径: 3. 工作流程 (1) 创建项目目录 在路 ...

  10. HR,OA,CRM,DRP,ERP什么意思?电商行业的特点?电商行业模式?专业术语?

    HR,OA,CRM,DRP,ERP HR----Human Resource人力资源管理 OA----Office Automation办公自动化 CRM---Customer Relationshi ...