• 牛客OI周赛11-普及组 B Game with numbers (数学,预处理真因子)


    链接:https://ac.nowcoder.com/acm/contest/942/B
    来源:牛客网

    Game with numbers
    时间限制:C/C++ 1秒,其他语言2秒
    空间限制:C/C++ 262144K,其他语言524288K
    64bit IO Format: %lld
    题目描述
    给定大小为
    n
    n的集合
    S
    S
    一个数
    x
    x被称为合法的,当且仅当
    x

    S
    x∈S或者
    x
    x存在大于
    1
    1的真因数,且这些真因数均是合法的
    求有多少个数
    x
    x满足
    2

    x

    m
    2≤x≤m且
    x
    x合法
    PS:
    x
    x的真因数指不是
    x
    x本身的约数
    输入描述:
    第一行数据组数
    T
    T,表示共
    T
    T组数据
    对于每组数据
    第一行数字
    n
    ,
    m
    n,m,含义如上文所示
    接下来一行
    n
    n个数字,表示
    S
    S中的元素
    输出描述:
    对于每组数据输出一行一个数字表示答案
    示例1
    输入
    复制
    1
    3 10
    2 3 4
    输出
    复制
    6
    说明
    2
    ,
    3
    ,
    4
    ,
    6
    ,
    8
    ,
    9
    2,3,4,6,8,9是合法的
    备注:
    对于
    20
    %
    20%的数据,
    1

    n
    ,
    m

    100
    1≤n,m≤100
    对于
    50
    %
    50%的数据,
    1

    n
    ,
    m

    1000
    1≤n,m≤1000
    对于
    100
    %
    100%的数据,
    1

    n
    <
    m

    3
    ×
    10
    5
    1≤n<m≤3×105,
    2

    S
    i

    m
    2≤Si≤m,
    S
    i
    Si互不相同,
    1

    T

    5
    1≤T≤5

    题意:

    思路:

    预处理真因子个数。
    时间复杂度 nlogn ,代码里有注释。

    细节见代码:

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    #include <cmath>
    #include <queue>
    #include <stack>
    #include <map>
    #include <set>
    #include <vector>
    #include <iomanip>
    #define ALL(x) (x).begin(), (x).end()
    #define rt return
    #define dll(x) scanf("%I64d",&x)
    #define xll(x) printf("%I64d
    ",x)
    #define sz(a) int(a.size())
    #define all(a) a.begin(), a.end()
    #define rep(i,x,n) for(int i=x;i<n;i++)
    #define repd(i,x,n) for(int i=x;i<=n;i++)
    #define pii pair<int,int>
    #define pll pair<long long ,long long>
    #define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
    #define MS0(X) memset((X), 0, sizeof((X)))
    #define MSC0(X) memset((X), '', sizeof((X)))
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define eps 1e-6
    #define gg(x) getInt(&x)
    #define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
    using namespace std;
    typedef long long ll;
    ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
    ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
    ll powmod(ll a, ll b, ll MOD) {ll ans = 1; while (b) {if (b % 2)ans = ans * a % MOD; a = a * a % MOD; b /= 2;} return ans;}
    inline void getInt(int* p);
    const int maxn = 1000010;
    const int inf = 0x3f3f3f3f;
    /*** TEMPLATE CODE * * STARTS HERE ***/
    const int M = 3e5;
    int vis[maxn];
    int n, m, x;
    int a[maxn];
    int main()
    {
        //freopen("D:\common_text\code_stream\in.txt","r",stdin);
        //freopen("D:\common_textcode_stream\out.txt","w",stdout);
        // vis[i] 代表 数字i的真因数的个数
        // 如果i是质数,那么vis[i]应该设为无限大,才方便处理本题。
        repd(i, 2, M)
        {
            for (int j = 2 * i; j <= M; j += i)
            {
                vis[j]++;
            }
            if (!vis[i])
            {
                vis[i] = inf; // prime
            }
        }
        int t;
        gbtb;
        cin >> t;
        while (t--)
        {
    
            cin >> n >> m;
            repd(i, 2, m)
            {
                a[i] = vis[i];
            }
            repd(i, 1, n)
            {
                cin >> x;
                a[x] = 0; // 把集合中的数赋值为0,
            }
            int ans = 0;
            for (int i = 2; i <= m; ++i)
            {
                if (a[i] <= 0)
                {
                    for (int j = 2 * i; j <= m; j += i) // 枚举合法数字的倍数
                    {
                        vis[j]--;// 合法数字的倍数j中,去掉真因子i的影响。
                    }
                    ans++;// 答案加上 i作为合法数字的贡献。
                }
            }
            cout << ans << endl;
        }
    
    
    
    
        return 0;
    }
    
    inline void getInt(int* p) {
        char ch;
        do {
            ch = getchar();
        } while (ch == ' ' || ch == '
    ');
        if (ch == '-') {
            *p = -(getchar() - '0');
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 - ch + '0';
            }
        }
        else {
            *p = ch - '0';
            while ((ch = getchar()) >= '0' && ch <= '9') {
                *p = *p * 10 + ch - '0';
            }
        }
    }
    
    
    
    
    
    本博客为本人原创,如需转载,请必须声明博客的源地址。 本人博客地址为:www.cnblogs.com/qieqiemin/ 希望所写的文章对您有帮助。
  • 相关阅读:
    C#网络编程系列(两)它Socket同步TCPserver
    [LeetCode] ZigZag Conversion [9]
    设计模式迭代器模式
    Android_WebServices_介绍
    (UML两个汇总)九种图。
    你不明白 String 类别
    Mockito使用注意事项
    Xcode6为什么干掉pch(Precompile Prefix Header)&amp;怎样加入pch文件
    atoi()函数的实现
    多种方法求解八数码问题
  • 原文地址:https://www.cnblogs.com/qieqiemin/p/11164955.html
Copyright © 2020-2023  润新知