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. Javascript模式(三) 策略模式

    var data = { "username" : "zhangsan", "password" : "12345690" ...

  2. 35:字符串单词倒排 ReverseWords

    题目描述:对字符串中的所有单词进行倒排. 说明: 1.每个单词是以26个大写或小写英文字母构成: 2.非构成单词的字符均视为单词间隔符: 3.要求倒排后的单词间隔符以一个空格表示:如果原字符串中相邻单 ...

  3. Unity3D研究院之Inspector视图中的get/set使用

    get set 使用起来很方便,但是编辑时在Inspector视图中问题就来了,因为get/set的属性即使是public了,但是在Inspector视图中依然不显示..谷歌一下估计就是下面这样的答案 ...

  4. JBoss AS 7之域名直接訪问配置(The Return Of The King)

    1.6域名直接訪问配置 部署JBoss的时候,默认情况下訪问是须要加项目名字才可以訪问的. 可是怎样才可以"IP+port"訪问呢?以下将解说怎样配置. 配置域名訪问须要在项目以下 ...

  5. 在安装ubuntu时,卡在启动画面

    在我安装ubuntu时发生的情况,记录下来希望能帮助到需要帮助的朋友. 我先后尝试16.04与14.05两个版本的Ubuntu系统,方法均为:https://www.ubuntu.com/downlo ...

  6. Mysql代码建外键问题

    用下面代码建外键 运行之后 没有提示错误 但是打开建好的表格 外键并没有建立上 打开外键栏 里面并没有外键 在从表设置了外键列里面输入东西没有任何限制 成功建立应该是下面这样 什么情况???????? ...

  7. 大白第一章第四节dp例题

    入口 UVALive - 3882 #include<cstdio> using namespace std; ; int n,m,k,f[N]; int main(){ //f[i]表示 ...

  8. 详解Vue 实例中的生命周期钩子

    Vue 框架的入口就是 Vue 实例,其实就是框架中的 view model ,它包含页面中的业务处理逻辑.数据模型等,它的生命周期中有多个事件钩子,让我们在控制整个Vue实例的过程时更容易形成好的逻 ...

  9. linux c编程:gdb的使用

    首先用一个简单的打印字符的程序来做下示例 #include<stdio.h>#include<string.h>void main(){    int i=0;    char ...

  10. 为什么Java中的字符串是不可变的?

    原文链接:https://www.programcreek.com/2013/04/why-string-is-immutable-in-java/ java字符串是不可变的.不可变类只是一个不能修改 ...