The Chosen One
https://www.hackerrank.com/contests/101hack45/challenges/the-chosen-one
找出一个数字,使得,数组中只有一个数字不是这个数的约数,而且其他数都是这个数的约数。
如果暴力枚举每一个i,表示,就是要排除这个数字,那么,需要找到一个数能被剩下的数都整除。那么最优解就是gcd了。
因为他们的gcd,就是最大公约数,被排除的那个数字最不可能整除这个数。
所以快速求剩下数字的gcd就需要预处理pre[i]表示前i个数的gcd,last[i]表示i--n的gcd,然后再gcd
注意特判n = =
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <assert.h>
#define IOS ios::sync_with_stdio(false)
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
#include <bitset>
const int maxn = 1e5 + ;
LL pre[maxn];
LL last[maxn];
LL a[maxn];
void work() {
int n;
scanf("%d", &n);
for (int i = ; i <= n; ++i) {
cin >> a[i];
}
pre[] = ;
pre[] = a[];
for (int i = ; i <= n; ++i) {
pre[i] = __gcd(pre[i - ], a[i]);
}
last[n + ] = ;
last[n] = a[n];
for (int i = n - ; i >= ; --i) {
last[i] = __gcd(last[i + ], a[i]);
}
for (int i = ; i <= n; ++i) {
LL t;
if (i == ) {
t = last[i + ];
} else if (i == n) {
t = pre[i - ];
} else t = __gcd(pre[i - ], last[i + ]);
if (a[i] % t != ) {
cout << t << endl;
return;
}
}
cout << a[n] + << endl;
} int main() {
#ifdef local
freopen("data.txt", "r", stdin);
// freopen("data.txt", "w", stdout);
#endif
work();
return ;
}
The Chosen One的更多相关文章
- jQuery下拉框扩展和美化插件Chosen
Chosen 是一个支持jquery的select下拉框美化插件,它能让丑陋的.很长的select选择框变的更好看.更方便.不仅如此,它更扩展了select,增加了自动筛选的功能.它可对列表进行分组, ...
- chosen组件实现下拉框
chosen组件用于增强原生的select控件,使之有更好的用户体验.官方demo https://harvesthq.github.io/chosen/ 目前项目中碰到的使用,比如一个页面中有两个不 ...
- android xml 布局错误(黑掉了)Missing styles. Is the correct theme chosen for this layout?
发现在调整页面的时候 ,老是报以下错误,导致无法静态显示ui效果. Missing styles. Is the correct theme chosen for this layout? Use t ...
- chosen PersistenceUnitInfo does not specify a provider class name
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceI ...
- 带搜索框的下拉框chosen.jQury.js
下载所需js,css png资源 <link href="chosen.css" rel="stylesheet" type="text ...
- jquery.chosen.js实现模糊搜索
jquery.chosen.js查询时,chosen默认从第一个字符搜索,所以写中间的字符搜索时,是搜索不出来的 若想实现中间字符的模糊查询,下面的js中(search_contains属性为true ...
- jquery plugins —— Chosen
官网地址:http://harvesthq.github.io/chosen/ Chosen (v1.4.2) Chosen has a number of options and attribute ...
- Chosen中选择项的更新
Chosen 选择项的动态修改/更新 如果你需要去动态更新select选择框里的选择项,你需要通知Chosen去响应这个变动,你需要在这个选项框是触发一个"liszt:updated&quo ...
- 带搜索的下拉框Chosen
一:参考 https://harvesthq.github.io/chosen/ Chosen是一个jQuery插件 二:引入js文件 <link href="plug-in/chos ...
- chosen 下拉框
$("#teams").trigger("liszt:updated");//更新重新绑定 $(" ...
随机推荐
- Bug记载2之Vue.JS路由定义的位置
<!DOCTYPE html><html lang="en"> <head> <meta charset="UTF-8" ...
- Mysql数据库再度使用
查看数据库端口: show global variables like 'port'; 谨记:每一条sql结束的语句后都要接上分号. 在连接数据库时遇到过这种问题: Fatal error: Call ...
- ViewPager学习之仿微信主界面
由于素材的原因,这里都是从网上找的图片,所以所谓的仿微信实际上最后成了下图这货.. .,点击变色也是自己用的windows自带绘图的颜料桶填充的空白. .. watermark/2/text/aHR0 ...
- Go语言核心之美 3.2-slice切片
Slice(切片)是长度可变的元素序列(与之相应,上一节中的数组是不可变的),每一个元素都有同样的类型.slice类型写作[]T.T是元素类型.slice和数组写法非常像,差别在于slice没有指定长 ...
- Velocity高速新手教程
变量 (1)变量的定义: #set($name = "hello") 说明:velocity中变量是弱类型的. 当使用#set 指令时,括在双引號中的字面字符串将解析和又 ...
- Unix/Linux 软件安装
Unix/Linux 软件安装 首先我们明确.在Unix like的机器上,一套软件并不唯独一个程序,而是一堆程序代码文件. 比如main.c,haha.c,sin_value.c这三个源码文件. 1 ...
- 【WinHec启发录】透过Windows 10技术布局,谈微软王者归来
每一个时代都有王者,王者的成功,往往是由于恰逢其时地公布了一个成功的产品(具有里程碑意义,划时代的产品).Windows 95的成功标示着微软是PC时代的王者:WinXP的成功标示着微软是互联网时代的 ...
- web 开发之js---巧用iframe实现jsp无刷新上传文件
首先要说的就是 ajax 是无法实现上传文件的,可以想一下ajax与后台通信都是通过传递字符串,怎么能传递文件呢?其实出于安全考虑js是不能操作文件的,所以就不要再说用ajax来实现文件的上传了,这是 ...
- go1
关键字: break default func interface select case defer go map struct chan else goto package switch cons ...
- 8-13 canvas专题-阶段练习二(下)
8-13 canvas专题-阶段练习二(下) <!DOCTYPE html> <html lang="zh-cn"> <head> <me ...