zoukankan      html  css  js  c++  java
  • Kattis之旅——Number Sets

    You start with a sequence of consecutive integers. You want to group them into sets.

    You are given the interval, and an integer P. Initially, each number in the interval is in its own set.

    Then you consider each pair of integers in the interval. If the two integers share a prime factor which is at least P, then you merge the two sets to which the two integers belong.

    How many different sets there will be at the end of this process?

    Input

    One line containing an integer C, the number of test cases in the input file.

    For each test case, there will be one line containing three single-space-separated integers A, B, and P. A and B are the first and last integers in the interval, and P is the number as described above.

    Output

    For each test case, output one line containing the string "Case #X: Y" where X is the number of the test case, starting from 1, and Y is the number of sets.

    Limits

    Small dataset

    1 <= C <= 10

    1 <= A <= B <= 1000

    2 <= P <= B

    Large dataset

    1 <= C <= 100

    1 <= A <= B <= 1012

    B <= A + 1000000

    2 <= P <= B

     Sample Input 1Sample Output 1
    2
    10 20 5
    10 20 3
    
    Case #1: 9
    Case #2: 7
    

    题目大概意思就是——给你一个范围A到B,范围中每个数就是一个集合,再给你一个素数P,如果这个范围的两个数有大于或者等于P的素数因子,那么合并两个数所在的集合作为一个集合。

    并查集。

    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    typedef long long ll;
    
    static bool test_prime(ll p)
    {
        if (p < 2) return false;
        for (ll i = 2; i * i <= p; i++)
            if (p % i == 0)
                return false;
        return true;
    }
    
    static int parent[1000001];
    
    static int root(int x)
    {
        if (parent[x] < 0)
            return x;
        else
            return parent[x] = root(parent[x]);
    }
    
    static void merge(int a, int b)
    {
        a = root(a);
        b = root(b);
        if (a == b) return;
        if (parent[a] > parent[b])
            swap(a, b);
        parent[a] += parent[b];
        parent[b] = a;
    }
    
    int main()
    {
        int cases;
        cin >> cases;
    
        for (int cas = 0; cas < cases; cas++)
        {
            ll A, B, P;
            cin >> A >> B >> P;
    
            for (ll i = A; i <= B; i++)
                parent[i - A] = -1;
            for (ll i = P; i <= B - A; i++)
                if (test_prime(i))
                {
                    ll t = B - B % i;
                    while (t - i >= A)
                    {
                        merge(t - A, t - i - A);
                        t -= i;
                    }
                }
            ll ans = 0;
            for (ll i = A; i <= B; i++)
                if (parent[i - A] < 0)
                    ans++;
            cout << "Case #" << cas + 1 << ": " << ans << "
    ";
        }
        return 0;
    }
  • 相关阅读:
    利用avicap32.dll实现的实时视频传输
    异常错误:在可以调用 OLE 之前,必须将当前线程设置为单线程单元(STA)模式
    很不错的python 机器学习资源
    基于C#的机器学习--目录
    C#WinForm无边框窗体移动----模仿鼠标单击标题栏移动窗体位置
    C# WinForm窗体控件GroupBox修改边框颜色控件
    wireshark抓包新手使用教程
    Winform开发框架之权限管理系统功能介绍
    自定义控件开发的调试及DesignMode的状态处理
    Winform开发框架之权限管理系统改进的经验总结(4)--用户分级管理
  • 原文地址:https://www.cnblogs.com/Asimple/p/6773230.html
Copyright © 2011-2022 走看看