HDU 2669 第六周 I题
Description
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
Output
Sample Input
Sample Output
#include <stdio.h>
void gcd(int a,int b,int &d,int &x,int &y)
{
if(!b)
{
d=a;
x=;
y=;
}
else
{
gcd(b,a%b,d,y,x);
//printf("a=%d b=%d d=%d y=%d x=%d\n",a,b,d,y,x);
y-=x*(a/b);
//printf("y=-%d*(%d/%d) %d \n",x,a,b,y);
}
} int main()
{
int a,b,d,x,y;
while(scanf("%d%d",&a,&b)==)
{
gcd(a,b,d,x,y);
if(d==) //d=gcd(a,b),d为a,b的最大公约数。
{
if(x>)
printf("%d %d\n",x,y);
else
{
while(x<=) //求另外的解 例如:5x+6y=1 第一种解:x=-1,y=1 第二种 x=5 y=-4
{ // 这里通过x=x+b和y=y-a来算。 就等于 (x+b)*a+(y-a)*b 最终算式结果还是不变的
x+=b;
y-=a;
}
printf("%d %d\n",x,y);
}
}
else
printf("sorry\n");
}
return ;
}
/*int main()
{
int a,b,d,x,y;
while(scanf("%d%d",&a,&b)==2)
{
gcd(a,b,d,x,y);
//printf("%d %d %d\n",x,y,d);
if(x%d==0&&y%d==0&&x/d>0)
printf("%d %d\n",x/d,y/d);
else
printf("sorry\n");
}
return 0;
}*/
网上的:
#include<cstdio>
using namespace std;
int exgcd(int a,int b,int &x,int &y)
{
if(b==)
{
x=;
y=;
return a;
}
int r=exgcd(b,a%b,x,y);
//printf("a=%d b=%d\n",a,b);
int t=x;
//printf("t=%d\n",x);
x=y;
//printf("x=%d\n",y);
y=t-a/b*y;
//printf("y=%d\n",y);
return r;
}
int main()
{
int a,b,x,y,m;
while(scanf("%d%d",&a,&b)!=EOF)
{ m=exgcd(a,b,x,y);
if(m==)
{
while(x<)
{
x+=b;
y-=a;
}
printf("%d %d\n",x,y);
} else
printf("sorry\n");
}
return ;
}
HDU 2669 第六周 I题的更多相关文章
- hdu 4548 第六周H题(美素数)
第六周H题 - 数论,晒素数 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u De ...
- HDU 1465 第六周L题
Description 大家常常感慨,要做好一件事情真的不容易,确实,失败比成功容易多了! 做好“一件”事情尚且不易,若想永远成功而总从不失败,那更是难上加难了,就像花钱总是比挣钱容易的道理一样. ...
- HDU 1405 第六周 J题
Description Tomorrow is contest day, Are you all ready? We have been training for 45 days, and all ...
- 程序设计入门—Java语言 第六周编程题 1 单词长度(4分)
第六周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 1 单词长度(4分) 题目内容: 你的程序要读入一行文本,其中以空格分隔为若干个单词,以'.'结束.你要输出这行文本中每个单词的长度.这 ...
- Codeforces 559A 第六周 O题
Description Gerald got a very curious hexagon for his birthday. The boy found out that all the angle ...
- HDU1465 第六周L题(错排组合数)
L - 计数,排列 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- 第六周 E题 期望.....
Description Given a dice with n sides, you have to find the expected number of times you have to thr ...
- 第六周 N题
Description As Harry Potter series is over, Harry has no job. Since he wants to make quick money, (h ...
- 第六周O题(等边三角形个数)
O - 计数 Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Descripti ...
随机推荐
- mac jdk设置
mac系统一般默认会安装jdk 1.6,路径为/System/Library/Java/JavaVirtualMachines/1.6.0.jdk,此Jdk为系统默认jdk; 但某些框架/应用要求jd ...
- javaweb学习总结二十四(servlet经常用到的对象)
一:ServletConfig对象 1:用来封装数据初始化参数,在服务器web.xml配置文件中可以使用<init-param>标签配置初始化参数. 2:实例演示 web.xml文件中配置 ...
- javascript中Math ceil(),floor(),round()三个函数的对比
Math.ceil()执行的是向上舍入 Math.floor()执行向下舍入 Math.round()执行标准舍入 一下是一些补充: ceil():将小数部分一律向整数部分进位. 如: Math.ce ...
- HDU 2845 Beans (DP)
Beans Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit Status ...
- OpenShare新功能@2014年第三季度
新功能@2014第三季度 新增"多租户"的支持:巨型企业集团的私有云需要多租户模式,现在OpenShare可以从根本上完美支持 租户之间完全隔离,这是最高安全性 同时每个租户可以使 ...
- C#反射(转载)
[转]C#反射 反射(Reflection)是.NET中的重要机制,通过放射,可以在运行时获得.NET中每一个类型(包括类.结构.委托.接口和枚举等)的成员,包括方法.属性.事件,以及构造函数等. ...
- android:launchMode概述
android:launchMode An instruction on how the activity should be launched. There are four modes that ...
- C#去掉周六周日的算法
/// <summary> /// 用来获取工作日(不含周六周日) /// </summary> /// <param name="dtSub"> ...
- chcon可实现对文件的SEAndroid安全标签的修改
chcon可实现对文件的SEAndroid安全标签的修改 参考使用如下: chcon -u u system/app/ chcon -r object_r system/app/ chcon -t s ...
- redis setnx 分布式锁
private final String RedisLockKey = "RedLock"; private final long altTimeout = 1 * 60 * 60 ...