| ||||||||||
Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
我也使用欧几里德扩展算法做得,到底错在哪里?都快没信心了!#include<stdio.h> #include<iostream.h> #include<math.h> int euclid(int a,int b) { if (b==0) return a; else return(euclid(b,a%b)); } int ext_euclid(int a,int b,int &x,int &y) { int t,d; if (b==0) {x=1;y=0;return a;} d=ext_euclid(b,a %b,x,y); t=x; x=y; y=t-a/b*y; return d; } void modular_linear_equation_solver(int a,int b,int n) { int e,i,d; int x,y; int min,k; d=ext_euclid(a,n,x,y); if (b%d>0) cout <<"FOREVER\n"; else { min=0xfffffff; e=(x*(b/d))%n; for (i=0;i<d;i++) { k=((e+i*(n/d))%n); if (k>=0&&k<min) min=k; } cout <<min<<'\n'; } } int main() { int a,b,c,d,i,m; while(1) { cin>>a>>b>>c>>d; if(a==0&&b==0&&c==0&&d==0) break; m=1; if(d==0) m=1; else for(i=1;i<=d;i++) m*=2; modular_linear_equation_solver(c,b-a,m); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator