序列(数组简化递归时间复杂度)
2017-03-25 20:14
295 查看
序列
Time Limit: 1000MSMemory Limit: 65536KB
Submit
Statistic
Problem Descriptionimport java.util.Scanner;
import java.util.Arrays;
import java.math.*;
public class Main{
static int a[] = new int[1100];
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
while(input.hasNextInt())
{
int n = input.nextInt();
System.out.println(f(n)+1);
}
}
public static int f(int n)
{
int sum = 0;
if(n==1)
return 0;
else
{
for(int i=n/2;i>=1;i--)
{
if(a[i]!=0)
sum+= a[i]+1;
else
{
a[i]=f(i);
sum+=a[i]+1;
}
}
}
return sum;
}
}
我们来定义这样一种序列,这个序列的第一个数字是n,并且数列中的每一项必须大于等于下一项的二倍(如果存在下一项)。我们想知道满足条件的合法序列有多少个?比如n = 6
6
6 3
6 2
6 1
6 3 1
6 2 1
一共有6个合法的。
Input
多组输入。输入数据的第一行包含数字n(1<=n<=1000)。
Output
输出所求的结果。Example Input
6
Example Output
6
Hint
Author
cz相关文章推荐
- MIT:算法导论——2.渐近符号、递归及解法_和连续子数组的最大和、主方法/主定理求时间复杂度
- 数组连续子序列的最大的和;四种算法,四种时间复杂度
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)函数原型:
- 动态规划求数组中和最大字串的值 以及 字符串的循环移位(要求空间复杂度O(1),时间复杂度O(n))
- 百度笔试:给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数。 要求:空间复杂度O(1),时间复杂度为O(n)
- 访问单向链表和顺序数组第i个元素的时间复杂度的分析
- 求递归算法时间复杂度:递归树
- 作业:递归实现插入排序和在o(nlgn)时间复杂度内寻找和为定值的两个元素
- O(n)时间找出无序数组中最长的连续递增序列
- 求递归算法时间复杂度:递归树
- 线性时间复杂度求数组中第K大数
- 线性时间复杂度求数组中第K大数
- 对一个简单递归的 时间复杂度的分析
- PHP 中巧用数组降低程序的时间复杂度
- PHP 用数组降低程序的时间复杂度
- 给定一个存放整数的数组,重新排列数组使得数组左边为奇数,右边为偶数。 要求:空间复杂度O(1),时间复杂度为O(n)。
- 数组a[N],存放了1至N-1个数,其中某个数重复一次。写一个函数,找出被重复的数字.时间复杂度必须为o(N)
- PHP 中巧用数组降低程序的时间复杂度
- 在旋转后的有序数组中查找元素,要求O(logn)的时间复杂度
- 空间换时间,把递归的时间复杂度降低到O(2n)