zoukankan      html  css  js  c++  java
  • acdream 1014 Dice Dice Dice

    这题的想法就是直接枚举1-m每一个数的数量,先给这n个数全排列,然后除以相同的数的阶乘就可以了。枚举的方法就是dfs了。这里分了两步来完成这个任务,首先找出前k个数的组合,然后再进行任意的组合。

    代码如下:

    #include <cstdlib>
    #include <cstdio>
    #include <cstring>
    #include <iostream>
    using namespace std;
    // 对于一个给定的m面色子,我们抛掷n次,前k大的数相加为p 
    // 错误的思路为枚举最大的k个数,然后再将剩下的n-k个小于
    // 前k个数中最小的数进行排列组合
    // 给定n,m,p,k
    
    int n, m, p, k;
    
    long long fac[25];
    
    long long Sdfs(int x, int box, long long tot, int last) {
        if (x == 0 && box != 0) {// 如果这个数已经枚举到0或者已经没有了放置的位置
            return 0;
        }
        if (box == 0)
            return tot / fac[last]; // 如果过来的时候box已经等于0 
        long long ret = 0;
        for (int i = 0;  i <= box; ++i) {
            ret += Sdfs(x-1, box-i, tot / fac[i+last], 0);
        }
        return ret;
    }
    
    long long Fdfs(int x, int n, int k, int p, long long tot) {
        long long ret = 0;
        if (x == 0 || n == 0) return 0;
        for (int i = 0; i <= k; ++i) {
            if (i * x < p) {
                ret += Fdfs(x-1, n-i, k-i, p-i*x, tot/fac[i]);
            } else if (i * x == p && k == i) {
                ret += Sdfs(x, n-i, tot, i);
            }
        }
        return ret;
    }
    
    int main() {
        fac[0] = fac[1] = 1;
        for (int i = 2; i <= 20; ++i) {
            fac[i] = fac[i-1] * i;
        } // 先计算出所有的阶乘出来
        while (scanf("%d %d %d %d", &n, &m, &k, &p) == 4) {
            if (m * k < p) {
                puts("0");
                continue;
            }
            printf("%lld\n", Fdfs(m, n, k, p, fac[n]));
        }
        return 0;    
    }
  • 相关阅读:
    VS code常用的几个插件
    vue项目,ie11 浏览器报 Promise 未定义的错误
    npm 安装卸载模块
    System.Reflection.MethodBody.cs
    System.RuntimeMethodHandle.cs
    System.Reflection.MethodBase.cs
    System.Runtime.Serialization.IDeserialezationCallback.cs
    System.Globalization.CompareOptions.cs
    System.Globalization.CompareInfo.cs
    System.Globalization.TextInfo.cs
  • 原文地址:https://www.cnblogs.com/Lyush/p/2810385.html
Copyright © 2011-2022 走看看