题意:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1805

题解:

根据cayley公式,无向图的每一个生成树就对应一个序列(共有n^(n-2)个),具体定义见 http://www.matrix67.com/blog/archives/682

根据定义,这个n-2项中没有出现的点为叶子结点,所以我们先求C(n,m)表示那些点为叶子,再乘上序列的数量

S(n,m) = C(m,0)m^n - C(m,1)(m-1)^n + C(m,2)(m-2)^n -  .... (容斥定理)

#include<bits\stdc++.h>
using namespace std;
#define LL long long
const int maxn = 1000100;
const LL Mod = 1e9 + 7;
LL jc[maxn];
LL ny[maxn]; LL ksm(LL x,LL y)
{
LL ans = 1;
while(y){
if(y&1){
ans = ans*x%Mod;
}
y >>= 1;
x = x*x%Mod; }
return ans;
} LL getc(LL n,LL m)
{
if( m == 0 || n == m)
return 1;
return (jc[n]*ny[m]%Mod)*ny[n-m]%Mod;
} void pre(int n)
{
jc[1] = ny[1] = 1; for(int i = 2; i <= n; i++){
jc[i] = i * jc[i-1] % Mod;
ny[i] = (Mod - Mod/i) * ny[Mod%i] % Mod;
}
for(int i = 2; i <= n; i++){
ny[i] = ny[i] * ny[i-1] % Mod;
} } int main()
{
LL ans = 0;
LL n,m;
cin>>n>>m;
pre(n); LL c = getc(n,m);
LL k = n - m;
while(k){
if((k&1) == ((n-m)&1))
ans = (ans+ getc(n-m,k) * ksm(k,n-2) % Mod )% Mod;
else
ans = (Mod + ans - getc(n-m,k) * ksm(k,n-2) % Mod)% Mod; k--;
}
if(n == 2 && m == 2)
cout<<1<<endl;
else
cout<<c*ans%Mod<<endl;
return 0;
}

51nod 1805 小树 (组合数模板,逆元公式)的更多相关文章

  1. 牛客网 Wannafly挑战赛11 B.白兔的式子-组合数阶乘逆元快速幂

    链接:https://www.nowcoder.com/acm/contest/73/B来源:牛客网 B.白兔的式子   时间限制:C/C++ 1秒,其他语言2秒空间限制:C/C++ 262144K, ...

  2. HDU 6114 Chess【逆元+组合数】(组合数模板题)

    <题目链接> 题目大意: 車是中国象棋中的一种棋子,它能攻击同一行或同一列中没有其他棋子阻隔的棋子.一天,小度在棋盘上摆起了许多車……他想知道,在一共N×M个点的矩形棋盘中摆最多个数的車使 ...

  3. 51nod 1119 组合数,逆元

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1119 1119 机器人走方格 V2 基准时间限制:1 秒 空间限制:13 ...

  4. (light oj 1102) Problem Makes Problem (组合数 + 乘法逆元)

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1102 As I am fond of making easier problems, ...

  5. 牛客网 牛客小白月赛1 I.あなたの蛙が帰っています-卡特兰数,组合数阶乘逆元快速幂

    I.あなたの蛙が帰っています   链接:https://www.nowcoder.com/acm/contest/85/I来源:牛客网     这个题有点意思,是卡特兰数,自行百度就可以.卡特兰数用处 ...

  6. hdu5698瞬间移动-(杨辉三角+组合数+乘法逆元)

    瞬间移动 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submis ...

  7. POI SXSSFWorkbook 读取模板 存在公式解决

    package com.baoqilai.base.service.export; import java.io.File; import java.io.FileInputStream; impor ...

  8. 使用POI操作Excel时对事先写入模板的公式强制执行

    场景:POI读取Excel模板. 当使用POI操作Excel时,发现由POI生成的公式能够在打开Excel是被执行, 而事先手工写入Excel模板文件的公式则不自动被调用,必须手动双击该Cell才能生 ...

  9. K - Wand(组合数+错排公式)

    N wizards are attending a meeting. Everyone has his own magic wand. N magic wands was put in a line, ...

随机推荐

  1. EF Migrations error: No connection string named 'MpDb' could be found in the application config file.

    solution:  update-database 命令查找连接字符是在当前启动项目中找的 确保启动项目中connectiongString配置存在.

  2. Navicat Win 和 Mac 视图类快捷键对比

    Navicat 查询是根据用户需求从数据库提取可读格式的数据,Navicat 提供两个强大的工具与 SQL 查询工作:查询创建工具和查询编辑器,查询创建工具可视觉化地创建查询,查询编辑器可直接编辑查询 ...

  3. mysql多实例配置下,用脚本启动mysql时,出现Please read "Security" section of the manual to find out how to run mysqld as root!

    [root@localhost 3308]# mysqld stop170414 0:35:28 [Note] --secure-file-priv is set to NULL. Operation ...

  4. HighLight.js 使用Demo

    复制下面代码,保存为html,浏览器打开预览即可. <!DOCTYPE html> <html> <head> <meta charset="utf ...

  5. Effective Java 第三版——69. 仅在发生异常的条件下使用异常

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  6. Effective Java 第三版——67. 明智谨慎地进行优化

    Tips 书中的源代码地址:https://github.com/jbloch/effective-java-3e-source-code 注意,书中的有些代码里方法是基于Java 9 API中的,所 ...

  7. RabbitMQ 特性

    1RabbitMQ 特点 与 SpringAMQP 完美整合.API 丰富. 集群模式丰富,表达式配置,HA 模式,镜像队列模型. 保证数据不丢失的前提做到高可靠性.可用性.

  8. LeetCode 232:Implement Queue using Stacks

     Implement the following operations of a queue using stacks. push(x) -- Push element x to the back ...

  9. jQuery - Detect value change on hidden input field

    You can simply use the below function, You can also change the type element. $("input[type=hidd ...

  10. (10) 如何MySQL读压力大的问题

    如何进行读写分离 由开发人员根据所执行的SQL类型连接不同的服务器 由数据库中间层实现读写分离 读写分离时,需要注意,对于实时性要求比较高的数据,不适合在从库上查询(因为主从复制存在一定延迟(毫秒级) ...