POJ 3187 Backward Digit Sums (递推,bruteforce)

本文介绍了一种使用next_permutation函数解决杨辉三角组合问题的方法,通过生成排列并验证是否满足特定条件,实现从第一行第j列的1加到最后一行的组合数相等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

第1行j列的一个1加到最后1行满足杨辉三角,可以先推出组合数来

然后next_permutation直接暴。

#include<cstdio>
#include<iostream>
#include <iterator>
#include<string>
#include<cstring>
#include<queue>
#include<vector>
#include<stack>
#include<vector>
#include<map>
#include<set>
#include<algorithm>
using namespace std;

int n,tar;
const int maxn = 11;
int a[maxn];
int C[maxn][maxn];

inline bool ok()
{
    int re = 0;
    for(int i = 0; i < n; i++){
        re += a[i]*C[n-1][i];
    }
    return re == tar;
}

//#define LOCAL
int main()
{
#ifdef LOCAL
    freopen("in.txt","r",stdin);
#endif
    for(int i = 0; i < maxn; i++){
        C[i][0] = C[i][i] = 1;
        for(int j = 1; j < i; j++){
            C[i][j] = C[i-1][j] + C[i-1][j-1];
        }
    }
    scanf("%d%d",&n,&tar);
    for(int i = 0; i < n; i++)a[i] = i+1;
    while(!ok() && next_permutation(a,a+n));
    copy(a, a + n, ostream_iterator<int>(cout, " "));
    return 0;
}

 

转载于:https://www.cnblogs.com/jerryRey/p/4889670.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值