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 + 20;
LL pre[maxn];
LL last[maxn];
LL a[maxn];
void work() {
    int n;
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i) {
        cin >> a[i];
    }
    pre[0] = 1;
    pre[1] = a[1];
    for (int i = 2; i <= n; ++i) {
        pre[i] = __gcd(pre[i - 1], a[i]);
    }
    last[n + 1] = 1;
    last[n] = a[n];
    for (int i = n - 1; i >= 1; --i) {
        last[i] = __gcd(last[i + 1], a[i]);
    }
    for (int i = 1; i <= n; ++i) {
        LL t;
        if (i == 1) {
            t = last[i + 1];
        } else if (i == n) {
            t = pre[i - 1];
        } else t = __gcd(pre[i - 1], last[i + 1]);
        if (a[i] % t != 0) {
            cout << t << endl;
            return;
        }
    }
    cout << a[n] + 1 << endl;
}

int main() {
#ifdef local
    freopen("data.txt", "r", stdin);
//    freopen("data.txt", "w", stdout);
#endif
    work();
    return 0;
}
View Code

 

转载于:https://www.cnblogs.com/liuweimingcprogram/p/6295383.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值