hdu 2669(扩展欧几里得)

Romantic

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

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

题解:由ax+by = gcd(a,b) 当 a,b互素是才会有解。然后X要是尽量小的正数,假设我们得到的是 X0 ,在有解的情况下方程ax+by=c的通解为 {X0*(c/d)+k*b/gcd(a,b)} (k = ..-2,-1,0,1,2..)

所以我们可以得到在最小的X为 (X0%b+b)%b.代入得Y

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
typedef long long LL;

LL extend_gcd(LL a,LL b,LL &x,LL &y){
    if(!b){
        x=1,y = 0;
        return a;
    }else{
        LL x1,y1;
        LL d = extend_gcd(b,a%b,x1,y1);
        x = y1;
        y = x1 - a/b*y1;
        return d;
    }
}
int main()
{
    LL a,b,x,y;
    while(~scanf("%lld%lld",&a,&b)){
        LL d = extend_gcd(a,b,x,y);
        if(d!=1){
            printf("sorry\n");
        }else{
            x = (x%b+b)%b;
            y = (1-a*x)/b;
            printf("%lld %lld\n",x,y);
        }
    }
    return 0;
}
时间: 2024-11-10 07:42:32

hdu 2669(扩展欧几里得)的相关文章

hdu 1576 扩展欧几里得

(A/B)%9973=K A/B=K+9973*X A=BK+9973*X*B A%9973=n; BK%9973=n; BK=n+9973*Y (K/n)*B+(-Y/n)*9973=GCD(B,9973)=1; 求出k/n,求出k 1 /* 2 扩展欧几里得 3 扩展欧几里德算法是用来在已知a, b求解一组x,y使得ax+by = Gcd(a, b) =d(解一定存在,根据数论中的相关定理) 4 */ 5 #include<cstdio> 6 #include<iostream>

HDU RSA 扩展欧几里得

Problem Description RSA is one of the most powerful methods to encrypt data. The RSA algorithm is described as follow: > choose two large prime integer p, q> calculate n = p × q, calculate F(n) = (p - 1) × (q - 1)> choose an integer e(1 < e &l

HDU 4180 扩展欧几里得

RealPhobia Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 376    Accepted Submission(s): 151 Problem Description Bert is a programmer with a real fear of floating point arithmetic. Bert has qui

HDU 2669 (扩展欧几里得入门)

练习一下数学知识了.. [题目链接]click here~~ [题目大意]Find the nonnegative integer X and integer Y to satisfy X*a + Y*b = 1. If no such answer print "sorry" instead. 求满足式子的x和y否则输出"sorry" [解题思路]扩展欧几里得的基础了, 扩展欧几里德算法是用来在已知a, b求解一组x,y,使它们满足 等式: ax+by = gcd

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

题目大意:给两个数a和b,找出一组x,y使得a*x + b*y = 1,如果找不出输出sorry   题解:显然是用扩展欧几里得定理求解. 又扩展欧几里得定理有,如果a*x+b*y = d   要使得方程有解必有gcd(a,b)为d的约数. 而此题的d = 1  所以若gcd(a,b)!=1,则应该输出sorry #include <bits/stdc++.h> using namespace std; long long e_gcd(long long a,long long b,long

[ACM] hdu 3923 Invoker (Poyla计数,快速幂运算,扩展欧几里得或费马小定理)

Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael can control the elements and combine them to invoke a powerful skill. Vance like Kael very much so he changes the map to make Kael more powerful. In

hdu 5512 Pagodas 扩展欧几里得推导+GCD

题目链接 题意:开始有a,b两点,之后可以按照a-b,a+b的方法生成[1,n]中没有的点,Yuwgna 为先手, Iaka后手.最后不能再生成点的一方输: (1 <= n <= 20000) T组数据T <= 500; 思路:由扩展欧几里得知道对于任意正整数,一定存在整数x,y使得 x*a + y*b = gcd(a,b);并且这个gcd是a,b组成的最小正整数:同时也知道了这也是两个点之间的最小距离: 之后直接求点的个数即可: ps:这道题我竟然想到了组合游戏..明显没有说双方都要用

扩展欧几里得 HDU 1576

题意;要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1). 因为:A%9973=n; 所以:9973*y+n=A: 设:A/B=x;(可以整除) 所以:9973*y+n=B*x; 所以:B*x-9973*y=n; ①式 又因为:gcd(B,9973) = 1; 所以必存在 x1*B+9973*y1=1;②式 ②式*n=①式 所以只要求出x1,就可以得到x,又因为x=a/b,只要在 mod 9973就是答案了. 现

[ACM] hdu 3923 Invoker (Poyla计数,高速幂运算,扩展欧几里得或费马小定理)

Invoker Problem Description On of Vance's favourite hero is Invoker, Kael. As many people knows Kael can control the elements and combine them to invoke a powerful skill. Vance like Kael very much so he changes the map to make Kael more powerful. In