当前位置: 首页 > news >正文

牛客周赛 Round 60 折返跑(组合数学)

题目链接:题目

大意:

1 1 1 n n n之间往返跑m趟,推 m − 1 m-1 m1次杆子,每次都向中间推,不能推零次,问有多少种推法(mod 1e9+7)。

思路:

一个高中学过的组合数学问题,实际上就是把 n − 2 n-2 n2(不算两头)个位置分配给 m − 1 m-1 m1次操作,也就是 C ( n − 2 , m − 1 ) C(n-2, m-1) C(n2,m1)
之后的关键在于怎么实现计算。计算组合数要涉及阶乘,阶乘每次计算费时间,那么我们可以用数组把阶乘计算结果储存起来 ,可以利用动态规划辅助计算。由于组合数设计除法,不方便取模,所以要计算逆阶乘,使用费马小定理,另外还需要快速幂计算乘方。

代码:

#include <bits/stdc++.h>  
using namespace std;  typedef long long ll;  const int MOD = 1e9 + 7;  
const int MAX = 1e6 + 5;  ll pow_mod_func(ll a, ll b, ll mod) {  ll res = 1;  a %= mod;  while(b > 0){  if(b & 1){  res = res * a % mod;  }  a = a * a % mod;  b >>= 1;  }  return res;  
}  ll fact[MAX];  
ll inv_fact_arr[MAX];  void precompute() {  fact[0] = 1;  for(int i = 1; i < MAX; ++i){  fact[i] = fact[i-1] * i % MOD;  }    inv_fact_arr[MAX-1] = pow_mod_func(fact[MAX-1], MOD-2, MOD);  for(int i = MAX-2; i >=0; --i){  inv_fact_arr[i] = inv_fact_arr[i+1] * (i+1) % MOD;  }  
}  ll comb(int n, int k){  if(k <0 || k >n) return 0;  return fact[n] * inv_fact_arr[k] % MOD * inv_fact_arr[n - k] % MOD;  
}  int main(){  ios::sync_with_stdio(false);  cin.tie(0);  precompute();  int t;  cin >> t;  while(t--){  int n, m;  cin >> n >> m;  int k = m -1;  if(k == 0){  cout << "1\n";  continue;  }  if(k > n - 2){  cout << "0\n";  continue;  }  ll ans = comb(n - 2, k);  cout << ans << '\n';  }  return 0;  
}
http://www.lryc.cn/news/439142.html

相关文章:

  • 深入浅出Java匿名内部类:用法详解与实例演示
  • 数据库MySQL、Mariadb、PostgreSQL、MangoDB、Memcached和Redis详细介绍
  • 【ArcGIS Pro实操第七期】栅格数据合并、裁剪及统计:以全球不透水面积为例
  • 【Linux】Image、zImage与uImage的区别
  • 算子加速(3):自定义cuda扩展
  • 信息安全数学基础(14)欧拉函数
  • 7-17 汉诺塔的非递归实现
  • word文档无损原样转pdf在windows平台使用python调用win32com使用pip安装pywin32
  • 海康威视相机在QTcreate上的使用教程
  • 进程状态、进程创建和进程分类
  • java后端请求调用三方接口
  • C#使用TCP-S7协议读写西门子PLC(三)
  • 铝型材及其常用紧固件、连接件介绍
  • 【裸机装机系列】7.kali(ubuntu)-安装开发所需工具
  • [C语言]第九节 函数一基础知识到高级技巧的全景探索
  • 1.1 计算机网络基本概述
  • Linux环境基础开发工具使用(gcc/g++与makefile)
  • PointNet++改进策略 :模块改进 | EdgeConv | DGCNN, 动态图卷积在3d任务上应用
  • FFmpeg源码:skip_bits、skip_bits1、show_bits函数分析
  • 加密
  • Kibana:如何使用魔法公式创建具有影响力的可视化效果?(第 1 部分)
  • 【C++】多态and多态原理
  • C# 实现二维数据数组导出到 Excel
  • nlohmann::json中有中文时调用dump转string抛出异常的问题
  • Unity中InputField一些属性的理解
  • 【webpack4系列】webpack构建速度和体积优化策略(五)
  • 从零开始搭建 PHP
  • 【数据结构】8——图3,十字链表,邻接多重表
  • eth-trunk 笔记
  • 通信工程学习:什么是接入网(AN)中的TF传送功能