Codeforces Round #777 (Div. 2)

比赛链接

Codeforces Round #777 (Div. 2)

B. Madoka and the Elegant Gift

Madoka's father just reached 1 million subscribers on Mathub! So the website decided to send him a personalized award - The Mathhub's Bit Button!

The Bit Button is a rectangular table with \(n\) rows and \(m\) columns with 0 or 1 in each cell. After exploring the table Madoka found out that:

  • A subrectangle \(A\) is contained in a subrectangle \(B\) if there's no cell contained in \(A\) but not contained in \(B\).
  • Two subrectangles intersect if there is a cell contained in both of them.
  • A subrectangle is called black if there's no cell with value 0 inside it.
  • A subrectangle is called nice if it's black and it's not contained in another black subrectangle.
  • The table is called elegant if there are no two nice intersecting subrectangles.
    For example, in the first illustration the red subrectangle is nice, but in the second one it's not, because it's contained in the purple subrectangle.
    image
    Help Madoka to determine whether the table is elegant.

Input

Each test contains multiple test cases. The first line contains a single integer \(t(1 \leq t \leq 200)-\) the number of test cases. Description of the test cases follows.
The first line of each test case contains two positive integers \(n, m(1 \leq n, m \leq 100)\)
The next \(n\) lines contain strings of length \(m\) consisting of zeros and ones - the description of the table.
It is guaranteed that the sum of the values of \(n\) and the sum of the values of \(m\) for all test cases do not exceed 777 .

Output

For each test case print "YES" if its table is elegant or print "NO" otherwise.
You may print each letter in any case (for example, "YES", "Yes", "Yes", "yEs" will all be recognized as positive answer).

Example

input

5
3 3
100
011
011
3 3
110
111
110
1 5
01111
4 5
11111
01010
01000
01000
3 2
11
00
11

output

YES
NO
YES
NO
YES

Note

In the second test case the table is not elegant, because the red and the purple subrectangles are nice and intersect.
image

In the fourth test case the table is not elegant, because the red and the purple subrectangles are nice and intersect.

image

解题思路

规律

可发现交叉处的点满足四个角中仅有一个角为黑色,枚举每一个交叉点判断是否有这样的点

  • 时间复杂度:\(O(nm)\)

代码

// Problem: B. Madoka and the Elegant Gift
// Contest: Codeforces - Codeforces Round #777 (Div. 2)
// URL: https://codeforces.com/contest/1647/problem/B
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)
 
// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}
 
const int N=105;
int n,m,t;
char s[N][N];
bool f;
int dx[]={-1,0,1,0},dy[]={0,1,0,-1};
bool ck(int x,int y,int _x,int _y)
{
	if(s[x][y]=='0'||s[_x][_y]=='0')return false;
	return true;
}
void dfs(int x,int y)
{
	// if(!f)return ;
	s[x][y]='2';
	int _x[4],_y[4];
	for(int i=0;i<4;i++)
		_x[i]=x+dx[i],_y[i]=y+dy[i];
	for(int i=0;i<4;i++)
	{
		if(_x[i]<1||_x[i]>n||_y[i]<1||_y[i]>m||_x[(i+1)%4]<1||_x[(i+1)%4]>n||_y[(i+1)%4]<1||_y[(i+1)%4]>m)continue;
		if(ck(_x[i],_y[i],_x[(i+1)%4],_y[(i+1)%4])&&s[x+dx[i]+dx[(i+1)%4]][y+dy[i]+dy[(i+1)%4]]=='0')
			f=false;
			
	}
	if(!f)
	return ;
	for(int i=0;i<4;i++)
	{
		if(_x[i]>n||_x[i]<1||_y[i]>m||_y[i]<1)continue;
		if(s[_x[i]][_y[i]]=='1')dfs(_x[i],_y[i]);
	}
		
}
int main()
{
    for(cin>>t;t;t--)
    {
    	cin>>n>>m;
    	for(int i=1;i<=n;i++)cin>>(s[i]+1);
    	f=true;
    	for(int i=1;i<=n;i++)
    		for(int j=1;j<=m;j++)
    			if(s[i][j]=='1')
    				dfs(i,j);
    	puts(f?"YES":"NO");
    }
    return 0;
}

C. Madoka and Childish Pranks

Madoka as a child was an extremely capricious girl, and one of her favorite pranks was drawing on her wall. According to Madoka's memories, the wall was a table of \(n\) rows and \(m\) columns, consisting only of zeroes and ones. The coordinate of the cell in the \(i\)-th row and the \(j\)-th column \((1 \leq i \leq n, 1 \leq j \leq m)\) is \((i, j)\).

One day she saw a picture "Mahou Shoujo Madoka Magica" and decided to draw it on her wall. Initially, the Madoka's table is a table of size \(n \times m\) filled with zeroes. Then she applies the following operation any number of times:

Madoka selects any rectangular subtable of the table and paints it in a chess coloring (the upper left corner of the subtable always has the color 0 ). Note that some cells may be colored several times. In this case, the final color of the cell is equal to the color obtained during the last repainting.
image
White color means 0 , black means 1 . So, for example, the table in the first picture is painted in a chess coloring, and the others are not.
For better understanding of the statement, we recommend you to read the explanation of the first test.
Help Madoka and find some sequence of no more than \(n \cdot m\) operations that allows you to obtain the picture she wants, or determine that this is impossible.

Input

Each test contains multiple test cases. The first line contains a single integer \(t(1 \leq t \leq 10)-\) the number of test cases. Description of the test cases follows.

The first line of each test case contains two integers \(n\) and \(m(1 \leq n, m \leq 100)-\) the size of the table. Each of the following \(n\) lines contains a string of length \(m\) consisting only of 1 and 0 - description of the picture that Madoka wants to obtain.

Output

If it is impossible to obtain the given picture, print \(-1\).
Otherwise, print in the first line a single integer \(q(0 \leq q \leq n \cdot m)\) - the number of operations you need to obtain the picture. Note that you do not need to minimize the number of operations.

Then for each operation (in the order of execution) print a single line containing four numbers — the coordinates of the upper-left corner and the lower-right corner of the rectangle.

Example

input

4
4 5
01000
10100
01010
00110
2 3
001
010
3 3
110
101
000
1 1
0

output

4
1 1 3 3
3 3 4 4
4 3 4 4
4 2 4 3
1
1 2 2 3
-1
0

Note

The description of the first test case is below.
image
In the third test case, it is impossible to paint the desired picture.

In the fourth test case, the initial table is already the desired picture.

解题思路

思维

每次只用 \(1\times 2\)\(2\times 1\) 的矩形操作即可,即对于每一列,从下往上如果 \((i,j)\) 要求为黑色,则填充 \((i-1,j),(i,j)\),列都填充完后,这时只剩第一行,同理填充,另外如果第一行第一列要求黑色,则显然无法满足条件

  • 时间复杂度:\(O(nm)\)

代码

// Problem: C. Madoka and Childish Pranks
// Contest: Codeforces - Codeforces Round #777 (Div. 2)
// URL: https://codeforces.com/contest/1647/problem/C
// Memory Limit: 256 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=105;
int t,n,m;
char s[N][N];
pair<PII,PII> res[10005];
int main()
{
    for(cin>>t;t;t--)
    {
    	cin>>n>>m;
    	for(int i=1;i<=n;i++)cin>>(s[i]+1);
    	int cnt=0;
    	for(int j=1;j<=m;j++)
    		for(int i=n;i>=2;i--)
    			if(s[i][j]=='1')
    				res[++cnt]={{i-1,j},{i,j}};    	
    	for(int j=m;j>=2;j--)
    		if(s[1][j]=='1')res[++cnt]={{1,j-1},{1,j}};
    	if(s[1][1]=='1')puts("-1");
    	else
    	{
    		cout<<cnt<<'\n';
    		for(int i=1;i<=cnt;i++)
    			cout<<res[i].fi.fi<<' '<<res[i].fi.se<<' '<<res[i].se.fi<<' '<<res[i].se.se<<'\n';
    	}
    }
    return 0;
}

D. Madoka and the Best School in Russia

Madoka is going to enroll in "TSUNS PTU". But she stumbled upon a difficult task during the entrance computer science exam:

  • A number is called good if it is a multiple of \(d\).
  • A number is called beatiful if it is good and it cannot be represented as a product of two good numbers.

Notice that a beautiful number must be good.
Given a good number \(x\), determine whether it can be represented in at least two different ways as a product of several (possibly, one) beautiful numbers. Two ways are different if the sets of numbers used are different.
Solve this problem for Madoka and help her to enroll in the best school in Russia!

Input

The first line contains a single integer \(t(1 \leq t \leq 100)\) - number of test cases. Below comes their description.
Each test case consists of two integers \(x\) and \(d\), separated by a space \(\left(2 \leq x, d \leq 10^{9}\right)\). It is guaranteed that \(x\) is a multiple of \(d\).

Output

For each set of input data, output "NO" if the number cannot be represented in at least two ways. Otherwise, output "YES".
You can output each letter in any case (for example, "YES", "Yes", "Yes", "YEs", "YEs" will be recognized as a positive answer).

Example

input

8
6 2
12 2
36 2
8 2
1000 10
2376 6
128 4
16384 4

output

NO
NO
YES
NO
YES
YES
NO
YES

Note

In the first example, 6 can be represented as \(6,1 \cdot 6,2 \cdot 3\). But 3 and 1 are not a good numbers because they are not divisible by 2 , so there is only one way.

In the second example, 12 can be represented as \(6 \cdot 2,12,3 \cdot 4\), or \(3 \cdot 2 \cdot 2\). The first option is suitable. The second is- no, because 12 is not beautiful number \((12=6 \cdot 2)\). The third and fourth are also not suitable, because 3 is not good number.
In the third example, 36 can be represented as \(18 \cdot 2\) and \(6 \cdot 6\). Therefore it can be decomposed in at least two ways.

解题思路

分解质因数

分类讨论:
首先要求 \(x\)\(d^2\) 的倍数,另外 \(x\) 能分出 \(d\) 则尽量分出,假设还剩 \(t\),如果 \(t\) 为 非质数,则其可以分出多个质数形成至少两种方案;否则如果 \(t\) 为 质数,特判 \(t=1\) 的情况,如果 \(d\) 为质数时,则其方案总是一种即本身;如果只能分出 \(2\)\(d\),则无论 \(t\) 怎么分配都只有一种方案;如果可以分出多于 \(2\)\(d\),拿出一个 \(d\) 出来,如果 \(d\) 质因数分解的质数个数大于 \(1\) 的话,可以把每个质因数分开分配,否则假设 \(d\) 只有一个质数且数量为 \(cnt\),已分配的 \(d\) 每个至多分配到 \(cnt-1\) 个质数,如果总的数量大于这个数值则总会有一个已分配的 \(d\) 不满足条件,\(\color{red}{这里为什么要尽量分配 d 呢?}\),为了满足要求,应使剩余的数越小越好,分配出的 \(d\) 越多越好,这样分配出去的数越小更容易满足要求

  • 时间复杂度:\(O(xlogx)\)

代码

// Problem: D. Madoka and the Best School in Russia
// Contest: Codeforces - Codeforces Round #777 (Div. 2)
// URL: https://codeforces.com/contest/1647/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
//#define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

const int N=1e5+5;
int cnt,t;
PII a[N],b[N];
LL x,d;
unordered_map<int,int> mp1,mp2;
void div(LL x,unordered_map<int,int> &mp)
{
	for(int i=2;i*i<=x;i++)
		if(x%i==0)
		{
			while(x%i==0)mp[i]++,x/=i;
		}
	if(x>1)mp[x]++;
}
bool is_prime(LL x)
{
	for(int i=2;i*i<=x;i++)
		if(x%i==0)return false;
	return true;
}
int main()
{
    for(cin>>t;t;t--)
    {
    	mp1.clear();
    	mp2.clear();
    	cin>>x>>d;
    	bool f=true;
    	if(x%(d*d))f=false;
    	else
    	{
    		LL t=x;
    		int cnt=0;
    		while(t%d==0)
    		{
    			cnt++;
    			t/=d;
    		}
    		
    		if(is_prime(t))
    		{
    			if(cnt==2)f=false;
    			if(t==1)
    			{
    				if(is_prime(d))f=false;
    			}
    			else
    			{
    				cnt--;
    				div(d,mp1);
    				t*=d;
    				div(t,mp2);
    				int s=0;
    				if(mp1.size()==1)
    				{
    					for(auto it:mp1)
			    		{
			    			int t1=it.fi,t2=it.se;
			    			if(mp2[t1]-(t2-1)*(cnt-1)>=t2)f=false;
			    		}
    				}
    			}
    		}
    	}
    	puts(f?"YES":"NO");
    }
    return 0;
}
posted @ 2022-03-12 11:09  zyy2001  阅读(174)  评论(0编辑  收藏  举报