题目

//第一眼看题目觉得好熟悉,但是还是没想起来
//洪湖来写不出来去看了解题报告,发现是裸的 扩展欧几里得 - -

/*

//扩展欧几里得算法(求 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(扩展欧几里德, 数学题)的更多相关文章

  1. HDU 2669 Romantic 扩展欧几里德---->解不定方程

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

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

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

  3. hdu 2669 Romantic 扩展欧几里得

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

  4. HDU 2669 Romantic(扩展欧几里德)

    题目链接:pid=2669">http://acm.hdu.edu.cn/showproblem.php?pid=2669 Problem Description The Sky is ...

  5. HDU 2669 Romantic【扩展欧几里德】

    裸的扩展欧几里德,求最小的X,X=((X0%b)+b)%b,每个X都对应一个Y,代入原式求解可得 #include<stdio.h> #include<string.h> ty ...

  6. hdu 2669 Romantic

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

  7. HDU2669 Romantic 扩展欧几里德 对我来说有陷阱

    这道题对我来说有陷阱虽说是赤果果的扩展欧几里德,看样子基本攻还是不够哈,基本功夫一定要好,准备每天上那种洗脑课时分  多看看数论书,弥补一下 自己 狗一样的基础, 这道题用到了一个性质: 对于不定整数 ...

  8. hdu 2669 Romantic (乘法逆元)

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

  9. HDU 2669 Romantic(裸的拓展欧几里得)

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

随机推荐

  1. FLEX AS3.0 百度地图

    window xp系统 FlashBuilder4.5 先上百度下载flash api 下载地址http://developer.baidu.com/map/flash.htm 新建一个flex项目 ...

  2. 【PHP】PHP中的类与对象

    面向对象并不是PHP的关键,但PHP确实能很好的支持面向对象编程,而且类与对象也成为了PHP5的核心组成部分.PHP的面向对象特性让构建大型的复制应用成为可能,有了类与对象,就自然产生了各种编程范式和 ...

  3. JS把函数当作参数传递

    getDescPage("commonPage","/page/common/tips/tips.html",init()); $("#"+ ...

  4. Python数学运算

    python中的加减乘除比其他的语言简单,不需要对其赋值变量 (1)加减乘除 ) #加法 ) #减法 ) #乘法 ) #除法 5.0 ) #乘方 (2)判断 判断返回的是True或者False ) # ...

  5. umask设置导致的weblogic中的应用上传的文件没有权限打开

    去年,在公司的某一weblogic上部署的web应用上传文件后却没有读的权限.因为weblogic在Linux上部署,上传文件是mount到了一台安装了NFS的Windows Server上. 当时本 ...

  6. android 下滤镜效果的实现

    android 下滤镜效果的实现 滤镜过滤颜色已实现,简单版本可通过下面代码的3个参数实现黑白.红.绿...等7种过滤(RGB的7种组合). 理论上讲可以过滤为任意颜色.调整混合结果的比值就行了. p ...

  7. Redis 在windows环境下安装

    一.下载适合自己的windows版本 下载地址:https://github.com/dmajkic/redis/downloads 当你解压后,就拥有了全套的应用文件

  8. 8 C#中的字符串输出

    我们在前面已经用Console.WriteLine("*********")往dos窗口中输出过字符串.我们还定义过字符串的变量. string words ="我喜欢D ...

  9. mysql 任意连接

    例如,你想myuser使用mypassword从任何主机连接到mysql服务器的话. mysql> GRANT ALL PRIVILEGES ON *.* TO 'myuser'@'%' IDE ...

  10. Linux学习之路--启动VNC服务

    我的Linux是Fedora 13,安装方法如下: 1.打开终端,执行 # yum install -y tigervnc tigervnc-server 2.编辑/etc/sysconfi/vncs ...