【HDOJ】5446 Unknown Treasure

1. 题目描述
题目很简单,就是求$C(n,m) % M$。

2. 基本思路
这是一道应用了众多初等数论定理的题目,因为数据范围较大因此使用Lucas求$C(n,m) % P$。
而M较大,因此通过$a[i] = C(n,m)%P_i$再综合中国剩余定理可解。由于数据可能为$10^{18}$,再进行乘法可能超long long。
因此,还需要模拟乘法。

3. 代码

  1 /* 5446 */
  2 #include <iostream>
  3 #include <sstream>
  4 #include <string>
  5 #include <map>
  6 #include <queue>
  7 #include <set>
  8 #include <stack>
  9 #include <vector>
 10 #include <deque>
 11 #include <bitset>
 12 #include <algorithm>
 13 #include <cstdio>
 14 #include <cmath>
 15 #include <ctime>
 16 #include <cstring>
 17 #include <climits>
 18 #include <cctype>
 19 #include <cassert>
 20 #include <functional>
 21 #include <iterator>
 22 #include <iomanip>
 23 using namespace std;
 24 //#pragma comment(linker,"/STACK:102400000,1024000")
 25
 26 #define sti                set<int>
 27 #define stpii            set<pair<int, int> >
 28 #define mpii            map<int,int>
 29 #define vi                vector<int>
 30 #define pii                pair<int,int>
 31 #define vpii            vector<pair<int,int> >
 32 #define rep(i, a, n)     for (int i=a;i<n;++i)
 33 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 34 #define clr                clear
 35 #define pb                 push_back
 36 #define mp                 make_pair
 37 #define fir                first
 38 #define sec                second
 39 #define all(x)             (x).begin(),(x).end()
 40 #define SZ(x)             ((int)(x).size())
 41 #define lson            l, mid, rt<<1
 42 #define rson            mid+1, r, rt<<1|1
 43
 44 typedef long long LL;
 45 const int maxn = 1e5+15;
 46 LL fact[maxn];
 47 LL P[15], a[15];
 48 LL n, m;
 49 int k;
 50
 51 void init_fact(LL mod) {
 52     fact[0] = 1;
 53     rep(i, 1, maxn) fact[i] = fact[i-1] * i % mod;
 54 }
 55
 56 LL Pow(LL base, LL n, LL mod) {
 57     LL ret = 1;
 58
 59     while (n) {
 60         if (n & 1)
 61             ret = ret * base % mod;
 62         base = base * base % mod;
 63         n >>= 1;
 64     }
 65
 66     return ret;
 67 }
 68
 69 inline LL Inv(LL a, LL mod) {
 70     return Pow(a, mod-2, mod);
 71 }
 72
 73 LL C(LL n, LL m, LL mod) {
 74     if (n < m)    return 0;
 75     #ifndef ONLINE_JUDGE
 76     assert(n >= m);
 77     #endif
 78     return fact[n] * Inv(fact[n-m]*fact[m]%mod, mod) % mod;
 79 }
 80
 81 LL Lucas(LL n, LL m, LL mod) {
 82     if (m == 0)    return 1;
 83     return C(n%mod, m%mod, mod) * Lucas(n/mod, m/mod, mod) % mod;
 84 }
 85
 86 void egcd(LL a, LL b, LL& d, LL& x, LL& y) {
 87     if (!b) {
 88         d = a;
 89         x = 1;
 90         y = 0;
 91     } else {
 92         egcd(b, a%b, d, y, x);
 93         y -= a/b * x;
 94     }
 95 }
 96
 97 LL Mul(LL base, LL n, LL mod) {
 98     LL ret = 0;
 99
100     while (n) {
101         if (n & 1)
102             ret = (ret + base) % mod;
103         base = (base + base) % mod;
104         n >>= 1;
105     }
106
107     return ret;
108 }
109
110 LL china(int n, LL *a, LL *m) {
111     LL M = 1, w, d, x = 0, y;
112     LL tmp;
113     bool sign;
114
115     rep(i, 0, n) M *= m[i];
116     rep(i, 0, n) {
117         w = M/m[i];
118         egcd(m[i], w, d, d, y);
119         sign = y < 0;
120         tmp = Mul(w, abs(y), M);
121         tmp = Mul(tmp, a[i], M);
122         if (sign) tmp = -tmp;
123         x = (x + tmp) % M;
124     }
125
126     return (x + M) % M;
127 }
128
129 void solve() {
130     rep(i, 0, k) {
131         init_fact(P[i]);
132         a[i] = Lucas(n, m, P[i]);
133     }
134
135     LL ans = china(k, a, P);
136     printf("%I64d\n", ans);
137 }
138
139 int main() {
140     cin.tie(0);
141     ios::sync_with_stdio(false);
142     #ifndef ONLINE_JUDGE
143         freopen("data.in", "r", stdin);
144         freopen("data.out", "w", stdout);
145     #endif
146
147     int t;
148
149     scanf("%d", &t);
150     while (t--) {
151         scanf("%I64d%I64d%d", &n,&m,&k);
152         rep(i, 0, k)
153             scanf("%I64d", &P[i]);
154         solve();
155     }
156
157     #ifndef ONLINE_JUDGE
158         printf("time = %ldms.\n", clock());
159     #endif
160
161     return 0;
162 }
时间: 2024-12-19 14:13:39

【HDOJ】5446 Unknown Treasure的相关文章

【HDOJ】1448 The Treasure

这就是个简单的bfs.真没什么好说的,三维的状态就可以了.每次预处理一下monster的位置,然后再恢复. 1 /* 1924 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!

状态压缩+优先级bfs. 1 /* 1818 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXM 105 11 12 typedef struct {

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t

【HDOJ】2425 Hiking Trip

优先级队列+BFS. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 #define MAXN 25 8 9 typedef struct node_st { 10 int x, y, t; 11 node_st() {} 12 node_st(int xx, int yy, int tt)

【HDOJ】1686 Oulipo

kmp算法. 1 #include <cstdio> 2 #include <cstring> 3 4 char src[10005], des[1000005]; 5 int next[10005], total; 6 7 void kmp(char des[], char src[]){ 8 int ld = strlen(des); 9 int ls = strlen(src); 10 int i, j; 11 12 total = i = j = 0; 13 while (