CodeForces 7C Line

扩展欧几里德算法:

已知a, b求解一组x,y,使它们满足等式: ax+by = gcd(a, b) =d(解一定存在,根据数论中的相关定理)。

扩展欧几里德常用在求解模线性方程及方程组中。

ax+by+c=0可以转化为ax+by=-c;

可以用扩展欧几里德算法来求ax1+by1=gcd(a,b)来求出x1,y1

此时gcd(a,b)不一定等于-c,假设-c=gcd(a,b)*z,可得z=-c/gcd(a,b);

则ax+by=-c <==> (ax1+by1)*z=gcd(a,b)z;

<==> ax1*z+bx2*z=gcd(a,b)z;

因此可以得知x与x1的关系,y与y1的关系:

x=x1*z,y = y1*z(z上面已经求出来了)

时间: 2024-10-24 09:49:18

CodeForces 7C Line的相关文章

codeforces 7c line(扩展欧几里得)

C. Line A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coordinates are integer numbers from  - 5·1018 to 5·1018 inclusive, or to find out that such points do not exist. Input The first l

欧几里德和扩展欧几里德详解 以及例题CodeForces 7C

欧几里德定理: 对于整数a,b来说,gcd(a, b)==gcd(b, a%b)==d(a与b的最大公约数),又称为辗转相除法 证明: 因为a是d的倍数,b是d的倍数:所以a%d==0:b%d==0: 设k=a/b:r=a%b:则 a=k*b+r: 由上得出:r=a-k*b: 因为a和b都是d的倍数,所以(a-k*b)也是d的倍数,所以r也是d的倍数: 所以gcd(a, b)==gcd(b, a%b)==d 而为什么要证明gcd(a, b)==gcd(b, a%b)==d这个式子成立呢? 其实证

codeforce 7C &amp;&amp;拓展欧几里得 详解

比如说  ax+by=gcd(a,b) 假设  excgcd(int a,int  b,int&x,int&y)是求解这个方程的函数 其返回值是gcd(a,b)(ps: a和b的最大公因子) 假设我们已经求得了b*x1+(a%b)*y1=gcd(a,b): x1 ,y1即为其解 又有  a%b=a-(a/b)*b; 带入得 a*y1+b*(x1-(a/b))=gcd(a,b); 而当 b=0时有 a*1+b*0=a=gcd(a,b); 解为...... x=y1,y=y-(a/b)*x;

B - 楼下水题(扩展欧几里德)

B - 楼下水题 Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 7C Description A line on the plane is described by an equation Ax + By + C = 0. You are to find any point on this line, whose coo

CodeForces 251A. Points on Line(数学 lower_bound )

题目链接:http://codeforces.com/problemset/problem/251/A Little Petya likes points a lot. Recently his mom has presented him n points lying on the line OX. Now Petya is wondering in how many ways he can choose three distinct points so that the distance be

Codeforces Round #247 (Div. 2) B - Shower Line

模拟即可 #include <iostream> #include <vector> #include <algorithm> using namespace std; int main(){ vector<int> a(5); for(int i = 0; i < 5; ++ i) a[i] = i; int g[5][5]; for(int i = 0 ; i < 5; ++ i){ for(int j = 0 ; j < 5; ++

CodeForces 549G Happy Line

Happy Line Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice CodeForces 549G Description Do you like summer? Residents of Berland do. They especially love eating ice cream in the hot summer. So this

Codeforces Round #189 (Div. 1) B. Psychos in a Line 单调队列

B. Psychos in a Line Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/problemset/problem/319/B Description There are n psychos standing in a line. Each psycho is assigned a unique integer from 1 to n. At each step every psycho who h

Codeforces 319B. Psychos in a Line【单调栈】

题目链接: http://codeforces.com/problemset/problem/319/B 题意: 一串数列,每一个值如果大于相邻右一位的值的话,那么就可以把右边这个值"吃掉"(右一位消失,原来的值不变),问需要吃多少次才能到达无法再吃的状态. 思路: 利用栈.遍历一遍数组,处理每个值的时候,如果栈顶的元素小于该值,那么将其弹出,知道栈顶元素大于该值或者栈为空,栈内的每个元素记录下一个属性:他是在第几次被"吃掉",进栈的新元素的被吃次数就是它弹出去的元