AtCoder Grand Contest 018 A - Getting Difference
A - Getting Difference
Time limit : 2sec / Memory limit : 256MB
Score : 300 points
Problem Statement
There is a box containing N balls. The i-th ball has the integer Ai written on it. Snuke can perform the following operation any number of times:
- Take out two balls from the box. Then, return them to the box along with a new ball, on which the absolute difference of the integers written on the two balls is written.
Determine whether it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written.
Constraints
- 1≤N≤105
- 1≤Ai≤109
- 1≤K≤109
- All input values are integers.
Input
Input is given from Standard Input in the following format:
N K
A1 A2 … AN
Output
If it is possible for Snuke to reach the state where the box contains a ball on which the integer K is written, print POSSIBLE; if it is not possible, print IMPOSSIBLE.
Sample Input 1
3 7
9 3 4
Sample Output 1
POSSIBLE
First, take out the two balls 9 and 4, and return them back along with a new ball, abs(9−4)=5. Next, take out 3 and 5, and return them back along with abs(3−5)=2. Finally, take out 9 and 2, and return them back along with abs(9−2)=7. Now we have 7 in the box, and the answer is therefore POSSIBLE.
Sample Input 2
3 5
6 9 3
Sample Output 2
IMPOSSIBLE
No matter what we do, it is not possible to have 5 in the box. The answer is therefore IMPOSSIBLE.
Sample Input 3
4 11
11 3 7 15
Sample Output 3
POSSIBLE
The box already contains 11 before we do anything. The answer is therefore POSSIBLE.
Sample Input 4
5 12
10 2 8 6 4
Sample Output 4
IMPOSSIBLE
首先,k>max(a[0],a[1],a[2],.......)进行特判,一定不满足,之后,对序列两两之间取GCD,判断即可
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
set<ll>s;
ll a[],n,k;
ll gcd(ll x,ll y)
{
return y==?x:gcd(y,x%y);
}
int main()
{
scanf("%lld%lld",&n,&k);
int ok=,maxn=-;
for(int i=;i<n;i++)
{
cin>>a[i];
if(a[i]==k)ok=;
maxn=max(maxn,a[i]);
}
sort(a,a+n);
if(ok) {puts("POSSIBLE");return ;}
if(k>maxn) {puts("IMPOSSIBLE");return ;}
for(int i=;i<n;i++)
{
s.insert(gcd(a[i],a[i-]));
}
for(set<ll>::iterator it=s.begin();it!=s.end();it++)
{
if(k%*it==)
{
puts("POSSIBLE");
return ;
}
}
puts("IMPOSSIBLE");
return ;
}
AtCoder Grand Contest 018 A - Getting Difference的更多相关文章
- 【GCD】AtCoder Grand Contest 018 A - Getting Difference
从大到小排序,相邻两项作差,求gcd,如果K是gcd的倍数并且K<=max{a(i)},必然有解,否则无解. 可以自己手画画证明. #include<cstdio> #include ...
- AtCoder Grand Contest 018 A
A - Getting Difference Time limit時間制限 : 2sec / Memory limitメモリ制限 : 256MB 配点 : 300 点 問題文 箱に N 個のボールが入 ...
- AtCoder Grand Contest 018 D - Tree and Hamilton Path
题目传送门:https://agc018.contest.atcoder.jp/tasks/agc018_d 题目大意: 给定一棵\(N\)个点的带权树,求最长哈密顿路径(不重不漏经过每个点一次,两点 ...
- AtCoder Grand Contest 018 E Sightseeing Plan
题意: 给定三个矩形,选定三个点,答案加上第一个点出发经过第二个点在第三个点结束的方案数,只能往右或往下走. 折腾了我半个多下午的题. 设三个矩形为$A,B,C$一个思路是枚举$B$的那个点$s(x, ...
- 【贪心】【堆】AtCoder Grand Contest 018 C - Coins
只有两维的时候,我们显然要按照Ai-Bi排序,然后贪心选取. 现在,也将人按照Ai-Bi从小到大排序,一定存在一个整数K,左侧的K个人中,一定有Y个人取银币,K-Y个人取铜币: 右侧的X+Y+Z-K个 ...
- 【贪心】AtCoder Grand Contest 018 B - Sports Festival
假设我们一开始选取所有的运动项目,然后每一轮将当前选择人数最多的运动项目从我们当前的项目集合中删除,尝试更新答案.容易发现只有这样答案才可能变优,如果不动当前选取人数最多的项目,答案就不可能变优. 我 ...
- AtCoder Grand Contest 018题解
传送门 \(A\) 根据裴蜀定理显然要\(k|\gcd(a_1,...,a_n)\),顺便注意不能造出大于\(\max(a_1,...,a_n)\)的数 int n,g,k,x,mx; int mai ...
- AtCoder Grand Contest 012
AtCoder Grand Contest 012 A - AtCoder Group Contest 翻译 有\(3n\)个人,每一个人有一个强大值(看我的假翻译),每三个人可以分成一组,一组的强大 ...
- AtCoder Grand Contest 011
AtCoder Grand Contest 011 upd:这篇咕了好久,前面几题是三周以前写的... AtCoder Grand Contest 011 A - Airport Bus 翻译 有\( ...
随机推荐
- 《剑指offer》变态跳台阶
一.题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级--它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. 二.输入描述 n级台阶 三.输出描述 一共有多少种不同的跳法 四.牛客网提 ...
- 学习Go语言之使用原子访问或互斥锁解决竞态问题
使用原子访问或互斥锁 // 解决竞态问题 package main import ( "fmt" "sync" "sync/atomic" ...
- 学习参考《Python基础教程(第3版)》中文PDF+英文PDF+源代码
python基础教程ed3: 基础知识 列表和元组 字符串 字典 流程控制 抽象(参数 作用域 递归) 异常 魔术方法/特性/迭代器 模块/标准库 文件 GUI DB 网络编程 测试 扩展python ...
- Unity Launcher类,轻松打开网页,照片,app 等
using UnityEngine; using UnityEngine.WSA; public class test : MonoBehaviour { void Start () { //打开百度 ...
- U-boot 启动内核
1:什么是UBOOT,为什么要有UBOOT? UBOOT的主要作用是用来启动linux内核,因为CPU不能直接从块设备中执行代码,需要把块设备中的程序复制到内存中,而复制之前还需要进行很多初始化工作, ...
- C++ 容器(一):顺序容器简介
C++提供了使用抽象进行高效编程的方式,标准库中定义了许多容器类以及一系列泛型函数,使程序员可以更加简洁.抽象和有效地编写程序,其中包括:顺序容器,关联容器和泛型算法.本文将简介顺序容器(vector ...
- nginx和apache
apache所占用的内存资源较多,并且处理较慢 apache的全部模块都支持动静态编译 apache对Fcgi的支持不好 apache不支持epoll apache相对于nginx是一个庞然大物 ng ...
- Centos7 zabbix3.4.6的安装部署 (一)
部署zabbix主要为了监控日常主机.服务器.Web服务器.数据库.路由器.交换机等日常设备,功能强大,稳定性好 现在通过使用虚拟机VM搭建的Centos7部署zabbix服务 实现简单监控功能 本章 ...
- sql 向上取整 向下取整 四舍五入的实例;
SELECT CEILING(23.5/4)'向上取整' ---6 :SELECT FLOOR(23.5/4)'向下取整' ---5 :SELECT ROUND(23.5/4,1)'四舍五入' --5 ...
- SQL中union union all 和in的查询效率问题
UNION用的比较多union all是直接连接,取到得是所有值,记录可能有重复 union 是取唯一值,记录没有重复 1.UNION 的语法如下: [SQL 语句 1] UNION [SQL 语句 ...