题意:给一个矩阵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定理)的更多相关文章

  1. 组合数取模Lucas定理及快速幂取模

    组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以 ...

  2. hdu 3944 DP? 组合数取模(Lucas定理+预处理+帕斯卡公式优化)

    DP? Problem Description Figure 1 shows the Yang Hui Triangle. We number the row from top to bottom 0 ...

  3. 组合数取模&&Lucas定理题集

    题集链接: https://cn.vjudge.net/contest/231988 解题之前请先了解组合数取模和Lucas定理 A : FZU-2020  输出组合数C(n, m) mod p (1 ...

  4. [转]组合数取模 Lucas定理

    对于C(n, m) mod p.这里的n,m,p(p为素数)都很大的情况.就不能再用C(n, m) = C(n - 1,m) + C(n - 1, m - 1)的公式递推了. 这里用到Lusac定理 ...

  5. hdu 3037 费马小定理+逆元除法取模+Lucas定理

    组合数学推推推最后,推得要求C(n+m,m)%p 其中n,m小于10^9,p小于1^5 用Lucas定理求(Lucas定理求nm较大时的组合数) 因为p数据较小可以直接阶乘打表求逆元 求逆元时,由费马 ...

  6. hoj3152-Dice 等比数列求和取模

    http://acm.hit.edu.cn/hoj/problem/view?id=3152 Dice My Tags (Edit) Source : Time limit : sec Memory ...

  7. 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: ...

  8. 【阔别许久的博】【我要开始攻数学和几何啦】【高精度取模+同余模定理,*】POJ 2365 The Embarrassed Cryptographer

    题意:给出一大数K(4 <= K <= 10^100)与一整数L(2 <= L <= 106),K为两个素数的乘积(The cryptographic keys are cre ...

  9. The Embarrassed Cryptographer(高精度取模+同余模定理)

    Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11435   Accepted: 3040 Description The ...

随机推荐

  1. Windows安装Tesseract-OCR 4.00并配置环境变量

    一.前言 Tesseract-OCR 是一款由HP实验室开发由Google维护的开源OCR(Optical Character Recognition , 光学字符识别)引擎.与Microsoft O ...

  2. 小小小小小flag

    2020:300道题 小小小小小flag 150红题 100道橙题 50道黄题 努力变强!加油 我的主页: 主页https://www.luogu.com.cn/user/306734 谢谢大家,目前 ...

  3. gitbook命令

    安装gitbook命令 前提:已经安装nodejs npm install -g gitbook-cli 查看版本号 gitbook -V gitbook命令 gitbook -h Usage: gi ...

  4. Trie树-提高海量数据的模糊查询性能

    今天这篇文章源于上周在工作中解决的一个实际问题,它是个比较普遍的问题,无论做什么开发,估计都有遇到过.具体是这样的,我们有一份高校的名单(2657个),需要从海量的文章标题中找到包含这些高校的标题,其 ...

  5. 我个人常用的git命令

    在还没有习惯用命令行之前,我建议用一下sourcetree这个软件熟悉一下流程. 使用 git clone 拷贝一个 Git 仓库到本地:git clone url 添加所有的文件到缓存区: git ...

  6. 超详细步骤---Linux下的最新Git版本安装

    原文地址:https://blog.csdn.net/u010887744/article/details/53957613 [标注大头] 1.查看当前git版本:git --version 查看最新 ...

  7. 徐州A

    #include<bits/stdc++.h> using namespace std; #define rep(i,a,b) for(int i=a;i<=b;++i) #defi ...

  8. 流畅的python读书笔记-第十章-继承优缺点

    继承的优缺点 推出继承的初衷是让新手顺利使用只有专家才能设计出来的框架.--Alan Kay 子类化内置类型很麻烦 (如 list 或 dict)) ,别搞这种 直接子类化内置类型(如 dict.li ...

  9. DeepWalk论文精读:(3)实验

    模块三 1 实验设计 1.1 数据集 BLOGCATALOG[39]:博客作者网络.标签为作者感兴趣的主题. FLICKR[39]:照片分享网站的用户网络.标签为用户的兴趣群组,如"黑白照片 ...

  10. python(类多态)

    一.多态 (以封装和继承为前提)不同的子类调用相同的方法,产生不同的结果 class Dog(): def __init__(self,name): self.name = name def game ...