1 条题解

  • 0
    @ 2024-12-24 9:49:40

    C++ :

    #include <cstdio>
    #include <iostream>
    #include <cmath>
    #include <algorithm>
    #include <cstring>
    #include <vector>
    #include <string>
    #define maxn 40000
    using namespace std;
    long long c[62][62];
    void initc()
    {
        int i,j;
        for (i=0;i<=60;i++)
        for (j=0;j<=i;j++)
        {
            if (!j||i==j) c[i][j]=1;
            else c[i][j]=c[i-1][j-1]+c[i-1][j];
        }
        return;
    }
    long long gcd(long long a,long long b)
    {
        if(b==0)
            return a;
        return gcd(b,a%b);
    }
    long long exgcd(long long a,long long b,long long &x,long long &y)
    {
        if(b==0)
        {
            x=1;
            y=0;
            return a;
        }
        long long ans=exgcd(b,a%b,x,y);
        long long tmp=x;
        x=y;
        y=tmp-a/b*y;
        return ans;
    }
    int main()
    {
        //freopen("test.in","r",stdin);
        //freopen("test.out","w",stdout);
        initc();
        long long m,a,b;
        while(scanf("%lld%lld%lld",&m,&a,&b)!=EOF)
        {
            if(m==0 && a==0 && b==0)
                break;
            if((m-1)%gcd(a,b)!=0)
            {
                printf("dead\n");
                continue;
            }
            long long x,y,g=gcd(a,b);
            long long cc=(m-1)/g;
            a/=g,b/=g;
            exgcd(a,b,x,y);
            x*=cc;
            x%=b;
            y=(cc-a*x)/b;
            while(x<0||y>0)
            {
                x+=b;
                y=(cc-a*x)/b;
            }
            y*=-1;
            printf("%lld\n",c[x+y][y]);
        }
        return 0;
    }
    
    
    • 1

    信息

    ID
    984
    时间
    1000ms
    内存
    128MiB
    难度
    (无)
    标签
    (无)
    递交数
    0
    已通过
    0
    上传者