P1118 [USACO06FEB]数字三角形`Backward Digit Su`… (dfs)
https://www.luogu.org/problemnew/show/P1118
看的出来是个dfs 本来打算直接从下到上一顿搜索 但是不会
看了题解才知道系数是个杨辉三角.......
这样就好写了 但是还是踩了一些坑
一开始只有70分 是因为我最后再把a[i]求和 再判断的 改了之后80分......
加了一个剪枝:
if(s>sums)
return;
如果当前和大于sum就返回
成功AC
#include<bits/stdc++.h>
using namespace std; int n,sums,a[],flag,yh[][],vis[],s; void dfs(int t) //t标记当前确定了几个数
{
int i;
if(s>sums)
return;
if((t==n+)&&s==sums)
{
//cout<<a[0];
for(i=;i<=n;i++)
cout<<a[i]<<' ';
cout<<endl;
exit();
}
//else
{
for(i=;i<=n;i++)
{
if(!vis[i])
{
vis[i]=;
a[t]=i;
s+=yh[n][t]*a[t];
dfs(t+);
s-=yh[n][t]*a[t];
vis[i]=;
}
}
}
} int main()
{
int i,j;
cin>>n>>sums;
for(i=;i<=n;i++)
{
yh[i][]=;
yh[i][i]=;
}
for(i=;i<=n;i++)
for(j=;j<i;j++)
{
yh[i][j]=yh[i-][j-]+yh[i-][j];
//cout<<yh[i][j];
}
dfs();
}
P1118 [USACO06FEB]数字三角形`Backward Digit Su`… (dfs)的更多相关文章
- P1118 [USACO06FEB]数字三角形`Backward Digit Su`… 回溯法
有这么一个游戏: 写出一个11至NN的排列a_iai,然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少11,直到只剩下一个数字位置.下面是一 ...
- 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 \ ...
- 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 ...
- 洛谷—— P1118 [USACO06FEB]数字三角形Backward Digit Su…
https://www.luogu.org/problem/show?pid=1118#sub 题目描述 FJ and his cows enjoy playing a mental game. Th ...
- 洛谷P1118 [USACO06FEB]数字三角形`Backward Digit Su`…
#include<iostream> using namespace std ; ; int y[N][N]; int n; int a[N]; bool st[N]; int sum; ...
- luoguP1118 [USACO06FEB]数字三角形`Backward Digit Su`… 题解
一上午都在做有关搜索的题目,,, 看到这题之后就直接开始爆搜 结果只有70分, 其余的点硬生生的就是那么WA了. 我的天哪~ 70分代码: #include<iostream> #incl ...
- Luogu P1118 [USACO06FEB]数字三角形 Backward Digit Sums | 搜索、数学
题目链接 思路:设一开始的n个数为a1.a2.a3...an,一步一步合并就可以用a1..an表示出最后剩下来的数,不难发现其中a1..an的系数恰好就是第n层杨辉三角中的数.所以我们可以先处理出第n ...
- 【洛谷P1118】数字三角形
数字三角形 题目链接 4 16 3 1 2 4 3 1 2 4 (3+1) (1+2) (2+4)(3+1+1+2) (1+2+2+4) (3+1+1+1+2+2+2+4)16=1*3+3*1+3*2 ...
- 洛谷 P1118 数字三角形游戏 Label:dfs
题目描述 有这么一个游戏: 写出一个1-N的排列a[i],然后每次将相邻两个数相加,构成新的序列,再对新序列进行这样的操作,显然每次构成的序列都比上一次的序列长度少1,直到只剩下一个数字位置.下面是一 ...
随机推荐
- shred命令
不做陈冠希必备.... shred --help 用法:shred [选项]... 文件... Overwrite the specified FILE(s) repeatedly, in order ...
- print的位置差异
第一种,for循环和print是同级的,只有当for循环结束了才会print d=[]for i in range(1, 101): d.append(i)print d[6::7] 第二种,也就是p ...
- UVA 11090 : Going in Cycle!! 【spfa】
题目链接 题意及题解参见lrj训练指南 #include<bits/stdc++.h> using namespace std; const double INF=1e18; ; ; in ...
- 10.django的一些方法理解
django get_object_or_404 get_object_or_404是django的django shortcuts的方法,用来查询数据,或者抛出一个DoesNotExist的异常 用 ...
- 微信公众号发送告警Python脚本
调用该脚本,可以向微信公众号发生告警. #!/bin/env python #coding:utf-8 #Author: Hogan #Descript : 微信公众号发送告警脚本 import ur ...
- N皇后问题 --使用位运算解决
关键位运算 x & (-x) 取得最低位1 x & (x-1) 去掉最低位1 class Solution(object): def totalNQueens(self, n): &q ...
- datatable 和实体互转
public static class ModelConvertHelper<T> where T : class,new() { public static List<T> ...
- SQL 行转列(列的值不规则的数目)
--创建一个临时表用来存储数据 create table #tmp_SNValue_Table (FieldName nvarchar(20), [Value] nvarchar(max)) inse ...
- ArrayList,Vector ,LinkedList的存储性能和特性
ArrayList,Vector,LinkedList : 两者都采用数组元素方式存储数据,此数组元素数大于实际存储的数据(以便于增加和插入元素),允许直接按照序号索引元素,但是插入元素涉及数组元素移 ...
- QPS、TPS、PV、UV、GMV、IP、RPS
摘自:https://www.citrons.cn/jishu/226.html 关于 QPS.TPS.PV.UV.GMV.IP.RPS 这些词语,看起来好像挺专业.但实际上,我认为是这是每个程序员必 ...