Romantic

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10883    Accepted Submission(s):
4610

Problem 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
 
翻译:输入a和b,找到x和y满足ax+by=1,x为正数。
分析:显然是扩展欧几里得定理的模板题,当gcd(a,b)=1时,有解,然而扩欧模板解出来是特解,不能保证x为正数,需要遍历通解。通解公式:x = x0 + (b/gcd)*t; y = y0 + (a/gcd)*t;
注意:为什么是b/gcd和a/gcd,而不是b和a?
a(x+b*t/gcd) + b(y-a*t/gcd) = gcd;
ax + a*b*t/gcd + by - b*a*t/gcd = gcd = ax + by;
对于x = x0 + b*t和y = y0 - a*t;
a(x+bt) + b(y-at) = gcd
ax+abt + by-abt = gcd = ax + by
显然b/gcd比b更小,通解找得全面一些。
 #include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<math.h>
#include<string>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
// ax + by = gcd(a,b)
ll exgcd(ll a, ll b, ll &x, ll &y)//扩展欧几里德定理
{
if(b==)//终有一次a%b传进来是0,递归出口
{
x=;y=;
return a;
}
ll q=exgcd(b,a%b,y,x);
//最终递归出来,y1=1,x1=0
y=y-(a/b)*x;
//后面的y相当于下一个递归的x2,x相当于下一个递归的y2,符合推导公式
//x1=y2; y1=x2-[a/b]*y2;
return q;
} int main()
{
ll a,b;
while(scanf("%lld %lld",&a,&b)!=EOF)
{
ll x,y;
ll gcd=exgcd(a,b,x,y);
if(gcd==)
{
while(x<)
{
x=x+b;
y=y-a;
}
printf("%lld %lld\n",x,y);
}
else printf("sorry\n");
}
return ;
}

hdu2669-Romantic-(扩展欧几里得定理)的更多相关文章

  1. HDU 2669 Romantic (扩展欧几里得定理)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...

  2. poj1061-青蛙的约会-(贝祖定理+扩展欧几里得定理+同余定理)

    青蛙的约会 Time Limit: 1000MS   Memory Limit: 10000K Total Submissions:132162   Accepted: 29199 Descripti ...

  3. poj 1061(扩展欧几里得定理求不定方程)

    两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面.它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止.可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特 ...

  4. hdu_2669 Romantic(扩展欧几里得)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2669 Romantic Time Limit: 2000/1000 MS (Java/Others)  ...

  5. hdu 2669 Romantic 扩展欧几里得

    Now tell you two nonnegative integer a and b. Find the nonnegative integer X and integer Y to satisf ...

  6. hdu3579-Hello Kiki-(扩展欧几里得定理+中国剩余定理)

    Hello Kiki Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

  7. hdu1573-X问题-(扩展欧几里得定理+中国剩余定理)

    X问题 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  8. poj2115-Looooops-(扩展欧几里得定理)

    C Looooops Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:33752   Accepted: 9832 Descri ...

  9. 扩展欧几里得 求ax+by == n的非负整数解个数

    求解形如ax+by == n (a,b已知)的方程的非负整数解个数时,需要用到扩展欧几里得定理,先求出最小的x的值,然后通过处理剩下的区间长度即可得到答案. 放出模板: ll gcd(ll a, ll ...

随机推荐

  1. admin源码解析以及仿照admin设计stark组件

    ---恢复内容开始--- admin源码解析 一 启动:每个APP下的apps.py文件中. 首先执行每个APP下的admin.py 文件. def autodiscover(): autodisco ...

  2. 报错,但不影响运行ERROR: JDWP Unable to get JNI 1.2 environment, jvm->GetEnv() return code = -2

    参考:http://blog.csdn.net/zxl0016/article/details/7327125 eclipse 3.4+jdk1.6 编译正常通过,运行debug模式时报错 ERROR ...

  3. 权重比较(id class 标签)

    权重的取值: 按照 id   class  标签 的顺序在其位置上标出1或者0 例如下面的例子 <!DOCTYPE html> <html lang="en"&g ...

  4. sql server转oracle需要注意的几点

    1.  字符型的字段相加需要用“||”,如果用“+”的话,会报“无效的数字”的错误.   2.  Top 1 类似的脚本需要通过where rownum<=1来实现.   3.  ISNULL函 ...

  5. ORACLE 归档日志打开关闭方法

    一 设置为归档方式 1 sql> archive log list;   #查看是不是归档方式 2 sql> alter system set log_archive_start=true ...

  6. java BASE64流 输出图片。

    亲测3个请求都可用,没有测试性能问题.仅供参考 BASE64Decoder Eclipsse 类可能引用不了解决方案链接:http://blog.csdn.net/JBxiaozi/article/d ...

  7. 22.天眼查cookie模拟登陆采集数据

    通过账号登录获取cookies,模拟登录(前提有天眼查账号),会员账号可查看5000家,普通只是100家,同时也要设置一定的反爬措施以防账号被封.拿有权限的账号去获取cookies,去访问页面信息,不 ...

  8. 如何利用Social Listening从社会化媒体中“提炼”有价值的信息?

    本文转自知乎 作者:苏格兰折耳喵 ----------------------------------------------------- 在本文中,笔者将会介绍大数据分析主要的处对象---社会化媒 ...

  9. 5. MYSQL问题:Access denied for user 'root'@'localhost' (using password:YES)

    开发Web项目时,连接MYSQL数据库,出现问题:Access denied for user 'root'@'localhost' (using password:YES).       解决方案: ...

  10. scrapy之parallel

    Limiting Parallelism jcalderone May 22nd, 2006 This blog has moved! Read this post and its comments ...