hdu 1099 Lottery

#include<stdio.h>
__int64 gcd(__int64 a,__int64 b)
{
    return b==0?a:gcd(b,a%b); 
}
int main()
{
    int i,j,prime[]={2,3,5,7,11,13,17,19},n,t;
    __int64 s1,s2,m,d,gd;
    while(scanf("%d",&n)==1)
    {
        s2=1;s1=0;t=0;
        if(n==21 || n==22)
        {
            for(i=2;i<21;i++) s2*=i;d=s2/n;
            for(i=1;i<=n;i++)
            {
                s1+=s2/i;
                while(s1>d)
                {
                    s1-=d;t++;
                }
            }
            gd=gcd(d,s1);s1/=gd;s2=d/gd;
        }
        else
        {
            for(i=2;i<n;i++) s2*=i;
            for(i=1;i<=n;i++) s1+=n*s2/i;
            t=s1/s2;s1%=s2;
            gd=gcd(s2,s1);s1/=gd;s2/=gd;
        }
        m=t;
        if(s1==0)
        {
            printf("%d\n",t);continue;
        }
        while(m/=10) printf(" ");
        printf("  %I64d\n",s1);
        printf("%d ",t);
        m=s2;
        while(m/=10) printf("-");
        printf("-\n");
        m=t;
        while(m/=10) printf(" ");
        printf("  %I64d\n",s2);
    }
    return 0;
}

你可能感兴趣的:(ACM,HDU,gcd)