首页 > 其他分享 >codeforce959

codeforce959

时间:2024-07-19 23:19:14浏览次数:13  
标签:case codeforce959 int leq test output cases

Codeforces Round 959 sponsored by NEAR (Div. 1 + Div. 2)

A. Diverse Game

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

Petr, watching Sergey's stream, came up with a matrix \(a\), consisting of \(n\) rows and \(m\) columns (the number in the \(i\)-th row and \(j\)-th column is denoted as \(a_{i, j}\)), which contains all integers from \(1\) to \(n \cdot m\). But he didn't like the arrangement of the numbers, and now he wants to come up with a new matrix \(b\), consisting of \(n\) rows and \(m\) columns, which will also contain all integers from \(1\) to \(n \cdot m\), such that for any \(1 \leq i \leq n, 1 \leq j \leq m\) it holds that \(a_{i, j} \ne b_{i, j}\).

You are given the matrix \(a\), construct any matrix \(b\) that meets Petr's requirements, or determine that it is impossible.

Hurry up! Otherwise, he will donate all his money to the stream in search of an answer to his question.

Input

Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \leq t \leq 10^3\)) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains two integers \(n\) and \(m\) (\(1 \leq n, m \leq 10\)) — the number of rows and columns of matrix \(a\).

The next \(n\) lines contain \(m\) integers each, describing matrix \(a\). The \(i\)-th of these lines contains the elements of matrix \(a_{i, 1}, a_{i, 2}, \ldots, a_{i, m}\).

It is guaranteed that all numbers in matrix \(a\) are distinct and \(1 \leq a_{i, j} \leq n \cdot m\).

It is guaranteed that the sum of \(n \cdot m\) over all test cases does not exceed \(5 \cdot 10^4\)​.

Output

For each test case, output \(n \cdot m\) integers — any suitable matrix \(b\), or \(-1\)​ if such a matrix does not exist.

Example

input

5
1 1
1
2 1
2
1
1 5
2 4 5 3 1
2 4
1 2 3 4
5 6 7 8
3 3
4 2 1
9 8 3
6 7 5

output

-1
1 
2 
4 5 3 1 2 
6 7 8 5 
2 3 4 1 
8 3 9 
7 5 6 
2 1 4 

Note

In the first test case, there is only one element in the matrix, so matrix \(b\) is the only matrix and it does not fit.

In the second test case \(a_{1, 1} = 2 \neq 1 = b_{1, 1}\), \(a_{2, 1} = 1 \neq 2 = b_{2, 1}\).

题意

给你一个\(n*m\)的矩阵,矩阵中每一位不重复,要求你对其进行变换,使得变化后的每一位和原矩阵中不同,如果可以输出变化后的矩阵,不行输出\(-1\)

除矩阵为\(1*1\)时存在无解的情况,其他情况只需要将矩阵每行向上交换再向左交换即可

code

//
// Created by DH_xlx on 2024/7/19.
//
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

typedef pair<int,int> PII;
typedef array<int,3> a3;
typedef long long LL;

const int N = 2000010;
const int M = 200;
const int mod = 1e9+7;

void solve(){
	int n , m;
    cin >> n >> m;
    vector<vector<int>> a(n+1,vector<int>(m+1));
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++) cin >> a[i][j];
    }
    if(n==1&&m==1){
        cout << -1 << endl;
        return;
    }
    if(n!=1){
        for(int i=2;i<=n;i++){
            swap(a[i],a[i-1]);
        }
    }
    if(m!=1){
        for(int i=1;i<=n;i++)
        for(int j=2;j<=m;j++){
            swap(a[i][j],a[i][j-1]);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=m;j++){
            cout << a[i][j] << " ";
        }
        cout << endl;
    }

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
	int T = 1;
	cin >> T;
	while(T--){
		solve();
	}
	return 0;
}

B. Fun Game

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

Vova really loves the XOR operation (denoted as \(\oplus\)). Recently, when he was going to sleep, he came up with a fun game.

At the beginning of the game, Vova chooses two binary sequences \(s\) and \(t\) of length \(n\) and gives them to Vanya. A binary sequence is a sequence consisting only of the numbers \(0\) and \(1\). Vanya can choose integers \(l, r\) such that \(1 \leq l \leq r \leq n\), and for all \(l \leq i \leq r\) simultaneously replace \(s_i\) with \(s_i \oplus s_{i - l + 1}\), where \(s_i\) is the \(i\)-th element of the sequence \(s\).

In order for the game to be interesting, there must be a possibility to win. Vanya wins if, with an unlimited number of actions, he can obtain the sequence \(t\) from the sequence \(s\). Determine if the game will be interesting for the sequences \(s\) and \(t\)​.

Input

Each test consists of multiple test cases. The first line contains an integer \(q\) (\(1 \le q \le 10^{4}\)) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains a single integer \(n\) (\(1 \leq n \leq 2 \cdot 10^5\)) — the length of the sequences \(s\) and \(t\).

The second line of each test case contains a binary sequence \(s\) of length \(n\).

The third line of each test case contains a binary sequence \(t\) of length \(n\).

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).

Output

For each test case, output "Yes" if the game will be interesting, otherwise output "No".

You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

Example

input

6
1
0
1
7
0110100
0110100
9
100101010
101111110
4
0011
1011
4
0100
0001
8
10110111
01100000

output

NO
YES
YES
NO
YES
YES

Note

In the first test case, Vanya will not be able to change the sequence \(s\) with the only possible action of choosing \(l = r = 1\).

In the second test case, the sequences \(s\) and \(t\) are already equal.

In the third test case, Vanya can act as follows:

  1. Choose \(l = 3\) and \(r = 5\), then \(s\) will become \(\mathtt{101101010}\).
  2. Choose \(l = 5\) and \(r = 6\), then \(s\) will become \(\mathtt{101111010}\).
  3. Choose \(l = 7\) and \(r = 7\), then \(s\) will become \(\mathtt{101111110}\).

题意

给你两个\(01\)串,\(s\)和\(t\),问你经过无数次操作后使得\(s \equiv t\)

操作如下:

你可以选取一段\(l\)到\(r\)的\(s\),即\(s.substr(l,r-l+1)\),进行 \(s_i \oplus = s_{i - l + 1}\)​

那么如果\(s_i\) 是\(0\)要变化成\(1\)的话,只需要让前面出现一个\(1\)即可;\(si\)是\(1\)要变化成\(0\)的话,只需要把\(l\)和\(r\)都定为\(i\)即可(自己\(\oplus\)自己\(= 0\) )

那么我们就可以把\(s\)的最大部分先全变成\(1\),判断是否覆盖\(t\)中的\(1\)即可

code

//
// Created by DH_xlx on 2024/7/19.
//
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

typedef pair<int,int> PII;
typedef array<int,3> a3;
typedef long long LL;

const int N = 2000010;
const int M = 200;
const int mod = 1e9+7;

void solve(){
    int n;
    cin >> n;
    string s , t;
    cin >> s >> t;
    s = " "+s;
    t = " "+t;
    vector<int> pos;
    vector<int> cnt(n+1) , cnt1(n+1);
    for(int i=1;i<=n;i++){
        cnt[i] = cnt[i-1];
        cnt1[i] = cnt1[i-1];
        if(s[i]=='0') cnt[i]++;
        if(s[i]=='1') cnt1[i]++;
        if(s[i]=='0'&&t[i]=='1') pos.push_back(i);
    }
    bool fla = 1;
    for(int i=0;i<pos.size();i++){
        int v = pos[i];
        if(cnt1[v]==0) fla = 0;
    }
    if(fla) cout << "YES" << endl;
    else cout << "NO" << endl;

}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
	int T = 1;
	cin >> T;
	while(T--){
		solve();
	}
	return 0;
}

C. Hungry Games

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

Yaroslav is playing a computer game, and at one of the levels, he encountered \(n\) mushrooms arranged in a row. Each mushroom has its own level of toxicity; the \(i\)-th mushroom from the beginning has a toxicity level of \(a_i\). Yaroslav can choose two integers \(1 \le l \le r \le n\), and then his character will take turns from left to right to eat mushrooms from this subsegment one by one, i.e., the mushrooms with numbers \(l, l+1, l+2, \ldots, r\).

The character has a toxicity level \(g\), initially equal to \(0\). The computer game is defined by the number \(x\) — the maximum toxicity level at any given time. When eating a mushroom with toxicity level \(k\), the following happens:

  1. The toxicity level of the character is increased by \(k\).
  2. If \(g \leq x\), the process continues; otherwise, \(g\) becomes zero and the process continues.

Yaroslav became interested in how many ways there are to choose the values of \(l\) and \(r\) such that the final value of \(g\) is not zero. Help Yaroslav find this number!

Input

Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^{4}\)) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains two integers \(n\), \(x\) (\(1 \leq n \leq 2 \cdot 10^5, 1 \le x \le 10^9\)) — the number of mushrooms and the maximum toxicity level.

The second line of each test case contains \(n\) numbers \(a_1, a_2, \ldots, a_n\) (\(1 \leq a_i \leq 10^9\)).

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2 \cdot 10^5\).

Output

For each test case, output a single number — the number of subsegments such that the final value of \(g\) will not be zero.

Example

input

5
4 2
1 1 1 1
3 2
1 2 3
1 6
10
6 3
1 2 1 4 3 8
5 999999999
999999999 999999998 1000000000 1000000000 500000000

output

8
2
0
10
7

Note

In the first test case, the subsegments \((1, 1)\), \((1, 2)\), \((1, 4)\), \((2, 2)\), \((2, 3)\), \((3, 3)\), \((3, 4)\) and \((4, 4)\) are suitable.

In the second test case, non-zero \(g\) will remain only on the subsegments \((1, 1)\) and \((2, 2)\).

In the third test case, on the only possible subsegment, \(g\) will be zero.

题意

给你一个长度为\(n\)的数组\(a\),问你有多少个连续的\(\sum_l^r a_i\)进行下面过程

  1. 角色的毒性等级会增加 \(a_i\) 。
  2. 如果是 \(\sum a_i \leq x\) ,过程继续;否则, \(\sum a_i\) 变为零,过程继续。

\(\sum_l^r a_i\)不为零

连续的一段还求和,自然先前缀和处理一下

然后当某一段求和之后\(\leq x\),那么当前这一段可以出现的方案数即为\(r-l+1\),再考虑后面的,如果加完正好大于,那么\(r+1\)不可取,再考虑\(r+2\) ,可行的值,即为\(r+2\)后面可取的方案数

那么不妨从后向前遍历\(i\),再从\(i\)~\(n\) 中二分找到最远端的使得\(l\)~\(r+1\)为零的位置\(r\),那么当前\(i\)中可以凑得的方案数即为\(r-i+1+(r+2)\)位置的方案数

code

//
// Created by DH_xlx on 2024/7/19.
//
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

typedef pair<int,int> PII;
typedef array<int,3> a3;
typedef long long LL;

const int N = 2000010;
const int M = 200;
const int mod = 1e9+7;

void solve(){
    int n , m;
    cin >> n >> m;
    vector<int> a(n+1) , s(n+1);
    for(int i=1;i<=n;i++) cin >> a[i];
    for(int i=1;i<=n;i++) s[i] = s[i-1] + a[i];
    int ans  =0;
    vector<int> f(n+10,0);
    for(int i=n;i>0;i--){
        int l = i , r = n , p = i-1;
        while(l<=r){
            int mid = (l+r) >> 1;
            if(s[mid]-s[i-1]<=m){
                p = mid;
                l = mid+1;
            }
            else{
                r = mid-1;
            }
        }
        f[i] = (p-i+1) + f[p+2];
        ans += f[i];
    }
    cout << ans << endl;
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
	int T = 1;
	cin >> T;
	while(T--){
		solve();
	}
	return 0;
}

D. Funny Game

time limit per test: 2 seconds

memory limit per test: 256 megabytes

input: standard input

output: standard output

Vanya has a graph with \(n\) vertices (numbered from \(1\) to \(n\)) and an array \(a\) of \(n\) integers; initially, there are no edges in the graph. Vanya got bored, and to have fun, he decided to perform \(n - 1\) operations.

Operation number \(x\) (operations are numbered in order starting from \(1\)) is as follows:

  • Choose \(2\) different numbers \(1 \leq u,v \leq n\), such that \(|a_u - a_v|\) is divisible by \(x\).
  • Add an undirected edge between vertices \(u\) and \(v\) to the graph.

Help Vanya get a connected\(^{\text{∗}}\) graph using the \(n - 1\) operations, or determine that it is impossible.

\(^{\text{∗}}\)A graph is called connected if it is possible to reach any vertex from any other by moving along the edges.

Input

Each test consists of multiple test cases. The first line contains an integer \(t\) (\(1 \le t \le 10^{3}\)) — the number of test cases. Then follows the description of the test cases.

The first line of each test case contains the number \(n\) (\(1 \leq n \leq 2000\)) — the number of vertices in the graph.

The second line of each test case contains \(n\) numbers \(a_1, a_2, \cdots a_n\) (\(1 \leq a_i \leq 10^9\)).

It is guaranteed that the sum of \(n\) over all test cases does not exceed \(2000\).

Output

For each test case, if there is no solution, then output "No" (without quotes).

Otherwise, output "Yes" (without quotes), and then output \(n - 1\) lines, where in the \(i\)-th line, output the numbers \(u\) and \(v\) that need to be chosen for operation \(i\).

You can output each letter in any case (for example, the strings "yEs", "yes", "Yes", and "YES" will be recognized as a positive answer).

题意

给你n个点的独立点集,每个点有个对应的值\(a_i\)

现在需要添加\(n-1\)条边,使得成为一个连通图

添加边有以下限制

当添加到第\(i\)条边的时候,$\vert a_x - a_y \vert $ 要为\(i\)的倍数

如果可以连成连通图,输出"YES",并输出对应的连边方案

先解决连边的条件$\vert a_x - a_y \vert $ 要为\(i\) 的倍数,即$\vert a_x - a_y \vert $ % \(i = 0\)

进一步分解,即\(a_x\) % \(i\) \(\equiv\) \(a_y\) % \(i\)

即\(a_x\) 和\(a_y\) 关于\(i\) 同余

然后,处理连通图的话就用并查集维护一下当前连通的块即可

\(n\)的数据范围就2000,数据范围比较小,就写的一坨,\(O(n^3)\) 写法

code

//
// Created by DH_xlx on 2024/7/19.
//
#include <bits/stdc++.h>
#define int long long
#define fi first
#define se second

using namespace std;

typedef pair<int,int> PII;
typedef array<int,3> a3;
typedef long long LL;

const int N = 2010;
const int M = 200;
const int mod = 1e9+7;

vector<int> g[N][N];
int fa[N];
int find(int x){
    if(x==fa[x]) return x;
    return fa[x] = find(fa[x]);
}

void solve(){
    memset(fa,0,sizeof fa);
    int n;
    cin >> n;
    for(int i=0;i<=n;i++){
        for(int j=0;j<=n;j++){
            g[i][j].clear();
        }
    }

    vector<int> a(n+1);
    vector<PII> ans;
    for(int i=1;i<=n;i++) cin >> a[i] , fa[i]=i;
    for(int i=1;i<=n;i++){
        for(int j=1;j<n;j++){
            g[j][a[i]%j].push_back(i);
        }
    }

    for(int i=n-1;i>0;i--){
        int fla = 0;
        for(int j=0; j<i;j++){
            if(fla) break;
            if(g[i][j].size()>=2){
                for(int k=0;k<g[i][j].size();k++){
                    int x = g[i][j][k];
                    if(fla) break;
                    for(int k2=0;k2<g[i][j].size();k2++){
                        int y = g[i][j][k2];
                        if(x==y) continue;
                        if(find(x)==find(y)) continue;
                        fa[find(x)] = find(y);
                        ans.push_back({x,y});
                        fla = 1;
                        break;
                    }
                }
            }
        }
    }

    if(ans.size()<n-1){
        cout << "NO" << endl;
        return;
    }
    reverse(ans.begin(),ans.end());
    cout << "YES" << endl;
    for(auto [x,y]:ans){
        cout << x << " " << y << endl;
    }
}

signed main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
	int T = 1;
	cin >> T;
	while(T--){
		solve();
	}
	return 0;
}

标签:case,codeforce959,int,leq,test,output,cases
From: https://www.cnblogs.com/DHxlx/p/18312536

相关文章