题意:给你一个集合,让你找到一个最大的子集,使得子集里面的元素的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 }
再附上打表的暴力代码
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 }