3 条题解

  • 0
    @ 2026-4-13 13:21:15
    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    const int N=1e6+10,P=1e9+7;
    int fac[N],dp[N];
    void init()
    {
    	dp[0]=1;dp[1]=0;fac[0]=1;
    	for(int i=2;i<N;i++)
    		dp[i]=(dp[i-1]+dp[i-2])%P*(i-1)%P;
    	for(int i=1;i<N;i++)fac[i]=fac[i-1]*i%P;
    }
    int qpow(int a,int b)
    {
    	int res=1;
    	for(;b;b>>=1,a=a*a%P)
    		if(b&1)res=res*a%P;
    	return res;
    }
    int C(int n,int m)
    {
    	if(m>n)return 0;
    	return fac[n]*qpow(fac[n-m],P-2)%P*qpow(fac[m],P-2)%P;
    }
    void text()
    {
    	int n,m;scanf("%lld%lld",&n,&m);
    	printf("%lld\n",C(n,m)*dp[n-m]%P);
    }
    signed main()
    {
    	init();
    	int T;scanf("%lld",&T);
    	while(T--)text();
    	return 0;
    }
    
    • 0
      @ 2025-10-8 17:10:56
      #include<bits/stdc++.h>
      using namespace std;
      typedef long long LL;
      const LL P=1e9+7;
      const int N=1e6+10;
      LL f[N],d[N];
      template<typename T>void qr(T &x)
      {
          x=0;int f=1;char ch=getchar();
          for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
          for(; isdigit(ch);ch=getchar()) x=x*10+(ch&15);
          x=x*f;
      }
      inline LL qpow(LL a,LL b)
      {
          LL res=1;a=a%P;
          for(;b;b>>=1,a=a*a%P)if(b&1)res=res*a%P;
          return res;
      }
      inline LL calc(LL n,LL m)//组合数:n个选m个
      {
          return f[n]*qpow(f[m],P-2)  %P * qpow(f[n-m],P-2) % P;//n! / m! / (n-m)! -> n! * (m!)^-1 * ((n-m)!)^-1
      }
      int main()
      {
          f[0]=1;for(int i=1;i<=N-10;i++)f[i]=f[i-1]*i%P;
          d[1]=0,d[2]=1;for(int i=3;i<=N-10;i++) d[i]=(i-1)*(d[i-1]+d[i-2])%P;
          
          int T;qr(T);
          while(T--)
          {
              LL n,m;qr(n);qr(m);
              if(n==m) printf("1\n");
              else printf("%lld\n",calc(n,m)%P*d[n-m]%P);//组合数n个选m个 乘以 n-m的错排
          }
          return 0;
      }
      
      • 0
        @ 2025-10-8 17:10:34
        #include<bits/stdc++.h>
        using namespace std;
        typedef long long LL;
        const LL P=1e9+7;
        const int N=1e6+10;
        LL f[N],d[N];
        template<typename T>void qr(T &x)
        {
            x=0;int f=1;char ch=getchar();
            for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-1;
            for(; isdigit(ch);ch=getchar()) x=x*10+(ch&15);
            x=x*f;
        }
        inline LL qpow(LL a,LL b)
        {
            LL res=1;a=a%P;
            for(;b;b>>=1,a=a*a%P)if(b&1)res=res*a%P;
            return res;
        }
        inline LL calc(LL n,LL m)//组合数:n个选m个
        {
            return f[n]*qpow(f[m],P-2)  %P  *qpow(f[n-m],P-2) % P;//n! / m! / (n-m)! -> n! * (m!)^-1 * ((n-m)!)^-1
        }
        int main()
        {
            f[0]=1;for(int i=1;i<=N-10;i++)f[i]=f[i-1]*i%P;
            d[1]=0;d[2]=1;for(int i=3;i<=N-10;i++) d[i]=(i-1)*(d[i-1]+d[i-2])%P;
            
            int T;qr(T);
            while(T--)
            {
                LL n,m;qr(n);qr(m);
                if(n==m) printf("1\n");
                else printf("%lld\n",calc(n,m)%P*d[n-m]%P);//组合数n个选m个 乘以 n-m的错排
            }
            return 0;
        }
        • 1

        信息

        ID
        6182
        时间
        3000ms
        内存
        256MiB
        难度
        8
        标签
        递交数
        197
        已通过
        28
        上传者