hdu 2669 题目链接
Romantic
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3898 Accepted Submission(s): 1614
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.
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)
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
类型1:a*x+b*y=1
code:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
__int64 x,y;
__int64 ext_gcd(__int64 a,__int64 b)//返回值为a,b的最大公约数
{
__int64 tem;
if(b==0)
{
x=1;y=0;
return a;
}
__int64 r=ext_gcd(b,a%b);
tem=x;
x=y;
y=tem-a/b*y;
return r;
}
int main()
{
__int64 a,b,d;
while(scanf("%I64d%I64d",&a,&b)!=EOF)
{
d=ext_gcd(a,b);
if(d>1)
{
printf("sorry\n");
continue;
}
while(x<0)
{
x+=b;
y-=a;
}
printf("%I64d %I64d\n",x,y);
}
return 0;
}
题目链接
青蛙的约会
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 9884 | Accepted: 1880 |
Description
两只青蛙在网上相识了,它们聊得很开心,于是觉得很有必要见一面。它们很高兴地发现它们住在同一条纬度线上,于是它们约定各自朝西跳,直到碰面为止。可是它们出发之前忘记了一件很重要的事情,既没有问清楚对方的特征,也没有约定见面的具体位置。不过青蛙们都是很乐观的,它们觉得只要一直朝着某个方向跳下去,总能碰到对方的。但是除非这两只青蛙在同一时间跳到同一点上,不然是永远都不可能碰面的。为了帮助这两只乐观的青蛙,你被要求写一个程序来判断这两只青蛙是否能够碰面,会在什么时候碰面。
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。
我们把这两只青蛙分别叫做青蛙A和青蛙B,并且规定纬度线上东经0度处为原点,由东往西为正方向,单位长度1米,这样我们就得到了一条首尾相接的数轴。设青蛙A的出发点坐标是x,青蛙B的出发点坐标是y。青蛙A一次能跳m米,青蛙B一次能跳n米,两只青蛙跳一次所花费的时间相同。纬度线总长L米。现在要你求出它们跳了几次以后才会碰面。
Input
输入只包括一行5个整数x,y,m,n,L,其中x≠y < 2000000000,0 < m、n < 2000000000,0 < L < 2100000000。
Output
输出碰面所需要的跳跃次数,如果永远不可能碰面则输出一行"Impossible"
Sample Input
1 2 3 4 5
Sample Output
4
类型2:a*x+b*y=c
code:
#include<stdio.h>
#include<iostream>
#include<algorithm>
using namespace std;
__int64 x1,y1;
__int64 ext_gcd(__int64 a,__int64 b)//fanhuizhi wei zuidagongyueshu
{
__int64 tem;
if(b==0)
{
x1=1;y1=0;
return a;
}
__int64 r=ext_gcd(b,a%b);
tem=x1;
x1=y1;
y1=tem-a/b*y1;
return r;
}
int main()
{
__int64 x,y,m,n,l;
__int64 a,b,c,d;
while(scanf("%I64d%I64d%I64d%I64d%I64d",&x,&y,&m,&n,&l)!=EOF)
{
a=n-m;
c=x-y;
d=ext_gcd(a,l);
if(c%d)
{
printf("Impossible\n");
continue;
}
a=a/d;l=l/d;c=c/d;
d=ext_gcd(a,l);
x1=c*x1;
y1=c*y1;
x1=(x1%l+l)%l;
printf("%I64d\n",x1);
}
return 0;
}
利用扩展欧几里得算法求解不定方程a * x + b * y = n的整数解的求解全过程,步骤如下:1、先计算Gcd(a,b),若n不能被Gcd(a,b)整除,则方程无整数解;否则,在方程两边同时除以Gcd(a,b),得到新的不定方程a' * x + b' * y = n',此时Gcd(a',b')=1;
2、利用扩展欧几里德算法求出方程a' * x + b' * y = 1的一组整数解x0,y0,则n' * x0,n' * y0是方程a' * x + b' * y = n'的一组整数解;
3、根据数论中的相关定理,可得方程a' * x + b' * y = n'的所有整数解为:
x = n' * x0 + b' * t
y = n' * y0 - a' * t (t=0,1,2,……)
上面的解也就是a * x + b * y = n 的全部整数解