题意:给你一个集合,让你找到一个最大的子集,使得子集里面的元素的gcd!=1,输出子集的元素的个数。

思路:先将集合里面的元素哈希,再线性筛。

PS:明明以前做过类似的题的,结果又忘了,卡了好久最后打表过的。

 1 #include <iostream>
 2 #include <queue>
 3 #include <stack>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <bitset>
 9 #include <algorithm>
10 #include <cmath>
11 #include <cstring>
12 #include <cstdlib>
13 #include <string>
14 #include <sstream>
15 #include <time.h>
16 #define x first
17 #define y second
18 #define pb push_back
19 #define mp make_pair
20 #define lson l,m,rt*2
21 #define rson m+1,r,rt*2+1
22 #define mt(A,B) memset(A,B,sizeof(A))
23 #define mod 1000000007
24 using namespace std;
25 typedef long long LL;
26 const double PI = acos(-1);
27 const int N=1e5+10;
28 const int inf = 0x3f3f3f3f;
29 const LL INF=0x3f3f3f3f3f3f3f3fLL;
30 int vis[N],vik[N],ans=-inf;
31 void solve(int n)
32 {
33     int p=0;
34     mt(vis,0);
35     for(int i=2;i<=n;i++)
36     {
37         if(!vis[i])
38         {
39             p=0;
40             for(int j=i;j<=n;j+=i)
41             {
42                 vis[j]=1;
43                 p+=vik[j];
44             }
45             ans=max(ans,p);
46         }
47     }
48 }
49 int main()
50 {
51 #ifdef Local
52     freopen("data.txt","r",stdin);
53 #endif
54     int n,x,maxn=-inf;
55     cin>>n;
56     for(int i=0;i<n;i++)
57     {
58         cin>>x;
59         vik[x]++;
60         maxn=max(maxn,x);
61     }
62     solve(maxn);
63     if(ans==-inf)ans=1;
64     cout<<ans<<endl;
65 #ifdef Local
66     cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
67 #endif
68 }

View Code

再附上打表的暴力代码

 1 #include <iostream>
 2 #include <queue>
 3 #include <stack>
 4 #include <cstdio>
 5 #include <vector>
 6 #include <map>
 7 #include <set>
 8 #include <bitset>
 9 #include <algorithm>
10 #include <cmath>
11 #include <cstring>
12 #include <cstdlib>
13 #include <string>
14 #include <sstream>
15 #include <time.h>
16 #define x first
17 #define y second
18 #define pb push_back
19 #define mp make_pair
20 #define lson l,m,rt*2
21 #define rson m+1,r,rt*2+1
22 #define mt(A,B) memset(A,B,sizeof(A))
23 #define mod 1000000007
24 using namespace std;
25 typedef long long LL;
26 const double PI = acos(-1);
27 const int N=2e5+10;
28 const int inf = 0x3f3f3f3f;
29 const LL INF=0x3f3f3f3f3f3f3f3fLL;
30 LL a[N],vis[N],via[N];
31 vector<LL> Q;
32 vector<LL> P[N];
33 void init()
34 {
35     mt(vis,0);
36     for(LL i=2;i<=100000;i++)
37     {
38         if(!vis[i])
39         {
40             Q.push_back(i);
41             for(LL j=i*i;j<=100000;j+=i)
42             {
43                 vis[j]=1;
44             }
45         }
46     }
47 }
48 void solve(LL n)
49 {
50      LL o=n;
51      for(int i=0;i<Q.size()&&n!=1;i++)
52      {
53          if(n%Q[i]==0)
54          {
55              P[o].pb(Q[i]);
56              while(n%Q[i]==0)
57              {
58                  n/=Q[i];
59              }
60          }
61      }
62 }
63 int main()
64 {
65 #ifdef Local
66     freopen("data.txt","r",stdin);
67 #endif
68     int n;
69     LL sum=-INF;
70     init();
71     cin>>n;
72     mt(via,0);
73     for(int i=0;i<n;i++)scanf("%I64d",&a[i]);
74     for(LL i=1;i<=100000;i++)
75     {
76         solve(i);
77     }
78     for(int i=0;i<n;i++)
79     {
80         for(int j=0;j<P[a[i]].size();j++)
81         {
82             via[P[a[i]][j]]++;
83         }
84     }
85     for(int i=0;i<Q.size();i++)
86     {
87         sum=max(sum,via[Q[i]]);
88     }
89     if(sum==0)sum=1;
90     cout<<sum<<endl;
91     //for(int i=0;i<10;i++)cout<<Q[i]<<" ";*/
92
93 #ifdef Local
94     cerr << "time: " << (LL) clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
95 #endif
96 }

View Code

转载于:https://www.cnblogs.com/27sx/p/6283704.html

CodeForces 757B Bash's Big Day(线性筛)相关推荐

  1. Codeforces 757B - Bash's Big Day(分解因子+hashing)

    757B - Bash's Big Day 思路:筛法.将所有因子个数求出,答案就是最大的因子个数,注意全为1的特殊情况. 代码: #include<bits/stdc++.h> usin ...

  2. Educational Codeforces Round 37-F.SUM and REPLACE (线段树,线性筛,收敛函数)

    F. SUM and REPLACE time limit per test2 seconds memory limit per test256 megabytes inputstandard inp ...

  3. shell编程-实现线性筛

    shell编程-实现线性筛 #!/bin/bash arr=(1 1) prime_arr=()function initArray(){for (( a=2; $a<$end_num; a++ ...

  4. [CodeForces1603D] Artistic Partition(四边形不等式 + 决策单调性优化dp + 分治 + 线性筛 + 数论分块)

    problem codeforces 对于给定的正整数 l≤l\leql≤,定义 c(l,r)c(l,r)c(l,r) 为满足下列条件的正整数对 (i,j)(i,j)(i,j) 的数量: l≤i≤j≤ ...

  5. 【数学知识】三种方法求 [1,n] 中所有数欧拉函数(线性筛欧拉函数优化至 O(n) )

    整理的算法模板合集: ACM模板 ①直接求小于或等于n,且与n互质的数个数(求[1,n]中所有数的欧拉函数时间复杂度:O(nn)O(n\sqrt{n})O(nn​)) ②求[1,n]之间每个数的质因数 ...

  6. 【bzoj2694】Lcm 莫比乌斯反演+线性筛

    题目描述 求$\sum\limits_{i=1}^n\sum\limits_{j=1}^m|\mu(gcd(i,j))|lcm(i,j)$,即$gcd(i,j)$不存在平方因子的$lcm(i,j)$之 ...

  7. [SDOI2008]沙拉公主的困惑 线性筛 素数+欧拉

    本文为博主原创文章,欢迎转载,请注明出处 www.cnblogs.com/yangyaojia [SDOI2008]沙拉公主的困惑 线性筛 素数+欧拉 题目大意 给定n,m,求在1到n!内与m!互质的 ...

  8. 【bzoj3309】DZY Loves Math 莫比乌斯反演+线性筛

    Description 对于正整数n,定义f(n)为n所含质因子的最大幂指数.例如f(1960)=f(2^3 * 5^1 * 7^2)=3, f(10007)=1, f(1)=0. 给定正整数a,b, ...

  9. ACM入门之【线性筛】

    线性筛模板,时间复杂度O(n) const int N=1e6+10; int prime[N],st[N],cnt,n; void init(int n) {for(int i=2;i<=n; ...

最新文章

  1. 一文告诉你,谷歌是否真的实现了「量子至上」
  2. 域密码自助重置系统----绑定私人邮箱信息自助重置(一)
  3. Python-EEG工具库MNE中文教程(8)-参考电极简介
  4. 实验 3:备份和还原配置文件
  5. VTK:模型之ContourTriangulator
  6. 数据预处理(part3)--缺失值处理和区间化
  7. ASP.NET Web API 异常日志记录
  8. RMAN 前期准备工作和实例
  9. Java中如何使某个类的对象唯一性,只能创建一个对象
  10. libsvm python Linux Ubuntu下编程操作实践
  11. 怎样让超星图书浏览器不会过期 (转)
  12. Java使用comms-net jar包完成ftp文件上传进度的检测功能
  13. c语言一个数平方表示,C语言 - 利用 汇编思想 写一个数的平方
  14. mysql 正则表达式 包含中文_MYSQL 中文检索匹配与正则表达式
  15. 手把手教你搭建DHCP服务器
  16. mysql 联合主键 加锁_MySQL 加锁处理分析
  17. 武田以3.22亿美元剥离中国大陆非核心业务至海森
  18. c语言绘画哆啦a梦源码,简单壁纸制作-哆啦A梦
  19. Oracle给查询结果从1到n添加序号
  20. Camera基本概念

热门文章

  1. 判断 小程序 是否 滚动到页面底部 scrolltolower_微信小程序长列表性能优化——recycle-view
  2. order by 影响效率么_提升开发效率N倍的20+命令行神器
  3. python3鄙视python2_Python3 正在毁灭 Python的原因分析
  4. C 20 协程初探
  5. linux centos7安装ngix,centos7 环境下安装nginx--Linux
  6. python中可变参数怎么传递的呢_在python中,你可以在命名参数后传递可变参数吗?...
  7. 巴特沃斯滤波器python_如何用Scipy.signal.bu实现带通巴特沃斯滤波器
  8. 关于视频光端机调制方式及介质特点的介绍
  9. 【渝粤教育】国家开放大学2018年秋季 2083T信息技术与教育技术(2) 参考试题
  10. 【渝粤教育】国家开放大学2018年秋季 2111T病理学与病理生理学 参考试题