HDU 1293 The Number of Paths
来源:互联网 发布:matlab mac安装教程 编辑:程序博客网 时间:2024/06/06 05:04
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1293
Problem Description
Let f (n) be the number of paths with n steps starting from O (0, 0), with steps of the type (1, 0), or (-1, 0), or (0, 1), and never intersecting themselves. For instance, f (2) =7, as shown in Fig.1. Equivalently, letting E=(1,0),W=(-1,0),N=(0,1), we want the number of words A1A2...An, each Ai either E, W, or N, such that EW and WE never appear as factors.
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 steps(1<=n<=1000).
Output
For each test case, there is only one integer means the number of paths.
Sample Input
12
Sample Output
37
Author
SmallBeer (CML)
题目大意:给出已知线段的段数(即前进的步数),线段的前进方向不可以向下,求有多少种走方。
要求第N步的走方数:
走第N步就必须走完N-1步,而在N-1步里面有方向向上,向左,向右三种情况。向左和向右的下一步只有俩种走法(即向上、同向),向上的下一步有三种走法。
所以需要统计在N-1步里面有多少向上的。
在N-2的步法里面每一种都只能产生一种向上的步法,即在N-1里面有向上的步法q[n-2]种。
递推式:q[n]=2*q[n-1]+q[n-2];
AC代码:
import java.math.BigInteger;import java.util.Scanner;public class Main { public static void main(String[] args) { int n; Scanner cin =new Scanner(System.in); while(cin.hasNext()) { BigInteger ans=BigInteger.valueOf(3); BigInteger count=BigInteger.valueOf(1); BigInteger sum; n=cin.nextInt(); for(int i=2;i<=n;i++) { sum=ans; ans=ans.multiply(BigInteger.valueOf(2)); ans=ans.add(count); count=sum; } System.out.println(ans); } }}
路途中。。。。。
- HDU 1293 The Number of Paths
- The Number of Paths - HDU 1293 DP
- hdu 1293 The Number of Paths
- HDU The Number of set
- hdoj.1293 The Number of Paths【大数+排列组合】 2015/08/07
- SGU 407-Number of Paths in the Empire【DP】
- hdu 3006 the number of set 【dp】
- hdu 3006 The Number of set
- hdu 3006 The Number of set
- HDU 3006 The Number of set
- HDU-3006The Number of set
- hdu 3948 The Number of Palindromes
- HDU,3006,The Number of set
- hdu 4727 The Number Off of FFF
- Hdu 4727 The Number Off of FFF
- HDU 4727 The Number Off of FFF
- hdu The Number Off of FFF
- HDU 4727 The Number Off of FFF
- zlib简单示例代码,compress、uncompress
- 如何为 Linux 卸载 Java
- POJ-2339-Rock, Scissors, Paper
- PPTP穿透NAT之深入分析
- Qt属性系统
- HDU 1293 The Number of Paths
- 图的广度遍历(BFS)
- 每日学习心得:未定义的命名空间前缀"xsd"问题和<%%>、<%=%>、<%$%>、<%@%>的区别
- 《人人都是产品经理》笔记【二】
- 存储过程调试
- 如何编译PHP源代码(Zend Encode使用方法)
- QT 4.8 静态库编译方法(转载)
- Dynamic Lover HDU4270
- 关于使用MASM的技巧