[暑假集训--数论]poj2657 Comfort

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到某一个障碍位置的步数,说明先到障碍位置,就不行

 1 #include<cstdio>
 2 #include<iostream>
 3 #define LL long long
 4 using namespace std;
 5 inline LL read()
 6 {
 7     LL x=0,f=1;char ch=getchar();
 8     while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
 9     while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
10     return x*f;
11 }
12 int n,z,m;
13 int b[100010];
14 inline int exgcd(int a,int b,int &x,int &y)
15 {
16     if (!b){x=1;y=0;return a;}
17     int gcd=exgcd(b,a%b,x,y);
18     int t=x;x=y;y=t-a/b*y;
19     return gcd;
20 }
21 inline int calc(int a,int b,int c)//a*x==b(mod c)
22 {
23     int x=0,y=0;
24     int tt=exgcd(a,c,x,y);
25     if (b%tt!=0)return -1;x=(x*b/tt)%c;
26     int ss=c/tt;
27     x=(x%ss+ss)%ss;
28     return x;
29 }
30 int main()
31 {
32     while (~scanf("%d%d%d",&n,&z,&m))
33     {
34         z--;
35         for(int i=1;i<=m;i++)
36             b[i]=read()-1;
37         for (int i=1;i<=z;i++)
38         {
39             bool ok=1;
40             int step=calc(i,z,n);
41             if (step==-1)continue;
42             for (int j=1;j<=m;j++)
43             {
44                 int now=calc(i,b[j],n);
45                 if (now==-1||now>step)continue;
46                 ok=0;break;
47             }
48             if (ok){printf("%d\n",i);break;}
49         }
50     }
51 }

poj 2657

时间: 2025-01-07 15:03:55

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

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

Everybody in the Prime Land is using a prime base number system. In this system, each positive integer x is represented as follows: Let {pi}i=0,1,2,... denote the increasing sequence of all prime numbers. We know that x > 1 can be represented in only

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

Given a sequence of consecutive integers n,n+1,n+2,...,m, an anti-prime sequence is a rearrangement of these integers so that each adjacent pair of integers sums to a composite (non-prime) number. For example, if n = 1 and m = 10, one such anti-prime

2017暑假集训前总结和规划

距离大一进来已经一年了啊,感觉还是啥也不会,哎,太差了,总结一下这一年都学了写什么吧! 大一寒假开始专题,刷过的有:dp,dfs和bfs,数论(gcd拓展gcd,欧拉定理等等,但是中国剩余定理没学,等复习的时候再学吧),并查集,最短路(bellman-fprd,dijkstra,floyd-warshall,spfa),最小生成树(prim,kruskal),线段树,二分三分 大一下学期有:拓扑排序,基础计算几何(直线线段相交,快速排除实验,跨立实验),矩阵快速幂,博弈基础(nim博弈,威佐夫博

暑假集训结束

暑假集训结束了!!! 说实话其实还是有些遗憾的,我没有和同学一起去诸暨集训,但是我还是很高兴,我的同学将试题都发给了我,以至于我没有落后太多,所以这里致谢机房其他4位大佬. 我重新翻了一下我的做题记录,虽然做题不多,但是至少每道题都有收益,于是想要总结一下. 6.16 期末考试告一段落 6.17 复习了并查集,知道了并查集记录链头和链长的方法P1196 6.18 继续研究DP,加深了树形DP,学习了悬线法,用此方法解决了以前乱搞的题 6.19 复习状压DP 6.20 pb学哥来讲课了,然后学习了

暑假集训0815

最近打了两场多校和一场中期比赛 中期比赛的话也就那么回事= =水题集合(从各种意义上 两场多校被各种虐,已经弱到谁都可以上(rbq)的程度了TOT 团队配合还要慢慢磨合,毕竟刚刚组队不久 这两次比赛有好几题都是差一点就想出来,毕竟还是图样 继续加油(shui)吧 还有就是一定要好好读题啊!最好读完题之后就手算一下数据,以防理解错题意.如果理解错了不会做就罢了,像中期比赛的时候读完题愉悦的打完代码才发现理解错题意了简直不能忍(论坑队友的正确姿势) Multi-University Training

暑假集训7.11 字符串回文暴力

#include #include #include #include #include #include #include #include #include #include #include using namespace std; typedef long long ll; typedef unsigned long long ull; #define MM(a,b) memset(a,b,sizeof(a)); const double eps = 1e-10; const int i

暑假集训0808

来HIT参加暑假集训也有将近一周了,一直什么都没写= =. 记一下今天的比赛吧,以后争取每天更新一篇总结. 我是弱比.只能出6题. A:POJ1417 很容易发现yes表示两个人是同一类,no是不同类,然后怎么判断方案是否唯一我就不会了...我是有多么的弱...类似于背包DP,就是用前i个大类填满j个人数的方案. B:HDU1711 裸KMP C:POJ 2503 一个字典的实现.很显然的Trie.(我就想说出题人就不能给个n么..读入麻烦死..) D:POJ2828  比赛的时候还是想的有问题

2017暑假集训~心路历程

2017年暑假集训的个人赛阶段已经结束了. 回想这7场比赛,前几场发挥的还可以. 但是第4场之后开始状态开始下滑.第4场是在TOJ上做的,题数马马虎虎,就是罚时有点多. 第5场的FOJ的比赛打得十分糟糕.因为并不熟悉这个OJ, 赛前测试工作也没有作足. 比赛的时候各种意想不到的CE,WA.这样一来我前两小时只过了一题. 不过最后两小时至少翻到了不至于垫底的位置…… 第6场发挥也不好,被一道树形DP搞住了(这应该是我的强项),卡了一个半小时发现自己题目读错了.结果还是想不出来……然后另一道贪心因为

暑假集训(2)第五弹 ----- Who&#39;s in the Middle(poj2388)

G - Who's in the Middle Crawling in process... Crawling failed Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'median