1 条题解

  • 0
    @ 2025-9-10 8:58:59

    C++ :

    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <iostream>
    #include <queue>
    #include <stack>
    #include <string>
    #include <vector>
    using namespace std;
    const int mod = 1000000007;
    struct P
    {
        long long a[3][3];
    };
    P mut(P a,P b)
    {
        P c;
        for(int i=0;i<2;i++)
        {
            for(int j=0;j<2;j++)
            {
                c.a[i][j]=0;
                for(int k=0;k<2;k++)
                {
                    c.a[i][j]+=a.a[i][k]*b.a[k][j];
                    c.a[i][j]%=mod;
                }
            }
        }
        return c;
    }
    P solve(P a,long long k)
    {
        P res;
        for(int i=0;i<2;i++)
        {
            for(int j=0;j<2;j++)
            {
                res.a[i][j]=0;
                if(i==j) res.a[i][j]=1;
            }
        }
        while(k)
        {
            if(k&1) res=mut(res,a);
            a=mut(a,a);
            k>>=1;
        }
        return res;
    }
    long long ss(long long a,long long k)
    {
        long long res=1;
        while(k)
        {
            if(k&1)
            {
                res*=a;
                res%=mod;
            }
            a*=a;
            a%=mod;
            k>>=1;
        }
        return res;
    }
    long long a[10005];
    int main()
    {
        //freopen("I.in","r",stdin);
        int t;
        scanf("%d",&t);
        while(t--)
        {
            long long n,m,k;
            cin>>n>>m>>k;
            for(int i=0;i<k;i++) cin>>a[i];
            if(n==1&&m==1)
            {
                printf("1\n");
                continue;
            }
            else if(m==1)
            {
                printf("0\n");
                continue;
            }
            if(k==0)
            {
                long long temp=m;
                long long a=m-1;
                long long k=n-1;
                temp*=ss(a,k);
                temp%=mod;
                cout<<temp<<endl;
                continue;
            }
            sort(a,a+k);
            bool flag=0;
            for(int i=0;i<k-1;i++)
            {
                if(a[i]==a[i+1]-1)
                {
                    flag=1;
                    break;
                }
            }
            if(flag)
            {
                printf("0\n");
                continue;
            }
            long long temp=m;
            temp*=ss(m-1,a[0]-1);
            temp%=mod;
            temp*=ss(m-1,n-a[k-1]);
            temp%=mod;
            long long now=a[0];
            P jz;
            jz.a[0][0]=m-2;
            jz.a[1][0]=m-1;
            jz.a[0][1]=1;
            jz.a[1][1]=0;
            P h;
            memset(h.a,0,sizeof(h.a));
            h.a[0][0]=m-1;
            for(int i=1;i<k;i++)
            {
                if(a[i]==now) continue;
                P res=solve(jz,a[i]-now-2);
                res=mut(h,res);
                temp*=res.a[0][0];
                temp%=mod;
                now=a[i];
            }
            cout<<temp<<endl;
        }
    }
    
    
    • 1

    信息

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