hdu 1297 Children’s Queue 递推 大数

题目连接:点击打开链接

原文链接:点击打开链接

Children’s Queue

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5660 Accepted Submission(s): 1755

Description

There are many students in PHT School. One day, the headmaster whose name is PigHeader wanted all students stand in a line. He prescribed that girl can not be in single. In other words, either no girl in the queue or more than one girl stands side by side. The case n=4 (n is the number of children) is like
FFFF, FFFM, MFFF, FFMM, MFFM, MMFF, MMMM
Here F stands for a girl and M stands for a boy. The total number of queue satisfied the headmaster’s needs is 7. Can you make a program to find the total number of queue with n children?

Input

There are multiple cases in this problem and ended by the EOF. In each case, there is only one integer n means the number of children (1<=n<=1000)

Output

For each test case, there is only one integer means the number of queue satisfied the headmaster’s needs.

Sample Input
 
 
  1.  
  2. 1
  3. 2
  4. 3
Sample Output
 
 
  1.  
  2. 1
  3. 2
  4. 4
Source

HDU1297

思路如下:

F(n)表示n个人的合法队列,则:
按照最后一个人的性别分析,他要么是男,要么是女,所以可以分两大类讨论:
1、如果n个人的合法队列的最后一个人是男,则对前面n-1个人的队列没有任何限制,他只要站在最后即可,所以,这种情况一共有F(n-1);

2、如果n个人的合法队列的最后一个人是女,则要求队列的第n-1个人务必也是女生,这就是说,限定了最后两个人必须都是女生,这又可以分两种情况:
(1)、如果队列的前n-2个人是合法的队列,则显然后面再加两个女生,也一定是合法的,这种情况有F(n-2);
(2)难点在于,即使前面n-2个人不是合法的队列,加上两个女生也有可能是合法的,当然,这种长度为n-2的不合法队列,不合法的地方必须是尾巴,就是说,这里说的长度是n-2的不合法串的形式必须是“F(n-4)+男+女”,这种情况一共有F(n-4).

所以递推公式为F(n)=F(n-1)+F(n-2)+F(n-4).

若感觉本题较难,可先查看文章:[ACM_HDU_2045]LELE的RPG难题,思路与本题类似,但较为简单。

得出递推公式如下:

 
 
  1. f[n] = f[n - 1] + f[n - 2] + f[n - 4]

还有需要注意的是本题中可能得出极大的数字,如当n为1000时,结果为:

 
 
  1. 12748494904808148294446671041721884239818005733501580815621713101333980596197474744336199742452912998225235910891798221541303838395943300189729514282623665199754795574309980870253213466656184865681666106508878970120168283707307150239748782319037
这种超大数完全不是 C++提供的数值类型能够储存的,因此我们可以使用数组来模拟超大数运算,详情请看 [ACM_ZJUT_1382]计算N!(数组模拟超大数运算)

代码:

    #include<stdio.h>
    int main(){
    	int n;
    	int f[1001][101] = {0};
    	f[0][1] = 1;
    	f[1][1] = 1;
    	f[2][1] = 2;
    	f[3][1] = 4;
    	for(int i = 4; i < 1001; ++i){
    		for(int j = 1; j < 101; ++j){
    			f[i][j] += f[i - 1][j] + f[i - 2][j] + f[i - 4][j];	//数组的每一位相加
    			f[i][j + 1] += f[i][j] / 10000;	//超过4位的部分保存至数组下一位中
    			f[i][j] %= 10000;	//每位数组只保存其中4位
    		}
    	}
    	while(scanf("%d", &n) != EOF){
    		int k = 100;
    		while(!f[n][k--]);	//排除前面为空的数组
    		printf("%d", f[n][k + 1]);	//输出结果的前四位
    		for(; k > 0; --k){
    			printf("%04d", f[n][k]);	//输出其余的所有四位数字,若数字小于四位,则前面用0填充
    		}
    		printf("\n");
    	}
    	return 0;
    }


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,HDU1622是一道关于二叉树的题目,要求读入一系列二叉树的节点信息,输出它们的层序遍历结果。如果输入的二叉树不完整或存在重复节点,则输出"not complete"。下面是Java的实现代码: ```java import java.util.LinkedList; import java.util.Queue; import java.util.Scanner; public class Main { static class Node { int val; Node left, right; public Node(int val) { this.val = val; } } public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { String s = sc.nextLine(); if (s.isEmpty()) { continue; } String[] nodes = s.split("\\s+"); Node root = new Node(Integer.parseInt(nodes[0].substring(1))); Queue<Node> queue = new LinkedList<>(); queue.offer(root); boolean isComplete = true; for (int i = 1; i < nodes.length - 1; i += 2) { Node cur = queue.poll(); if (!nodes[i].equals("()")) { cur.left = new Node(Integer.parseInt(nodes[i].substring(1))); queue.offer(cur.left); } else { isComplete = false; } if (!nodes[i + 1].equals("()")) { cur.right = new Node(Integer.parseInt(nodes[i + 1].substring(0, nodes[i + 1].length() - 1))); queue.offer(cur.right); } else { isComplete = false; } } if (!isComplete) { System.out.println("not complete"); continue; } StringBuilder sb = new StringBuilder(); queue.offer(root); while (!queue.isEmpty()) { Node cur = queue.poll(); sb.append(cur.val).append(" "); if (cur.left != null) { queue.offer(cur.left); } if (cur.right != null) { queue.offer(cur.right); } } System.out.println(sb.toString().trim()); } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值