UVa 11964 - Equation

contents

  1. 1. Problem
  2. 2. Sample Input
  3. 3. Sample Output
  4. 4. Solution

Problem

$x_{1} + 2 x_{2} + 4 x_{3} + 8 x_{4} + 16 x_{5} + ... + 2^{t} x_{t}= K \text{ where } x_{i} \geq 0$

找到所有組合數並且 mod M 的結果,其中 M 可以拆成數個小於 150 的質因數分解。

Sample Input

1
2
3
4
3
15 99
101 123
1234 536870912

Sample Output

1
2
3
Case 1: 26
Case 2: 111
Case 3: 176223474

Solution

其中 M 可以拆成數個小於 150 的質因數分解。

這句話可說是暗藏玄機,由於測資組數太多,對於每次 mod 情況都建表太慢。不然這題單純套用硬幣問題就能計算個數收尾。

因此,先將 M 進行質因數分解$M = \prod_{i} p_{i}^{x_{i}}$,之後用中國餘式定理求解。

由於質因數的個數可能大於 1,那其實也可以發現$\text{count mod } p_{i}$$\text{count mod } p_{i - 1}$ 之間的關係$\text{count mod } p_{i-1} = \text{count mod } p_{i} \text{ mod } p_{i-1}$

這裡可以 O(1) 算出來,而在 150 以內總共有 35 個質數,分別對這些質數計算出 mod 盡可能大$p_{i} \le 10^{15}$ 為準。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include <stdio.h>
#include <vector>
using namespace std;
#define maxL (150>>5)+1
#define GET(x) (mark[x>>5]>>(x&31)&1)
#define SET(x) (mark[x>>5] |= 1<<(x&31))
int mark[maxL];
int P[150], Pt = 0;
void sieve() {
register int i, j, k;
SET(1);
int n = 150;
for(i = 2; i <= n; i++) {
if(!GET(i)) {
for(k = n/i, j = i*k; k >= i; k--, j -= i)
SET(j);
P[Pt++] = i;
}
}
}
long long mulmod(long long a, long long b, long long mod) {
long long ret = 0;
for ( ; b != 0;b>>=1, (a<<=1) %= mod)
if (b&1) (ret += a) %= mod;
return ret;
}
long long inv(long long n, long long m) { // get n*? = 1 (mod m)
long long la = 1, lb = 0, ra = 0, rb = 1;
long long i = 0, t, mod = m;
while(n%m) {
if(!i) {
la -= n/m*ra;
lb -= n/m*rb;
} else {
ra -= n/m*la;
rb -= n/m*lb;
}
i = !i;
t = n, n = m, m = t%m;
}
return i ? (la%mod+mod)%mod : (ra%mod+mod)%mod;
}
long long chinese_remainder(int n, long long m[], long long a[]) {
long long M = 1, ret = 0;
for(int i = 0; i < n; i++)
M *= m[i];
for(int i = 0; i < n; i++) {
ret += a[i] * inv(M/m[i], m[i]) %M * (M/m[i]);
ret %= M;
}
return (ret%M + M)%M;
}
vector< pair<int, int> > factor(long long n) {
vector< pair<int, int> > R;
for(int i = 0, j; i < Pt && P[i] * P[i] <= n; i++) {
if(n%P[i] == 0) {
for(j = 0; n%P[i] == 0; n /= P[i], j++);
R.push_back(make_pair(P[i], j));
}
}
if(n != 1) R.push_back(make_pair(n, 1));
return R;
}
long long mpow(long long x, long long y) {
long long ret = 1;
for (int i = 0; i < y; i++)
ret *= x;
return ret;
}
vector<long long> dp[37];
int main() {
sieve();
for (int i = 0; i < Pt; i++) {
long long m = P[i];
int p = 0;
for (m = P[i], p = 0; m * P[i] <= 1e+15; p++, m *= P[i]);
dp[i].resize(100005, 0);
dp[i][0] = 1;
for (int j = 1; j <= 100000; j <<= 1) {
for (int k = j; k <= 100000; k++)
dp[i][k] = (dp[i][k] + dp[i][k - j])%m;
}
}
int testcase, cases = 0;
scanf("%d", &testcase);
while (testcase--) {
int K;
long long M;
scanf("%d %lld", &K, &M);
vector< pair<int, int> > f = factor(M);
long long m[35], a[35];
for (int i = 0; i < f.size(); i++) {
for (int j = 0; j < Pt; j++)
if (f[i].first == P[j])
m[i] = mpow(P[j], f[i].second), a[i] = dp[j][K] % m[i];
}
long long ret = chinese_remainder(f.size(), m, a);
printf("Case %d: %lld\n", ++cases, ret);
}
return 0;
}