第六周练习I题

I - 数论,线性方程

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

 

Description

The Sky is Sprite.  The Birds is Fly in the Sky.  The Wind is Wonderful.  Blew Throw the Trees  Trees are Shaking, Leaves are Falling.  Lovers Walk passing, and so are You.  ................................Write in English class by yifenfei 
Girls are clever and bright. In HDU every girl like math. Every girl like to solve math problem!  Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead. 
 

Input

The input contains multiple test cases.  Each case two nonnegative integer a,b (0<a, b<=2^31) 
 

Output

output nonnegative integer X and integer Y, if there are more answers than the X smaller one will be choosed. If no answer put "sorry" instead. 
 

Sample Input

77 51
10 44
34 79
 

Sample Output

2 -3
sorry
7 -3
 
 扩展欧几里德算法详解
http://www.cnblogs.com/hfc-xx/p/4744462.html
题解:紫书p313
扩展欧几里得算法是 用来在已知a,b 求解一组x,y使得x*a+y*b=gcd(a,b)

因为已知欧几里得算法gcd(a,b)=gcd(b,a%b)  所以x*a+y*b=gcd(a,b)=gcd(b,a%b)=x*b+y*a%b=x*b+y*(a-a/b*b)=y*a+(x-a/b*y)*b;

注意;a-a/b*b=a%b 这样就将a,b的线性组合化简b为a%b与的线性组合. 根据我的输出图可以看到: a,b都在减小,当b减小到0时, 我们就可以得出x=1,y=0; 然后递归回去就可以求出最终的x,y了

 
#include<iostream>
using namespace std;
void gcd(int a,int b,int & d,int &x,int &y)
{
if(!b)
{
d=a;x=;y=;
// cout<<d<<" "<<x<<" "<<y<<endl; //输出
}
else
{
gcd(b,a%b,d,y,x);
// cout<<b<<" "<<a%b<<" "<<d<<" "<<y<<" "<<x<<endl; //输出
y-=a/b*x;
// cout<<x<<" "<<y<<endl; //输出
}
}
int main()
{
int a,b,d,x,y;
while(cin>>a>>b)
{
gcd(a,b,d,x,y);
if(d!=) cout<<"sorry"<<endl;
else
{
while(x<) //x不能小于0
x+=b,y-=a;
cout<<x<<" "<<y<<endl;
}
}
return ;
}

HDU2669 第六周练习I题(扩展欧几里算法)的更多相关文章

  1. SGU 106 The equation 扩展欧几里得好题

    扩展欧几里得的应用……见算法竞赛入门经典p.179 注意两点:1.解不等式的时候除负数变号 2.各种特殊情况的判断( a=0 && b=0 && c=0 ) ( a=0 ...

  2. 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个点,求射 ...

  3. poj 2891 扩展欧几里得迭代解同余方程组

    Reference: http://www.cnblogs.com/ka200812/archive/2011/09/02/2164404.html 之前说过中国剩余定理传统解法的条件是m[i]两两互 ...

  4. poj 2142 扩展欧几里得解ax+by=c

    原题实际上就是求方程a*x+b*y=d的一个特解,要求这个特解满足|x|+|y|最小 套模式+一点YY就行了 总结一下这类问题的解法: 对于方程ax+by=c 设tm=gcd(a,b) 先用扩展欧几里 ...

  5. Codeforces7C 扩展欧几里得

    Line Time Limit: 1000MS   Memory Limit: 262144KB   64bit IO Format: %I64d & %I64u Submit Status ...

  6. 【数学】【NOIp2012】同余方程 题解 以及 关于扩展欧几里得与同余方程

    什么是GCD? GCD是最大公约数的简称(当然理解为我们伟大的党也未尝不可).在开头,我们先下几个定义: ①a|b表示a能整除b(a是b的约数) ②a mod b表示a-[a/b]b([a/b]在Pa ...

  7. 【扩展欧几里得】NOIP2012同余方程

    题目描述 求关于 x 的同余方程 ax ≡ 1 (mod b)的最小正整数解. 输入输出格式 输入格式: 输入只有一行,包含两个正整数 a, b,用一个空格隔开. 输出格式: 输出只有一行,包含一个正 ...

  8. hdu_1576A/B(扩展欧几里得求逆元)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1576 A/B Time Limit: 1000/1000 MS (Java/Others)    Me ...

  9. Root(hdu5777+扩展欧几里得+原根)2015 Multi-University Training Contest 7

    Root Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Su ...

随机推荐

  1. cannot be deleted directly via the port API: has device owner network:floatingip

  2. Linux散列表(一)——操作函数

    散列表(又名哈希表)仅仅需要一个包含单一指针的链表头.它是双向链表的变体.它不同于双链表——表头和结点使用相同的结构体——散列表对表头和结点有不同的定义.如下: struct hlist_head { ...

  3. angular2的管道初体验

    ng管道是应用里面比较重要的一个技术,他涉及很多功能 包括排序过滤 废话不说 直接上代码 新建个文件夹吧这个samplepipe.ts放进去 然后 你要做什么 在里面写管道代码 然后在app.modu ...

  4. android string.xml前后加空格的技巧

    方法1: <string name="password">密    码</string>&#160 这个就代表着空格. 方法2:用\u0020代表空 ...

  5. PHP Ctype函数(转)

    Ctype函数是PHP内置的字符串体测函数.主要有以下几种 ctype_alnum -- Check for alphanumeric character(s)检测是否是只包含[A-Za-z0-9] ...

  6. 【IOS】 XML解析和xml转plist文件(GDataXML)

    iOS对于XML的解析有系统自带的SDK--NSXMLParser,鄙人愚拙,只会用GDataXML进行解析,这里仅介绍GData的使用.(以下图为例) 1.对于一个xml文件,先读取出来 NSDat ...

  7. sublime text3 插件配置

    (转) sublme text 全程指引:http://www.cnblogs.com/figure9/p/sublime-text-complete-guide.html 使用Package Con ...

  8. centos6.3 配置NTP服务

    NTP简介: NTP(Network Time Protocol)是用来使计算机时间同步化的一种协议,它可以使计算机对其服务器或时钟源做同步化,它可以提供高精准度的时间校正.本例讲解如何在CentOS ...

  9. maya和Unity中的坐标系旋转

    maya软件是用的右手坐标系,默认旋转顺序是ZYX,即先绕Z轴旋转,再绕Y轴旋转,最后绕X轴旋转. 比如在maya软件中,右侧的旋转顺序是可选的,默认的选择是“XYZ”,其实物体旋转顺序是倒着念,即上 ...

  10. 千万数量级分页存储过程 +AspNetPager现实分页

    存储过程 USE [ForeignTradeDB] GO /****** Object: StoredProcedure [dbo].[CommonGetDataPager] Script Date: ...