URAL——DFS找规律——Nudnik Photographer

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:

  1. The 1 year old student is to sit at the left end of the row.
  2. 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[n] = 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[n]);
    }
    return 0;
}

  

时间: 2024-10-09 23:37:16

URAL——DFS找规律——Nudnik Photographer的相关文章

ural 1260. Nudnik Photographer 规律dp

点击打开链接 1260. Nudnik Photographer Time limit: 1.0 second Memory limit: 64 MB 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 ar

Ural 1260 A nudnik photographer(DP)

A nudnik photographer 大意: 对1到N这些数进行排列,1必须要在最左边,相邻的两个数之间的差值不能超过2,问有多少种排列的方法. 思路: 对座位进行DP,当第一个是1,第二个是2的时候,组合为dp[i-1]:当第一个是1,第二个是3的时候,第三个也确定了是2,组合为dp[i-3]:还有最后一种情况是1357--8642. 所以DP方程为dp[i] = dp[i-1]+dp[i-3]+1. #include <stdio.h> int n; int dp[100]; int

递推DP URAL 1260 Nudnik Photographer

题目传送门 1 /* 2 递推DP: dp[i] 表示放i的方案数,最后累加前n-2的数字的方案数 3 */ 4 #include <cstdio> 5 #include <algorithm> 6 #include <cmath> 7 #include <cstring> 8 using namespace std; 9 10 const int MAXN = 1e4 + 10; 11 const int INF = 0x3f3f3f3f; 12 int

URAL 1295. Crazy Notions(数学啊 &amp; 找规律)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1295 1295. Crazy Notions Time limit: 0.5 second Memory limit: 64 MB For five days robot-loader JK546L54p has been buried under the thick layer of the Sibelian plutonium slag. The terrible strike of th

【模拟题(63550802...)】解题报告【贪心】【拓扑排序】【找规律】【树相关】

目录: 1.A[树相关]    2.B[找规律]    3.C[贪心][拓扑排序] A. 描述(A 输入文件 : A.input 输出文件 : A.output)一个城市的构成是一颗n 个节点的树(2 ≤ n ≤ 200), 现在需要在树中找出两条不相交的路径(即两条路径不能有重边也不能有重点),使得路径的长度的乘积最大.输入描述第一行一个数n 表示这个城市一共有 n 个节点.接下来 n-1 行,每行两个数ai 和bi (1 ≤ ai,bi ≤ n ),分别表示从ai 到bi,有一条边,每条边的

HDU 4588 Count The Carries 数位DP || 打表找规律

2013年南京邀请赛的铜牌题...做的很是伤心,另外有两个不太好想到的地方....a 可以等于零,另外a到b的累加和比较大,大约在2^70左右. 首先说一下解题思路. 首先统计出每一位的1的个数,然后统一进位. 设最低位为1,次低位为2,依次类推,ans[]表示这一位上有多少个1,那么有 sum += ans[i]/2,ans[i+1] += ans[i]/2; sum即为答案. 好了,现在问题转化成怎么求ans[]了. 打表查规律比较神奇,上图不说话. 打表的代码 #include <algo

HDU 4919 打表找规律 java大数 map 递归

== oeis: 点击打开链接 瞎了,x.mod(BigInteger.ValueOf(2)).equal( BigInteger.ValueOf(1)) 写成了 x.mod(BigInteger.ValueOf(2)).equal( 1 ) T^T100块没了... import java.math.*; import java.util.*; import static java.lang.System.out; import java.io.*; public class Main { s

(zst的博弈) 【推理+找规律】

题目: 甲乙两人玩一个游戏: 一张卡片上有个数字,甲乙两人轮流操作, 若当前卡片上的数字为x, 每次操作可以把它变为x+1或2x, 且不能超过n (例如n=8,x=6,只能变为7而不能变为12), 每次甲首先在卡片上写1,规定写n的人获胜.给定n, 问甲是否有必胜策略? 分析: 看起来像一道博弈论的题,但实际上仅需细心的推理,耐心的找规律即可. 1.因为甲先写的1,所以乙的所有奇数全部要从甲的偶数哪里加1得来,因此如果n为奇数,甲只要保证自己写的数全是      奇数即可,因此奇数的情况下,甲赢

hdoj 2047 阿牛的EOF牛肉串 【找规律】

这道题再一次证明找规律真不是我的强项.... 阿牛的EOF牛肉串 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 20312    Accepted Submission(s): 9528 Problem Description 今年的ACM暑期集训队一共有18人,分为6支队伍.其中有一个叫做EOF的队伍,由04级的阿牛.XC以及05级