Codeforces Round #382 (Div. 1)

A. Tennis Championship
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Famous Brazil city Rio de Janeiro holds a tennis tournament and Ostap Bender doesn't want to miss this event. There will be n players participating, and the tournament will follow knockout rules from the very first game. That means, that if someone loses a game he leaves the tournament immediately.

Organizers are still arranging tournament grid (i.e. the order games will happen and who is going to play with whom) but they have already fixed one rule: two players can play against each other only if the number of games one of them has already played differs by no more than one from the number of games the other one has already played. Of course, both players had to win all their games in order to continue participating in the tournament.

Tournament hasn't started yet so the audience is a bit bored. Ostap decided to find out what is the maximum number of games the winner of the tournament can take part in (assuming the rule above is used). However, it is unlikely he can deal with this problem without your help.

Input

The only line of the input contains a single integer n (2 ≤ n ≤ 1018) — the number of players to participate in the tournament.

Output

Print the maximum number of games in which the winner of the tournament can take part.

Examples
input
2
output
1
input
3
output
2
input
4
output
2
input
10
output
4
Note

In all samples we consider that player number 1 is the winner.

In the first sample, there would be only one game so the answer is 1.

In the second sample, player 1 can consequently beat players 2 and 3.

In the third sample, player 1 can't play with each other player as after he plays with players 2 and 3 he can't play against player 4, as he has 0 games played, while player 1 already played 2. Thus, the answer is 2 and to achieve we make pairs (1, 2) and (3, 4) and then clash the winners.

f[n] 表示一个人要想参加n场比赛最少需要多少人

#include <cstdio>
#include <vector>
#include <iostream>
using namespace std;

typedef long long ll;
ll a[120];

int main()
{
    ll n;
    a[1] = 2;
    a[2] = 3;
    scanf("%lld", &n);
    for (int i = 3; i < 100; i++)
    {
        a[i] = a[i - 1] + a[i - 2];
    }
    for (int i = 1; i < 100; i++)
    {
        if (a[i] > n)
        {
            printf("%d\n", i - 1);
            break;
        }
    }
    return 0;
}

C. Ostap and Tree
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Ostap already settled down in Rio de Janiero suburb and started to grow a tree in his garden. Recall that a tree is a connected undirected acyclic graph.

Ostap's tree now has n vertices. He wants to paint some vertices of the tree black such that from any vertex u there is at least one black vertex v at distance no more than kDistance between two vertices of the tree is the minimum possible number of edges of the path between them.

As this number of ways to paint the tree can be large, Ostap wants you to compute it modulo 109 + 7. Two ways to paint the tree are considered different if there exists a vertex that is painted black in one way and is not painted in the other one.

Input

The first line of the input contains two integers n and k (1 ≤ n ≤ 1000 ≤ k ≤ min(20, n - 1)) — the number of vertices in Ostap's tree and the maximum allowed distance to the nearest black vertex. Don't miss the unusual constraint for k.

Each of the next n - 1 lines contain two integers ui and vi (1 ≤ ui, vi ≤ n) — indices of vertices, connected by the i-th edge. It's guaranteed that given graph is a tree.

Output

Print one integer — the remainder of division of the number of ways to paint the tree by 1 000 000 007 (109 + 7).

Examples
input
2 0
1 2
output
1
input
2 1
1 2
output
3
input
4 1
1 2
2 3
3 4
output
9
input
7 2
1 2
2 3
1 4
4 5
1 6
6 7
output
91
Note

In the first sample, Ostap has to paint both vertices black.

In the second sample, it is enough to paint only one of two vertices, thus the answer is 3: Ostap can paint only vertex 1, only vertex 2, vertices 1 and 2 both.

In the third sample, the valid ways to paint vertices are: {1, 3}{1, 4}{2, 3}{2, 4}{1, 2, 3}{1, 2, 4}{1, 3, 4}{2, 3, 4}{1, 2, 3, 4}.


树dp,dp[x][j]表示x这棵子树控制范围为j的方案数,j < 0表示该棵子树可以向外控制 -j 个单位,j > 0 表示这棵子树需要外界的 j 个单位。

#include <cstdio>
#include <vector>
#include <cstring>
using namespace std;

typedef long long ll;
const int N = 110;
const ll mod = 1000000007;
ll dp[N][N];
int  n, dis;
vector <int> edge[N];

int F(int x)
{
    return x + dis;
}

void dfs(int x, int fa)
{
    ll tmp[N];
    dp[x][F(-dis)] = dp[x][F(1)] = 1;
    for (int i = 0; i < edge[x].size(); i++)
    {
        int u = edge[x][i];
        if (u == fa) continue;
        dfs(u, x);
        memset(tmp, 0, sizeof(tmp));
        for (int a = -dis; a <= dis; a++)
        {
            for (int b = -dis; b <= dis; b++)
            {
                ll num = dp[x][F(a)] * dp[u][F(b)] % mod;
                if (a > 0 && b > 0) 
                {
                    int pos = max(a, b + 1);
                    tmp[F(pos)] = (tmp[F(pos)] + num) % mod;
                }
                else if (a <= 0 && b <= 0)
                {
                    int pos = min(a, b + 1);
                    tmp[F(pos)] = (tmp[F(pos)] + num) % mod;
                }
                else if (a > 0 && b <= 0)
                {
                    int pos = (-b >= a) ? b + 1 : a;
                    tmp[F(pos)] = (tmp[F(pos)] + num) % mod;
                }
                else if (a <= 0 && b > 0)
                {
                    int pos = (-a >= b) ? a : b + 1;
                    tmp[F(pos)] = (tmp[F(pos)] + num) % mod;
                }
            }
        }
        memcpy(dp[x], tmp, sizeof(tmp));
    }
}

int main()
{
    scanf("%d%d", &n, &dis);
    int a, b;
    for (int i = 1; i < n; i++)
    {
        scanf("%d%d", &a, &b);
        edge[a].push_back(b);
        edge[b].push_back(a);
    }
    dfs(1, -1);
    ll ans = 0;
    for (int i = 0; i <= dis; i++)
        ans = (ans + dp[1][i]) % mod;
    printf("%lld\n", ans);
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值