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:
| |||||||||||||
Definition | |||||||||||||
| |||||||||||||
Limits | |||||||||||||
| |||||||||||||
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) | |||||||||||||
| |||||||||||||
1) | |||||||||||||
| |||||||||||||
2) | |||||||||||||
| |||||||||||||
3) | |||||||||||||
| |||||||||||||
4) | |||||||||||||
|
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!