第六周练习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. jquery常用见的正则表达式

    quickexpr = /^(?:[^<]*(<[ww]+>)[^>]*$|#([w-]+)$)/  (?:…)表示是一个非捕获型 [^<]表示是以"<& ...

  2. 一个IT男的创业感言

    2014年的一月 我和高中的一个同学開始人生的第一次创业.就写点第一次创业的经验吧! 创业的最初 须要心中大概有个规划 这个规划可能是非常粗糙的 但大的方向一定要有.详细就是你进入的行业 行业已经存在 ...

  3. mac下使用brew安装svn javahl的问题

    eclipse老提示javahl太久必须得1.8以上,以前不知道什么时候在/usr/bin装过1.7的svn. 1. 删除1.7的svn sudo rm /usr/bin/svn 2.使用brew安装 ...

  4. 汉诺塔III 递推题

    题目描述: 约19世纪末,在欧州的商店中出售一种智力玩具,在一块铜板上有三根杆,最左边的杆上自上而下.由小到大顺序串着由64个圆盘构成的塔.目的是将最左边杆上的盘全部移到右边的杆上,条件是一次只能移动 ...

  5. 会用errno,事半功倍

    参考一 参考二 参考三 参考四 一. errno二. 把errno的数字转换成相应的文字说明1. 使用strerror函数2. 使用perror函数三. errno的线程/进程安全性附录 一. err ...

  6. 从实践谈iOS生命周期

    从实践谈iOS生命周期 个人感觉生命周期无论在Android,还是iOS都是很重要的概念,因为在每个声明周期的状态下我们可以做很多预加载或者处理的操作.因此在这里主要总结下ViewController ...

  7. Android音频开发之——如何播放一帧音频

    本文重点关注如何在Android平台上播放一帧音频数据.阅读本文之前,建议先读一下<Android音频开发(1):基础知识>,因为音频开发过程中,经常要涉及到这些基础知识,掌握了这些重要的 ...

  8. C#解leetcode 106. Construct Binary Tree from Inorder and Postorder Traversal

    Given inorder and postorder traversal of a tree, construct the binary tree. Note:You may assume that ...

  9. CSS入门学习(转)

    一.基础学习 1.何为CSS CSS是Cascading Style Sheets(层叠样式表)的简称,是一种标记语言,它不需要编译,可以直接由浏览器执行(属于浏览器解释型语 言). CSS文件也可以 ...

  10. Linq101-Quantifiers

    using System; using System.Collections.Generic; using System.Linq; namespace Linq101 { class Quantif ...