使用Memoization以p / q形式的cos(nx)

时间:2018-02-12 01:14:42

标签: c++ algorithm recursion

我想计算cos(nx)= p / q使用递归和memoization所以我尝试了Chebyshev方法的递归方法它适用于n的小输入,但我想做更大的输入,如10 ^ 18等。我应该接近它来计算p / q形式的cos(nx)吗? 对于更大的n值,我得到内存问题我该如何解决? cos(x)= a / b已知

#include <bits/stdc++.h>

using namespace std;
map<long, long> numerator;
map<long, long> denominator;
#define MODULO 1000000007
long a, b, n;

int main() {
    cin >> a >> b;
    cin >> n;
    numerator[0] = 1;
    numerator[1] = a;
    denominator[0] = 1;
    denominator[1] = b;
    for (long i = 2; i <= n; ++i) {
        numerator[i] = (long) (2 * a * numerator[i - 1] - b*numerator[i - 2]) % MODULO;
        denominator[i] = (denominator[i - 1] * denominator[i - 2]) % MODULO;
    }
    cout << "numerator of cos(nx) = " << numerator[n] << " denominator = " << denominator[n] << endl;
}

1 个答案:

答案 0 :(得分:0)

lol这是你要求的竞赛问题。你太无耻了。这是Codechef二月挑战赛的问题。对于某些评级,你有多低? SMH