csust 1467: 数学(扩展欧几里得)
根据朴素的欧几里德原理有 gcd(a,b)=gcd(b,a mod b);
则:ax1+by1=bx2+(a mod b)y2;
即:ax1+by1=bx2+(a-[a/b]*b)y2=ay2+bx2-(a/b)*by2;
根据恒等定理得:x1=y2; y1=x2-[a/b]*y2;
本题直接利用这个结论。
#include <iostream> #include <stdio.h> #include <string> #include <string.h> #include <algorithm> #include <math.h> #include <fstream> #include <vector> #include <map> #include <queue> #include <stack> #include <math.h> #include <stdlib.h> using namespace std ; typedef long long ll; const int maxn = 100005; const int mod = 1000000007; int a[maxn]; ll extend_Eulid(ll a,ll b,ll &x,ll &y){ if(!b){ x = 1; y = 0; return a; } ll gcd = extend_Eulid(b,a%b,x,y); ll temp = x; x = y; y = temp - a/b*y; return gcd; } int main(){ int n; while(scanf("%d",&n)&&n){ ll sum = 1; ll x,y; for(int i = 0;i < n;i++){ scanf("%d",a+i); sum = (sum*a[i]+mod)%mod; } extend_Eulid(a[0],mod,x,y); printf("%lld",(sum*x%mod+mod)%mod); for(int i = 1;i < n;i++){ extend_Eulid(a[i],mod,x,y); printf(" %lld",(sum*x%mod+mod)%mod); } printf("\n"); } return 0 ; }