题解 P4549 【【模板】裴蜀定理】
此题是裴蜀定理的模板题,那么——先上定理内容:
对任何整数
a 、b 和它们的最大公约数d ,关于未知数x 和y 的线性不定方程(称为裴蜀等式):若a ,b 是整数,且\gcd(a,b)=d ,那么对于任意的整数x,y,ax+by 都一定是d的倍数,特别地,一定存在整数x ,y ,使ax+by=d 成立。 ——百度百科
emm,说白了,其实就是两个东西:
其中
证明
引理①:我已经化成那个样子了,原因显然。
引理②证明:
略去过程QED,由上可知证毕。
回到题目
显然,
代码:
#include<bits/stdc++.h>
using namespace std;
inline int read()//快读
{
int s=0,f=1;
char ch=getchar();
while(!isdigit(ch))
{
if(ch=='-')f=-1;
ch=getchar();
}
while(isdigit(ch))
{
s=(s<<3)+(s<<1)+ch-48;
ch=getchar();
}
return s*f;
}
inline void write(int x)//快写
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10^48);
}
int n,ans;
int main()//美丽的主程序
{
n=read(),ans=read();//ans默认为第一个数
while(--n)ans=__gcd(ans,abs(read()));//懒得手写gcd,虽然NOIP不能用
write(ans);//输出
return 0;
}