Recursive recursive change

This content is actually very simple. . .

We will encounter some problems recursion, recursive process is very complicated and can not articulate in the analysis.

At this time, there is a good thing: recursion.

example:

NOIP2017-PJ · Preliminary Round: perfect program T3:

#include <iostream>
using namespace std;
int n, m;

int findans(int n, int m) {
    if (n == 0) return m;
    if (m == 0) return n % 3;
    return findans(n - 1, m) - findans(n, m - 1) + findans(n - 1, m - 1);
}

int main(){
    cin >> n >> m;
    cout << findans(n, m) << endl;
    return 0;
}

Input: $ \ text {5 6} $

If you write recursive tree, it is estimated that no less than one-page written.

That should be how to do it?

We define $ f_ {i, j} $ is the $ \ text results {findans (i, j)} $, then there will be some obvious conclusion:

$f_{0, x} = x$,$f_{x, 0} = x % 3.$

Then a recurrence formula can be obtained: $ f_ {i, j} = f_ {i-1, j} - f_ {i, j-1} + f_ {i-1, j-1} $

Then we can put all $ f_ {i, j} $ fitted to a matrix, delivery Release $ f (n, m) $ can.

Guess you like

Origin www.cnblogs.com/zengpeichen/p/11348774.html