Codeforces7C-扩展欧几里德解线性方程(Ax+By+C=0)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <time.h>
#include <string.h>
#include <iostream>
#include <vector>
#include <list>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <algorithm>

typedef short int int16;///32767
typedef int int32;///2147483647
typedef long long int64;///9223372036854775807
const double PI=acos(-1.0);///3.141593
const long long MOD=(long long)1E9+7LL;///1000000007
template <class T> T Susake_pow(T a,T b)///pow
{T res;if(b==0) return 1;else while((b&1)==0){b>>=1;a*=a;}res=a;b>>=1;while(b!=0){a*=a;if((b&1)!=0)res*=a;b>>=1;}return res;}
template<class T> inline T gcd(T a,T b)///gcd
{if(a<0)return gcd(-a,b);if(b<0)return gcd(a,-b);return (b==0)?a:gcd(b,a%b);}
template<class T> inline T lcm(T a,T b)///lcm
{if(a<0)return lcm(-a,b);if(b<0)return lcm(a,-b);return a*(b/gcd(a,b));}
template<class T> inline char *Susake_nsystem(T n)///itoa(26)
{T t=0,i;char *s,*p;s=(char *)malloc(sizeof(char)*1000);p=(char *)malloc(sizeof(char)*1000);
while(n){s[t]=n%26+64;if(s[t]==64){s[t]+=26;n-=26;}t++;n/=26;}s[t]=‘\0‘;for(i = 0; i < t; i++)p[i]=s[t-1-i];p[i]=‘\0‘;free(s);return p;}
int Susake_system(char *s)///atoi(26)
{int len=strlen(s),i,sum=0;char p[1000];for(i=0;i<len;i++)p[i]=s[len-1-i]-64;for(i=0;i<len;i++)sum+=p[i]*Susake_pow(26,i);return sum;}

void exgcd(long long a, long long b, long long& d,long long& x, long long& y)
{
    if(!b) {d = a; x = 1; y = 0;}
    else
    {
        exgcd(b, a % b, d, y, x);
        y -= x * (a / b);
    }
}

int main(int argc, char *argv[])
{
    long long a, b, c, d, x, y;
    std::cin >> a >> b >> c;
    exgcd(a, b, d, x, y);
    if(c % d != 0)
        puts("-1");
    else
        std::cout << -x * (c / d) << " " << -y * (c / d) << std::endl;
    return 0;
}
时间: 2024-08-07 04:34:17

Codeforces7C-扩展欧几里德解线性方程(Ax+By+C=0)的相关文章

扩展欧几里德--解的个数

解的个数 已知整数x,y满足如下面的条件: ax+by+c = 0 p<=x<=q r<=y<=s 求满足这些条件的x,y的个数. 输入描述 Input Description 第一行有一个整数n(n<=10),表示有n个任务.n<=10 以下有n行,每行有7个整数,分别为:a,b,c,p,q,r,s.均不超过108. 输出描述 共n行,第i行是第i个任务的解的个数. 样例输入 2 2 3 -7 0 10 0 10 1 1 1 -10 10 -9 9 样例输出 1 19

7C-扩展欧几里德解线性方程

Ax+By+C=0,给你A,B,C求x,y #include<iostream> #include<cstdio> #include<cstring> using namespace std; long long INF = 5 * 1e18; void gcd(long long a, long long b, long long& d,long long& x, long long& y) { if(!b) {d = a; x = 1; y

扩展欧几里德解的数量(51nod 1352)

题意:给出N,A,B:求A*x+ B*y = N+1   的大于0 的解的数量: 思路:先用exgcd求出大于0的初始解x,rest = N - x*A; sum = rest/LCM(A, B); #include <iostream> #include <algorithm> #include <stdlib.h> #include <time.h> #include <cmath> #include <cstdio> #incl

POJ 2142-The Balance(扩展欧几里德)

题目地址:POJ 2142 题意:有两种类型的砝码质量分别为a和b,要求称出质量为d的物品,要求a的数量x和b的数量y的和x+y最小,若有多个x+y的值,取ax+by最小的. 思路:我们应该求ax+by=d.这里我们应用扩展欧几里德求出ax+by=gcd(a,b),那么ax/gcd(a,b)+by/gcd(a,b)=1,然后求出来特解,令x=x*n,把x转化为最小正值,即x=(x%b+b)%b,求出此时的y=(d-ax)/b,若求出的y是负值,把y变成正的,因为砝码的位置涉及左右之分.同理求出y

URAL 1204. Idempotents 扩展欧几里德

题目来源:URAL 1204. Idempotents 题意:输入n(n = p*q p,q是质数) 并且x*x=x(mod n) 求x 思路: x*x=x(mod n)  -> x*x+k*n=x -> x*(x-1)/n = k 所以 0 和 1 是一组解 因为n = p*q 且x*(x-1)%(p*q)== 0 x < n 因为x*x%n == x 模n之后才是x 1.x有p因子x-1有q因子 x%p == 0且(x-1)%q == 0 a*p == x且b*q == x-1 得到

poj 1061 青蛙约会(扩展欧几里德)

题目链接: http://poj.org/problem?id=1061 题目大意: 中文题目,题意一目了然,就是数据范围大的出奇. 解题思路: 假设两只青蛙都跳了T次,可以列出来不定方程:p*l + (n-m)*T == x - y.列出等式以后,利用扩展欧几里德计算不定方程的解.在求出整数最小解的地方卡了好久,好久. 想具体了解扩展欧几里德的用法和证明的话,可以看一下神牛的博文,我自认弱绞尽脑汁也写不来这么好,附上链接:http://www.cnblogs.com/frog112111/ar

扩展欧几里德求解ax + by = c 的 最小正整数解 ( x, y)

转自: https://blog.csdn.net/qq_38177302/article/details/78449982 第一步 : 给出方程 ax + by = c . 第二步 : 算出 辗转相除法 gcd(a, b) . 第三步 : 运用 扩展欧几里德 ex_gcd(a, b)-> ax + by = gcd(a,b) 的 一组解(x, y) . 第三步: 根据 c % gcd(a, b) 判断是否 ax + by = c 有解 . 第四步 : 根据 ax + by = c 的通解公式

拓展gcd解不定线性方程ax+by=c模版

拓展gcd解不定线性方程ax+by=c模版 /** 解不定方程 ax+by=c */ ll a,b,c; ll x,y; ll exgcd(ll a,ll b,ll &x,ll &y) { if(b==0){ x=1;y=0; return a; } ll r=exgcd(b,a%b,x,y); ll t=y; y=x-a/b*y; x=t; return r; } bool NLE(ll a,ll b,ll c,ll &x,ll &y) /**解不定方程 ax+by=c;

POJ2115——C Looooops(扩展欧几里德+求解模线性方程)

C Looooops DescriptionA Compiler Mystery: We are given a C-language style for loop of type for (variable = A; variable != B; variable += C) statement;I.e., a loop which starts by setting variable to value A and while variable is not equal to B, repea