Niuke Programming Summit S2 Game 3

How to use an array to assign values ​​to a vector?
 ① When the vector is just created, it can be initialized directly.

int a[5]={
    
    1,2,3,4,5};
vector<int> v(a+a+5);

 ② When the vector has been created, you can use the Insert function.

int a[5]={
    
    1,2,3,4,5};
vector<int> v;
v.insert(v.begin(),a,a+5);

Tournament address:
Bronze & Silver & Gold
Diamond & King


Question A: Fighting monsters

  Simple simulation.

Question B: Simple formula

  The common matrix fast exponentiation method can be used to satisfy f (n) = A ∗ f (n − 1) + B ∗ f (n − 2) f(n)=A*f(n-1)+B*f (n-2)f(n)=Af(n1)+Bf(n2 ) This type of sequence.

C 题 : Tree VI

  Data structure questions. First of all, this is a complete k-ary tree. Given a complete tree, then a problem of preorder/middle order/postorder traversal order is given. There is a general solution step: first traverse through the hierarchy , Use the relationship between the sequence numbers of the nodes to construct the entire tree, and then traverse and assign values ​​to this tree in a prescribed manner .

const int maxn=1e5+10;
vector<int> v[maxn],aa;
queue<int> q;
int rec[maxn];
long long n,tot=0,kk;
long long ans=0;
class Solution {
    
    
public:
    /**
     *
     * @param k int整型 表示完全k叉树的叉数k
     * @param a int整型vector 表示这棵完全k叉树的Dfs遍历序列的结点编号
     * @return long长整型
     */
    void DFS(int root)
    {
    
    
        int size=v[root].size();rec[root]=aa[tot++];
        if(size==0) return;
        for(int i=0;i<size;++i)
        {
    
    
            DFS(root*kk+i+1);
            ans+=rec[root]^rec[root*kk+i+1];
        }
    }
    long long tree6(long long k, vector<int>& a) {
    
    
        n=a.size();aa=a;kk=k;
        while(!q.empty())  q.pop();
        q.push(0);
        while(!q.empty())
        {
    
    
            int cur=q.front();q.pop();
            for(long long i=1;i<=k;++i)
            {
    
    
                if((long long)cur*k+i>=n) break;//注意不能爆Long Long
                q.push(cur*k+i);
                v[cur].push_back(cur*k+i);
            }
        }
        DFS(0);
        return ans;
    }
};

D: Divide problem

  The counting problem is not important and not missing, and the principle of tolerance and exclusion is used here. Here we first find that there are only four factors for 2021 based on the example: 1,43,47,2021.
  The question then becomes the question of who produces the factor of 2021 for the product of the two: whether a is produced; or b is produced; or a is 43 and b is 47; or a is 47 and b is 43 .

class Solution {
    
    
public:
    /**
     * 寻找所有能整除 2021 的数对个数
     * @param a long长整型 
     * @param b long长整型 
     * @param c long长整型 
     * @param d long长整型 
     * @return long长整型
     */
    long long f(long long a,long long b)
    {
    
    
        long long ans=0;
        ans+=(a/2021)*b+a*(b/2021)-(a/2021)*(b/2021);
        ans+=(a/47-a/2021)*(b/43-b/2021)+(a/43-a/2021)*(b/47-b/2021);
        return ans;
    }
    long long findPairs(long long a, long long b, long long c, long long d) {
    
    
        return f(b,d)-f(a-1,d)-f(b,c-1)+f(a-1,c-1);
    }
};

Guess you like

Origin blog.csdn.net/CUMT_William_Liu/article/details/111190921