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 |
贴一下代码。Accept了,但是速度不算快。之后的打算是使用扩展欧几里得算法。 #include <stdio.h> int main(int argc, char* argv[]) { int x, y, m, n, L, dd, dv, ltimes, tmp, first, tmp2; scanf("%d%d%d%d%d", &x, &y, &m, &n, &L); x %= L; y %= L; m %= L; n %= L; if(m>n) { dd = (x<y) ? y-x : L-(x-y); dv = m-n; } else if(m<n) { dd = x>y ? x-y : L-(y-x); dv = n-m; } else { printf("Impossible\n"); return 0; } tmp = dd; ltimes = 0; first = 1; while(1) { if(tmp%dv == 0) { ltimes += (tmp/dv); printf("%d\n", ltimes); break; } if( (!first) && (tmp >= dd) && (((tmp-dd)%dv) == 0)) { printf("Impossible\n"); break; } tmp2 = tmp/dv; ltimes += tmp2+1; tmp = L + ( (tmp-(tmp2*dv))-dv); first = 0; } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator