Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

贴一下代码。

Posted by Leon_Xu at 2010-12-05 21:24:33 on Problem 1061
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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator