社区讨论

神秘现象:不小心交了EXCTR结果过了几个样例

P4720【模板】扩展卢卡斯定理 / exLucas参与者 4已保存回复 6

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
6 条
当前快照
1 份
快照标识符
@mhj04njw
此快照首次捕获于
2025/11/03 18:33
4 个月前
此快照最后确认于
2025/11/03 18:33
4 个月前
查看原帖
没看清楚题不小心交了EXCTR的代码结果过了几个样例,这是怎么回事?(我还复制错了,交了一份测试时的大杂烩代码) 是神秘特解还是纯巧合 以下是提交时代码(微改,但功能一样):
CPP
#include <bits/stdc++.h>
typedef __int128_t ll;
using namespace std;
ll a[100005], b[100005];

int read() {
    int ans = 0, f = 1;
    char c = getchar();
    while (!isdigit(c)) { if (c == '-') f = -1; c = getchar(); }
    while (isdigit(c)) { ans = (ans<<3) + (ans<<1) + (c-'0'); c = getchar(); }
    return f * ans;
}

void write(ll x) {
   static int sta[130];
   int top = 0;
   do {
       sta[top++] = x % 10;
       x /= 10;
   } while (x);
   while (top) putchar(sta[--top] + '0');
   putchar('\n');
}

ll exgcd(ll a, ll b, ll& x, ll& y) {
    if (b == 0) {
        x = 1, y = 0;
        return a;
    }
    ll ret = exgcd(b, a % b, x, y), z = x;
    x = y, y = z - (a / b) * y;
    return ret;
}

ll gcd(ll a, ll b) {
    if (b == 0) return a;
    return gcd(b, a % b);
    
}

int main () {
    ios::sync_with_stdio(false);
    cin.tie(nullptr), cout.tie(nullptr);
    ll n, m = 1, ans, M = 1;
    n = read();
    for (int i = 1; i <= n; ++i)
        a[i] = read(), b[i] = read();
    for (int i = 1; i <= n; ++i) {
        ll x, y;
        exgcd(m, a[i], x, y);
        ans = (ans + ((x * (b[i] - ans) / gcd(m, a[i])) * m) );
        m *= a[i] / gcd(m, a[i]);
    }
    write((ans % m + m) % m);
    return 0;
}

回复

6 条回复,欢迎继续交流。

正在加载回复...