链接: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\n",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), '\0', 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_text\code_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 == '\n');
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';
}
}
}

牛客OI周赛11-普及组 B Game with numbers (数学,预处理真因子)的更多相关文章

  1. 牛客OI周赛9-提高组题目记录

    牛客OI周赛9-提高组题目记录 昨天晚上做了这一套比赛,觉得题目质量挺高,而且有一些非常有趣而且非常清奇的脑回路在里边,于是记录在此. T1: 扫雷 题目链接 设 \(f_i\) 表示扫到第 \(i\ ...

  2. 牛客OI周赛8-提高组A-用水填坑

    牛客OI周赛8-提高组A-用水填坑 题目 链接: https://ac.nowcoder.com/acm/contest/403/A 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制: ...

  3. 牛客OI周赛2-提高组

    A.游戏 链接:https://www.nowcoder.com/acm/contest/210/A来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 131072K,其他语 ...

  4. 牛客OI周赛7-提高组 A 小睿睿的等式

    链接:https://ac.nowcoder.com/acm/contest/371/A来源:牛客网 小睿睿在游戏开始时有n根火柴棒,他想知道能摆成形如“A+B=n”的等式且使用的火柴棒数也恰好等于n ...

  5. 牛客OI周赛7-提高组 B小睿睿的询问(ST打表)

    链接:https://ac.nowcoder.com/acm/contest/371/B来源:牛客网 小睿睿的n个妹纸排成一排,每个妹纸有一个颜值val[i].有m个询问,对于每一个询问,小睿睿想知道 ...

  6. 牛客OI周赛7-普及组 解题报告

    出题人好评. 评测机差评. A 救救喵咪 二位偏序.如果数据范围大的话直接树状数组,不过才1000就\(O(n^2)\)暴力就ok了. #include <bits/stdc++.h> s ...

  7. 牛客OI周赛10-普及组-A眼花缭乱的街市-(加速+二分)

    https://ac.nowcoder.com/acm/contest/901/A 很简单的一道题,全场只有20+AC,卡时间.新学了cin加速语法和数组二分查找的函数调用. 知道有个读写挂,可以加速 ...

  8. 补比赛——牛客OI周赛9-普及组

    比赛地址 A 小Q想撸串 题目分析 普及T1水题惯例.字符串中找子串. Code #include<algorithm> #include<iostream> #include ...

  9. 牛客OI周赛8-普及组

    https://ac.nowcoder.com/acm/contest/543#question A. 代码: #include <bits/stdc++.h> using namespa ...

随机推荐

  1. Flask基础以及Response三剑客

    Flask的特点: 优点:小而精.三方组件全    缺点: 性能相对较差   因为依赖三方组件所以在更新的时候难免不同步 基础模板 from flask import Flask app = Flas ...

  2. jest 提示 Unexpected identifier 的解决方案

    概述 今天在玩 jest 的时候,发现用 import 就会报 Unexpected identifier 的错误.查了很久的资料,最后终于解决了. 参考资料:Jest tests can't pro ...

  3. 思维导图之kubernetes

    k8s docker

  4. Selenium学习之==>常见面试题

    转自:http://www.imdsx.cn/ 一.selenium中如何判断元素是否存在? expected_conditions模块提供了多种校验方式,我常用的一种是presence_of_ele ...

  5. flutter dialog异常Another exception was thrown: Navigator operation requested with a context that does not include a Navigator

    我在使用flutter里的对话框控件的时候遇到了一个奇怪的错误 Another exception was thrown: Navigator operation requested with a c ...

  6. c++ 创建 uuid guid

    如果没安装,先安装: [root@localhost]# yum install libuuid-devel #include "uuid/uuid.h" 引用 libuuid.s ...

  7. java通过jna调用so

    c++: FirstEliteValidate.h #pragma once void __attribute__((constructor)) startup();void __attribute_ ...

  8. 实体类的[Serializable]标签造成WebAPI Post接收不到值

    WebAPI: [HttpPost] public HttpResponseMessage test([FromBody]List<Class1> list) { return Commo ...

  9. charles抓包教程

    百度搜索下载charles 默认安装即可完成 1.双击charles.exe启动,我的是4.2.7版本.最好下载原版的不要去破解中文,会有不兼容 1.搜索该软件许可证书并输入即可长期使用 2.设置代理 ...

  10. 你知道e.g.和i.e.的区别吗?

    见 i.e. 是对前面的完全举例,特指 e.g. 则是不完全举例,有可能是...也有可能是...还可能是其他. 注意,i.e. 和 e.g. 第二个点后面都常跟一个逗号.