zoukankan      html  css  js  c++  java
  • P1118 数字三角形(技巧)

    题见洛谷
    带有技巧的搜索,用到杨辉三角形

    这里写图片描述
    不难看出 第几个(k)拆的数(虽说并不是拆的),系数为杨辉三角第n行,第k列的数字

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    #include<algorithm>
    #include<vector>
    using namespace std;
    int n,sum,a[124];
    int yh[20][20];
    bool f[20]; 
    void dfs(int k,int tot)
    {
        //if(tot>sum) return;
        if(k==n+1){
            if(tot==sum){
              for(int i=1;i<=n;i++)
               printf("%d ",a[i]);
              exit(0);
            }
    
            return;
        }
        for(int i=1;i<=n;i++){
          if(tot+i*yh[n][k]<=sum)
           if(!f[i]){
             a[k]=i;f[i]=true;
             dfs(k+1,tot+i*yh[n][k]);
             a[k]=0;f[i]=false;//回溯! 
           }
        }
    
    }
    int main()
    {
        scanf("%d%d",&n,&sum);
        yh[1][1]=1;
        for(int i=2;i<=n;i++)
         for(int j=1;j<=i;j++)
            yh[i][j]=yh[i-1][j-1]+yh[i-1][j];
        /*for(int i=1;i<=n;i++){
          printf("%d ",yh[n][i]);
        }*/
    
        dfs(1,0);
    
        return 0;
    } 
  • 相关阅读:
    concate string when group by
    Regular Expression
    Login failed for user
    SQL Performance Tools
    Web References
    ARTetris-AR版俄罗斯方块的源码解析
    ARKit_1
    链表
    线性表
    ORB-SLAM2的特征提取算法
  • 原文地址:https://www.cnblogs.com/dfsac/p/6819777.html
Copyright © 2011-2022 走看看