an infinite sequence
来源:互联网 发布:飞常准网络值机登机牌 编辑:程序博客网 时间:2024/05/01 21:21
Problem Statement
NOTE: This problem statement contains subscripts that may not display properly if viewed outside of the applet.
Let's consider an infinite sequence A defined as follows:
A0 = 1;
Ai = A[i/p] + A[i/q] for all i >= 1, where [x] denotes the floor function of x. (see Notes)
You will be given n, p and q. Return the n-th element of A (index is 0-based).Definition
Class:
InfiniteSequence
Method:
calc
Parameters:
long long, int, int
Returns:
long long
Method signature:
long long calc(long long n, int p, int q)
(be sure your method is public)Notes
- [x] denotes the floor function of x which returns the highest integer less than or equal to x. For example, [3.4] = 3, [0.6] = 0.Constraints
- n will be between 0 and 10^12, inclusive.
- p and q will both be between 2 and 10^9, inclusive.Examples
0)
0
2
3
Returns: 1A[0] = 1.
1)
7
2
3
Returns: 7A[0] = 1; A[1] = A[0] + A[0] = 2; A[2] = A[1] + A[0] = 2 + 1 = 3; A[3] = A[2] + A[1] = 3 + 2 = 5; A[7] = A[3] + A[2] = 5 + 3 = 8.
2)
10000000
3
3
Returns: 327683)
256
2
4
Returns: 894)
1
1000000
1000000
Returns: 2This problem statement is the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly prohibited. (c)2003, TopCoder, Inc. All rights reserved.
解法:
1. 直接递归可能导致栈溢出, 因此使用map保存中间已经计算出的结果
2. 交换p & q, 可以减少递归的深度
- an infinite sequence
- Infinite Sequence
- Infinite Sequence
- Infinite Sequence
- codeforces_622A. Infinite Sequence
- A. Infinite Sequence
- A. Infinite Sequence
- CF Infinite Sequence
- [arc071f]Infinite Sequence
- Codeforces 622A Infinite Sequence
- CodeForces 622A Infinite Sequence
- CodeForces 622A Infinite Sequence
- CodeForces 622A Infinite Sequence
- CodeForces 622A-Infinite Sequence
- CodeForces 622A Infinite Sequence
- codeforces 675A Infinite Sequence
- CodeForces 675 A Infinite Sequence
- 【CodeForces】675A - Infinite Sequence
- 不错的网站
- 框计算
- “我的电脑”打开很慢的一种可能解决方法
- 郑州龙朗文化传播
- 对offsetLet,offsetTop,scrollLeft,scrollTop几个方法的理解
- an infinite sequence
- centos下samba配置-zt
- 职场值得学习的6种精神
- 鼠标的位置
- 存储过程加密
- Microsoft 官方安全指南
- 有兴趣的朋友可以看看,两个lisi参数能否传出
- cisco路由器配置ACL详解
- 使用批处理自动备份并压缩Oracle数据