Codeforces Round #511 (Div. 2) C. Enlarge GCD
题目链接
题目就是找每个数的最小素因子,然后递归除,本来没啥问题,结果今天又学习了个新坑点。
我交了题后,
疯狂CE,我以为爆内存,结果是,我对全局数组赋值,

如果直接赋值,会直接在exe内产生内存,否则只会在运行时才分配内存。

#include <bits/stdc++.h>
using namespace std; const int maxn = 1e7 + 5e6 + ; //线性素数筛
int prime[],num_prime = ;
int vis[maxn];
void is_prime(int N)
{
for(int i=;i<N;i++)
{
if(!vis[i])
{
prime[num_prime++] = i;
vis[i] = i;
}
for(int j=;j<num_prime&&i*prime[j]<N;j++)
{
vis[i*prime[j]] = prime[j];
if(!(i%prime[j]))
{
break;
}
}
}
return;
}
int a[];
int fp[maxn]; int main()
{
int n; scanf("%d", &n);
for(int i = ; i <= n; i++) scanf("%d", &a[i]);
int g = a[];
for(int i = ; i <= n; i++) g = __gcd(g, a[i]);
for(int i = ; i <= n; i++) a[i] /= g;
memset(fp, , sizeof(fp));
is_prime(maxn);
int maxlen = ;
for(int i = ; i <= n; i++)
{
while(a[i] > )
{
fp[vis[a[i]]]++;
maxlen = max(maxlen, fp[vis[a[i]]]);
int tmp = vis[a[i]];
while(a[i] % tmp == )
{
a[i] = a[i] / tmp;
}
if(a[i] == ) break;
}
}
if(maxlen == ) printf("-1\n");
else printf("%d\n", n - maxlen);
return ;
}
/*
4
18 27 45 30
*/
Codeforces Round #511 (Div. 2) C. Enlarge GCD的更多相关文章
- Codeforces Round #511 (Div. 2)-C - Enlarge GCD (素数筛)
传送门:http://codeforces.com/contest/1047/problem/C 题意: 给定n个数,问最少要去掉几个数,使得剩下的数gcd 大于原来n个数的gcd值. 思路: 自己一 ...
- Codeforces Round #511 (Div. 2) C. Enlarge GCD (质因数)
题目 题意: 给你n个数a[1]...a[n],可以得到这n个数的最大公约数, 现在要求你在n个数中 尽量少删除数,使得被删之后的数组a的最大公约数比原来的大. 如果要删的数小于n,就输出要删的数的个 ...
- Codeforces Round #511 (Div. 2)
Codeforces Round #511 (Div. 2) #include <bits/stdc++.h> using namespace std; int n; int main() ...
- Codeforces Round #511 (Div. 2):C. Enlarge GCD(数学)
C. Enlarge GCD 题目链接:https://codeforces.com/contest/1047/problem/C 题意: 给出n个数,然后你可以移除一些数.现在要求你移除最少的数,让 ...
- 2018.9.21 Codeforces Round #511(Div.2)
只写了AB,甚至还WA了一次A题,暴露了蒟蒻的本质=.= 感觉考的时候有好多正确或和正解有关的思路,但是就想不出具体的解法或者想的不够深(长)(怕不是过于鶸) 话说CF的E题怎么都这么清奇=.= A. ...
- 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 integ ...
- Codeforces Round #554 (Div. 2)-C(gcd应用)
题目链接:https://codeforces.com/contest/1152/problem/C 题意:给定a,b(<1e9).求使得lcm(a+k,b+k)最小的k,若有多个k,求最小的k ...
- Codeforces Round #347 (Div.2)_A. Complicated GCD
题目链接:http://codeforces.com/contest/664/problem/A A. Complicated GCD time limit per test 1 second mem ...
- Codeforces Round #651 (Div. 2) A. Maximum GCD(数论)
题目链接:https://codeforces.com/contest/1370/problem/A 题意 有 $n$ 个数大小分别为 $1$ 到 $n$,找出两个数间最大的 $gcd$ . 题解 若 ...
随机推荐
- github+hexo+themes搭建简易个性主题博客
0x00 install Node.js and git 安装Node.js:http://www.runoob.com/nodejs/nodejs-install-setup.html 安装git ...
- 设置tableview的滚动范围--iOS开发系列---项目中成长的知识三
设置tableview的滚动范围 有时候tableview的footerview上的内容需要向上拖动界面一定距离才能够看见, 项目中因为我需要在footerviw上添加一个按钮,而这个按钮又因为这个原 ...
- EAGLView介绍
http://book.51cto.com/art/201108/285446.htm
- 安装ruby开发环境
如何快速正确的安装 Ruby, Rails 运行环境 对于新入门的开发者,如何安装 Ruby, Ruby Gems 和 Rails 的运行环境可能会是个问题,本页主要介绍如何用一条靠谱的路子快速安装 ...
- 【Java_多线程并发编程】JUC原子类——原子类中的volatile变量和CAS函数
JUC中的原子类是依靠volatile变量和Unsafe类中的CAS函数实现的. 1. volatile变量的特性 内存可见性(当一个线程修改volatile变量的值后,另一个线程就可以实时看到此变量 ...
- 【Java_基础】Java中Native关键字的作用
本篇博文转载与:Java中Native关键字的作用
- 用xtrabackup实现mysql的主从复制 阿里云rds到自己创建mysql
来源 http://blog.51cto.com/825536458/1803968参考https://segmentfault.com/a/1190000003063874 如果我们用传统的mysq ...
- (41)zabbix监控api接口性能及可用性 天气预报api为例
现在各种应用都走api,例如淘宝,天气预报等手机.pad客户端都是走api的,那么平时也得对这些api做监控了.怎么做呢?zabbix的web监控是不二选择了.今天就以天气预报api作为一个例子. 天 ...
- perl学习之:正则表达式
- Verilog学习笔记基本语法篇(五)········ 条件语句
条件语句可以分为if_else语句和case语句两张部分. A)if_else语句 三种表达形式 1) if(表达式) 2)if(表达式) 3)if(表达 ...