[hdu5226]组合数求和取模(Lucas定理)
题意:给一个矩阵a,a[i][j] = C[i][j](i>=j) or 0(i < j),求(x1,y1),(x2,y2)这个子矩阵里面的所有数的和。
思路:首先问题可以转化为求(0,0),(n,m)这个子矩阵的所有数之和。画个图容易得到一个做法,对于n<=m,答案就是2^0+2^1+...+2^m=2^(m+1)-1,对于n>m,答案由两部分构成,一部分是2^(m+1)-1,另一部分是sigma i:m+1->n f[i][m],f[i][m]表示第i行前m列的数之和,f数组存在如下关系,f[i][m]=f[i-1][m]*2-C[i-1][m],f[m][m]=2^m。还有另一种思路:第i列的所有数之和为C(i,i)+C(i+1,i)+...+C(n,i)=C(n+1,i+1),于是答案就是sigma i:0->min(n,m) C(n+1,i+1)。
Lucas定理:由于题目给定的模是可变的质数,且质数可能很小,那么就不能直接用阶乘和阶乘的逆相乘了,需要用到Lucas定理,公式:C(n,m)%P=C(n/P,m/P)*C(n%P,m%P)%P,c(n,m)=0(n<m)。当然最终还是要预处理阶乘和阶乘的逆来得到答案。复杂度O(nlogP+nlogn)
下面是第一种思路的代码:
#pragma comment(linker, "/STACK:10240000,10240000") #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <map>
#include <queue>
#include <deque>
#include <cmath>
#include <vector>
#include <ctime>
#include <cctype>
#include <set>
#include <bitset>
#include <functional>
#include <numeric>
#include <stdexcept>
#include <utility> using namespace std; #define mem0(a) memset(a, 0, sizeof(a))
#define mem_1(a) memset(a, -1, sizeof(a))
#define lson l, m, rt << 1
#define rson m + 1, r, rt << 1 | 1
#define define_m int m = (l + r) >> 1
#define rep_up0(a, b) for (int a = 0; a < (b); a++)
#define rep_up1(a, b) for (int a = 1; a <= (b); a++)
#define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
#define rep_down1(a, b) for (int a = b; a > 0; a--)
#define all(a) (a).begin(), (a).end()
#define lowbit(x) ((x) & (-(x)))
#define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
#define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
#define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
#define pchr(a) putchar(a)
#define pstr(a) printf("%s", a)
#define sstr(a) scanf("%s", a)
#define sint(a) scanf("%d", &a)
#define sint2(a, b) scanf("%d%d", &a, &b)
#define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
#define pint(a) printf("%d\n", a)
#define test_print1(a) cout << "var1 = " << a << endl
#define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
#define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
#define mp(a, b) make_pair(a, b)
#define pb(a) push_back(a) typedef unsigned int uint;
typedef long long LL;
typedef pair<int, int> pii;
typedef vector<int> vi; const int dx[] = {, , -, , , , -, -};
const int dy[] = {-, , , , , -, , - };
const int maxn = 1e8 + ;
const int md = 1e9 + ;
const int inf = 1e9 + ;
const LL inf_L = 1e18 + ;
const double pi = acos(-1.0);
const double eps = 1e-; template<class T>T gcd(T a, T b){return b==?a:gcd(b,a%b);}
template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
template<class T>T condition(bool f, T a, T b){return f?a:b;}
template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
int make_id(int x, int y, int n) { return x * n + y; } struct ModInt {
static int MD;
int x;
ModInt(int xx = ) { if (xx >= ) x = xx % MD; else x = MD - (-xx) % MD; }
int get() { return x; } ModInt operator + (const ModInt &that) const { int x0 = x + that.x; return ModInt(x0 < MD? x0 : x0 - MD); }
ModInt operator - (const ModInt &that) const { int x0 = x - that.x; return ModInt(x0 < MD? x0 + MD : x0); }
ModInt operator * (const ModInt &that) const { return ModInt((long long)x * that.x % MD); }
ModInt operator / (const ModInt &that) const { return *this * that.inverse(); } ModInt operator += (const ModInt &that) { x += that.x; if (x >= MD) x -= MD; }
ModInt operator -= (const ModInt &that) { x -= that.x; if (x < ) x += MD; }
ModInt operator *= (const ModInt &that) { x = (long long)x * that.x % MD; }
ModInt operator /= (const ModInt &that) { *this = *this / that; } ModInt inverse() const {
int a = x, b = MD, u = , v = ;
while(b) {
int t = a / b;
a -= t * b; std::swap(a, b);
u -= t * v; std::swap(u, v);
}
if(u < ) u += MD;
return u;
} };
int ModInt::MD;
int p;
#define mint ModInt mint C(mint fact[], mint fact_inv[], int n, int m) {
if (n < m) return ;
if (n < p) return fact[n] * fact_inv[m] * fact_inv[n - m];
return C(fact, fact_inv, n / p, m / p) * C(fact, fact_inv, n % p, m % p);
} mint get(mint a[], mint fact[], mint fact_inv[], int n, int m) {
if (n < || m < ) return ;
if (n <= m) return a[n + ] - ;
mint ans = a[m + ] - ;
mint last = a[m];
rep_up1(i, n - m) {
int u = m + i;
last = last * - C(fact, fact_inv, u - , m);
ans += last;
}
return ans;
} int main() {
//freopen("in.txt", "r", stdin);
int a, b, c, d;
while (cin >> a >> b >> c >> d >> p) {
mint::MD = p;
mint x = ;
mint mi2[], fact[], fact_inv[];
mi2[] = fact[] = fact_inv[] = ;
rep_up1(i, ) {
mi2[i] = mi2[i - ] * ;
fact[i] = fact[i - ] * i;
fact_inv[i] = fact_inv[i - ] / i;
}
cout << (get(mi2, fact, fact_inv, c, d) - get(mi2, fact, fact_inv, a - , d) -
get(mi2, fact, fact_inv, c, b - ) + get(mi2, fact, fact_inv, a - , b - )).get() << endl;
}
return ;
}
[hdu5226]组合数求和取模(Lucas定理)的更多相关文章
- 组合数取模Lucas定理及快速幂取模
组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1) , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...
- hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)
DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...
- 组合数取模&&Lucas定理题集
题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020 输出组合数C(n, m) mod p (1 ...
- [转]组合数取模 Lucas定理
对于C(n, m) mod p.这里的n,m,p(p为素数)都很大的情况.就不能再用C(n, m) = C(n - 1,m) + C(n - 1, m - 1)的公式递推了. 这里用到Lusac定理 ...
- hdu 3037 费马小定理+逆元除法取模+Lucas定理
组合数学推推推最后,推得要求C(n+m,m)%p 其中n,m小于10^9,p小于1^5 用Lucas定理求(Lucas定理求nm较大时的组合数) 因为p数据较小可以直接阶乘打表求逆元 求逆元时,由费马 ...
- hoj3152-Dice 等比数列求和取模
http://acm.hit.edu.cn/hoj/problem/view?id=3152 Dice My Tags (Edit) Source : Time limit : sec Memory ...
- Educational Codeforces Round 80 C. Two Arrays(组合数快速取模)
You are given two integers nn and mm . Calculate the number of pairs of arrays (a,b)(a,b) such that: ...
- 【阔别许久的博】【我要开始攻数学和几何啦】【高精度取模+同余模定理,*】POJ 2365 The Embarrassed Cryptographer
题意:给出一大数K(4 <= K <= 10^100)与一整数L(2 <= L <= 106),K为两个素数的乘积(The cryptographic keys are cre ...
- The Embarrassed Cryptographer(高精度取模+同余模定理)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11435 Accepted: 3040 Description The ...
随机推荐
- 详解 JDK8 新增的日期时间类
JDK8 新增的日期时间类 在本人之前的博文<处理时间的类 -- System类.Date类 .SimpleDateFormat类 与 Calendar类>中,讲到过表示时间的类,有三类: ...
- 最新VMware虚拟机安装Linux系统-CentOS(详细教程)
一.前言 最近有网友反应初学Linx不会安装,找了许多教程不是太全面,总会遇到一些要不是启动不了,要不是连不上网,各种问题,为了让大家能够顺利的安装,小乐写了一个非常详细的教程,让大家少入坑. 二.背 ...
- Java 中正则表达式使用
正则表达式基本用法: 测试代码: @Test public void test01() { String str = "adsfd##4324"; // 创建正则表达式对象 Pat ...
- Java工作流程引擎系统的退回规则 专题说明
概述 说明:流程引擎的退回与发送,分别是前进与后退,它是流程引擎的基础功能操作,流程的退回根据不同的应用场景,也是需要不同的方式来控制,我们把这些方式叫做规则处理. 退回工作的场景相对复杂,由于与审核 ...
- mysql5.7免安装版配置
解压之后,新建一个my.ini 内容是: [mysql] # 设置mysql客户端默认字符集 default-character-set = utf8 [mysqld] #安装目录 basedir = ...
- thinkphp--多表查询
我们可以将两个表连起来一起查询数据,我现在有两张表,一个是feedback表和member表,如图: 总目录: 上代码: $where = array(); $"; $Model = M(' ...
- cdn服务器
CDN的基本原理和基础架构 CDN是将源站内容分发至最接近用户的节点,使用户可就近取得所需内容,提高用户访问的响应速度和成功率.解决因分布.带宽.服务器性能带来的访问延迟问题,适用于站点加速.点播.直 ...
- Linux查看端口或pid使用路径
1. lsof -i:10010 查看10010端口的占用情况 命令返回结果: 2. netstat -lpn | grep 80 查看80端口服务端socket占用状况 3. ll /proc/26 ...
- linux uniq 命令实用手册
Linux uniq 命令用于处理文本内容中的重复行. 这里我们只介绍其常用参数,其完整用法可参见man uniq. 例如,我们有如下文件内容: >>> cat log.txt __ ...
- POJ2044 天气预报---状态细则
墙角数枝梅,凌寒独自开. 遥知不是雪,为有暗香来.--王安石 题目:天气预报 网址:http://poj.org/problem?id=2044 你是一个可以控制降雨的神仙. 你是一个仁慈的神,希望土 ...