[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),c(n,m)=0(n<m)。当然最终还是要预处理阶乘和阶乘的逆来得到答案。复杂度O(nlogP+nlogn)

下面是第一种思路的代码:

  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 #include <bitset>
 17 #include <functional>
 18 #include <numeric>
 19 #include <stdexcept>
 20 #include <utility>
 21
 22 using namespace std;
 23
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define mem_1(a) memset(a, -1, sizeof(a))
 26 #define lson l, m, rt << 1
 27 #define rson m + 1, r, rt << 1 | 1
 28 #define define_m int m = (l + r) >> 1
 29 #define rep_up0(a, b) for (int a = 0; a < (b); a++)
 30 #define rep_up1(a, b) for (int a = 1; a <= (b); a++)
 31 #define rep_down0(a, b) for (int a = b - 1; a >= 0; a--)
 32 #define rep_down1(a, b) for (int a = b; a > 0; a--)
 33 #define all(a) (a).begin(), (a).end()
 34 #define lowbit(x) ((x) & (-(x)))
 35 #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) {}
 36 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 37 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 38 #define pchr(a) putchar(a)
 39 #define pstr(a) printf("%s", a)
 40 #define sstr(a) scanf("%s", a)
 41 #define sint(a) scanf("%d", &a)
 42 #define sint2(a, b) scanf("%d%d", &a, &b)
 43 #define sint3(a, b, c) scanf("%d%d%d", &a, &b, &c)
 44 #define pint(a) printf("%d\n", a)
 45 #define test_print1(a) cout << "var1 = " << a << endl
 46 #define test_print2(a, b) cout << "var1 = " << a << ", var2 = " << b << endl
 47 #define test_print3(a, b, c) cout << "var1 = " << a << ", var2 = " << b << ", var3 = " << c << endl
 48 #define mp(a, b) make_pair(a, b)
 49 #define pb(a) push_back(a)
 50
 51 typedef unsigned int uint;
 52 typedef long long LL;
 53 typedef pair<int, int> pii;
 54 typedef vector<int> vi;
 55
 56 const int dx[8] = {0, 0, -1, 1, 1, 1, -1, -1};
 57 const int dy[8] = {-1, 1, 0, 0, 1, -1, 1, -1 };
 58 const int maxn = 1e8 + 17;
 59 const int md = 1e9 + 7;
 60 const int inf = 1e9 + 7;
 61 const LL inf_L = 1e18 + 7;
 62 const double pi = acos(-1.0);
 63 const double eps = 1e-6;
 64
 65 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 66 template<class T>bool max_update(T &a,const T &b){if(b>a){a = b; return true;}return false;}
 67 template<class T>bool min_update(T &a,const T &b){if(b<a){a = b; return true;}return false;}
 68 template<class T>T condition(bool f, T a, T b){return f?a:b;}
 69 template<class T>void copy_arr(T a[], T b[], int n){rep_up0(i,n)a[i]=b[i];}
 70 int make_id(int x, int y, int n) { return x * n + y; }
 71
 72 struct ModInt {
 73     static int MD;
 74     int x;
 75     ModInt(int xx = 0) { if (xx >= 0) x = xx % MD; else x = MD - (-xx) % MD; }
 76     int get() { return x; }
 77
 78     ModInt operator + (const ModInt &that) const { int x0 = x + that.x; return ModInt(x0 < MD? x0 : x0 - MD); }
 79     ModInt operator - (const ModInt &that) const { int x0 = x - that.x; return ModInt(x0 < MD? x0 + MD : x0); }
 80     ModInt operator * (const ModInt &that) const { return ModInt((long long)x * that.x % MD); }
 81     ModInt operator / (const ModInt &that) const { return *this * that.inverse(); }
 82
 83     ModInt operator += (const ModInt &that) { x += that.x; if (x >= MD) x -= MD; }
 84     ModInt operator -= (const ModInt &that) { x -= that.x; if (x < 0) x += MD; }
 85     ModInt operator *= (const ModInt &that) { x = (long long)x * that.x % MD; }
 86     ModInt operator /= (const ModInt &that) { *this = *this / that; }
 87
 88     ModInt inverse() const {
 89         int a = x, b = MD, u = 1, v = 0;
 90         while(b) {
 91             int t = a / b;
 92             a -= t * b; std::swap(a, b);
 93             u -= t * v; std::swap(u, v);
 94         }
 95         if(u < 0) u += MD;
 96         return u;
 97     }
 98
 99 };
100 int ModInt::MD;
101 int p;
102 #define mint ModInt
103
104 mint C(mint fact[], mint fact_inv[], int n, int m) {
105     if (n < m) return 0;
106     if (n < p) return fact[n] * fact_inv[m] * fact_inv[n - m];
107     return C(fact, fact_inv, n / p, m / p) * C(fact, fact_inv, n % p, m % p);
108 }
109
110 mint get(mint a[], mint fact[], mint fact_inv[], int n, int m) {
111     if (n < 0 || m < 0) return 0;
112     if (n <= m) return a[n + 1] - 1;
113     mint ans = a[m + 1] - 1;
114     mint last = a[m];
115     rep_up1(i, n - m) {
116         int u = m + i;
117         last = last * 2 - C(fact, fact_inv, u - 1, m);
118         ans += last;
119     }
120     return ans;
121 }
122
123 int main() {
124     //freopen("in.txt", "r", stdin);
125     int a, b, c, d;
126     while (cin >> a >> b >> c >> d >> p) {
127         mint::MD = p;
128         mint x = 1;
129         mint mi2[100007], fact[100007], fact_inv[100007];
130         mi2[0] = fact[0] = fact_inv[0] = 1;
131         rep_up1(i, 100002) {
132             mi2[i] = mi2[i - 1] * 2;
133             fact[i] = fact[i - 1] * i;
134             fact_inv[i] = fact_inv[i - 1] / i;
135         }
136         cout << (get(mi2, fact, fact_inv, c, d) - get(mi2, fact, fact_inv, a - 1, d) -
137                  get(mi2, fact, fact_inv, c, b - 1) + get(mi2, fact, fact_inv, a - 1, b - 1)).get() << endl;
138     }
139     return 0;
140 }

时间: 2024-10-12 20:42:27

[hdu5226]组合数求和取模(Lucas定理)的相关文章

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

组合数取模就是求的值,根据,和的取值范围不同,采取的方法也不一样. 下面,我们来看常见的两种取值情况(m.n在64位整数型范围内) (1)  , 此时较简单,在O(n2)可承受的情况下组合数的计算可以直接用杨辉三角递推,边做加法边取模. (2) ,   ,并且是素数 本文针对该取值范围较大又不太大的情况(2)进行讨论. 这个问题可以使用Lucas定理,定理描述: 其中 这样将组合数的求解分解为小问题的乘积,下面考虑计算C(ni, mi) %p. 已知C(n, m) mod p = n!/(m!(

【日常学习】【组合数取模Lucas定理】HDU3037 Saving Beans题解

[提前声明:此题没有通过!WA!有待进一步研究修改.放在这里只是起一个例子的作用,其实这道题鄙人并没有真正掌握= =]. [本文努力抄袭模仿了小花妹妹的博文0戳我0)] 题目大意:共T个测试点,每个测试点中,给定n.m,求将不超过m个种子放入n个坑的方案总数,最后答案对质数p取模.(一共m个,每个坑放多少无所谓,最后没放完m个也无所谓) 数据范围:1 <= n, m <= 1000000000, 1 < p < 100000. 思路:原题意即求方程x1+-+xn=m解的个数,因为中

[转]组合数取模 Lucas定理

对于C(n, m) mod p.这里的n,m,p(p为素数)都很大的情况.就不能再用C(n, m) = C(n - 1,m) + C(n - 1, m - 1)的公式递推了. 这里用到Lusac定理 For non-negative integers m and n and a prime p, the following congruence relation holds: where and are the base p expansions of m and n respectively.

hoj3152-Dice 等比数列求和取模

http://acm.hit.edu.cn/hoj/problem/view?id=3152 Dice My Tags (Edit) Source : Time limit : 1 sec Memory limit : 128 M Submitted : 82, Accepted : 18 You have a dice with M faces, each face contains a distinct number. Your task is to calculate the expect

等比数列二分求和取模

题意:Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. n (n ≤ 30), k (k ≤ 109) and m (m < 104) 输出结果矩阵 解法: 若 n是偶数 Sn= a+...+an/2 + an/2+1 + an/2+2 +...+ an/2+n/2 =(a+...+an/2) + an/2(a+...+an/2) =Sn/2+ an/2Sn/2 =(1+

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: the length of both arrays is equal to mm ; each element of each array is an integer between 11 and nn (inclusive); ai≤biai≤bi for any index ii from 1

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

题意:给出一大数K(4 <= K <= 10^100)与一整数L(2 <= L <= 106),K为两个素数的乘积(The cryptographic keys are created from the product of two primes) 问构成K的最小素数是否绝对小于L,若是,则输出BAD p,p为最小素数,否则输出GOOD; 分析:从小到大枚举1~10^6内的素数p,while(p<L)时,判断K是否能被p整除,若能则证明构成K的最小素数绝对小于L,反之则大于L

B-Icebound and Sequence(等比数列求和取模)

题目传送门:B-Icebound and Sequence(19年河北省赛) 题目大意: 等比数列求和,结果取模 分析: 因为取模操作,直接运用等比数列求和公式无法做出,所以需要用到公式 求等比为k的等比数列之和S[n]..当n为偶数..S[n] = S[n/2] + pow(k,n/2) * S[n/2] n为奇数...S[n] = S[n/2] + pow(k,n/2) * S[n/2] + pow(k,n)等比数列第n个数的值 代码: #include<bits/stdc++.h> us

POJ 2635 The Embarrassed Cryptographer(高精度取模 + 同余模定理)

The Embarrassed Cryptographer Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12905   Accepted: 3472 Description The young and very promising cryptographer Odd Even has implemented the security module of a large system with thousands of