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 |
这个程序没有这种条件,不知道为什么是对的,好像需要点数论的知识In Reply To:同请教…… Posted by:frkstyc at 2005-12-06 22:26:17 #include<stdio.h> __int64 inv(__int64 a,__int64 n) { __int64 g[100],v[100],i,x,y; g[0]=n;g[1]=a; v[0]=0;v[1]=1; i=1; while(g[i]) { y=g[i-1]/g[i]; g[i+1]=g[i-1]-y*g[i]; //u[i+1]=u[i-1]-y*u[i]; v[i+1]=v[i-1]-y*v[i]; i++; } x=v[i-1]; if(x>=0) return x; else return x+n; } __int64 gcd(__int64 x,__int64 y) { __int64 z; z=x%y; while(z) {x=y; y=z;z=x%y;} return y; } int main() { __int64 n,a,b,s,t,x,y,c,d; __int64 xc,yc,an,bn,x1,y1,x2,y2,ab,minx,miny,sum; int i,j,k,start,end; while(scanf("%I64d",&n)>0) { if(n==0) break; scanf("%I64d %I64d %I64d %I64d",&a,&b,&s,&t); if(s<0||t<0||s>=n||t>=n) {printf("no solution\n");continue;} a%=n;b%=n; a=(a+n)%n;b=(b+n)%n; if(s==t) {printf("0 0\n");continue;} c=t-s; if(c<0) c+=n; bn=gcd(b,n); y=inv(b/bn,n/bn); an=gcd(a,n); x=inv(a/an,n/an); ab=gcd(an,bn); if(c%ab) {printf("no solution\n");continue;} for(x1=0;x1<=n;x1++) { d=((c-a*x1)%n+n)%n; if(d%bn==0) break; } y1=(y*d/bn)%(n/bn); xc=bn/gcd(bn,n-a); //n/=bn; yc=(y*(n-a)/gcd(bn,n-a))%n; k=d=0; sum=x1+y1;minx=x1;miny=y1; while(x1<n) { while(d<n/bn-y1) {k++;d=(d+yc)%(n/bn);} x1+=k*xc; y1=(y1+d)%(n/bn); if(x1+y1<=sum) {sum=x1+y1;minx=x1;miny=y1;} if(x1>sum||y1==0||d==n/bn-1) break; } if(k*xc==1&&d==n/bn-1) {minx=sum;miny=0;} printf("%I64d %I64d\n",minx,miny); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator