POJ2657Comfort(扩展欧几里得基础)
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
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
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(扩展欧几里得基础)的更多相关文章
- 51nod--1256 乘法逆元 (扩展欧几里得)
题目: 1256 乘法逆元 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < ...
- 同余问题(一)——扩展欧几里得exgcd
前言 扩展欧几里得算法是一个很好的解决同余问题的算法,非常实用. 欧几里得算法 简介 欧几里得算法,又称辗转相除法. 主要用途 求最大公因数\(gcd\). 公式 \(gcd(a,b)=gcd(b,a ...
- 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个点,求射 ...
- 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- ...
- UVA 10090 Marbles 扩展欧几里得
来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...
- POJ 1061 青蛙的约会 扩展欧几里得
扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...
- 【64测试20161112】【Catalan数】【数论】【扩展欧几里得】【逆】
Problem: n个人(偶数)排队,排两行,每一行的身高依次递增,且第二行的人的身高大于对应的第一行的人,问有多少种方案.mod 1e9+9 Solution: 这道题由1,2,5,14 应该想到C ...
- poj 2891 扩展欧几里得迭代解同余方程组
Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...
- poj 2142 扩展欧几里得解ax+by=c
原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...
随机推荐
- 面试宝典之预处理、const与sizeof
#include <stdio.h> #define SUB(x, y) x - y #define ACCESS_BEFORE(element, offset, value) *SUB( ...
- mini2440裸机试炼之——Uart与pc端实现文件、字符传输
1. 波特率(Baud rate)即调制速率,1波特即指每秒传输1个符号. 2. 非FIFO模式,即数据传输不利用FIFO缓存,一个字节一个字节地传输. 3. 位能够用来推断发送缓存器中是否为空 ...
- 一份还热乎的蚂蚁面经(已拿Offer)!附答案!!
本文来自我的知识星球的球友投稿,他在最近的校招中拿到了蚂蚁金服的实习生Offer,整体思路和面试题目由作者--泽林提供,部分答案由Hollis整理自知识星球<Hollis和他的朋友们>中「 ...
- (4.5.4)Android測试TestCase单元(Unit test)測试和instrumentationCase单元測试
Android单元和instrumentation单元測试 Developing Android unit and instrumentation tests Android的单元測试是基于JUnit ...
- 有关SQL注入的知识
SQL注入攻击是非常令人讨厌的安全漏洞,是所有的web开发人员,不管是什么平台,技术,还是数据层,需要确信他们理解和防止的东西.不幸的是,开发人员往往不集中花点时间在这上面,以至他们的应用,更糟糕的是 ...
- Eclipse工程前面有个红色的感叹号的解决办法
今天从SVN下载下工程之后,编译完,发现有两个工程有个红色的感叹号,一直没找到什么原因,问百度老师,发现问题的解决办法了. 1.先在控制台上点击Problems 如果控制台没有Problems,点击工 ...
- vs2013数据库连接对应的dll
mysql for visual studio 1.1.1mysql connector net 6.3.9mysql connector/odbc 5.3
- Docker入门系列5:常见问题小结
重启容器 再次运行容器: docker start container_id 然后 docker attach container_id 就可以继续下命令了. [编辑]命名 --name [编辑]端口 ...
- Kindeditor 修改内容时如何不让 及 <> 被自动转义
$html = str_replace(' ', ' ', $html); $html = str_replace('>', '>', $html); $html ...
- 首选项框架PreferenceFragment部分源代码分析
由于要改一些settings里面的bug以及之前在里面有做过勿扰模式,准备对勿扰模式做一个总结,那先分析一下settings的源代码,里面的核心应该就是android3.0 上面的首选项框架Prefe ...