luoguP1118 [USACO06FEB]数字三角形`Backward Digit Su`… 题解
一上午都在做有关搜索的题目,,,
看到这题之后就直接开始爆搜
结果只有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`… 题解的更多相关文章
- 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`… (dfs)
https://www.luogu.org/problemnew/show/P1118 看的出来是个dfs 本来打算直接从下到上一顿搜索 但是不会 看了题解才知道系数是个杨辉三角....... 这样就 ...
- 洛谷P1118 [USACO06FEB]数字三角形`Backward Digit Su`…
#include<iostream> using namespace std ; ; int y[N][N]; int n; int a[N]; bool st[N]; int sum; ...
- Luogu P1118 [USACO06FEB]数字三角形 Backward Digit Sums | 搜索、数学
题目链接 思路:设一开始的n个数为a1.a2.a3...an,一步一步合并就可以用a1..an表示出最后剩下来的数,不难发现其中a1..an的系数恰好就是第n层杨辉三角中的数.所以我们可以先处理出第n ...
- [USACO06FEB]数字三角形
题目描述 FJ and his cows enjoy playing a mental game. They write down the numbers from 1 to N (1 <= N ...
- P1118 [USACO06FEB]Backward Digit Sums G/S
P1118 [USACO06FEB]Backward Digit Sums G/S 题解: (1)暴力法.对1-N这N个数做从小到大的全排列,对每个全排列进行三角形的计算,判断是否等于N. 对每个 ...
随机推荐
- JAAS configuration for Kafka clients
Clients may configure JAAS using the client configuration property sasl.jaas.config or using the sta ...
- java jsp文件报错解决方法
初次使用java开发 下载好代码之后,用maven编译都是ok的,第二天,打开项目一看,好的web项目的jsp文件提示错误,后面,查了下问题,是tomcat没有配置路径导致的问题,现在大致记录一下解决 ...
- DevExpress之GridControl控件小知识
DevExpress之GridControl控件小知识 一.当代码中的DataTable中有建数据关系时,DevExpress 的 GridControl 会自动增加一个子视图 .列名也就是子表的字段 ...
- StarGAN学习笔记
11 December 2019 20:32 来自 <https://zhuanlan.zhihu.com/p/44563641> StarGAN StarGAN是CVPR2018 ...
- 2019 翔通动漫java面试笔试题 (含面试题解析)
本人5年开发经验.18年年底开始跑路找工作,在互联网寒冬下成功拿到阿里巴巴.今日头条.翔通动漫等公司offer,岗位是Java后端开发,因为发展原因最终选择去了翔通动漫,入职一年时间了,也成为了面 ...
- 嵌入式Web服务器boa在ARM平台的移植步骤
1.下载http://www.boa.org/ 2.解压tar xzf boa-0.94.13.tar.gz 3.编译cd boa-0.94.13/src./configure 生成了makefile ...
- 全面了解Cookie
一.Cookie的出现 浏览器和服务器之间的通信少不了HTTP协议,但是因为HTTP协议是无状态的,所以服务器并不知道上一次浏览器做了什么样的操作,这样严重阻碍了交互式Web应用程序的实现. 针对上述 ...
- 单词diamaund钻石diamaund英文
Diamond Di"a*mond (?; 277), n. [OE. diamaund, the hardest iron, steel, diamond, Gr. . Perh. the ...
- adb shell get/setprop, setenforce...
adb shell getprop <key> 获取设备参数信息adb shell setprop <key> <value> 设置设备参数信息 例子1:>C ...
- Android-----解析xml文件的三种方式
SAX解析方法介绍: SAX(Simple API for XML)是一个解析速度快并且占用内存少的XML解析器,非常适合用于Android等移动设备.SAX解析XML文件采用的是事件驱动,也就是说, ...