当前位置:网站首页>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;
}
边栏推荐
- Database security - create login user + configure permissions [notes]
- Uni app advanced creation component / native rendering
- 13. Hash table - the first common node of two linked lists
- brief introduction
- Match file names from file paths using regular expressions
- [wechat applet] project practice - lottery application
- 15、判断二维数组中是否存在目标值
- Leetcode -- minimum number of rotation array
- LIBCMTD.lib
- SQL Server 2016 学习记录 --- 数据更新
猜你喜欢

SQL Server 2016学习记录 --- 连接查询

SQL Server 2016 learning record - Data Definition

SuperMap iServer发布管理以及调用地图服务

机器学习--手写英文字母3--工程特点

C语言 二级指针详解及示例代码

用两个栈实现一个队列【C语言】
![Database security - create login user + configure permissions [notes]](/img/02/0c3eb542593e8e0a3a62db75c52850.png)
Database security - create login user + configure permissions [notes]

第一篇:UniAPP的小程序跨端开发-----创建uniapp项目

Aqua Data Studio 18.5.0导出insert语句

SQL Server 2016 学习记录 --- 数据更新
随机推荐
AP Autosar平台设计 3架构
Install MySQL under centos7, and the online articles are not accurate
问题总结档案
SQL Server 2016学习记录 --- 连接查询
字符串匹配
Hurun released the 2020 top 10 Chinese chip design private enterprises: Huawei Hisilicon did not appear on the list!
IDEA创建我的第一个项目
Typora tutorial
3.用数组逆序打印链表
按位与、或、异或等运算方法
Codeforces Round #614 (Div. 2) B. JOE is on TV!
ACM寒假集训#4
ACM寒假集训#7
Uni app advanced life cycle
漏洞分析丨HEVD-0x8.IntegerOverflow[win7x86]
死锁算法:银行家算法和安全性算法
Massive data topn problem
India plans to ban China Telecom equipment! Can we really do without Huawei and ZTE?
15. Judge whether the target value exists in the two-dimensional array
[cloud co creation] enterprise digital transformation, Huawei cloud consulting is with you