A game-board consists of N fields placed around a circle. Fields are successively numbered from1 to N clockwise. In some fields there may be obstacles.

Player starts on a field marked with number 1. His goal is to reach a given field marked with number Z. The only way of moving is a clockwise jump of length K. The only restriction is that the fields the player may jump to should not contain any obstacle.

For example, if N = 13, K = 3 and Z = 9, the player can jump across the fields 1, 4, 7, 10, 13, 3, 6 and 9, reaching his goal under condition that none of these fields is occupied by an obstacle.

Your task is to write a program that finds the smallest possible number K.

Input

First line of the input consists of integers N, Z and M, 2 <= N <= 1000, 2 <= Z <= N, 0 <= M <= N - 2. N represents number of fields on the game-board and Z is a given goal-field.

Next line consists of M different integers that represent marks of fields having an obstacle. It is confirmed that fields marked 1 and Z do not contain an obstacle.

Output

Output a line contains the requested number K described above.

Sample Input

9 7 2
2 3

Sample Output

3

题意:

现在我们要找一个最小的a,使得男主从一号点出发,每次跳a格,中途不遇到障碍,最终走到Z点。

思路:

有方程 1+ax=ny+z。 最小的a需要保证x正整数解,且不存在s<x,使得1+as=ny+障碍的位置。

#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
int r[],n,z,m;
void Ex_gcd(int a,int b,int &d,int &x,int &y)
{
if(b==){x=;y=;d=a;return ;}
Ex_gcd(b,a%b,d,y,x); y-=a/b*x;
}
bool check(int a)
{
int d,x,y,X;
Ex_gcd(a,n,d,X,y);
if((z-)%d!=) return false;
X=(((z-)/d*X)%(n/d)+n/d)%(n/d);
for(int i=;i<=m;i++){
Ex_gcd(a,n,d,x,y);
if((r[i]-)%d!=) continue;
x=(((r[i]-)/d*x)%(n/d)+n/d)%(n/d);
if(x<=X) return false;
} return true;
}
int main()
{
while(~scanf("%d%d%d",&n,&z,&m)){
for(int i=;i<=m;i++) scanf("%d",&r[i]);
for(int i=;i<n;i++)
if(check(i)){
printf("%d\n",i); break;
}
} return ;
}

POJ2657Comfort(扩展欧几里得基础)的更多相关文章

  1. 51nod--1256 乘法逆元 (扩展欧几里得)

    题目: 1256 乘法逆元 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < ...

  2. 同余问题(一)——扩展欧几里得exgcd

    前言 扩展欧几里得算法是一个很好的解决同余问题的算法,非常实用. 欧几里得算法 简介 欧几里得算法,又称辗转相除法. 主要用途 求最大公因数\(gcd\). 公式 \(gcd(a,b)=gcd(b,a ...

  3. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  4. UVA 12169 Disgruntled Judge 枚举+扩展欧几里得

    题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...

  5. UVA 10090 Marbles 扩展欧几里得

    来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...

  6. POJ 1061 青蛙的约会 扩展欧几里得

    扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...

  7. 【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】

    Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到C ...

  8. poj 2891 扩展欧几里得迭代解同余方程组

    Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...

  9. poj 2142 扩展欧几里得解ax+by=c

    原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...

随机推荐

  1. VueJS构造器:new Vue({})

    构造器 每个 Vue.js 应用都是通过构造函数 Vue 创建一个 Vue 的根实例来启动的: var vm = new Vue({ // 选项 }) 属性与方法 每个 Vue 实例都会代理其 dat ...

  2. Java储存过程

    存储过程:是指保存在数据库并在数据库端执行的程序. CallableStatement 对象为所有的 DBMS 提供了一种以标准形式调用已储存过程的方法.已储存过程储存在数据库中. 对已储存过程的调用 ...

  3. 效率提升最重要的原则 - Doing one thing at a time

    前段时间流行的时间管理方法 - url=NotLrz-4f4eCgENFAECrXNw88mSLoJ2Rc2MrkP4aes1yQvPjNQRlmdYcbz9oP9U8JoBzJeY-DSUhhInx ...

  4. 详解Linux三剑客之awk

    第一篇 awk简介与表达式实例 一种名字怪异的语言 模式扫描和处理,处理数据和生成报告. awk不仅仅是linux系统中的一个命令,而且是一种编程语言:它可以用来处理数据和生成报告(excel):处理 ...

  5. 删除rabbitmq的队列和队列中的数据

      访问http://{rabbitmq安装IP}:15672,帐号guest,密码guest(也可以使用自己创建的帐号). 登录后访问http://{rabbitmq安装IP}:15672/#/qu ...

  6. mybatis--foreach,choose 小结

    写博客个人不喜欢写那种长篇大论.富有文採与哲学的文章,搞开发的就喜欢直击重点,仅仅记录重要的信息就好了,以后查看的时候也很方便! mybatis 中 在if语句或when中 假设推断一个字段是否和1同 ...

  7. visual studio 2010 c++ 打印 Hello world

    由于好奇心驱使温习下c高级简化语言语言(个人解释可能不太准确).下面用visual studio 2010 实现 HelloWord 打印 第一步:visual studio 2010 打开.文件-- ...

  8. UITableView使用指南

    本文转载至 http://blog.csdn.net/yu0089/article/details/8227402 一.概述 UITableView是iOS开发比不可少也是最重要的一个控件类.可以说任 ...

  9. Java 内存分配全面浅析(转)

    本文将由浅入深详细介绍Java内存分配的原理,以帮助新手更轻松的学习Java.这类文章网上有很多,但大多比较零碎.本文从认知过程角度出发,将带给读者一个系统的介绍. 进入正题前首先要知道的是Java程 ...

  10. centos设置固定IP方法

    首先网络模式设为桥接 [root@centos64 ~]# vi /etc/sysconfig/network-scripts/ifcfg-eth0 DEVICE=eth0HWADDR=00:0C:2 ...