题意:给一个矩阵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