URAL——DFS找规律——Nudnik Photographer
2015-05-07 14:55
309 查看
Description
If two people were born one after another with one second difference and one of them is a child, then the other one is a child too. We get by induction that all the people are children.
Everyone knows that the mathematical department of the Ural State University is a big family of N persons, 1, 2, 3, …, N years old respectively.
Once the dean of the department ordered a photo if his big family. There were to be present all the students of the department arranged in one row. At first the dean wanted to arrange them by their age starting from the youngest student, but than he decided that it would look unnatural. Than he advised to arrange the students as follows:
The 1 year old student is to sit at the left end of the row.
The difference in ages of every two neighbors mustn’t exceed 2 years.
The dean decided that thereby the students would seem look as they were arranged by their ages (one can hardly see the difference in ages of 25 and 27 years old people). There exist several arrangements satisfying to the requirements. Photographer didn’t want to thwart dean’s desire and made the photos of all the possible mathematical department students’ arrangements.
Input
There is the integer number N, 1 ≤ N ≤ 55.
Output
the number of photos made by the photographer.
Sample Input
大意:让你排座位,第一个人必须是年龄为1的,其他必须相邻的年龄间隔不超过2,可以DFS打表过。。
说是道DP。。得到dp
= dp[n-1] + dp[n-3] + 1看传送门
AC代码
If two people were born one after another with one second difference and one of them is a child, then the other one is a child too. We get by induction that all the people are children.
Everyone knows that the mathematical department of the Ural State University is a big family of N persons, 1, 2, 3, …, N years old respectively.
Once the dean of the department ordered a photo if his big family. There were to be present all the students of the department arranged in one row. At first the dean wanted to arrange them by their age starting from the youngest student, but than he decided that it would look unnatural. Than he advised to arrange the students as follows:
The 1 year old student is to sit at the left end of the row.
The difference in ages of every two neighbors mustn’t exceed 2 years.
The dean decided that thereby the students would seem look as they were arranged by their ages (one can hardly see the difference in ages of 25 and 27 years old people). There exist several arrangements satisfying to the requirements. Photographer didn’t want to thwart dean’s desire and made the photos of all the possible mathematical department students’ arrangements.
Input
There is the integer number N, 1 ≤ N ≤ 55.
Output
the number of photos made by the photographer.
Sample Input
input | output |
---|---|
4 | 4 |
Notes
If N = 4 then there are following possible arrangements: (1,2,3,4), (1,2,4,3), (1,3,2,4) and (1,3,4,2).大意:让你排座位,第一个人必须是年龄为1的,其他必须相邻的年龄间隔不超过2,可以DFS打表过。。
说是道DP。。得到dp
= dp[n-1] + dp[n-3] + 1看传送门
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int vis[60]; int n; int ans; void dfs(int m,int num) { if(num == n){ ans++; return ; } for(int i = m - 2; i <= m + 2; i++){ if(i <= 1 || i > n ) continue; if(!vis[i]){ vis[i] = 1; dfs(i,num+1); vis[i] = 0; } } } int main() { while(~scanf("%d",&n)){ ans = 0; memset(vis,0,sizeof(vis)); vis[1] = 1; dfs(1,1); printf("%d\n",ans); } return 0; }
AC代码
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; int dp[60]; int main() { int n; dp[1] = 1; dp[2] = 1; dp[3] = 2; while(~scanf("%d",&n)){ for(int i = 3; i <= n ;i++) dp[i] = dp[i-1] + dp[i-3] + 1; printf("%d\n",dp ); } return 0; }
相关文章推荐
- 【URAL 1260】 DP (dfs打表之后找规律也行)
- ural1009 第一个动态规划(dp)题目 。。。似乎也可以用dfs+记忆化收索 解。。。
- URAL 1169. Pairs 图中抽象出数学公式dfs
- URAL 1225-Flags(规律递推)
- URAL 1295. Crazy Notions(数学啊 & 找规律)
- URAL1223——DFS—— Chernobyl’ Eagle on a Roof
- zoj2686 Cycle Game---dfs 找规律 博弈
- ural 1033. Labyrinth(dfs)
- URAL 1152 False Mirrors (DFS枚举)
- poj1176 Party Lamps dfs优化+规律枚举
- ural 1056 Computer Net(树形DP)需要用到两遍dfs
- URAL 1501. Sense of Beauty(记忆化搜索 dfs)
- 【DFS】URAL - 2104 - Game with a Strip
- Problem A. Arithmetic Derivative-XVII Open Cup named after E.V. Pankratiev||找规律&数学&DFS
- ural 2029 Towers of Hanoi Strike Back (数学找规律)
- URAL 2065 Different Sums (找规律)
- 【博弈 && dfs】URAL 2104 Game with a Strip
- jzoj P3769 A+B___dfs+规律
- URAL 1051 跳跳棋(孔明棋)(加强版) 数学找规律(较弱哦)
- [构造 找规律 孔明棋] Ural 1051 Simple Game on a Grid