当前位置:网站首页>Inverse element & combinatorial number & fast power
Inverse element & combinatorial number & fast power
2022-07-28 10:29:00 【MJ's Manon space】
Inverse element & Combinatorial number & Fast power
The first is the derivation of the inverse element

Fermat's theorem mentioned here :

Then we know the combination number Ckn=Akn/Akk=n! / (n-k)! / k!
According to what we deduced before 
set up inv[x] by x % mod Inverse element //mod For the value of remainder
Then use the fast power to get the factorial , Then take the inverse element by factorial
Here is the fast power :

Then use the fast power to get the power , Then use the power to find the inverse element :
inv[N - 1] = pow_mod(f[N - 1], mod - 2, mod); // The inverse element of the preconditioned factorial is deduced , among pow_mod Is a fast power
for (int i = N - 2; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;// Can be understood as 1/i!=1/(i+1)! *(i+1)
}
What we use here is :
inv[a]=am-2=1/a(mod m)
Pushable inv[x] % mod = 1/x;
Available Ckn=Akn/Akk=n! / (n-k)! / k!=n! * inv[k] % mod * inv[n - k] % mod
The combinatorial number can be obtained by fast power and inverse element .
Attach title Bheith i ngra le
link :https://ac.nowcoder.com/acm/contest/18362/B
source : Cattle from
Komorebi is a primary student who is learning how to draw a mountain.
First of all, he gets an m\times nm×n grid, and he can paint some cells black and others white. Let’s define a grid is good if:
All the black cells are connected.
For every column of the grid, if there are black cells, all the black cells must be connected and there must be one on the bottom.
Let’s call the height of the highest black cell in the ii-th column h_ih
i
. Specially, if there isn’t any black cell in the ii-th column, then h_i=0h
i
=0. To make the good graph more likely to be a mountain, the graph should satisfy that there at least exists a pair of integers (l,r)(l,r):
1≤l≤r≤n1≤l≤r≤n
All the h_ih
i
(1≤i≤l1≤i≤l) are non-decreasing.
All the h_ih
i
(r≤i≤nr≤i≤n) are non-increasing.
All the h_ih
i
(l≤i≤rl≤i≤r) are equal.
Komorebi wants to know how many kinds of mountain graphs he can draw by his m\times nm×n grid. Can you help him?
Input description :
One line contains two integers nn and mm(1≤n,m≤20001≤n,m≤2000), the number of columns and rows of the grid.
Output description :
One line contains one integer indicates the number of mountain graphs Komorebi can draw.
Answer should mod 10^9+7.
Example 1
Input
1 1
Output
2
Example 2
Input
2 2
Output
9
Example 3
Input
2 3
Output
16
The code is as follows :
#include<iostream>
#include<cstring>
#include<stack>
#include<queue>
#include<cstdio>
#include<string>
#include<cstring>
#include<cstdio>
#include<set>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<map>
#define ll long long
using namespace std;
const int maxn = 1000;
using namespace std;
const int N = 2010, mod = 1e9 + 7;
int n, m;
ll num[N][N], f[N], inv[N];
ll ans;
ll pow_mod(ll a, ll b, ll c) {
// Fast power
ll ans = 1;
ll base = a % c;
while (b) {
if (b & 1) ans = (ans * base) % c;
base = (base * base) % c;
b >>= 1;
}
return ans % c;
}
inline ll C(int k, int n) {
// Find the combination number
if (k > n) return 0;
return f[n] * inv[k] % mod * inv[n - k] % mod;
}
void init() {
//rev2 = pow_mod(2, mod-2,mod); // 2 Inverse element
f[0] = f[1] = 1;
for (ll i = 2; i < N; i++) {
// Preprocessing factorials
f[i] = f[i - 1] * i % mod;
}
inv[N - 1] = pow_mod(f[N - 1], mod - 2, mod); // The inverse element of the preconditioned factorial is deduced
for (int i = N - 2; i >= 0; i--) {
inv[i] = inv[i + 1] * (i + 1) % mod;
}
return;
}
int main() {
cin >> n >> m;
for (int k = 1;k <= m;k++) {
for (int i = n;i >= 1;i--) {
num[i][k] = (num[i + 1][k] + C(k - 1, n - i + k - 1)) % mod;
}
}
ans = 1;
for (int i = 1;i <= n;i++) {
for (int k = 1;k <= m;k++) {
ans = (ans + C(k - 1, i + k - 2) * num[i][k] % mod) % mod;
}
}
cout << ans;
return 0;
}
边栏推荐
- Chapter 1: cross end development of small programs of uniapp ----- create a uniapp project
- 在mt6735中添加新的开机logo与开\关机动画
- 中芯国际科创板IPO顺利过会,市值有望突破2000亿!
- Ie compatibility problem handling
- uni-app项目目录、文件作用介绍 及 开发规范
- DBeaver的操作日志
- Leetcode -- minimum number of rotation array
- Detailed explanation of thread synchronization volatile and synchronized
- 指令系统超全知识点详解
- MySQL的SQL TRACE一例
猜你喜欢

Multithreading and high concurrency (III) -- source code analysis AQS principle

7. Dichotomy -- find a set of repeated or ordered but rotating arrays

SQL Server 2016学习记录 --- 单表查询

django-celery-redis异步发邮件

gcc: error trying to exec 'as': execvp: No such file or directory

SQL Server 2016 学习记录 --- 数据定义

SQL Server 2016 learning records - data update

15、判断二维数组中是否存在目标值

uni-app项目目录、文件作用介绍 及 开发规范

Get to know SuperMap idesktop for the first time
随机推荐
ogg里用多个filter语法应该怎么写?
C语言 输入带空格的字符串
【微信小程序】项目实战—抽签应用
9. Delete nodes in the linked list
16、字符串反转
第一篇:UniAPP的小程序跨端开发-----创建uniapp项目
胡润发布2020中国芯片设计10强民营企业:华为海思竟然没有上榜!
Multithreading and high concurrency (III) -- source code analysis AQS principle
UEditor V1.4.3控制文件压缩
PL/SQL server语法详解
Record a parent-child project in idea, modify the name of project and module, and test it personally!
SQL Server 2016学习记录 --- 连接查询
初识SuperMap iDesktop
Troubleshooting of tool failure caused by Chinese characters in PT kill query
Problem summary file
Detailed explanation of super complete knowledge points of instruction system
SQL Server 2016 学习记录 --- 嵌套查询
SQL Server 2016学习记录 --- 单表查询
a different object with the same identifier value was already associated with the session
11、链表反转