hdu2669与hdu1576(扩展欧几里德)

本文介绍了一种用于求解整数最大公约数的算法——扩展欧几里得算法,并通过实例展示了其在不同场景下的应用,包括求解线性组合和解决同余方程组。
部署运行你感兴趣的模型镜像

模板:

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










您可能感兴趣的与本文相关的镜像

Qwen3-8B

Qwen3-8B

文本生成
Qwen3

Qwen3 是 Qwen 系列中的最新一代大型语言模型,提供了一整套密集型和专家混合(MoE)模型。基于广泛的训练,Qwen3 在推理、指令执行、代理能力和多语言支持方面取得了突破性进展

### 查找杭电OJ题库中编号为1002的题目及其C语言实现 对于杭电在线评测系统(HDU OJ)中的第1002号问题,该问题是关于计算多个整数的最大公约数(GCD),并进一步利用这些最大公约数来解决特定场景下的应用。然而,具体到此题目的描述并未直接给出,但可以推测其核心在于处理多组测试数据以及求解两个或更多整数之间的关系。 针对此类涉及最大公约数的问题,在C语言中可以通过欧几里得算法高效地解决问题。下面提供了一个基于给定条件的解决方案: ```c #include <stdio.h> // 计算两数的最大公约数函数定义 int gcd(int a, int b) { if (b == 0) return a; return gcd(b, a % b); } // 主程序入口 int main() { int T; // 测试案例数量 scanf("%d", &T); while(T--) { int n; // 整数的数量 scanf("%d", &n); int num[n]; for(int i = 0; i < n; ++i){ scanf("%d", &num[i]); } // 假设第一个数作为初始值 int result = num[0]; // 迭代计算所有数字间的最小公倍数(LCM), 使用gcd辅助计算LCM for(int i = 1; i < n; ++i){ result = ((result * num[i]) / gcd(result, num[i])); } printf("%d\n", result); } return 0; } ``` 上述代码实现了读取多组测试用例的功能,并对每一组内的若干个正整数进行了最小公倍数(LCM)的计算[^1]。这里采用了辗转相除法(也称为欧几里德算法)用于寻找任意一对整数的最大公约数(GCD),进而通过公式`lcm(a,b)=a*b/gcd(a,b)`得到两者之间最小公倍数的关系。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值