HDU 2669 Romantic(扩展欧几里德, 数学题)
//第一眼看题目觉得好熟悉,但是还是没想起来
//洪湖来写不出来去看了解题报告,发现是裸的 扩展欧几里得 - -
/*
//扩展欧几里得算法(求 ax+by=gcd )
//返回d=gcd(a,b);和对应于等式ax+by=d中的x,y
#define LL long long
LL extend_gcd(LL a,LL b,LL &x,LL &y){
if(a==0&&b==0) return -1;//无最大公约数
if(b==0){x=1;y=0;return a;}
LL d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
*/
/*
//扩展欧几里德 模版二
void ex_gcd(__int64 a, __int64 b, __int64 &d, __int64 &x, __int64 &y)
{
if(!b){ d = a; x = 1; y = 0;}
else
{
ex_gcd(b, a%b, d, y, x);
y -= x*(a/b);
}
} //ex_gcd
*/
//猜测:如果两者的最大公约数不是1,那么就无法达成目的,输出sorry。。。
//猜测正确^-^
//x>=0
//a * x + b * y = 1
//-> a*(x+b) + b*(y-a) = a*x + a*b + b*y - a*b = 1
#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; //返回d=gcd(a,b);和对应于等式ax+by=d中的x,y
#define LL __int64
LL extend_gcd(LL a,LL b,LL &x,LL &y){
if(a==&&b==) return -;//无最大公约数
if(b==){x=;y=;return a;}
LL d=extend_gcd(b,a%b,y,x);
y-=a/b*x;
return d;
} int main()
{
LL a, b, x, y, d;
while(scanf("%I64d%I64d",&a,&b)!=EOF)
{
d = extend_gcd(a, b, x, y);
if(d == )
{
while(x<)//output nonnegative integer X and integer Y
{
//a * x + b * y = 1
//-> a*(x+b) + b*(y-a) = a*x + a*b + b*y - a*b = 1
x = x+b;
y = y-a;
}
printf("%I64d %I64d\n",x,y);
}
else
{
printf("sorry\n");
} } return ; }
HDU 2669 Romantic(扩展欧几里德, 数学题)的更多相关文章
- HDU 2669 Romantic 扩展欧几里德---->解不定方程
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2669 Romantic (扩展欧几里得定理)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- hdu 2669 Romantic 扩展欧几里得
Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisf ...
- HDU 2669 Romantic(扩展欧几里德)
题目链接:pid=2669">http://acm.hdu.edu.cn/showproblem.php?pid=2669 Problem Description The Sky is ...
- HDU 2669 Romantic【扩展欧几里德】
裸的扩展欧几里德,求最小的X,X=((X0%b)+b)%b,每个X都对应一个Y,代入原式求解可得 #include<stdio.h> #include<string.h> ty ...
- hdu 2669 Romantic
Romantic Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Sta ...
- HDU2669 Romantic 扩展欧几里德 对我来说有陷阱
这道题对我来说有陷阱虽说是赤果果的扩展欧几里德,看样子基本攻还是不够哈,基本功夫一定要好,准备每天上那种洗脑课时分 多看看数论书,弥补一下 自己 狗一样的基础, 这道题用到了一个性质: 对于不定整数 ...
- hdu 2669 Romantic (乘法逆元)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
- HDU 2669 Romantic(裸的拓展欧几里得)
Romantic Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Su ...
随机推荐
- 在Unity项目中使用Git
(搬运自我在SegmentFault的博客) 本文参考了Unity官网的Mastering Unity Project Folder Structure - Version Control Syste ...
- JavaWeb之 Servlet执行过程 与 生命周期
Servlet的概念 什么是Servlet呢? Java中有一个叫Servlet的接口,如果一个普通的类实现了这个接口,这个类就是一个Servlet.Servlet下有一个实现类叫HttpServle ...
- EF 随机排序
/// <summary> /// 数据上下文扩展 /// </summary> public partial class dbDataContext : IUnitOfWor ...
- JVM学习总结五(番外)——VisualVM
距离上次介绍Jconsole已经时隔两周了,这期间由于工作中要用go来做一个新项目,所以精力都用在入门go上了,不过发现go语言用起来真的挺不错的,比python感觉还好点,大家没事可以了解下. ...
- 15个最好的Bootstrap设计工具推荐
摘要:Bootstrap不单单是一个框架,更确切的说,它改变了整个游戏规则.该框架使得许多应用和网站的设计开发变得简便许多,而且它将大量的HTML框架普及成了产品. Bootstrap是由前Twitt ...
- 怎么解决/bin/sh: arm-linux-gcc: not found make
1.arm-linux-gcc 环境变量没有设,所以找不到这个编译器 在/etc/profile里添加arm-linux-gcc的存放路径 sudo -s gedit /etc/profile 编 ...
- mac os x 系统安装 genymotion android 模拟器
如果你有 apk 文件 想 运行一下看看 ,但是又没有 android 设备 ,那么 genymotion 将会是一个 很好的解决方案. 1.安装 下载链接: https://cloud.geny ...
- matlab 函数的编写与调用
matlab中写个函数,在主程序中调用该函数的方法 跟其它的编程语言都一样,但是子函数与主函数要存于不同的文件中,文件名就是函数名字.文件必须保存在current directory中,才能调用. 函 ...
- RSA算法详解
1.RSA加密算法是最常用的非对称加密算法 2.RSARSA以它的三个发明者Ron Rivest, Adi Shamir, Leonard Adleman的名字首字母命名, 3.目前学术界无法证明RS ...
- 【每日scrum】NO.7
Yesterday:学习和设计路线的编程 Today:编写代码 Problem:.在设计查询参观路线的时候,整个逻辑特别的混乱,设想了各种树,图以及网的遍历问题,但经过多次与同学的交流以及网上的查询资 ...