|洛谷|DFS|P1118 [USACO06FEB]数字三角形Backward Digit Su…

http://www.luogu.org/problem/show?pid=1118

每个数用的次数构成杨辉三角,用的次数i*排列i的所有和等于sum就行

STL只能80分,估计DFS全排序能AC

#include
#include
#include
#include
#define ms(i,j) memset(i,j, sizeof i);
using namespace std;
int c[20][20];
int a[20];
int main()
{
    int n,sum;
    scanf("%d%d", &n ,&sum);
    for (int i=1;i<=n;i++)
    {
        c[i][1] = 1;
        for (int j=2;j<=i;j++)
        c[i][j] = c[i-1][j-1]+c[i-1][j];
    }
    for (int i=1;i<=n;i++) a[i] = i;
    do
    {
        int tmp = 0;
        for (int i=1;i<=n;i++) tmp += a[i] * c[n][i];
        if (tmp==sum)
        {
            for (int i=1;i<=n;i++) printf("%d ", a[i]);
            return 0;
        }
    }while(next_permutation(a+1,a+n+1));
    return 0;
}


转载于:https://www.cnblogs.com/flyinthesky1/p/6384256.html

你可能感兴趣的:(|洛谷|DFS|P1118 [USACO06FEB]数字三角形Backward Digit Su…)