Description

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

问在长度为n的环上走,每一次走k步,最后要走到z。有m个点是不可走的,问最小的k是多少

用exgcd可以解方程ax==b(mod c),把这个式子写成ax-cy==b,exgcd解出ax+cy==gcd(a,c),然后调一下系数,就能知道最小的x。

如果0到z-1的步数大于了0到某一个障碍位置的步数,说明先到障碍位置,就不行

 #include<cstdio>
#include<iostream>
#define LL long long
using namespace std;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
int n,z,m;
int b[];
inline int exgcd(int a,int b,int &x,int &y)
{
if (!b){x=;y=;return a;}
int gcd=exgcd(b,a%b,x,y);
int t=x;x=y;y=t-a/b*y;
return gcd;
}
inline int calc(int a,int b,int c)//a*x==b(mod c)
{
int x=,y=;
int tt=exgcd(a,c,x,y);
if (b%tt!=)return -;x=(x*b/tt)%c;
int ss=c/tt;
x=(x%ss+ss)%ss;
return x;
}
int main()
{
while (~scanf("%d%d%d",&n,&z,&m))
{
z--;
for(int i=;i<=m;i++)
b[i]=read()-;
for (int i=;i<=z;i++)
{
bool ok=;
int step=calc(i,z,n);
if (step==-)continue;
for (int j=;j<=m;j++)
{
int now=calc(i,b[j],n);
if (now==-||now>step)continue;
ok=;break;
}
if (ok){printf("%d\n",i);break;}
}
}
}

poj 2657

[暑假集训--数论]poj2657 Comfort的更多相关文章

  1. [暑假集训--数论]hdu2136 Largest prime factor

    Everybody knows any number can be combined by the prime number. Now, your task is telling me what po ...

  2. [暑假集训--数论]hdu1019 Least Common Multiple

    The least common multiple (LCM) of a set of positive integers is the smallest positive integer which ...

  3. [暑假集训--数论]poj2115 C Looooops

    A Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != ...

  4. [暑假集训--数论]poj1365 Prime Land

    Everybody in the Prime Land is using a prime base number system. In this system, each positive integ ...

  5. [暑假集训--数论]poj2034 Anti-prime Sequences

    Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement ...

  6. [暑假集训--数论]poj1595 Prime Cuts

    A prime number is a counting number (1, 2, 3, ...) that is evenly divisible only by 1 and itself. In ...

  7. [暑假集训--数论]poj2262 Goldbach's Conjecture

    In 1742, Christian Goldbach, a German amateur mathematician, sent a letter to Leonhard Euler in whic ...

  8. [暑假集训--数论]poj2909 Goldbach's Conjecture

    For any even number n greater than or equal to 4, there exists at least one pair of prime numbers p1 ...

  9. [暑假集训--数论]poj3518 Prime Gap

    The sequence of n − 1 consecutive composite numbers (positive integers that are not prime and not eq ...

随机推荐

  1. HTML5中的Web Workers

    https://www.cnblogs.com/yanan-boke/p/6954390.html https://segmentfault.com/a/1190000014938305 HTML5 ...

  2. Distinct Values(贪心)

    问题 D: Distinct Values 时间限制: 1 Sec  内存限制: 128 MB提交: 13  解决: 5[提交] [状态] [讨论版] [命题人:admin] 题目描述 Chiaki ...

  3. k8s1.13.0二进制部署-ETCD集群(一)

    Kubernetes集群中主要存在两种类型的节点:master.minion节点. Minion节点为运行 Docker容器的节点,负责和节点上运行的 Docker 进行交互,并且提供了代理功能.Ma ...

  4. ios基础学习

    action中调用函数方法别忘了冒号1. 各个视图之间的关系要分辨清楚 2. MVC (Model-View-Controller). In this pattern, models keep tra ...

  5. jvm | 基于栈的解释器执行过程

    一段简单的算术代码: public class Demo { public static void main(String[] args) { int a = 1; int b = 2; int c ...

  6. Dojo的declare接口

    declare(classname,[],{}) declare的第一个参数是可选的,代表类的名称 declare的第二个参数代表类的继承关系,比如继承哪一个父类,可以看到:第二个参数是一个数组,所以 ...

  7. TCP/IP协议头部结构体

    TCP/IP协议头部结构体(转) 网络协议结构体定义 // i386 is little_endian. #ifndef LITTLE_ENDIAN #define LITTLE_ENDIAN (1) ...

  8. lua在linxu和windows系统下的遍历目录的方法

    在windows下遍历目录使用lfs库:例如遍历整个目录下的所有文件 local lfs = require "lfs" function findPathName(path)  ...

  9. 【转】 VC中TCP实现 异步套接字编程的原理+代码

    所谓的异步套接字编程就是  调用了 如下函数   WSAAsyncSelect   设置了 套接字的状态为异步,有关函数我会在下面详细介绍... 异步套接字解决了 套接字编程过程中的堵塞问题 .... ...

  10. Matlab学习记录(函数)

    Matlab中的内建函数 Matlab自定义函数 用function构造函数 用inline构造函数 用syms构造符号函数 多项式相关函数 polyvalx convx 向量和矩阵运算函数 向量运算 ...