Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P3868 [TJOI2009] 猜数字

P3868 [TJOI2009] 猜数字

$$
\begin {array}{c}
& b _ i | (n - a _ i) \\
\Longrightarrow & n - a _ i = k b _ i \\
\Longrightarrow & a _ i \equiv n (\mod b _ i)
\end {array}
$$
直接 CRT 。

查看代码
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
#include <iostream>
#include <cstdio>
using namespace std;
typedef long long LL;
const int N = 11;
int n;
LL res, m = 1, A[N], B[N];
LL mul (LL x, LL k)
{
LL res = 0, b = x;
while (k)
{
if (k & 1)
res = (res + b) % m;
b = b * 2 % m;
k >>= 1;
}
return res;
}
LL exgcd (LL a, LL b, LL& x, LL& y)
{
if (!b)
{
x = 1, y = 0;
return a;
}
LL d = exgcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
LL inv (LL a, LL b)
{
LL x, y;
exgcd(a, b, x, y);
return (x % b + b) % b;
}
int main ()
{
scanf("%d", &n);
for (int i = 1; i <= n; i++)
scanf("%lld", &A[i]);
for (int i = 1; i <= n; i++)
{
scanf("%lld", &B[i]);
m *= B[i];
}
for (int i = 1; i <= n; i++)
res = (res + mul(mul(A[i], (m / B[i])), inv(m / B[i], B[i]))) % m;
printf("%lld", res);
return 0;
}