头像

zxxxz___




离线:4小时前


最近来访(9)
用户头像
Chen_acwing_1024
用户头像
天元之弈
用户头像
Fatin
用户头像
夜寐
用户头像
纵横
用户头像
hellojim
用户头像
Hollow

活动打卡代码 AcWing 872. 最大公约数

zxxxz___
9小时前
#include <iostream>

using namespace std;

int n;

int gcd(int a, int b)
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
    cin >> n;
    while(n --)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        cout << gcd(a, b) << endl;
    }

    return 0;
}


活动打卡代码 AcWing 871. 约数之和

#include <iostream>
#include <unordered_map>

using namespace std;

typedef long long LL;

const int N = 110, mod = 1e9 + 7;

int main()
{
    int n;
    cin >> n;

    unordered_map<int, int>primes;

    while(n --)
    {
        int x;
        cin >> x;

        for(int i = 2; i <= x / i; i ++)
            while(x % i == 0)
            {
                x /= i;
                primes[i] ++;
            }

        if(x > 1) primes[x] ++;
    }

    LL res = 1;
    for(auto p : primes)
    {
        LL a = p.first, b = p.second;
        LL t = 1;
        while(b --) t = (t * a + 1) % mod;
        res = res * t % mod;
    }
    cout << res << endl;

    return 0;
}


活动打卡代码 AcWing 870. 约数个数

    思路:约数个数等于,所有质因数的(质因数的指数+1)相乘积
    先分解所有数为质因数,并将质因数和指数存入哈希表。
    最后将所有指数+1并相乘
#include <iostream>
#include <unordered_map>

using namespace std;

typedef long long LL;

const int MOD = 1e9 + 7;

int n, x;

int main()
{
    cin >> n;
    unordered_map<int, int> primes;
    while(n --)
    {
        scanf("%d", &x);
        for(int i = 2; i <= x / i; i ++)
            while(x % i == 0)
            {
                primes[i] ++;
                x /= i;
            }
        if(x > 1) primes[x] ++;
    }

    LL res = 1;

    for(auto t : primes)
        res = res * (t.second + 1) % MOD;

    cout << res << endl;

    return 0;
}


活动打卡代码 AcWing 869. 试除法求约数

#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

vector<int> get_divisors(int x)
{
    vector<int> res;
    for (int i = 1; i <= x / i; i ++ )
        if (x % i == 0)
        {
            res.push_back(i);
            if (i != x / i) res.push_back(x / i);
        }
    sort(res.begin(), res.end());
    return res;
}

int main()
{
    int n;
    cin >> n;

    while (n -- )
    {
        int x;
        cin >> x;
        auto res = get_divisors(x);

        for (auto x : res) cout << x << ' ';
        cout << endl;
    }

    return 0;
}


活动打卡代码 AcWing 868. 筛质数

#include <iostream>

using namespace std;

const int N = 1e6 + 10;

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

void get_primes(int n)
{
    for(int i = 2; i <= n; i ++)
    {
        if(!st[i])
        {
            primes[cnt ++] = n;
            for(int j = i + i; j <= n; j += i) st[j] = true;
        }
    }
}

int main()
{
    cin >> n;
    get_primes(n);
    cout << cnt << endl;

    return 0;
}


活动打卡代码 AcWing 867. 分解质因数

#include <iostream>

using namespace std;

void divide(int x)
{
    if(x < 2)
    {
        puts("");
        return;
    }
    for(int i = 2; i <= x / i; i ++)
    {
        int s = 0;
        if(x % i == 0)
        {
            while(x % i == 0)
            {
                s ++;
                x /= i;
            }
            cout << i << ' ' << s << endl;
        }
    }
    if(x > 1) cout << x << ' ' << "1" << endl;
    puts("");
}

int main()
{
    int n;
    cin >> n;
    while(n --)
    {
        int x; 
        cin >> x;
        divide(x);
    }
    return 0;
}



#include <iostream>

using namespace std;

int n;

bool f(int x)
{
    if (x < 2) return false;
    for(int i = 2; i <= x / i; i ++)
    {
        if(x % i == 0)
            return false;
    }
    return true;
}

int main()
{
    cin >> n;
    for(int i = 0; i < n; i ++)
    {
        int x;
        scanf("%d", &x);
        if(f(x)) puts("Yes");
        else puts("No");
    }
    return 0;
}


活动打卡代码 AcWing 854. Floyd求最短路

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 210, INF = 1e9 + 10;

int n, m, q;
int d[N][N];

void floyd()
{
    for(int k = 1; k <= n; k ++)
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main()
{
    cin >> n >> m >> q;

    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= n; j ++)
            if(i == j) d[i][j] = 0;
            else d[i][j] = INF;

    while(m --)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        d[a][b] = min(d[a][b], c);
    }

    floyd();

    while(q --)
    {
        int a, b;
        scanf("%d%d", &a, &b);
        int t = d[a][b];
        if(t > INF / 2) puts("impossible");
        else printf("%d\n", t);
    }

    return 0;
}


活动打卡代码 AcWing 854. Floyd求最短路

#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 210, INF = 1e9;

int n, m, Q;
int d[N][N];

void floyed()
{
    for(int k = 1; k <= n; k ++)
        for(int i = 1; i <= n; i ++)
            for(int j = 1; j <= n; j ++)
                d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
}

int main()
{
    scanf("%d%d%d", &n, &m, &Q);

    for(int i = 1; i <= n; i ++)
        for(int j = 1; j <= n; j ++)
            if(i == j) d[i][j] = 0;
            else d[i][j] = INF;

    while(m --)
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        d[a][b] = min(d[a][b], c);
    }

    floyed();

    while(Q --)
    {
        int a, b;
        scanf("%d%d", &a, &b);

        int t = d[a][b];
        if(t > INF / 2) puts("impossible");
        else printf("%d\n", t);
    }

    return 0;
}


活动打卡代码 AcWing 852. spfa判断负环

#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

const int N = 2010, M = 10010;

int n, m;
int h[N], w[M], e[M], ne[M], idx;
int dist[N], cnt[N];
bool st[N];

void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}

bool spfa()
{
    queue<int> q;

    for (int i = 1; i <= n; i ++ )
    {
        st[i] = true;
        q.push(i);
    }

    while (q.size())
    {
        int t = q.front();
        q.pop();

        st[t] = false;

        for (int i = h[t]; i != -1; i = ne[i])
        {
            int j = e[i];
            if (dist[j] > dist[t] + w[i])
            {
                dist[j] = dist[t] + w[i];
                cnt[j] = cnt[t] + 1;

                if (cnt[j] >= n) return true;
                if (!st[j])
                {
                    q.push(j);
                    st[j] = true;
                }
            }
        }
    }

    return false;
}

int main()
{
    scanf("%d%d", &n, &m);

    memset(h, -1, sizeof h);

    while (m -- )
    {
        int a, b, c;
        scanf("%d%d%d", &a, &b, &c);
        add(a, b, c);
    }

    if (spfa()) puts("Yes");
    else puts("No");

    return 0;
}