Topcoder SRM 413 (Div 2) 1000.InfiniteSequence

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 np 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)

Limits

 
Time limit (s):2.000
Memory limit (MB):64

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: 1
A[0] = 1.
1) 
 
7
2
3
Returns: 7
A[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: 32768
3) 
 
256
2
4
Returns: 89
4) 
 
1
1000000
1000000
Returns: 2

This 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.


My Solution

Just memory search

// BEGIN CUT HERE

// END CUT HERE
#line 5 "InfiniteSequence.cpp"
#include <string>
#include <vector>
#include <map>
//#include <cmath>
using namespace std;
class InfiniteSequence {
	public:
    map<long long, long long> d;
	long long calc(long long n, int p, int q) {
		if(n == 0) return d[0] = 1;
		d[n] = (d[n/p] != 0) ? d[n/p] : calc(n/p, p, q);
		return d[n] += (d[n/q] != 0) ? d[n/q] : calc(n/q, p, q);
	}
};

Thank you!



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值