CodeForces 1047C Enlarge GCD(数论)题解
题意:n个数的gcd是k,要你删掉最少的数使得删完后的数组的gcd > k
思路:先求出k,然后每个数除以k。然后找出出现次数最多的质因数即可。
代码:
#include<cmath>
#include<set>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include <iostream>
#include<algorithm>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = 3e5 + 10;
const int M = 1.5 * 1e7 + 10;
const ull seed = 131;
const int INF = 0x3f3f3f3f;
const int MOD = 1e4 + 7;
int gcd(int a, int b){
return b == 0? a : gcd(b, a % b);
}
int a[maxn];
int p[4000], prime[4000], pn;
int num[M];
void init(){
pn = 0;
memset(p, 0, sizeof(p));
for(int i = 2; i < 4000; i++){
if(!p[i]){
prime[pn++] = i;
}
for(ll j = 0; j < pn && i * prime[j] < 4000; j++){
p[i * prime[j]] = 1;
if(i % prime[j] == 0) continue;
}
}
// printf("%d\n", pn);
}
int main(){
int n, need;
init();
scanf("%d", &n);
memset(num, 0, sizeof(num));
for(int i = 1; i <= n; i++){
scanf("%d", &a[i]);
if(i == 1) need = a[i];
else need = gcd(need, a[i]);
}
for(int i = 1 ; i <= n; i++) a[i] /= need;
int ans = -1;
for(int i = 1; i <= n; i++){
for(int j = 0; j < pn && prime[j] * prime[j] <= a[i]; j++){
if(a[i] % prime[j] == 0){
num[prime[j]]++;
ans = max(ans, num[prime[j]]);
while(a[i] % prime[j] == 0) a[i] /= prime[j];
}
}
if(a[i] > 1){
num[a[i]]++;
ans = max(ans, num[a[i]]);
}
}
if(ans == -1) printf("%d\n", ans);
else printf("%d\n", n - ans);
return 0;
}
CodeForces 1047C Enlarge GCD(数论)题解的更多相关文章
- [Codeforces Round511C] Enlarge GCD
[题目链接] https://codeforces.com/contest/1047/problem/C [算法] 首先求出n个数的最大公约数g , 将每个数除以g , 那么 , 问题就转化为在n个数 ...
- Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)
C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...
- Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论
Bash and a Tough Math Puzzle CodeForces 914D 线段树+gcd数论 题意 给你一段数,然后小明去猜某一区间内的gcd,这里不一定是准确值,如果在这个区间内改变 ...
- [CodeForces - 1225C]p-binary 【数论】【二进制】
[CodeForces - 1225C]p-binary [数论][二进制] 标签: 题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory limit 5 ...
- Codeforces Round #182 (Div. 1)题解【ABCD】
Codeforces Round #182 (Div. 1)题解 A题:Yaroslav and Sequence1 题意: 给你\(2*n+1\)个元素,你每次可以进行无数种操作,每次操作必须选择其 ...
- CodeFroces-- 511div2 C. Enlarge GCD
题目链接:C. Enlarge GCD 给你一个序列 删除一些数看可以让他们之间的gcd变大如果可以输出删除数量最小的个数 先求出共同 gcd 然后除去 找出出现最多的质数 然后减去就可以了 #inc ...
- Codeforces Round #410 (Div. 2) 题解 【ABCD】
A 题意:问你恰好修改一个字符,能不能使得字符串变成回文串 题解:显然直接for一遍,如果长度为偶数,那么不一样的必须是1个:如果长度为奇数,那么不一样的也可以是0个 #include<bits ...
- Codeforces Round #608 (Div. 2) 题解
目录 Codeforces Round #608 (Div. 2) 题解 前言 A. Suits 题意 做法 程序 B. Blocks 题意 做法 程序 C. Shawarma Tent 题意 做法 ...
- Codeforces Round #525 (Div. 2)题解
Codeforces Round #525 (Div. 2)题解 题解 CF1088A [Ehab and another construction problem] 依据题意枚举即可 # inclu ...
随机推荐
- [Usaco2008 Open]Roads Around The Farm分岔路口
题目描述 约翰的N(1≤N≤1,000,000,000)只奶牛要出发去探索牧场四周的土地.她们将沿着一条路走,一直走到三岔路口(可以认为所有的路口都是这样的).这时候,这一群奶牛可能会分成两群,分别沿 ...
- 云原生流水线 Argo Workflow 的安装、使用以及个人体验
注意:这篇文章并不是一篇入门教程,学习 Argo Workflow 请移步官方文档 Argo Documentation Argo Workflow 是一个云原生工作流引擎,专注于编排并行任务.它的特 ...
- mysql(连接查询和数据库设计)
--创建学生表 create table students ( id int unsigned not null auto_increment primary key, name varchar(20 ...
- 使用Jmeter对SHA1加密接口进行性能测试
性能测试过程中,有时候会遇到需要对信息头进行加密鉴权,下面我就来介绍如何针对SHA1加密鉴权开发性能测试脚本1.首先了解原理,就是需要对如下三个参数进行SHA1加密,(AppSecret + Nonc ...
- hive报错:Failed with exception java.io.IOException: rename for src path:
在hive中,会有这样一种情形: 1.创建一个分区外部表A(比如A表有5个字段),并且向A表里指定的分区(比如20160928这个分区)里插入数据 2.发现A表缺少一些字段,因为存在元数据不实时更新的 ...
- python 使用函数名的字符串调用函数(4种方法)_black-heart的专栏-CSDN博客 https://blog.csdn.net/mrqingyu/article/details/84403924
funcs = ['fetch_data_' + i for i in ( 'activities', 'banners', 'server_list')]# from operator import ...
- (013)每日SQL学习:日期的各种计算
1.确定两个日期之间的工作日天数 --确定两个日期之间的工作日天数with x0 as (select to_date('2018-01-01','yyyy-mm-dd') as 日期 from du ...
- Micro Frontends 微前端
Micro Frontends https://martinfowler.com/articles/micro-frontends.html Integration approaches Server ...
- spring restTemplate 进行http请求的工具类封装
本文为博主原创,未经允许不得转载: 1.对常用调用的方法进行封装: import org.springframework.http.HttpHeaders; import com.alibaba.fa ...
- RocketMQ 的几个简单问题与答案
1 单机版消息中心 一个消息中心,最基本的需要支持多生产者.多消费者,例如下: class Scratch { public static void main(String[] args) { // ...