Codeforces Round 992 (Div. 2)(A-E)

比赛链接

A. Game of Division

You are given an array of integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an of length n n n and an integer k k k.

Two players are playing a game. The first player chooses an index 1 ≤ i ≤ n 1 \le i \le n 1in. Then the second player chooses a different index 1 ≤ j ≤ n , i ≠ j 1 \le j \le n, i \neq j 1jn,i=j. The first player wins if ∣ a i − a j ∣ |a_i - a_j| aiaj is not divisible by k k k. Otherwise, the second player wins.

We play as the first player. Determine whether it is possible to win, and if so, which index i i i should be chosen.

The absolute value of a number x x x is denoted by ∣ x ∣ |x| x and is equal to x x x if x ≥ 0 x \ge 0 x0, and − x -x x otherwise.

Input

Each test contains multiple test cases. The first line of input contains a single integer t t t ( 1 ≤ t ≤ 100 1 \le t \le 100 1t100) — the number of test cases. The description of the test cases follows.

The first line of each test case contains two integers n n n and k k k ( 1 ≤ n ≤ 100 1 \le n \le 100 1n100; 1 ≤ k ≤ 100 1 \le k \le 100 1k100) — the length of the array and the number k k k.

The second line of each test case contains n n n integers a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an ( 1 ≤ a i ≤ 100 1 \le a_i \le 100 1ai100) — the elements of the array a a a.

Output

For each test case, if it is impossible for the first player to win, print “NO” (without quotes).

Otherwise, print “YES” (without quotes) and on the next line the appropriate index 1 ≤ i ≤ n 1 \le i \le n 1in. If there are multiple solutions, print any of them.

You can output each letter in any case (lowercase or uppercase). For example, the strings “yEs”, “yes”, “Yes” and “YES” will be recognized as a positive answer.

核心代码

int n,k;
int w[N];

void solve(){
    cin>>n>>k;

    for(int i=1;i<=n;i++)cin>>w[i];

    int ans=0;

    for(int i=1;i<=n;i++){
        bool f=false;
        for(int j=1;j<=n;j++){
            if(i!=j){
                if(abs(w[i]-w[j])%k==0){
                    f=true;
                }
            }
        }
        if(!f){
            cout<<"YES"<<endl;
            cout<<i<<endl;
            return;
        }
    }
    cout<<"NO"<<endl;
}

B. Paint a Strip

You have an array of zeros a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an of length n n n.

You can perform two types of operations on it:

  1. Choose an index i i i such that 1 ≤ i ≤ n 1 \le i \le n 1in and a i = 0 a_i = 0 ai=0, and assign 1 1 1 to a i a_i ai;
  2. Choose a pair of indices l l l and r r r such that 1 ≤ l ≤ r ≤ n 1 \le l \le r \le n 1lrn, a l = 1 a_l = 1 al=1, a r = 1 a_r = 1 ar=1, a l + … + a r ≥ ⌈ r − l + 1 2 ⌉ a_l + \ldots + a_r \ge \lceil\frac{r - l + 1}{2}\rceil al++ar2rl+1, and assign 1 1 1 to a i a_i ai for all l ≤ i ≤ r l \le i \le r lir.

What is the minimum number of operations of the first type needed to make all elements of the array equal to one?

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104). The description of the test cases follows.

The only line of each test case contains one integer n n n ( 1 ≤ n ≤ 1 0 5 1 \le n \le 10^5 1n105) — the length of the array.

Note that there is no limit on the sum of n n n over all test cases.

Output

For each test case, print one integer — the minimum number of needed operations of first type.

核心代码

int n;

void solve(){
    cin>>n;

    int m=0;

    if(n==1){
        cout<<"1"<<endl;
        return;
    }

    int ans=1;

    for(int i=1;i<n;i=(i+1)<<1){
        ans++;
    }

    cout<<ans<<endl;

}

C. Ordered Permutations

Consider a permutation ∗ ^{\text{∗}} p 1 , p 2 , … , p n p_1, p_2, \ldots, p_n p1,p2,,pn of integers from 1 1 1 to n n n. We can introduce the following sum for it † ^{\text{†}} :

S ( p ) = ∑ 1 ≤ l ≤ r ≤ n min ⁡ ( p l , p l + 1 , … , p r ) S(p) = \sum_{1 \le l \le r \le n} \min(p_l, p_{l + 1}, \ldots, p_r) S(p)=1lrnmin(pl,pl+1,,pr)

Let us consider all permutations of length n n n with the maximum possible value of S ( p ) S(p) S(p). Output the k k k-th of them in lexicographical ‡ ^{\text{‡}} order, or report that there are less than k k k of them.

∗ ^{\text{∗}} A permutation of length n n n is an array consisting of n n n distinct integers from 1 1 1 to n n n in arbitrary order. For example, [ 2 , 3 , 1 , 5 , 4 ] [2,3,1,5,4] [2,3,1,5,4] is a permutation, but [ 1 , 2 , 2 ] [1,2,2] [1,2,2] is not a permutation ( 2 2 2 appears twice in the array), and [ 1 , 3 , 4 ] [1,3,4] [1,3,4] is also not a permutation ( n = 3 n=3 n=3 but there is 4 4 4 in the array).

† ^{\text{†}} For example:

  • For the permutation [ 1 , 2 , 3 ] [1, 2, 3] [1,2,3] the value of S ( p ) S(p) S(p) is equal to min ⁡ ( 1 ) + min ⁡ ( 1 , 2 ) + min ⁡ ( 1 , 2 , 3 ) + min ⁡ ( 2 ) + min ⁡ ( 2 , 3 ) + min ⁡ ( 3 ) = \min(1) + \min(1, 2) + \min(1, 2, 3) + \min(2) + \min(2, 3) + \min(3) = min(1)+min(1,2)+min(1,2,3)+min(2)+min(2,3)+min(3)= 1 + 1 + 1 + 2 + 2 + 3 = 10 1 + 1 + 1 + 2 + 2 + 3 = 10 1+1+1+2+2+3=10
  • For the permutation [ 2 , 4 , 1 , 3 ] [2, 4, 1, 3] [2,4,1,3] the value of S ( p ) S(p) S(p) is equal to min ⁡ ( 2 ) + min ⁡ ( 2 , 4 ) + min ⁡ ( 2 , 4 , 1 ) + min ⁡ ( 2 , 4 , 1 , 3 )   + \min(2) + \min(2, 4) + \min(2, 4, 1) + \min(2, 4, 1, 3) \ + min(2)+min(2,4)+min(2,4,1)+min(2,4,1,3) + $ \min(4) + \min(4, 1) + \min(4, 1, 3) \ +$ min ⁡ ( 1 ) + min ⁡ ( 1 , 3 )   + \min(1) + \min(1, 3) \ + min(1)+min(1,3) + min ⁡ ( 3 ) = \min(3) = min(3)= 2 + 2 + 1 + 1 + 4 + 1 + 1 + 1 + 1 + 3 = 17 2 + 2 + 1 + 1 + 4 + 1 + 1 + 1 + 1 + 3 = 17 2+2+1+1+4+1+1+1+1+3=17.

‡ ^{\text{‡}} An array a a a is lexicographically smaller than an array b b b if and only if one of the following holds:

  • a a a is a prefix of b b b, but a ≠ b a \ne b a=b; or
  • in the first position where a a a and b b b differ, the array a a a has a smaller element than the corresponding element in b b b.
  • Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104). The description of the test cases follows.

The only line of each test case contains two integers n n n and k k k ( 1 ≤ n ≤ 2 ⋅ 1 0 5 1 \le n \le 2 \cdot 10^5 1n2105; 1 ≤ k ≤ 1 0 12 1 \le k \le 10^{12} 1k1012) — the length of the permutation and the index number of the desired permutation.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10 ^ 5 2105.
Output

For each test case, if there are less than k k k suitable permutations, print − 1 -1 1.

Otherwise, print the k k k-th suitable permutation.

核心代码


int n,k,m;

void solve(){
    cin>>n>>k;

    vector<bool>w(n+1,0);

    if(n<=60&&(1ll<<(n-1))<k) {
        cout<<-1<<endl;
        return;
    }
    k--;
    for(int i=n-1;i>=1;i--){
        if(k&1)w[i]=true;
        k/=2;
    }

    vector<int>ans(n+1,0);

    for(int i=1,l=0,r=n+1;i<=n;i++){
        if(w[i])ans[--r]=i;
        else ans[++l]=i;
    }

    for(int i=1;i<=n;i++)cout<<ans[i]<<" \n"[i==n];
}

D. Non Prime Tree

You are given a tree with n n n vertices.

You need to construct an array a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an of length n n n, consisting of unique integers from 1 1 1 to 2 ⋅ n 2 \cdot n 2n, and such that for each edge u i ↔ v i u_i \leftrightarrow v_i uivi of the tree, the value ∣ a u i − a v i ∣ |a_{u_i} - a_{v_i}| auiavi is not a prime number.

Find any array that satisfies these conditions, or report that there is no such array.

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 4 1 \le t \le 10^4 1t104). The description of the test cases follows.

The first line of each test case contains a single integer n n n ( 2 ≤ n ≤ 2 ⋅ 1 0 5 2 \le n \le 2 \cdot 10^5 2n2105) — the number of vertices in the tree.

The next n − 1 n - 1 n1 lines contain the edges of the tree, one edge per line. The i i i-th line contains two integers u i u_i ui and v i v_i vi ( 1 ≤ u i , v i ≤ n 1 \le u_i, v_i \le n 1ui,vin; u i ≠ v i u_i \neq v_i ui=vi), denoting the edge between the nodes u i u_i ui and v i v_i vi.

It’s guaranteed that the given edges form a tree.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 5 2 \cdot 10 ^ 5 2105.

Output

For each test case, if an array that satisfies the conditions exists, print its elements a 1 , a 2 , … , a n a_1, a_2, \ldots, a_n a1,a2,,an. Otherwise, print − 1 -1 1.

核心代码(解法1)

int n;
struct E{
    int x,y;
    bool operator<(const E& t)const{
        return x<t.x;
    }
};

void solve(){
    cin>>n;

    vector<vector<int>>G(n+n+1),C(n*2+1);

    for(int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }

    vector<int>siz(n+n+1,0),ans(n+n+1,0);
    int root=1,maxv=n+1;

    function<void(int,int)>dfs1=[&](int u,int fa){
        siz[u]=1;
        int mx=0;
        for(auto& j:G[u]){
            if(j==fa)continue;
            dfs1(j,u);
            siz[u]+=siz[j];
            mx=max(mx,siz[j]);
        }
        mx=max(mx,n-siz[u]);
        if(mx<maxv)maxv=mx,root=u;
    };

    function<void(int,int)>dfs2=[&](int u,int fa){
        siz[u]=1;
        for(auto& j:G[u]){
            if(j==fa)continue;
            dfs1(j,u);
            siz[u]+=siz[j];
        }
    };

    dfs1(1,0);
    dfs2(root,0);
    priority_queue<E>q;

    int t1=0,t=6,p=0;

    for(auto& j:G[root]){
        if(siz[j]==1&&!t1)t1=1,ans[j]=1;
        else q.push({siz[j],j});
    }

    while(q.size()){
        int u=q.top().y;
        q.pop();

        siz[u]--;
        C[u].push_back(t);
        t=t+2;

        if(siz[p])q.push({siz[p],p});
        p=u;
    }

    int a,b;

    function<void(int,int)>dfs3=[&](int u,int fa){
        ans[u]=C[a][b++];
        int t1=0;
        for(auto& j:G[u]){
            if(j==fa)continue;
            if(siz[j]==1&&!t1)t1=1,ans[j]=ans[u]-1;
            else dfs3(j,u);
        }
    };

    for(auto& j:G[root]){
        if(C[j].size()){
            a=j,b=0;
            dfs3(j,root);
        }
    }

    ans[root]=2;
    for(int i=1;i<=n;i++)cout<<ans[i]<<" \n"[i==n];

}

核心代码(解法二)

int n;

void solve(){
    cin>>n;

    vector<vector<int>>G(n+1);

    for(int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }

    vector<int>ans(n+1,0);

    int t=1;

    function<void(int,int)>dfs=[&](int u,int fa){
        ans[u]=t;
        bool f=true;
        for(auto& j:G[u]){
            if(j==fa)continue;
            if(f){
                t++;
                f=false;
                dfs(j,u);
                t++;
            }else{
                t+=2;
                dfs(j,u);
            }
        }
    };

    dfs(1,0);

    for(int i=1;i<=n;i++){
        cout<<ans[i]<<" \n"[i==n];
    }

}

核心代码(解法三)

这道题好像还有线性筛+二分的方法,建议看:value0的提交记录

E. Control of Randomness

You are given a tree with n n n vertices.

Let’s place a robot in some vertex v ≠ 1 v \ne 1 v=1, and suppose we initially have p p p coins. Consider the following process, where in the i i i-th step (starting from i = 1 i = 1 i=1):

  • If i i i is odd, the robot moves to an adjacent vertex in the direction of vertex 1 1 1;
  • Else, i i i is even. You can either pay one coin (if there are some left) and then the robot moves to an adjacent vertex in the direction of vertex 1 1 1, or not pay, and then the robot moves to an adjacent vertex chosen uniformly at random.

The process stops as soon as the robot reaches vertex 1 1 1. Let f ( v , p ) f(v, p) f(v,p) be the minimum possible expected number of steps in the process above if we spend our coins optimally.

Answer q q q queries, in the i i i-th of which you have to find the value of f ( v i , p i ) f(v_i, p_i) f(vi,pi), modulo ∗ ^{\text{∗}} 998   244   353 998\,244\,353 998244353.

∗ ^{\text{∗}} Formally, let M = 998   244   353 M = 998\,244\,353 M=998244353. It can be shown that the answer can be expressed as an irreducible fraction p q \frac{p}{q} qp, where p p p and q q q are integers and q ≢ 0 ( m o d M ) q \not \equiv 0 \pmod{M} q0(modM). Output the integer equal to p ⋅ q − 1   m o d   M p \cdot q^{-1} \bmod M pq1modM. In other words, output such an integer x x x that KaTeX parse error: Expected 'EOF', got '&' at position 9: 0 \le x &̲lt; M and x ⋅ q ≡ p ( m o d M ) x \cdot q \equiv p \pmod{M} xqp(modM).

Input

Each test contains multiple test cases. The first line contains the number of test cases t t t ( 1 ≤ t ≤ 1 0 3 1 \le t \le 10^3 1t103). The description of the test cases follows.

The first line of each test case contains two integers n n n and q q q ( 2 ≤ n ≤ 2 ⋅ 1 0 3 2 \le n \le 2 \cdot 10^3 2n2103; 1 ≤ q ≤ 2 ⋅ 1 0 3 1 \le q \le 2 \cdot 10^3 1q2103) — the number of vertices in the tree and the number of queries.

The next n − 1 n - 1 n1 lines contain the edges of the tree, one edge per line. The i i i-th line contains two integers u i u_i ui and v i v_i vi ( 1 ≤ u i , v i ≤ n 1 \le u_i, v_i \le n 1ui,vin; u i ≠ v i u_i \neq v_i ui=vi), denoting the edge between the nodes u i u_i ui and v i v_i vi.

The next q q q lines contain two integers v i v_i vi and p i p_i pi ( 2 ≤ v i ≤ n 2 \le v_i \le n 2vin; 0 ≤ p i ≤ n 0 \le p_i \le n 0pin).

It’s guaranteed that the given edges form a tree.

It is guaranteed that the sum of n n n over all test cases does not exceed 2 ⋅ 1 0 3 2 \cdot 10 ^ 3 2103.

It is guaranteed that the sum of q q q over all test cases does not exceed 2 ⋅ 1 0 3 2 \cdot 10 ^ 3 2103.
Output

For each test case, print q q q integers: the values of f ( v i , p i ) f(v_i, p_i) f(vi,pi) modulo 998   244   353 998\,244\,353 998244353.

Formally, let M = 998   244   353 M = 998\,244\,353 M=998244353. It can be shown that the answer can be expressed as an irreducible fraction p q \frac{p}{q} qp, where p p p and q q q are integers and q ≢ 0 ( m o d M ) q \not \equiv 0 \pmod{M} q0(modM). Output the integer equal to p ⋅ q − 1   m o d   M p \cdot q^{-1} \bmod M pq1modM. In other words, output such an integer x x x that KaTeX parse error: Expected 'EOF', got '&' at position 9: 0 \le x &̲lt; M and x ⋅ q ≡ p ( m o d M ) x \cdot q \equiv p \pmod{M} xqp(modM).

核心代码

int n,m,q;
int f[N][N];

void solve(){
    cin>>n>>q;

    for(int i=0;i<=n;i++)f[0][i]=-1e18;

    vector<vector<int>>G(n+n+1);

    for(int i=1;i<n;i++){
        int a,b;
        cin>>a>>b;
        G[a].emplace_back(b);
        G[b].emplace_back(a);
    }

    vector<int>fa(n+1,0);

    auto dfs=[&](auto& dfs,int u,int fp)->void{
        fa[u]=fp;
        if(u!=1){
            for(int p=0;p<=n;p++){
                f[u][p]=f[fa[fa[u]]][p]+(int)G[u].size()*2;
                if(p>0){
                    f[u][p]=min(f[u][p],f[fa[fa[u]]][p-1]+2);
                }
            }
        }
        for(auto& j:G[u]){
            if(j==fp)continue;
            dfs(dfs,j,u);
        }
    };

    dfs(dfs,1,0);

    while(q--){
        int a,b;
        cin>>a>>b;
        cout<<f[fa[a]][b]+1<<endl;
    }
}

后记

思路暂时就先不放了,发这篇文章只是想记录一下这场比赛,只写了前面A~C,D、E调了很久 掉大分,直到赛后才调好,太菜了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

green qwq

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值