SGU 141.Jumping Joe 数论,拓展欧几里得,二元不等式 难度:3
141. Jumping Joe
time limit per test: 0.25 sec.
memory limit per test: 4096 KB
Joe is a frog who likes to jump a lot. In fact, that's all he does: he jumps forwards and backwards on the integer axis (a straight line on which all the integer numbers, both positive and negative are marked). At first, Joe sits next to the point marked with 0. From here, he can jump in the positive or the negative direction a distance equal to either x1 or x2. From the point where he arrived, he can jump again a distance equal to x1 or x2, in the positive or the negative direction and so on.. Joe wants to arrive next to the point marked with the number P, after exactly K jumps. You have to decide whether such a thing is possible.
Input
The input will contain four integers: x1, x2 (0 < x1 , x2 < 40 000), P (-40 000 < P < 40 000) and K (0 <= K < 2 000 000 000), separated by blanks.
Output
The first line of output will contain the word "YES", in case Joe can reach the point marked with P after exactly K jumps, or "NO", otherwise. In case the answer is "YES", the next line should contain four integers, separated by blanks: P1 , N1 , P2 and N2. P1 is the number of times Joe jumped in the positive direction a distance equal to x1. N1 is the number of times Joe jumped in the negative direction a distance equal to x1. P2 is the number of times Joe jumped in the positive direction a distance equal to x2. N2 is the number of times Joe jumped in the negative direction a distance equal to x2. In other words, you should find four non-negative integers, so that:
P1*x1 - N1*x1 + P2*x2 - N2*x2 = P
P1 + N1 + P2 + N2 = K
In case there are more quadruples (P1,N1,P2,N2) which are solutions for the problem, you may print any of them.
Sample Input
2 3 -1 12
Sample Output
YES
1 0 5 6 这题第一眼觉得是拓展欧几里得,设pn0=p0-n0,pn1=p1-n1,则可以通过拓展欧几里得得到一组满足等式1 的解,但是怎么得到四个答案数字都非负这一点卡了好久
明显,数据之间相差越小越优,且n0>=max(-pn0,0),n1>=abs(-pn1,0),p0>=max(pn0,0),p1>=abs(pn1,0)
则p0+p1+n0+n1=k>=(max(pn0,0)+max(-pn0,0)+max(pn1,0)+max(-pn1,0)),也就是说abs(pn1)+abs(pb0)<=k
又因为已经求出了一组pn1*x1+pn0*x0=pp,所以总有某个t使得(pn1-x0/gcd*t)*x1+(pn0-x1/gcd*t)*x0=pp
这里题解使用了暴力,从-40000求到40000
当确定一对pn0,pn1的时候,若从n0开始推起,那么n0=max(0,-pn0)(后面的是pn0为负数的时候p0为0,有问题的是第一次用pn0或pn1推出另一个数字的步骤,可能会导致负数,先确保这一步骤),n0+n1=(k-pn0-pn1)/2,可以解出这四个数字
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll extgcd(ll a,ll b,ll & x,ll &y){
if(b==0){
y=0;
x=1;
return a;
}
int t=extgcd(b,a%b,y,x);
y-=a/b*x;
return t;
}
ll x[2],p[2],n[2],pp,k,pn[2];
int main(){
scanf("%I64d%I64d%I64d%I64d",x+0,x+1,&pp,&k);
ll gcd=extgcd(x[0],x[1],pn[0],pn[1]);
if(pp%gcd!=0){puts("NO");return 0;}
ll r=pp/gcd;
pn[0]*=r;pn[1]*=r;
ll t[2];
t[0]=x[1]/gcd;t[1]=x[0]/gcd;
pn[0]-=40000*t[0];pn[1]+=40000*t[1];
ll temp[2],sum=abs(pn[0])+abs(pn[1]);
for(int i=-40000;i<=40000;i++){
if((k+pn[0]+pn[1])%2==0&&(abs(pn[0])+abs(pn[1]))<sum){
sum=abs(pn[0])+abs(pn[1]);
temp[0]=pn[0];
temp[1]=pn[1];
}
pn[0]+=t[0];pn[1]-=t[1];
}
pn[1]=temp[1];pn[0]=temp[0];
if(sum>k){puts("NO");return 0;}
n[0]=max(0LL,-pn[0]);
p[0]=pn[0]+n[0];
p[1]=(pn[0]+pn[1]+k)/2-p[0];
n[1]=p[1]-pn[1];
{printf("YES\n%I64d %I64d %I64d %I64d\n",p[0],n[0],p[1],n[1]);}
return 0;
}
SGU 141.Jumping Joe 数论,拓展欧几里得,二元不等式 难度:3的更多相关文章
- ACM数论-欧几里得与拓展欧几里得
ACM数论——欧几里得与拓展欧几里得 欧几里得算法: 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd ...
- BZOJ-2242 计算器 快速幂+拓展欧几里得+BSGS(数论三合一)
污污污污 2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB Submit: 2312 Solved: 917 [Submit][S ...
- [zoj 3774]Power of Fibonacci 数论(二次剩余 拓展欧几里得 等比数列求和)
Power of Fibonacci Time Limit: 5 Seconds Memory Limit: 65536 KB In mathematics, Fibonacci numbe ...
- 51 Nod 1256 乘法逆元(数论:拓展欧几里得)
1256 乘法逆元 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 给出2个数M和N(M < N),且M与N互质,找出一个数K满足0 < K ...
- poj 1845 【数论:逆元,二分(乘法),拓展欧几里得,费马小定理】
POJ 1845 题意不说了,网上一大堆.此题做了一天,必须要整理一下了. 刚开始用费马小定理做,WA.(poj敢说我代码WA???)(以下代码其实都不严谨,按照数据要求A是可以等于0的,那么结果自然 ...
- NOIP2012拓展欧几里得
拉板题,,,不说话 我之前是不是说过数据结构很烦,,,我想收回,,,今天开始的数论还要恶心,一早上听得头都晕了 先来一发欧几里得拓展裸 #include <cstdio> void gcd ...
- poj 1061 青蛙的约会+拓展欧几里得+题解
青蛙的约会+拓展欧几里得+题解 纵有疾风起 题意 两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出 ...
- poj 1061 青蛙的约会 拓展欧几里得模板
// poj 1061 青蛙的约会 拓展欧几里得模板 // 注意进行exgcd时,保证a,b是正数,最后的答案如果是负数,要加上一个膜 #include <cstdio> #include ...
- bzoj4517: [Sdoi2016]排列计数--数学+拓展欧几里得
这道题是数学题,由题目可知,m个稳定数的取法是Cnm 然后剩下n-m本书,由于编号为i的书不能放在i位置,因此其方法数应由错排公式决定,即D(n-m) 错排公式:D[i]=(i-1)*(D[i-1]+ ...
随机推荐
- Qt 控制线程的顺序执行(使用QWaitCondition,并且线程类的run函数里记得加exec(),使得线程常驻)
背景项目中用到多线程,对线程的执行顺序有要求: A.一个线程先收数据 B.一个线程处理数据 C.一个线程再将处理后的数据发送出去 要求三个线程按照ABC的顺序循环执行. 思路子类化多线程方法 重写子类 ...
- python并发
并发方式 线程(Thread) 多线程几乎是每一个程序猿在使用每一种语言时都会首先想到用于解决并发的工具(JS程序员请回避),使用多线程可以有效的利用CPU资源(Python例外).然而多线程所带来的 ...
- 小米范工具系列之六:小米范 web查找器2.x版本发布
小米范web查找器是一款快速识别端口及服务的小工具. 此工具使用java 1.8以上版本运行. 下载地址:http://pan.baidu.com/s/1c1NDSVe 文件名web finder ...
- android 模仿今日头条ViewPager+TabLayout
导入依赖库: compile 'com.android.support:design:25.3.1' 1.fg_content_demo2.xml <?xml version="1.0 ...
- Red Hat Linux相关产品iso镜像下载
Linux系统各发行版镜像下载(持续更新) http://www.linuxfly.org/post/659/ http://www.linuxidc.com/Linux/2007-09/7399.h ...
- (转)通过HTTP RESTful API 操作elasticsearch搜索数据
样例数据集 这是编造的JSON格式银行客户账号信息文档,文档schema如下: { “account_number”: 0, “balance”: 16623, “firstname”: “Brads ...
- 004-notepad++安装。
1.下载地址. 官网:https://notepad-plus-plus.org/ 2.安装.
- VS2010/MFC编程入门之二十八(常用控件:列表视图控件List Control 上)
前面一节中,鸡啄米讲了图片控件Picture Control,本节为大家详解列表视图控件List Control的使用. 列表视图控件简介 列表视图控件List Control同样比较常见, ...
- uva1366 dp
这题说的是给了 一个矩阵在每个单元内有BLOHHLUM 种的资源 Bi,j, 有YEYENUM 种的 资源Ai,j , 资 源 从 该 单 位 出 发 不能 转 弯 直 接 运 送 到 像 B 类 资 ...
- mongoose 获取某个存在的collecion 里的数据
在创建Schma 时指定对应的collection var db_url = 'mongodb://localhost:27017/test'; var db = mongoose.connect(d ...