codeforce 787A The Monster 两种解法

本文探讨了扩展欧几里德算法的应用场景,详细解释了如何使用该算法解决特定类型的方程问题,并提供了逐步优化的实现代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意 : 问b+na = d+kc,有无解;
b-d = kc - na;

#include <bits\stdc++.h>
using namespace std;

 int gcd(int a,int b){
    return b == 0?  a:gcd(b,a%b);
 }

int main(){
    int a,b,c,d;
    scanf("%d%d%d%d",&a,&b,&c,&d);
        if( b < d){
            swap(b,d);swap(a,c);
        }
        int xx = gcd(a,c);
        if((b-d) % xx != 0){
            printf("-1\n");
        }
        else{
            for(int i=0;;i++){
                if((b+i*a-d )% c == 0){
                    printf("%d\n",b+i*a);
                    break;
                }
            }
        }
    return 0;
}

扩展欧几里德
b-d = kc - na; 则有 Ax + By = c,B<0; 通过 扩展欧几里德算法 可以得到一组 解:(x,y) if(x < 0 || y <0) 则x,y 就都增加: x += B/gcd;y -= A/gcd; 否则,就减少。这么考虑的话,gcd要为正数。所以当gcd为负数时 (-x,-y) 是 A(-x) + B(-y) = -gcd 的解。

#include <bits\stdc++.h>
using namespace std;
typedef int li ;

li ex_gcd(li a, li b, li& x, li& y) {
    if (!a) {
        x = 0, y = 1;
        return b;
    }
    li xx, yy, g = ex_gcd(b % a, a, xx, yy);
    x = yy - b / a * xx;
    y = xx;
    return g;
}

int main(){
    int a,b,c,d; cin>>a>>b>>c>>d;
    if(b<d){
      swap(a,c);
      swap(b,d); 
    }
    int A = c, B = -a, C = b-d;
    int x,y;
    int gcd = ex_gcd(A,B,x,y);// Ax + By = C 
     if(C%gcd!=0){
        cout<<"-1\n";return 0;
     }
    if(gcd < 0){
        gcd = -gcd;
        x = -x;
        y = -y;
     }
     //cout<<x<<" "<<y<<endl; 
    //cout<<A*x+B*y<<" "<<gcd<<endl;
     x = C*x/gcd; y = C*y/gcd;// 如果只逆转 gcd 的话, 则此处求一组解就会出错。 
    // cout<<A*x+B*y<<" "<<C<<endl;
     int flag = 0;
    if(x < 0 || y <0) flag = 1;
        for(int i=1;;i++){
            if(!flag){
                x += B/gcd;
                y -= A/gcd;

            }
            else {
                x -= B/gcd; 
                y += A/gcd;   
                //cout<<x<<" "<<y<<endl;
            }
        if(flag){
            if(x>=0&&y>=0){
                printf("%d\n",A*x+d);
                break;
            }
        }
        else{
             if(x  < 0 ){
            x -= B/gcd;
            printf("%d\n",A*x+d);
            break;
        }
        if(y < 0){
            y += A/gcd;
            printf("%d\n",-B*y+b);
            break;
         }
        }
    }
    return 0;
} 

对上一种优化:第一个 x>=0,y>=0 即为所求的解。所以可以直接求出。
x = x0 + B/gcd*t , y = y0 - A/gcd*t;



#include <bits\stdc++.h>
using namespace std;
typedef int li ;

li ex_gcd(li a, li b, li& x, li& y) {
    if (!a) {
        x = 0, y = 1;
        return b;
    }
    li xx, yy, g = ex_gcd(b % a, a, xx, yy);
    x = yy - b / a * xx;
    y = xx;
    return g;
}

int main(){
    int a,b,c,d; cin>>a>>b>>c>>d;
    if(b<d){
      swap(a,c);
      swap(b,d); 
    }
    int A = c, B = -a, C = b-d;
    int x,y;
    int gcd = ex_gcd(A,B,x,y);// Ax + By = C 
     if(C%gcd!=0){
        cout<<"-1\n";return 0;
     }
    if(gcd < 0){
        gcd = -gcd;
        x = -x;
        y = -y;
     }
     //cout<<x<<" "<<y<<endl; // 0 -1 
     //cout<<gcd<<endl;
     x = C*x/gcd; y = C*y/gcd;  
     //cout<<x<<" "<<y<<endl;

     int flag = 0;
     int k1 = B/gcd , k2 = A/gcd;
     if(k1 < 0) k1 = -k1;
     if(k2 < 0) k2 = -k2;
     //cout<<k1<<" "<<k2<<endl; 
     int t = x/k1;
     x -= t*k1, y -= t*k2;
     if( x < 0){
        x += k1, y+=k2;
     }
    // cout<<x<<" "<<y<<endl;
     if( y < 0){
        t = y/k2;
        x -= t*k1, y -= t*k2;
        if( y < 0){
        x += k1, y+=k2;
        }  
     }
    printf("%d\n",A*x+d);

    return 0;
} 
### Codeforces Problem or Contest 998 Information For the specific details on Codeforces problem or contest numbered 998, direct references were not provided within the available citations. However, based on similar structures observed in other contests such as those described where configurations often include constraints like `n` representing numbers of elements with defined ranges[^1], it can be inferred that contest or problem 998 would follow a comparable format. Typically, each Codeforces contest includes several problems labeled from A to F or beyond depending on the round size. Each problem comes with its own set of rules, input/output formats, and constraint descriptions. For instance, some problems specify conditions involving integer inputs for calculations or logical deductions, while others might involve more complex algorithms or data processing tasks[^3]. To find detailed information regarding contest or problem 998 specifically: - Visit the official Codeforces website. - Navigate through past contests until reaching contest 998. - Review individual problem statements under this contest for precise requirements and examples. Additionally, competitive programming platforms usually provide comprehensive documentation alongside community discussions which serve valuable resources when exploring particular challenges or learning algorithmic solutions[^2]. ```cpp // Example C++ code snippet demonstrating how contestants interact with input/output during competitions #include <iostream> using namespace std; int main() { int n; cin >> n; // Process according to problem statement specifics } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值