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. 2406: C语言习题 求n阶勒让德多项式

    2406: C语言习题 求n阶勒让德多项式 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 961  Solved: 570[Submit][Status ...

  2. 获取地址栏参数,json遍历

    1. 获取地址栏参数 GetQueryString: function(name){ // 获取地址栏参数 var reg = new RegExp("(^|&)"+ na ...

  3. 《毛毛虫团队》第八次团队作业:ALPHA冲刺

    一:实验名称:软件测试与ALPHA冲刺 二:实验目的与要求 (1)掌握软件测试基础技术. (2)学习迭代式增量软件开发过程(Scrum). 三:实验步骤 任务一:各个成员今日完成的任务: 任务二:明日 ...

  4. Python -- 可迭代对象和迭代器

    5.9 可迭代对象 可迭代对象: str , list , tuple , set , dict , range 1.在Python中,但凡内部有__iter__方法的对象,都是可迭代对象 2.查看对 ...

  5. Bootstrap历练实例:默认的面板(Panels)

    Bootstrap 面板(Panels) 本章将讲解 Bootstrap 面板(Panels).面板组件用于把 DOM 组件插入到一个盒子中.创建一个基本的面板,只需要向 <div> 元素 ...

  6. (69)zabbix监控惠普打印机

    假设公司有多个楼层或者分布在不同楼,打印机自然分布很广泛,打印机缺少油墨或者卡纸了,都需要员工找IT部门.我们使用zabbix对打印机进行监控,一旦缺少油墨,zabbix发出报警,it人员能够及时更换 ...

  7. crm项目之整体内容(一)

    一.项目背景 YW公司是一家电池供应商,目前由于公司内部的需要,需要做一个CRM项目,需要每一个不同角色的员工登陆系统后处理自己的事情.其流程大致如下: 其项目包括三部分内容: 1.权限分配组件(rb ...

  8. rs485多主

    因复位时I/O口都输出高电平.如果把I/O口直接与RS-485接口芯片的驱动器使能端DE端相连,会在CPU复位其间DE为高,从而使本节点处于发送状态.如果此时总线上其它节点在发送数据,则此次数据传输将 ...

  9. CentOS7.2下Hadoop2.7.2的集群搭建

    1.基本环境: 操作系统: Centos 7.2.1511 三台虚机: 192.168.163.224  master 192.168.163.225  node1 192.168.163.226   ...

  10. HTTP认证之基本认证——Basic(一)

    导航 HTTP认证之基本认证--Basic(一) HTTP认证之基本认证--Basic(二) HTTP认证之摘要认证--Digest(一) HTTP认证之摘要认证--Digest(二) 一.概述 Ba ...