模板:

int Extend_Euclid(int a, int b, int &x, int &y){
        if(b == 0){
            x = 1;

y = 0;
            return a;
        }
        else{
            int gcd,t;
            gcd = Extend_Euclid(b, a%b, x, y);
            t = x;
            x = y;
            y = t - (a / b) * y;
            return gcd;
        }

}

详见:http://www.cnblogs.com/yuelingzhi/archive/2011/08/13/2137582.html

hdu 2669

Sample Input
77 51
10 44
34 79
 
Sample Output
2 -3
sorry
7 -3

求 a*x + b*y = 1。输出一个正数x,一个y。

直接套模板,最后对x < 0时处理一下,∵a*x + b*y = 1,所以x+=b,y-=a来保持值不变

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int N=100050; ll ex_gcd(ll a,ll b,ll &x,ll &y) //扩展欧几里德
{
if(b ==0)
{
x = 1;y = 0;
return a;
}
else
{
ll t = ex_gcd(b,a%b,y,x);
y = y - x*(a/b);
return t;
}
} int main()
{
ll a,b;
while(scanf("%I64d%I64d",&a,&b)!= EOF)
{
ll x,y;
ll tmp = ex_gcd(a,b,x,y);
if(1 % tmp)
printf("sorry\n");
else
{
while(x < 0){
x += b;
y -= a;
}
printf("%I64d %I64d\n",x,y);
}
}
return 0;
}

  

hdu 1576

Problem Description
要求(A/B)%9973,但由于A很大,我们只给出n(n=A%9973)(我们给定的A必能被B整除,且gcd(B,9973) = 1)。
 
Input
数据的第一行是一个T,表示有T组数据。
每组数据有两个数n(0 <= n < 9973)和B(1 <= B <= 10^9)。
 
Output
对应每组数据输出(A/B)%9973。
 
Sample Input
2
1000 53
87 123456789
 
Sample Output
7922
6060

A % B = 0,A= Bx;

n = A%9973  , A  = 9973y + n;   Bx -9973y  = n;

GCD(b,9973) = 1,      b*x1 + 9973y1 = 1,    b*x1*n + 9973 *(n*y1) = n

∴ x = n*x1,  x1可以通多exGCD算出

最后的x通过    (x % MOD + MOD)%MOD 防止出现负数

#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <cmath>
#include <algorithm>
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
const int N=100050; void ex_gcd(int a,int b,int &x,int &y) //扩展欧几里德
{
if(b ==0)
{
x = 1;y = 0;
}
else
{
ex_gcd(b,a%b,y,x);
y = y - x*(a/b);
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,B;
scanf("%d%d",&n,&B);
int x,y;
ex_gcd(B,9973,x,y);
x *= n; printf("%d\n",(x%9973 + 9973)% 9973); //再加上一次,防止负
}
return 0;
}

  

hdu2669与hdu1576(扩展欧几里德)的更多相关文章

  1. hdu1576 扩展欧几里德 A/B

    A/B Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

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

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

  3. (扩展欧几里德算法)zzuoj 10402: C.机器人

    10402: C.机器人 Description Dr. Kong 设计的机器人卡尔非常活泼,既能原地蹦,又能跳远.由于受软硬件设计所限,机器人卡尔只能定点跳远.若机器人站在(X,Y)位置,它可以原地 ...

  4. [BZOJ1407][NOI2002]Savage(扩展欧几里德)

    题目:http://www.lydsy.com:808/JudgeOnline/problem.php?id=1407 分析: m,n范围都不大,所以可以考虑枚举 先枚举m,然后判定某个m行不行 某个 ...

  5. 欧几里德与扩展欧几里德算法 Extended Euclidean algorithm

    欧几里德算法 欧几里德算法又称辗转相除法,用于计算两个整数a,b的最大公约数. 基本算法:设a=qb+r,其中a,b,q,r都是整数,则gcd(a,b)=gcd(b,r),即gcd(a,b)=gcd( ...

  6. 51nod 1352 扩展欧几里德

    给出N个固定集合{1,N},{2,N-1},{3,N-2},...,{N-1,2},{N,1}.求出有多少个集合满足:第一个元素是A的倍数且第二个元素是B的倍数. 提示: 对于第二组测试数据,集合分别 ...

  7. CF 7C. Line(扩展欧几里德)

    题目链接 AC了.经典问题,a*x+b*y+c = 0整数点,有些忘记了扩展欧几里德,复习一下. #include <cstdio> #include <iostream> # ...

  8. poj2142-The Balance(扩展欧几里德算法)

    一,题意: 有两个类型的砝码,质量分别为a,b;现在要求称出质量为d的物品, 要用多少a砝码(x)和多少b砝码(y),使得(x+y)最小.(注意:砝码位置有左右之分). 二,思路: 1,砝码有左右位置 ...

  9. poj2115-C Looooops(扩展欧几里德算法)

    本题和poj1061青蛙问题同属一类,都运用到扩展欧几里德算法,可以参考poj1061,解题思路步骤基本都一样.一,题意: 对于for(i=A ; i!=B ;i+=C)循环语句,问在k位存储系统中循 ...

随机推荐

  1. 城市安全风险管理项目Postmortem结果

    设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 本系统希望实现快速识别危害因素,使工作人员对风险作出准确的评估.即让使用者熟悉潜在的危险因素,知道 ...

  2. PTA题目的處理(四)

    题目7-3 求交错序列前N项和 1.实验代码 #include <stdio.h> //#include <stdlib.h> int main() { ,N; double ...

  3. Tomcat 8项目无法启动,无报错

    作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs Tomcat 8启动很慢,且日志上无任何错误,在日志中查看到如下信息: Log4j:[2015-10-29 ...

  4. Python内置函数(25)——frozenset

    英文文档: class frozenset([iterable]) Return a new frozenset object, optionally with elements taken from ...

  5. Python内置函数(5)——pow

    英文文档: pow(x, y[, z]) Return x to the power y; if z is present, return x to the power y, modulo z (co ...

  6. Vue2学习小记-给Vue2路由导航钩子和axios拦截器做个封装

    1.写在前面 最近在学习Vue2,遇到有些页面请求数据需要用户登录权限.服务器响应不符预期的问题,但是总不能每个页面都做单独处理吧,于是想到axios提供了拦截器这个好东西,再于是就出现了本文. 2. ...

  7. Spring AOP AspectJ

    本文讲述使用AspectJ框架实现Spring AOP. 再重复一下Spring AOP中的三个概念, Advice:向程序内部注入的代码. Pointcut:注入Advice的位置,切入点,一般为某 ...

  8. 新概念英语(1-99)Ow!

    Lesson 99 Owl! 啊哟! Listen to the tape then answer this question. Must Andy go to see the doctor?听录音, ...

  9. 开源软件:NoSql数据库 - 图数据库 Neo4j

    转载自原文地址:http://www.cnblogs.com/loveis715/p/5277051.html 最近我在用图形数据库来完成对一个初创项目的支持.在使用过程中觉得这种图形数据库实际上挺有 ...

  10. bugfree,CDbConnection 无法开启数据库连线: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '192.168.0.99' (4)

    安装bugfree后,访问报错:CDbConnection 无法开启数据库连线: SQLSTATE[HY000] [2003] Can't connect to MySQL server on '19 ...