How to add this condition to it and make it optimal?

Question link: http://codeforces.com/problemset/problem/431/C

Most recently, a creative student, Lesha, had a lecture on trees. After the lecture, Alex was inspired and came up with his own tree, which he called the k-tree.

An A-tree is an infinite root tree, where:

  • each vertex has exactly k children;
  • each rib has some weight;
  • if we look at the edges going from some vertex to its children (exactly k edges), then their weights will be 1, 2, 3, ..., k.

The figure below shows part of a 3-tree.

enter image description here

, , , : " n ( ), k-, , d?". . , 1000000007 (10 ^ 9 + 7). ( )


, : n, k d (1 ≤ n, k ≤ 100; 1 ≤ d ≤ k).


- 1000000007 (10 ^ 9 + 7).

, . , , atleast d . ? :

void calc(int present, int total,int k) // Here, present is initialised to 0.
                                        // total is equal to n that is reqd.
                                        // k is the value in the question
{
    if (total == present)
    {
        ans++;
        ans = ans%val;
        return;
    }
    else
    {
        for ( int i = 1; i <= k; i++ )
        {
            if (present+i <= total)
                return calc(present+i,total,k);
        }
    }
}
+4
1

- d , d.

void calc(int present, int total,int k, int d, bool atleastd)
{

, atleastd.

    if (total == present && atleastd)
    {
        ans++;
        ans = ans%val;
        return;
    }
    else
    {
        for ( int i = 1; i <= k; i++ )
        {
            if (present+i <= total)

, , atleastd, , (i >= d).

                calc(present+i,total,k,d,atleastd || i >= d);

, return . - , == 1.

        }
    }
}

, ans val , ans , 0, val = 1000000007.


, , n <= 15, n = 100.

n = 100, memoization . .

+2

Source: https://habr.com/ru/post/1610079/


All Articles