Codeforces 338D GCD Table [中国剩余定理]

Description:
给一个 nm n ∗ m 的数表,第 i i j列的元素是 gcd(i,j) g c d ( i , j ) 。再给出一行数,问这行数是否出现在数表中。


Solution:
根据结论,我们知道行数是 lcm(a1,...,ak) l c m ( a 1 , . . . , a k ) ,然后我们列出方程,用 crt c r t 合并,这样可以解出列数。
代码抄了一份,并没有搞懂 crt c r t ,留坑。


#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n, m, L = 1, x, lcm, ans;
int k;
ll a[100005];
ll mul(ll x, ll y, ll P) {
    ll ret = 0;
    for(y = (y % P + P) % P; y; y >>= 1, x = (x + x) % P) {
        if(y & 1) {
            ret = (ret + x) % P;
        }
    }
    return ret;
} 
ll exgcd(ll a, ll b, ll &x, ll &y) {
    if(!b) {
        x = 1;
        y = 0;
        return a;
    }
    ll ret = exgcd(b, a % b, y, x);
    y -= (a / b) * x;
    return ret;
}
int main() {
    scanf("%lld%lld%d", &n, &m, &k);
    for(int i = 1; i <= k; ++i) {
        scanf("%lld", &a[i]);
        lcm = lcm / __gcd(a[i], lcm) * a[i];
        ll x, y, d = exgcd(L, a[i], x, y);
        if((ans + (i - 1) % a[i]) % d != 0 || L / d * a[i] > n) {
            return puts("NO"), 0;
        }
        x *= (ans + (i - 1) % a[i]) / d;
        ans = ans - mul(L, x, L / d * a[i]);
        L = L / d * a[i];
        ans = (ans % L + L) % L;
    }
    if(ans == 0) {
        ans = L;
    }
    if(ans + k - 1 > m) {
        return puts("NO"), 0;
    } 
    for(int i = 1; i <= k; ++i) {
        if(a[i] != __gcd(L, ans + i - 1)) {
            return puts("NO"), 0;
        }
    }
    puts("YES");
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值