一上午都在做有关搜索的题目,,,

看到这题之后就直接开始爆搜

结果只有70分,

其余的点硬生生的就是那么WA了。

我的天哪~

70分代码:

 #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,sum;
int ans;
bool dis[];
int f[][];
void shuchu() {
for(int i=; i<=n; i++)
cout<<f[][i]<<" ";
}
void check() {
for(int i=; i<=n; i++)
for(int j=; j<=n-i+; j++)
f[i][j]=f[i-][j]+f[i-][j+];
/* for(int i=1;i<=n;i++){
for(int j=1;j<=n-i+1;j++)
cout<<f[i][j]<<" ";
cout<<'\n';
}*/
// cout<<f[n][1]<<" ";
if(f[n][]==sum) {
shuchu();
ans++;
}
}
void pai(int k) {
if(k>n&&!ans) {
check();
return;
}
for(int i=; i<=n; i++) {
if(!dis[i]) {
dis[i]=true;
f[][k]=i;
pai(k+);
dis[i]=false;
}
}
}
inline int read() {
int s=,w=;
char ch=getchar();
while(ch<''||ch>'') {
if(ch=='-')w=-;
ch=getchar();
}
while(ch>=''&&ch<='')
s=(s<<)+(s<<)+(ch^),ch=getchar();
return s*w;
}
int main() {
n=read(),sum=read();
pai();
return ;
}

之后就开始手动模拟 check() 的过程

猛然发现

这貌似是个杨辉三角,哟哟哟。,,

满分代码:

 #include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,sum;
int ha[],flag,yh[][];
bool v[];
void print(){
for(int i=;i<=n;i++)
printf("%d ",ha[i]);
}
//那个函数被我删了
void dfs(int step,int ans){
if(ans>sum||flag)return;
if(step==n+&&ans==sum){
print();
flag=;//标记
return;
}
for(int i=;i<=n;i++)
if(!v[i]){
ha[step]=i;
v[i]=true;
dfs(step+,ans+i*yh[n][step]);//看顶楼的解释
v[i]=false;//回溯
}
} int main(){
scanf("%d%d",&n,&sum);
yh[][]=;
for(int i=;i<=n;i++)//构造杨辉三角
for(int j=;j<=i;j++)
yh[i][j]=yh[i-][j-]+yh[i-][j];
dfs(,);
return ;
}

luoguP1118 [USACO06FEB]数字三角形`Backward Digit Su`… 题解的更多相关文章

  1. P1118 [USACO06FEB]数字三角形`Backward Digit Su`… 回溯法

    有这么一个游戏: 写出一个11至NN的排列a_iai​,然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少11,直到只剩下一个数字位置.下面是一 ...

  2. P1118 [USACO06FEB]数字三角形`Backward Digit Su`…

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 11 to N(1 \le N \ ...

  3. P1118 [USACO06FEB]数字三角形Backward Digit Su…

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N ...

  4. 洛谷—— P1118 [USACO06FEB]数字三角形Backward Digit Su…

    https://www.luogu.org/problem/show?pid=1118#sub 题目描述 FJ and his cows enjoy playing a mental game. Th ...

  5. P1118 [USACO06FEB]数字三角形`Backward Digit Su`… (dfs)

    https://www.luogu.org/problemnew/show/P1118 看的出来是个dfs 本来打算直接从下到上一顿搜索 但是不会 看了题解才知道系数是个杨辉三角....... 这样就 ...

  6. 洛谷P1118 [USACO06FEB]数字三角形`Backward Digit Su`…

    #include<iostream> using namespace std ; ; int y[N][N]; int n; int a[N]; bool st[N]; int sum; ...

  7. Luogu P1118 [USACO06FEB]数字三角形 Backward Digit Sums | 搜索、数学

    题目链接 思路:设一开始的n个数为a1.a2.a3...an,一步一步合并就可以用a1..an表示出最后剩下来的数,不难发现其中a1..an的系数恰好就是第n层杨辉三角中的数.所以我们可以先处理出第n ...

  8. [USACO06FEB]数字三角形

    题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N ...

  9. P1118 [USACO06FEB]Backward Digit Sums G/S

    P1118 [USACO06FEB]Backward Digit Sums G/S 题解:  (1)暴力法.对1-N这N个数做从小到大的全排列,对每个全排列进行三角形的计算,判断是否等于N.  对每个 ...

随机推荐

  1. C#DataTable使用方法详解

    在项目中常常常使用到DataTable,假设DataTable使用得当,不仅能使程序简洁有用,并且可以提高性能,达到事半功倍的效果,现对DataTable的使用技巧进行一下总结. 1.添加引用 1 2 ...

  2. 字符串格式连接sqlserver数据库的字段概念解释

    以连接sqlserver数据库举例说明如:“Provider=SQLOLEDB.1;Password=******;Persist Security Info=True;User ID=sa;Init ...

  3. javascript:void(0)的含义

    void关键字介绍 首先,void关键字是javascript当中非常重要的关键字,该操作符指定要计算或运行一个表达式,但是不返回值. 语法格式: void func() void(func()) 实 ...

  4. Spark广播变量和累加器

    一.广播变量图解 二.代码 val conf = new SparkConf() conf.setMaster("local").setAppName("brocast& ...

  5. The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone 。

    The server time zone value '�й���׼ʱ��' is unrecognized or represents more than one time zone. 今天有Mys ...

  6. c#: 剪切板监视实现

    CR TubeGet中有用户需要剪切板监视功能,记录代码以做备忘: using System; using System.Runtime.InteropServices; using System.W ...

  7. 【Netty】初识Netty

    一.为什么会出现Netty 之前我们使用通用的应用程序或库来相互通信.例如,我们经常使用HTTP客户机库从web服务器检索信息,并通过web服务调用远程过程调用.然而,通用协议或其实现有时伸缩性不是很 ...

  8. Httpd服务进阶知识-基于Apache Modele的LAMP架构之Discuz!案例

    Httpd服务进阶知识-基于Apache Modele的LAMP架构之Discuz!论坛案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装依赖包及数据库  博主推荐阅读: ...

  9. Httpd服务入门知识-使用mod_deflate模块压缩页面优化传输速度

    Httpd服务入门知识-使用mod_deflate模块压缩页面优化传输速度 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.mod_deflate模块概述 mod_deflate ...

  10. linux下activemq安装与配置activemq-5.15.2

    linux下activemq安装与配置 前提 配置好jdk环境   一.下载:apache-activemq-5.15.2-bin.tar.gz https://archive.apache.org/ ...