没错 (3) 还欠着,由于学的没什么问题,所以不想写了,等未来吧。 话说学习报告这个名字是不是应该改成学习笔记?但是这个名字更加独特,学习笔记到处都是,学习报告仅此一家😋😋😋
这篇报告主要写中国剩余定理 (Chinese Remainder Theorem, CRT) 及其拓展,OI Wiki 上讲的半懵半懂,董晓老师讲的也差一点,所以想着把自己理解的方式写出来。
中国剩余定理(CRT):
中国剩余定理主要处理这样一个一元线性方程组(当然所有 两两互质):
我们知道余数的可加性:
当然它还可以这么写:
按照这种写法,我们知道,我们要求的答案(记为 )就可以转化为:
那么我们只要分别求出 就行了,我们拿求 做个示范:
这个应该还算好理解,这个就是余数的可乘性,比如说:。那么对于这样一个线性同余方程,我们能想到把它转化为线性不定方程……不是,说好的 呢, 搁着乘空气呢? 其实不然,我们知道, 模上其他的 都为 0,说明它是其他 的倍数,设为 ,那么 ,就有不定方程 ,我们知道 ,所以可以直接用拓展欧几里得求 ,那么 ,把它加上就行了,当然这样做最后我们要模加模算出最小正整数解。
另外笑点解析:有个人曾把柿子化为:,去求 ,最后发现它给我求出来一组特解名为 1)然后 ta 选择只要不满足条件就一直加 来求通解,最后满足条件就乘上 ,这样甚至拿了 60 分,是谁我不说。所以我们看到上一段的做法好处是求出来的特解满足所有条件,而不需要一直增加求别的通解。
它的模板题很坑,过程量很可能爆 long long,这里使用龟速乘防止出事:
//Just Sayori
#pragma GCC diagnostic ignored "-Wnarrowing"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <iostream>
#include <cstdio>
#include <algorithm>
#define ll long long
#define rnt register int
#define gr getchar
#define pr putchar
#define N 5000005
//#define M 1000000007
using namespace std;
inline ll read()
{
ll x = 0, f = 1;
char ch = gr();
while (ch < '0' || ch > '9') ch == '-' ? f = -1, ch = gr() : ch = gr();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = gr();
return x * f;
}
inline void write(ll x)
{
static int top = 0, sta[39];
if (x < 0) pr('-'), x = -x;
do sta[++top] = x % 10, x /= 10;
while (x);
while (top) pr(sta[top--] ^ 48);
}
ll n, ans;
ll M = 1;
ll a[N], m[N];
void get_gcd(ll a, ll b, ll &x, ll &y)
{
if (!b)
return x = 1, y = 0, void();
get_gcd(b, a % b, y, x);
y -= a / b * x;
}
ll ms(ll a, ll b)
{
ll ans = 0;
while (b)
{
if (b & 1) (ans += a) %= M;
(a += a) %= M;
b >>= 1;
}
return ans;
}
int main()
{
n = read();
for (rnt i = 1; i <= n; i++) m[i] = read(), a[i] = read(), M *= m[i];
for (rnt i = 1; i <= n; i++)
{
ll c = M / m[i], x, y;
get_gcd(c, m[i], x, y);
x = (x % M + M) % M;
ans += ms(ms(c, x), a[i]);
}
write((ans % M + M) % M);
return 0;
}拓展中国剩余定理(exCRT):
真是万物皆可拓展,之前的中国剩余定理要求每个 都两两互质,不然用不了。为什么?因为 不一定与 互质(这里 是除了 以外其他 的乘积),所以你不一定能找到它的逆元 。所以我们需要一个新的方法。
我们设两个线性同余方程:
则有:。 移项为:。 根据拓展欧几里得定理,我们知道这个不定方程有解仅限于 ,原因由裴蜀定理可知:只要 ,存在一组 使得。当然模版题是保证有解的,接下来我们思考一下如果有解该怎么做。
……
思考个啥啊,跟我的 exgcd 说去吧,我们直接用拓展欧几里得算法求出一组特解 ,当然这是余数为 的,我们再让它变成余数为 的特解:。然后我们求出通解:,注意这里的 只是个常数。代入得到 。
然后我们将这两个柿子合并成一个,具体如何合并呢,我们发现这个式子就相当于 ,所以我们把 更新成 , 更新为 ,这样合并 次就行了,复杂度是 的。
但这东西的细节还挺多的:
为什么要求通解?明明直接用特解就好了
因为求特解能求出模数,如这里求出模数等于 ,然后好取模。那为什么要取模?因为 exgcd 求出来的特解不一定是最小整数解,还可能是负数,我们要用模加模来处理。
模板题还要用龟速乘,但这又很坑: 不一定是正数,慢速乘会出错。所以我们也要用模加模来处理,但是模什么呢?我想(翻题解)了很久。后来我想明白(翻到证明)了:
我们要求一个数等于 ,那么:
所以模的就是 !当然这个柿子下一步就是把 提出来。
最后答案就是最后合并剩下的柿子的余数,这个就是最小整数解。
另外注意 ,前者比后者快很多。 最后给出代码:
//Just Sayori
#pragma GCC diagnostic ignored "-Wnarrowing"
#pragma GCC diagnostic ignored "-Wsign-compare"
#include <iostream>
#include <cstdio>
#include <algorithm>
#define ll long long
#define rnt register int
#define gr getchar
#define pr putchar
#define N 5000005
#define M 1000000007
using namespace std;
inline ll read()
{
ll x = 0, f = 1;
char ch = gr();
while (ch < '0' || ch > '9') ch == '-' ? f = -1, ch = gr() : ch = gr();
while (ch >= '0' && ch <= '9') x = (x << 3) + (x << 1) + (ch ^ 48), ch = gr();
return x * f;
}
inline void write(ll x)
{
static int top = 0, sta[39];
if (x < 0) pr('-'), x = -x;
do sta[++top] = x % 10, x /= 10;
while (x);
while (top) pr(sta[top--] ^ 48);
}
ll n, x, y, ans;
ll a1, a2, m1, m2;
ll a[N], m[N];
ll get_gcd(ll a, ll b, ll &x, ll &y)
{
if (!b)
return x = 1, y = 0, a;
ll d = get_gcd(b, a % b, y, x);
y -= a / b * x;
return d;
}
ll ms(ll a, ll b, ll m)
{
ll ans = 0;
while (b)
{
if (b & 1) (ans += a) %= m;
(a += a) %= m;
b >>= 1;
}
return ans;
}
int main()
{
n = read();
for (rnt i = 1; i <= n; i++) m[i] = read(), a[i] = read();
a1 = a[1], m1 = m[1];
for (rnt i = 2; i <= n; i++)
{
a2 = a[i], m2 = m[i];
ll d = get_gcd(m1, m2, x, y), c = ((a2 - a1) % m2 + m2) % m2;
x = ms(x, c / d, m2 / d);
x = (x % (m2 / d) + (m2 / d)) % (m2 / d);
a1 += m1 * x;
m1 *= m2 / d;
a1 = (a1 % m1 + m1) % m1;
}
write((a1 % m1 + m1) % m1);
return 0;
}