Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)

传送门:http://codeforces.com/contest/1047/problem/C

题意:

  给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值。

思路:

  自己一开始想把每个数的因子都找出来,找到这些因子中出现次数最多且因子大于n个数的最大公约数的,(n - 次数 )就是答案。但是复杂度是1e9,差那么一点。

自己还是对素数筛理解的不够深。这道题可以枚举素数x,对于每个x,找到所有(a【i】/gcd(all)) 是x倍数的个数,就是一个次数。找这个次数的过程正好与素数筛的过程一致。

#include <algorithm>
#include  <iterator>
#include  <iostream>
#include   <cstring>
#include   <cstdlib>
#include   <iomanip>
#include    <bitset>
#include    <cctype>
#include    <cstdio>
#include    <string>
#include    <vector>
#include     <stack>
#include     <cmath>
#include     <queue>
#include      <list>
#include       <map>
#include       <set>
#include   <cassert>
#include <unordered_map>
using namespace std;
//#pragma GCC optimize(3)
//#pragma comment(linker, "/STACK:102400000,102400000")  //c++
// #pragma GCC diagnostic error "-std=c++11"
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("-fdelete-null-pointer-checks,inline-functions-called-once,-funsafe-loop-optimizations,-fexpensive-optimizations,-foptimize-sibling-calls,-ftree-switch-conversion,-finline-small-functions,inline-small-functions,-frerun-cse-after-loop,-fhoist-adjacent-loads,-findirect-inlining,-freorder-functions,no-stack-protector,-fpartial-inlining,-fsched-interblock,-fcse-follow-jumps,-fcse-skip-blocks,-falign-functions,-fstrict-overflow,-fstrict-aliasing,-fschedule-insns2,-ftree-tail-merge,inline-functions,-fschedule-insns,-freorder-blocks,-fwhole-program,-funroll-loops,-fthread-jumps,-fcrossjumping,-fcaller-saves,-fdevirtualize,-falign-labels,-falign-loops,-falign-jumps,unroll-loops,-fsched-spec,-ffast-math,Ofast,inline,-fgcse,-fgcse-lm,-fipa-sra,-ftree-pre,-ftree-vrp,-fpeephole2",3)

#define lson (l , mid , rt << 1)
#define rson (mid + 1 , r , rt << 1 | 1)
#define debug(x) cerr << #x << " = " << x << "\n";
#define pb push_back
#define pq priority_queue
#define max3(a,b,c) max(max(a,b),c)

typedef long long ll;
typedef unsigned long long ull;

typedef pair<ll ,ll > pll;
typedef pair<int ,int > pii;
typedef pair<int,pii> p3;

//priority_queue<int> q;//这是一个大根堆q
//priority_queue<int,vector<int>,greater<int> >q;//这是一个小根堆q
#define fi first
#define se second
//#define endl ‘\n‘

#define OKC ios::sync_with_stdio(false);cin.tie(0)
#define FT(A,B,C) for(int A=B;A <= C;++A)  //用来压行
#define REP(i , j , k)  for(int i = j ; i <  k ; ++i)
//priority_queue<int ,vector<int>, greater<int> >que;

const ll mos = 0x7FFFFFFF;  //2147483647
const ll nmos = 0x80000000;  //-2147483648
const int inf = 0x3f3f3f3f;
const ll inff = 0x3f3f3f3f3f3f3f3f; //18
const int mod = 256;
const double esp = 1e-8;
const double PI=acos(-1.0);

template<typename T>
inline T read(T&x){
    x=0;int f=0;char ch=getchar();
    while (ch<‘0‘||ch>‘9‘) f|=(ch==‘-‘),ch=getchar();
    while (ch>=‘0‘&&ch<=‘9‘) x=x*10+ch-‘0‘,ch=getchar();
    return x=f?-x:x;
}

/*-----------------------showtime----------------------*/
            const int maxn = 3e5+9;
            const int maxnum = 1.5e7+9;
            int a[maxn],cnt[maxnum];
            int p[maxn];
            bool prime[maxnum];
            int gcd(int a,int b){
                if(b==0)return a;
                return gcd(b, a%b);
            }

int main(){
            int n,g=0;scanf("%d", &n);
            int ans = 0;
            for(int i=1; i<=n; i++){
                scanf("%d", &a[i]);
                g = gcd(g,a[i]);
            }
            for(int i=1; i<=n; i++)cnt[a[i]/g]++;
            int tot = 0;

            for(int i=2; i<maxnum; i++){
                if(!prime[i]){
                    int x = i;int h = 0;
                    for(int j=i; j<maxnum; j+=i){
                        prime[j] = 1;
                        h += cnt[j];
                    }
                    ans = max(ans, h);
                }
            }
            if(!ans)puts("-1");
            else printf("%d\n", n-ans);
            return 0;
}

CF 1047 c

原文地址:https://www.cnblogs.com/ckxkexing/p/9691598.html

时间: 2024-10-10 02:35:18

Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)的相关文章

Codeforces Round #511 (Div. 2) C. Enlarge GCD (质因数)

题目 题意: 给你n个数a[1]...a[n],可以得到这n个数的最大公约数, 现在要求你在n个数中 尽量少删除数,使得被删之后的数组a的最大公约数比原来的大. 如果要删的数小于n,就输出要删的数的个数, 否则输出 -1 . 思路: 设原来的最大公约数为 g, 然后a[1]...a[n]都除以g ,得到的新的a[1]...a[n],此时它们的最大公约数一定是1 . 设除以g之后的数组a为: 1    2    3     6      8   10  则它们的质因数分别是:  1    2   

Codeforces Round #511 (Div. 2) C. Enlarge GCD

题目链接 题目就是找每个数的最小素因子,然后递归除,本来没啥问题,结果今天又学习了个新坑点. 我交了题后,疯狂CE,我以为爆内存,结果是,我对全局数组赋值, 如果直接赋值,会直接在exe内产生内存,否则只会在运行时才分配内存. 1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 1e7 + 5e6 + 10; 5 6 //线性素数筛 7 int prime[2000000],num_prime = 0;

Codeforces Round #511 (Div. 2)

又到了摸鱼的时候了23333 A. Little C Loves 3 I 题意:给一个数,分解为不被3整除的3个数 题解:构造,如果这个数被3整除,就构造为1,1,n-2:否则构造为1,2,n-3 1 class Solution(object): 2 def run(self): 3 n = int(input()) 4 if n % 3 == 0: 5 print(1, 1, n - 2) 6 else: 7 print(1, 2, n - 3) 8 9 if __name__ == '__

Codeforces Round #511 (Div. 1)

A - Enlarge GCD 题意:给n个数,那么他们有gcd,去掉最多n-1个数使得他们的gcd变大.求去掉最少的数. 题解:首先如果所有数都相等,那么无解.否则一定有解:最多去掉只剩下最大的那个.gcd是没有影响的,可以直接除掉(注意gcd可以用0来初始化,0和x的gcd都等于x).然后除去gcd之后每个数有他独特的几种因子,把不含这种因子的数都去掉就可以把这种因子释放出来.暴力sqrt分解会T掉,线性筛/埃筛预处理出每个数的最小质因子(甚至不需要预处理出他的幂,反正除一除也是log级别的

C. Enlarge GCD Codeforces Round #511 (Div. 2)【数学】

题目: Mr. F has nn positive integers, a1,a2,-,an. He thinks the greatest common divisor of these integers is too small. So he wants to enlarge it by removing some of the integers. But this problem is too simple for him, so he does not want to do it by

A. Little C Loves 3 I Codeforces Round #511 (Div. 2) 【数学】

题目: Little C loves number ?3? very much. He loves all things about it. Now he has a positive integer nn. He wants to split nn into 3 positive integers a,b,ca,b,c, such that a+b+c=na+b+c=n and none of the 3 integers is a multiple of 3. Help him to fin

Codeforces Round 511 Div.1 B

Description Given a \(n \times m\) chessboard, every time put two chessman with Manhattan distance 3 between them. Calculate the maximum number of chessmen you can put on it. \(n, m \le 10^9\). Solution All possible pairs of position is a biparite gr

Codeforces Round #324 (Div. 2) (快速判断素数模板)

蛋疼的比赛,当天忘了做了,做的模拟,太久没怎么做题了,然后C题这么简单的思路却一直卡到死,期间看了下D然后随便猜了下,暴力了下就过了. A.找一个能被t整除的n位数,那么除了<=10以外,其他都可以用长度为n的10或100,1000 ... 来往上加几个数而得到 #include <iostream> #include <stdio.h> #include <set> #include <algorithm> #include <string.h

Codeforces Round #428 (Div. 2)

Codeforces Round #428 (Div. 2) A    看懂题目意思就知道做了 #include<bits/stdc++.h> using namespace std; #pragma comment(linker, "/STACK:102400000,102400000") #define rep(i,a,b) for (int i=a; i<=b; ++i) #define per(i,b,a) for (int i=b; i>=a; --i