zoukankan      html  css  js  c++  java
  • 今日训练 搜索

    D. Fun with Integers

    You are given a positive integer nn greater or equal to 22. For every pair of integers aa and bb (2|a|,|b|n2≤|a|,|b|≤n), you can transform aa into bb if and only if there exists an integer xx such that 1<|x|1<|x| and (ax=ba⋅x=b or bx=ab⋅x=a), where |x||x| denotes the absolute value of xx.

    After such a transformation, your score increases by |x||x| points and you are not allowed to transform aa into bb nor bb into aa anymore.

    Initially, you have a score of 00. You can start at any integer and transform it as many times as you like. What is the maximum score you can achieve?

    Input

    A single line contains a single integer nn (2n1000002≤n≤100000) — the given integer described above.

    Output

    Print an only integer — the maximum score that can be achieved with the transformations. If it is not possible to perform even a single transformation for all possible starting integers, print 00.

    Examples
    input
    Copy
    4
    output
    Copy
    8
    input
    Copy
    6
    output
    Copy
    28
    input
    Copy
    2
    output
    Copy
    0
    Note

    In the first example, the transformations are 24(2)(4)22→4→(−2)→(−4)→2.

    In the third example, it is impossible to perform even a single transformation.

    这是一个很简单的题目核心代码只有十几行,

    不过你要先看懂题目,这个题目意思是然你在2~n里面找到有倍数关系的a,b

    注意a,b可以取正取负,x也可以,这个就说明每找到一组得到的值就是4*|x|

    知道这个就很简单了,你就直接暴力吧。

    #include <cstdio>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <map>
    #include <cmath>
    #include <queue>
    #include <set>
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    const int maxn = 1e5 + 100;
    
    int main()
    {
        int n;
        ll ans = 0;
        cin >> n;
        for(int i=2;i<=n;i++)
        {
            int x = i;
            for(int j=2;x*j<=n;j++)
            {
                ans += j;
            }
        }
        printf("%lld
    ", ans * 4);
        return 0;
    }

    F1. Tree Cutting (Easy Version)

    You are given an undirected tree of nn vertices.

    Some vertices are colored blue, some are colored red and some are uncolored. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.

    You choose an edge and remove it from the tree. Tree falls apart into two connected components. Let's call an edge nice if neither of the resulting components contain vertices of both red and blue colors.

    How many nice edges are there in the given tree?

    Input

    The first line contains a single integer nn (2n31052≤n≤3⋅105) — the number of vertices in the tree.

    The second line contains nn integers a1,a2,,ana1,a2,…,an (0ai20≤ai≤2) — the colors of the vertices. ai=1ai=1 means that vertex ii is colored red, ai=2ai=2 means that vertex ii is colored blue and ai=0ai=0 means that vertex ii is uncolored.

    The ii-th of the next n1n−1 lines contains two integers vivi and uiui (1vi,uin1≤vi,ui≤n, viuivi≠ui) — the edges of the tree. It is guaranteed that the given edges form a tree. It is guaranteed that the tree contains at least one red vertex and at least one blue vertex.

    Output

    Print a single integer — the number of nice edges in the given tree.

    Examples
    input
    Copy
    5
    2 0 0 1 2
    1 2
    2 3
    2 4
    2 5
    
    output
    Copy
    1
    
    input
    Copy
    5
    1 0 0 0 2
    1 2
    2 3
    3 4
    4 5
    
    output
    Copy
    4
    
    input
    Copy
    3
    1 1 2
    2 3
    1 3
    
    output
    Copy
    0
    
    Note

    Here is the tree from the first example:

    The only nice edge is edge (2,4)(2,4). Removing it makes the tree fall apart into components {4}{4} and {1,2,3,5}{1,2,3,5}. The first component only includes a red vertex and the second component includes blue vertices and uncolored vertices.

    Here is the tree from the second example:

    Every edge is nice in it.

    Here is the tree from the third example:

    Edge (1,3)(1,3) splits the into components {1}{1} and {3,2}{3,2}, the latter one includes both red and blue vertex, thus the edge isn't nice. Edge (2,3)(2,3) splits the into components {1,3}{1,3} and {2}{2}, the former one includes both red and blue vertex, thus the edge also isn't nice. So the answer is 0.

    这个题目就是一个思维题,只要你想清楚,如果一棵树的子树都是红节点且没有蓝节点,那么就可以从这里划分。

    然后你就写一个dfs之后去记录每一个节点和它子树的的蓝节点和红节点数量就可以了。

    #include <cstdio>
    #include <iostream>
    #include <vector>
    #include <algorithm>
    #include <cstring>
    #include <string>
    #include <map>
    #include <cmath>
    #include <queue>
    #include <set>
    #define inf 0x3f3f3f3f
    using namespace std;
    typedef long long ll;
    const int maxn = 3e5 + 100;
    int f[maxn], a[maxn], r, b;
    vector<int>vec[maxn];
    struct node
    {
        int r, b;
        node(int r=0,int b=0):r(r),b(b){}
    };
    bool vis[maxn];
    int ans = 0;
    node dfs(int s)
    {
        int red = 0, blue = 0;
        if (a[s] == 1) red++;
        if (a[s] == 2) blue++;
        int len = vec[s].size();
        // if (len == 0)
        // {
        //     if (red == r && blue == 0 || red == 0 && blue == b) ans++;
        //     return node(red, blue);
        // }
        for(int i=0;i<len;i++)
        {
            if (vis[vec[s][i]]) continue;
            vis[vec[s][i]] = 1;
            node e=dfs(vec[s][i]);
            red += e.r;
            blue += e.b;
        }
        //printf("s=%d red=%d blue=%d
    ", s, red, blue);
        if (red == r && blue == 0 || red == 0 && blue == b) ans++;
        return node(red, blue);
    }
    
    int main()
    {
        int n;
        cin >> n;
        b = 0, r = 0;
        for (int i = 1; i <= n; i++) f[i] = i;
        for (int i = 1; i <= n; i++)
        {
            cin >> a[i];
            if (a[i] == 2) b++;
            if (a[i] == 1) r++;
        }
        for(int i=1;i<n;i++)
        {
            int x, y;
            cin >> x >> y;
            vec[x].push_back(y);
            vec[y].push_back(x);
            f[y] = x;
        }
        memset(vis, 0, sizeof(vis));
        int root = 1;
        while (root != f[root]) root = f[root];
    //    printf("root=%d
    ", root);
        node e = dfs(root);
        printf("%d
    ", ans);
        return 0;
    }
  • 相关阅读:
    Pascal's Triangle II
    Pascal's Triangle
    Path Sum II
    Path Sum
    plusOne
    Divide Two Integers
    64. ZigZag Conversion
    63. Swap Nodes in Pairs && Rotate List && Remove Nth Node From End of List
    62. Divide Two Integers
    61. Unique Paths && Unique Paths II
  • 原文地址:https://www.cnblogs.com/EchoZQN/p/10694157.html
Copyright © 2011-2022 走看看