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 ...
随机推荐
- 错误捕捉过滤器 .NetCore版
前言 继承ExceptionFilterAttribute后,重写OnException函数. 统一捕捉所有报错,格式化返回前端. 代码实现 基类控制器 在基类控制器上添加[ErrorCatch]特性 ...
- CMU数据库(15-445)-实验2-B+树索引实现(中)删除
3. Delete 实现 附上实验2的第一部分 https://www.cnblogs.com/JayL-zxl/p/14324297.html 3. 1 删除算法原理 如果叶子结点中没有相应的key ...
- jmeter三种阶梯式加压
一.前言 在做性能测试的时候,在某些场景下需要逐渐加压,不总是停下来再修改线程再加压,且可以对比加压,找出服务的性能拐点. 二.三种逐渐加压方式 备注:普通的压测方式,并发的Samples是可预知的: ...
- jQuery 当前展开其他收缩 三级下拉菜单(转载)
jQuery可展开收缩三级下拉菜单 <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- nginx proxy pass redirects ignore port
nginx proxy pass redirects ignore port $host in this order of precedence: host name from the request ...
- (转载)微软数据挖掘算法:Microsoft顺序分析和聚类分析算法(8)
前言 本篇文章继续我们的微软挖掘系列算法总结,前几篇文章已经将相关的主要算法做了详细的介绍,我为了展示方便,特地的整理了一个目录提纲篇:大数据时代:深入浅出微软数据挖掘算法总结连载,有兴趣的童鞋可以点 ...
- goroutine 分析 协程的调度和执行顺序 并发写 run in the same address space 内存地址 闭包 存在两种并发 确定性 非确定性的 Go 的协程和通道理所当然的支持确定性的并发方式(
package main import ( "fmt" "runtime" "sync" ) const N = 26 func main( ...
- SumatraPDF设置护眼背景
高级选项中: 1 FixedPageUI [ 2 TextColor = #000000 3 BackgroundColor = #C7EDCC 4 SelectionColor = #f5fc0c ...
- 服务注册中心:Eureka
第一章 注册中心介绍 1.1.什么是注册中心 注册中心可以说是微服务架构中的"通讯录",它记录了服务和服务地址的映射关系.在分布式系统中,服务会注册到这里,当服务需要调用其它服务时 ...
- 九鼎S5PV210开发板的SD卡启动、uboot tftp升级内核镜像
Note: 我的S5PV210板子的磁盘是emmc版本,不是nandflash版本. 1. SD卡内烧录uboot镜像,然后作为启动盘来启动emmc中的内核镜像和根文件系统. 只要破坏emmc内的ub ...