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(a, b) =d(解一定存在,根据数论中的相关定理)。扩展欧几里德常用在求解模线性方程及方程组中。

译文详解:扩展欧几里得入门篇

代码:

/*HDU 2669 扩展欧几里得算法*/
#include <bits/stdc++.h>
using namespace std;
int x,y,n,m,a,b,d;
void gcdexted(int a,int b,int &d,int &x,int &y){
    if(!b){
        d=a;
        x=1;
        y=0;
    }
    else{
        gcdexted(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}
int main()
{
   while(scanf("%d%d",&a,&b)!=EOF){
       gcdexted(a,b,d,x,y);
       if(d!=1) puts("sorry");
       else{
           while(x<0){
               x+=b;
               y-=a;
           }
            printf("%d %d\n",x,y);
       }
   }
   return 0;
}
时间: 2024-08-11 05:32:18

HDU 2669 (扩展欧几里得入门)的相关文章

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 t

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 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