Educational Codeforces Round 59 (Rated for Div. 2)

A. Digits Sequence Dividing

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a sequence ss consisting of nn digits from 11 to 99.

You have to divide it into at least two segments (segment — is a consecutive sequence of elements) (in other words, you have to place separators between some digits of the sequence) in such a way that each element belongs to exactly one segment and if the resulting division will be represented as an integer numbers sequence then each next element of this sequence will be strictly greater than the previous one.

More formally: if the resulting division of the sequence is t1,t2,…,tkt1,t2,…,tk, where kk is the number of element in a division, then for each ii from 11 to k−1k−1 the condition ti<ti+1ti<ti+1 (using numerical comparing, it means that the integer representations of strings are compared) should be satisfied.

For example, if s=654s=654 then you can divide it into parts [6,54][6,54] and it will be suitable division. But if you will divide it into parts [65,4][65,4] then it will be bad division because 65>465>4. If s=123s=123 then you can divide it into parts [1,23][1,23], [1,2,3][1,2,3] but not into parts [12,3][12,3].

Your task is to find any suitable division for each of the qq independent queries.

Input

The first line of the input contains one integer qq (1≤q≤3001≤q≤300) — the number of queries.

The first line of the ii-th query contains one integer number nini (2≤ni≤3002≤ni≤300) — the number of digits in the ii-th query.

The second line of the ii-th query contains one string sisi of length nini consisting only of digits from 11 to 99.

Output

If the sequence of digits in the ii-th query cannot be divided into at least two parts in a way described in the problem statement, print the single line "NO" for this query.

Otherwise in the first line of the answer to this query print "YES", on the second line print kiki — the number of parts in your division of the ii-th query sequence and in the third line print kiki strings ti,1,ti,2,…,ti,kiti,1,ti,2,…,ti,ki — your division. Parts should be printed in order of the initial string digits. It means that if you write the parts one after another without changing their order then you'll get the string sisi.

See examples for better understanding.

Example

input

Copy

4
6
654321
4
1337
2
33
4
2122

output

Copy

YES
3
6 54 321
YES
3
1 3 37
NO
YES
2
21 22
至少拆成兩部分,前面的部分比後面的部分小
#include<bits/stdc++.h>
using namespace std;
int n,t;
char s[305];
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>n>>s+1;
		int len=strlen(s+1);
		if(len==2)
		{
			if(s[1]-'0'>=s[2]-'0') 
			{
				printf("NO\n");
			}
			else
			{
				printf("YES\n2\n");
				printf("%c %c\n",s[1],s[2]);
			}
		}
		else
		{
			printf("YES\n2\n");
			printf("%c ",s[1]);
			for(int i=2;i<=len;i++) printf("%c",s[i]);
			puts("");
		}
	}
}

 

B. Digital root

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Today at the lesson of mathematics, Petya learns about the digital root.

The digital root of a non-negative integer is the single digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.

Let's denote the digital root of xx as S(x)S(x). Then S(5)=5S(5)=5, S(38)=S(3+8=11)=S(1+1=2)=2S(38)=S(3+8=11)=S(1+1=2)=2, S(10)=S(1+0=1)=1S(10)=S(1+0=1)=1.

As a homework Petya got nn tasks of the form: find kk-th positive number whose digital root is xx.

Petya has already solved all the problems, but he doesn't know if it's right. Your task is to solve all nn tasks from Petya's homework.

Input

The first line contains a single integer nn (1≤n≤1031≤n≤103) — the number of tasks in Petya's homework. The next nn lines contain two integers kiki (1≤ki≤10121≤ki≤1012) and xixi (1≤xi≤91≤xi≤9) — ii-th Petya's task in which you need to find a kiki-th positive number, the digital root of which is xixi.

Output

Output nn lines, ii-th line should contain a single integer — the answer to the ii-th problem.

Example

input

Copy

3
1 5
5 2
3 1

output

Copy

5
38
19

一個將其所有位加起來直到成爲一個數。

問第k個數能成爲 x(1~9)是哪個數?

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll k,n;
int t;
int main()
{
	cin>>t;
	while(t--)
	{
		cin>>k>>n;
		ll ans=(k-1)*9+n;
		printf("%lld\n",ans);
	}
}

 

C. Brutality

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are playing a new famous fighting game: Kortal Mombat XII. You have to perform a brutality on your opponent's character.

You are playing the game on the new generation console so your gamepad have 2626 buttons. Each button has a single lowercase Latin letter from 'a' to 'z' written on it. All the letters on buttons are pairwise distinct.

You are given a sequence of hits, the ii-th hit deals aiai units of damage to the opponent's character. To perform the ii-th hit you have to press the button sisi on your gamepad. Hits are numbered from 11 to nn.

You know that if you press some button more than kk times in a row then it'll break. You cherish your gamepad and don't want to break any of its buttons.

To perform a brutality you have to land some of the hits of the given sequence. You are allowed to skip any of them, however changing the initial order of the sequence is prohibited. The total damage dealt is the sum of aiai over all ii for the hits which weren't skipped.

Note that if you skip the hit then the counter of consecutive presses the button won't reset.

Your task is to skip some hits to deal the maximum possible total damage to the opponent's character and not break your gamepad buttons.

Input

The first line of the input contains two integers nn and kk (1≤k≤n≤2⋅1051≤k≤n≤2⋅105) — the number of hits and the maximum number of times you can push the same button in a row.

The second line of the input contains nn integers a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109), where aiai is the damage of the ii-th hit.

The third line of the input contains the string ss consisting of exactly nn lowercase Latin letters — the sequence of hits (each character is the letter on the button you need to press to perform the corresponding hit).

Output

Print one integer dmgdmg — the maximum possible damage to the opponent's character you can deal without breaking your gamepad buttons.

Examples

input

Copy

7 3
1 5 16 18 7 2 10
baaaaca

output

Copy

54

input

Copy

5 5
2 4 1 3 1000
aaaaa

output

Copy

1010

input

Copy

5 4
2 4 1 3 1000
aaaaa

output

Copy

1009

input

Copy

8 1
10 15 2 1 4 8 15 16
qqwweerr

output

Copy

41

input

Copy

6 3
14 18 9 19 2 15
cccccc

output

Copy

52

input

Copy

2 1
10 10
qq

output

Copy

10

Note

In the first example you can choose hits with numbers [1,3,4,5,6,7][1,3,4,5,6,7] with the total damage 1+16+18+7+2+10=541+16+18+7+2+10=54.

In the second example you can choose all hits so the total damage is 2+4+1+3+1000=10102+4+1+3+1000=1010.

In the third example you can choose all hits expect the third one so the total damage is 2+4+3+1000=10092+4+3+1000=1009.

In the fourth example you can choose hits with numbers [2,3,6,8][2,3,6,8]. Only this way you can reach the maximum total damage 15+2+8+16=4115+2+8+16=41.

In the fifth example you can choose only hits with numbers [2,4,6][2,4,6] with the total damage 18+19+15=5218+19+15=52.

In the sixth example you can change either first hit or the second hit (it does not matter) with the total damage 1010.

一個字母代表一個按鍵,一個按鍵不能連續按k次,問最大的按鍵產生的傷害之和

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
char s[N];
int a[N],n,k;
bool cmp(int x,int y)
{
	if(x>y) return 1;
	return 0;
}
int main()
{
	cin>>n>>k;
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	cin>>s+1;
	char pre=s[1];
	int sum=0;
	vector<int>ve;
	ll ans=0;
	for(int i=1;i<=n;i++)
	{
		if(s[i]==pre)
		{
			sum++;
			ve.push_back(a[i]);
		}
		else
		{
			if(sum<=k)
			{
				for(int j=0;j<sum;j++)
				{
					ans+=ve[j];
				// 		printf("ve[j]:%d\n",ve[j]);
				}
			}
			else
			{
				sort(ve.begin(),ve.begin()+sum,cmp);
				for(int j=0;j<k;j++)
				{
					ans+=ve[j];
				//	printf("ve[j]:%d\n",ve[j]);
				}
			
			}
			ve.clear();
			pre=s[i];
			sum=1;
			ve.push_back(a[i]);	
		}
	}
	if(sum<=k)
			{
				for(int j=0;j<sum;j++)
				{
					ans+=ve[j];
				//	printf("ve[j]:%d\n",ve[j]);
				}
			}
			else
			{
				sort(ve.begin(),ve.begin()+sum,cmp);
				for(int j=0;j<k;j++)
				{
					ans+=ve[j];
				//	printf("ve[j]:%d\n",ve[j]);
				}
			
			}
	printf("%lld\n",ans);
}

D. Compression

time limit per test

2.5 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a binary matrix AA of size n×nn×n. Let's denote an xx-compression of the given matrix as a matrix BB of size nx×nxnx×nx such that for every i∈[1,n],j∈[1,n]i∈[1,n],j∈[1,n] the condition A[i][j]=B[⌈ix⌉][⌈jx⌉]A[i][j]=B[⌈ix⌉][⌈jx⌉] is met.

Obviously, xx-compression is possible only if xx divides nn, but this condition is not enough. For example, the following matrix of size 2×22×2does not have any 22-compression:

0101

1010

For the given matrix AA, find maximum xx such that an xx-compression of this matrix is possible.

Note that the input is given in compressed form. But even though it is compressed, you'd better use fast input.

Input

The first line contains one number nn (4≤n≤52004≤n≤5200) — the number of rows and columns in the matrix AA. It is guaranteed that nn is divisible by 44.

Then the representation of matrix follows. Each of nn next lines contains n4n4 one-digit hexadecimal numbers (that is, these numbers can be represented either as digits from 00 to 99 or as uppercase Latin letters from AA to FF). Binary representation of each of these numbers denotes next 44 elements of the matrix in the corresponding row. For example, if the number BB is given, then the corresponding elements are 1011, and if the number is 55, then the corresponding elements are 0101.

Elements are not separated by whitespaces.

Output

Print one number: maximum xx such that an xx-compression of the given matrix is possible.

Examples

input

Copy

8
E7
E7
E7
00
00
E7
E7
E7

output

Copy

1

input

Copy

4
7
F
F
F

output

Copy

1

Note

The first example corresponds to the matrix:

1110011111100111

1110011111100111

1110011111100111

0000000000000000

0000000000000000

1110011111100111

1110011111100111

1110011111100111

It is easy to see that the answer on this example is 11.

這題不會,看別人補的

來源:https://www.luogu.org/problemnew/solution/CF1107D

雖然看不懂,但是覺得好厲害,求能分成k*k的矩陣,使得k儘量大,也就是矩陣儘量大。

#include<bits/stdc++.h>
using namespace std;
const int N=5e3+500;
int G[N][N],sum[N][N],n;
char s[20000];
bool valid(int id)
{
	int now=id*id;
	for(int i=1;i+id-1<=n;i+=id)
	for(int j=1;j+id-1<=n;j+=id)
	{
		int li=i+id-1;
		int lj=j+id-1;
		int ss=sum[li][lj]-sum[i-1][lj]-sum[li][j-1]+sum[i-1][j-1];
		if(ss!=0&&ss!=now) return 0;
	}
	return 1;
}
int main()
{
	cin>>n;
	for(int i=1;i<=n;i++)
	{
		scanf("%s",s+1);
		int num;
		for(int j=1;j<=n/4;j++)
		{
			if('A'<=s[j]&&s[j]<='F')
			num=s[j]-'A'+10;
			else num=s[j]-'0';
			for(int k=(j-1)*4+4;k>=(j-1)*4+1;k--)
			{
				G[i][k]=num%2;
				num/=2;
			}
		}
	}	
	
	
	for(int i=1;i<=n;i++)
	for(int j=1;j<=n;j++)
	sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1]+G[i][j];
	int ans=1;
	for(int i=2;i*i<=n;i++)
	{
		if(n%i==0)
		{
			if(valid(i)) ans=max(ans,i);
			if(valid(n/i)) ans=max(ans,n/i);
		}
	}
	if(valid(n)) ans=max(ans,n);
	printf("%d\n",ans);
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章