链接: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';
}
}
}

最新文章

  1. php 简单权限管理实现
  2. EhCache的配置
  3. python数据结构
  4. hdu 5534 (完全背包) Partial Tree
  5. Hibernate &lt;查询缓存&gt;
  6. C++ UFunction({FLAG}) 宏 FLAG 解释笔记
  7. Centos配置网卡
  8. HTML页面表单输入框去掉鼠标选中后边框变色的效果
  9. jQuery点击图片弹出放大可拖动图片查看
  10. Java数组排序
  11. HTML第六天学习笔记
  12. POJ 1988 Cube Stacking(带权并查集)
  13. IE6、IE7、IE8中overflow:hidden无效问题
  14. PHP小题目 求 1*3+5*7+…+97*99的值
  15. jQuery写选项卡
  16. react+redux+generation-modation脚手架添加一个todolist
  17. css样式实现立方体制作
  18. Oracle导入建表的sql文件类型
  19. Django-urls路由系统
  20. IDEA自动生成序列化ID

热门文章

  1. IFG以太网帧间隙
  2. 八、RF的内置变量
  3. flask + celery实现定时任务和异步
  4. spir 合并单元格
  5. 阶段3 1.Mybatis_09.Mybatis的多表操作_7 mybatis多对多准备角色表的实体类和映射配置
  6. 【工具安装】VMware 安装教程
  7. 我在DBGridEh增加一栏复选框及对应操作的解决方案
  8. oracle data guard --理论知识回顾01
  9. Spring源码入门——DefaultBeanNameGenerator解析 转发 https://www.cnblogs.com/jason0529/p/5272265.html
  10. cobbler批量安装系统