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的更多相关文章

  1. jQuery下拉框扩展和美化插件Chosen

    Chosen 是一个支持jquery的select下拉框美化插件,它能让丑陋的.很长的select选择框变的更好看.更方便.不仅如此,它更扩展了select,增加了自动筛选的功能.它可对列表进行分组, ...

  2. chosen组件实现下拉框

    chosen组件用于增强原生的select控件,使之有更好的用户体验.官方demo https://harvesthq.github.io/chosen/ 目前项目中碰到的使用,比如一个页面中有两个不 ...

  3. 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 ...

  4. chosen PersistenceUnitInfo does not specify a provider class name

    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userServiceI ...

  5. 带搜索框的下拉框chosen.jQury.js

    下载所需js,css png资源     <link href="chosen.css" rel="stylesheet" type="text ...

  6. jquery.chosen.js实现模糊搜索

    jquery.chosen.js查询时,chosen默认从第一个字符搜索,所以写中间的字符搜索时,是搜索不出来的 若想实现中间字符的模糊查询,下面的js中(search_contains属性为true ...

  7. jquery plugins —— Chosen

    官网地址:http://harvesthq.github.io/chosen/ Chosen (v1.4.2) Chosen has a number of options and attribute ...

  8. Chosen中选择项的更新

    Chosen 选择项的动态修改/更新 如果你需要去动态更新select选择框里的选择项,你需要通知Chosen去响应这个变动,你需要在这个选项框是触发一个"liszt:updated&quo ...

  9. 带搜索的下拉框Chosen

    一:参考 https://harvesthq.github.io/chosen/ Chosen是一个jQuery插件 二:引入js文件 <link href="plug-in/chos ...

  10. chosen 下拉框

    $("#teams").trigger("liszt:updated");//更新重新绑定                            $(" ...

随机推荐

  1. 关于变量__name__的理解

    __name__ 1. 基本含义 如果是放在Modules模块中,就表示是模块的名字: 如果是放在Classs类中,就表示类的名字: 2. 模块中的意义 这里重点说一下模块中的意义,这个用法在pyth ...

  2. POJ 3268_Silver Cow Party

    题意: n个地方,标号1~n,每个地方都有一头牛,现在要他们都去往标号为x的地方,再从x返回,每条道路都是单向的,求所有牛走的来回的最短路中的最大值. 分析: 注意在求每头牛走到x时,挨个算肯定超时, ...

  3. 洛谷 P3456 [POI2007]GRZ-Ridges and Valleys

    P3456 [POI2007]GRZ-Ridges and Valleys 题意翻译 给定一个地图,为小朋友想要旅行的区域,地图被分为n*n的网格,每个格子(i,j) 的高度w(i,j)是给定的.若两 ...

  4. 【.Net 学习系列】-- 反射的简单用法

    新建两个项目:类库(Model)和控制台应用程序(ReflectTest). 在[Model]中添加一个类[User]: namespace Model { public class User { p ...

  5. 手动安装Firefox Linux

    (2015-06-05 17:22:19)[编辑][删除] 转载▼ 标签: 股票 Firefox 下载文件以.tar和.bz2格式保存,必须从这些压缩包中提取文件.不想删除当前安装的 Firefox, ...

  6. 重置网络命令win7

    开始→运行→输入:CMD 点击确定(或按回车键),打开命令提示符窗口. 在命令提示符中输入:netsh winsock reset (按回车键执行命令) 稍后,会有成功的提示:成功地重置Winsock ...

  7. Java设计模式—单例设计模式(Singleton Pattern)全然解析

    转载请注明出处:http://blog.csdn.net/dmk877/article/details/50311791 相信大家都知道设计模式,听的最多的也应该是单例设计模式,这种模式也是在开发中用 ...

  8. Python3标准库(二) re模块

    正则表达式(Regular Expression)是字符串处理的常用工具,通常被用来检索.替换那些符合某个模式(Pattern)的文本.很多程序设计语言都支持正则表达式,像Perl.Java.C/C+ ...

  9. 在head里的CSS link 竟然粗如今body里了?

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvcGVhY2Vfb2Zfc291bA==/font/5a6L5L2T/fontsize/400/fill/I0 ...

  10. Lua学习笔记7:时间和日期

    lua中的时间类似于C语言中的时间,例如以下: local time = os.time() print(time) local t = os.date("*t") for k,v ...