头像

bjq

江苏省天一中学


访客:1978

离线:4天前



bjq
4天前
#include <bits/stdc++.h>

using namespace std;

int exgcd(int a, int b, int &x, int &y) {
    if (!b) {
        x = 1, y = 0;
        return a;
    }
    int d = exgcd(b, a % b, y, x);
    y -= a / b * x;
    return d;
}

int main() {
    int t, a, b, x, y;
    cin >> t;
    while (t--) {
        cin >> a >> b;
        exgcd(a, b, x, y);
        cout << x << " " << y << endl;
    }
}


活动打卡代码 AcWing 876. 快速幂求逆元

bjq
4天前
#include <bits/stdc++.h>

#define ll long long

using namespace std;

ll qpow(ll a, ll p, ll k) {
    ll ans = 1 % k;
    while (p) {
        if (p & 1) ans = (ans * a) % k;
        a = (a * a) % k;
        p >>= 1;
    }
    return ans;
}

int t, a, b;

int main() {
    cin >> t;
    while (t--) {
        cin >> a >> b;
        if (!(a % b)) puts("impossible");
        else cout << qpow(a, b - 2, b) << endl;
    }
}



bjq
6天前
#include <bits/stdc++.h>

using namespace std;

const int N = 1000010;

int primes[N], cnt, phi[N], n;
bool st[N];

int main() {
    cin >> n;

    phi[1] = 1;
    memset(st, 1, sizeof st);
    for (int i = 2; i <= n; i++) {
        if (st[i]) {
            primes[++cnt] = i;
            phi[i] = i - 1;
        } 
        for (int j = 1; primes[j] * i <= n; j++) {
            int t = primes[j] * i;
            st[t] = 0;
            if (!(i % primes[j])) {
                phi[t] = phi[i] * primes[j];
                break;
            }
            phi[t] = phi[i] * (primes[j] - 1);
        }
    }

    long long res = 0;
    for (int i = 1; i <= n; i++)
        res += phi[i];
    cout << res << endl;
}




bjq
9天前
#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int d[N], b[N], n, m, k;
bool st[N];

struct edge {
    int x, y, z;
} e[N];

void bellman_ford() {
    memset(d, 0x3f, sizeof d);
    d[1] = 0;
    for (int i = 1; i <= k; i++) {
        memcpy(b, d, sizeof d);
        for (int j = 1; j <= m; j++) 
            d[e[j].y] = min(d[e[j].y], b[e[j].x] + e[j].z);
    }
}

int main() {
    cin >> n >> m >> k;
    for (int i = 1; i <= m; i++)
        cin >> e[i].x >> e[i].y >> e[i].z;
    bellman_ford();

    if (d[n] > 0x3f3f3f3f / 2) puts("impossible");
    else cout << d[n] << endl;
}




bjq
9天前
#include <bits/stdc++.h>

using namespace std;

const int N = 510;

int g[N][N], d[N], n, m;
bool st[N];

void dijkstra() {
    memset(d, 0x3f, sizeof d);
    d[1] = 0;
    for (int i = 1; i <= n; i++) {
        int t = -1;
        for (int j = 1; j <= n; j++)
            if (!st[j] && (t == -1 || d[j] < d[t]))
                t = j;
        st[t] = 1;
        for (int j = 1; j <= n; j++)
            d[j] = min(d[j], d[t] + g[t][j]);
    }
}

int main() {
    memset(g, 0x3f, sizeof g);
    cin >> n >> m;
    while (m--) {
        int x, y, z;
        cin >> x >> y >> z;
        g[x][y] = min(g[x][y], z);
    }
    dijkstra();
    if (d[n] == 0x3f3f3f3f) puts("-1");
    else cout << d[n] << endl;
}



bjq
9天前
#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int h[N], e[N], ne[N], idx, n, m, d[N], q[N]; 

void add(int x, int y) {
    e[++idx] = y, ne[idx] = h[x], h[x] = idx;
}

bool toposort() {
    int hh = 1, tt = 0;
    for (int i = 1; i <= n; i++)
        if (!d[i]) q[++tt] = i;
    while (hh <= tt) {
        int x = q[hh++];
        for (int i = h[x]; i; i = ne[i]) {
            int y = e[i];
            if (!(--d[y]))
                q[++tt] = y;
        }
    }
    return tt == n;
}

int main() {
    cin >> n >> m;
    while (m--) {
        int x, y;
        cin >> x >> y;
        add(x, y); 
        d[y]++;
    }
    if (!toposort()) puts("-1");
    else {
        for (int i = 1; i <= n; i++)
            cout << q[i] << " ";
        cout << endl;
    }
}


活动打卡代码 AcWing 840. 模拟散列表

bjq
9天前

开放寻址法

#include <bits/stdc++.h>

using namespace std;

const int N = 200003, null = 0x3f3f3f3f;

int h[N], q;

int find(int x) {
    int k = (x % N + N) % N;
    while (h[k] != x && h[k] != null) {
        k++;
        if (k == N) k = 0;
    }
    return k;
}

int main() {
    memset(h, 0x3f, sizeof h);
    cin >> q;
    while (q--) {
        char op; int x;
        cin >> op >> x;
        if (op == 'I') h[find(x)] = x;
        else puts(h[find(x)] == null ? "No" : "Yes");
    }
}

拉链法

#include <bits/stdc++.h>

using namespace std;

const int N = 100003;

int q, e[N], h[N], ne[N], idx;

void add(int x) {
    int k = (x % N + N) % N;
    e[++idx] = x, ne[idx] = h[k], h[k] = idx;
}

bool find(int x) {
    int k = (x % N + N) % N;
    for (int i = h[k]; i; i = ne[i])
        if (e[i] == x) return 1;
    return 0;
}

int main() {
    cin >> q;
    while (q--) {
        char op; int x;
        cin >> op >> x;
        if (op == 'I') add(x);
        else puts(find(x) ? "Yes" : "No");
    }
}


活动打卡代码 AcWing 839. 模拟堆

bjq
9天前
#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int h[N], n, m, len;
int ph[N], hp[N];   /*
                     * ph -> position to heap
                     * hp -> heap to position
                     */

void hswap(int a, int b) {
    swap(ph[hp[a]], ph[hp[b]]);
    swap(hp[a], hp[b]);
    swap(h[a], h[b]);
}

void down(int u) {
    int v = u;
    if (u * 2 <= len && h[u * 2] < h[v]) v = u * 2;
    if (u * 2 + 1 <= len && h[u * 2 + 1] < h[v]) v = u * 2 + 1;
    if (v != u) {
        hswap(u, v);
        down(v);
    }
}

void up(int u) {
    while (u / 2 && h[u] < h[u / 2]) {
        hswap(u, u / 2);
        u >>= 1;
    }
}

int main() {
    cin >> m;
    while (m--) {
        string op; int k, x;
        cin >> op;
        if (op == "I") {
            cin >> x;
            len++, n++;
            ph[n] = len, hp[len] = n; 
            h[len] = x;
            up(len);
        } else if (op == "PM") 
            cout << h[1] << endl;
        else if (op == "DM") {
            hswap(1, len--);
            down(1);
        } else if (op == "D") {
            cin >> k;
            k = ph[k];
            hswap(k, len--);
            up(k); down(k);
        } else {
            cin >> k >> x;
            k = ph[k];
            h[k] = x;
            up(k); down(k);
        }
    }
}


活动打卡代码 AcWing 240. 食物链

bjq
9天前
#include <bits/stdc++.h>

using namespace std;

const int N = 100010;

int fa[N], n, q, res, d[N];

int get(int x) {
    if (x != fa[x]) {
        int f = get(fa[x]);
        d[x] += d[fa[x]];
        fa[x] = f;
    }
    return fa[x];
}

int main() {
    cin >> n >> q;
    for (int i = 1; i <= n; i++)    
        fa[i] = i;
    while (q--) {
        int op, x, y;
        cin >> op >> x >> y;
        if (x > n || y > n) {
            res++;
            continue;
        } 
        int px = get(x), py = get(y);
        if (op == 1) {
            if (px == py && (d[x] - d[y]) % 3) res++;
            else if (px != py) {
                fa[px] = py;
                d[px] = d[y] - d[x];
            }
        } else {
            if (px == py && (d[x] - d[y] - 1) % 3) res++;
            else if (px != py) {
                fa[px] = py;
                d[px] = d[y] + 1 - d[x];
            }
        }
    }
    cout << res << endl;

    return 0;
}


活动打卡代码 AcWing 143. 最大异或对

bjq
10天前
#include <bits/stdc++.h>

using namespace std;

const int N = 3000010;

int son[N][2], a[N], idx;

void insert(int x) {
    int p = 0;
    for (int i = 30; i >= 0; i--) {
        int &s = son[p][x >> i & 1];
        if (!s) s = ++idx;
        p = s;
    }
}

int ask(int x) {
    int p = 0, res = 0;
    for (int i = 30; i >= 0; i--) {
        int s = x >> i & 1;
        if (son[p][!s]) {
            res += 1 << i;
            s ^= 1;
        }
        p = son[p][s];
    }
    return res;
}

int main() {
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> a[i];
        insert(a[i]);
    }
    int res = 0;
    for (int i = 1; i <= n; i++)
        res = max(res, ask(a[i]));
    cout << res << endl;
}