Codeforces Round #528 (Div. 2, based on Technocup 2019 Elimination Round 4)

本文深入探讨了三种算法挑战:Right-LeftCipher字符串加密解密、DivTimesMod数学方程求解以及MinimumDiameterTree最小直径树问题。通过具体实例解析,提供了清晰的算法思路与代码实现。

A. Right-Left Cipher

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Polycarp loves ciphers. He has invented his own cipher called Right-Left.

Right-Left cipher is used for strings. To encrypt the string s=s1s2…sns=s1s2…sn Polycarp uses the following algorithm:

  • he writes down s1s1,
  • he appends the current word with s2s2 (i.e. writes down s2s2 to the right of the current result),
  • he prepends the current word with s3s3 (i.e. writes down s3s3 to the left of the current result),
  • he appends the current word with s4s4 (i.e. writes down s4s4 to the right of the current result),
  • he prepends the current word with s5s5 (i.e. writes down s5s5 to the left of the current result),
  • and so on for each position until the end of ss.

For example, if ss="techno" the process is: "t" →→ "te" →→ "cte" →→ "cteh" →→ "ncteh" →→ "ncteho". So the encrypted ss="techno" is "ncteho".

Given string tt — the result of encryption of some string ss. Your task is to decrypt it, i.e. find the string ss.

Input

The only line of the input contains tt — the result of encryption of some string ss. It contains only lowercase Latin letters. The length of tt is between 11 and 5050, inclusive.

Output

Print such string ss that after encryption it equals tt.

Examples

input

ncteho

output

techno

input

erfdcoeocs

output

codeforces

input

z

output

z
#include<bits/stdc++.h>
using namespace std;
int main(){
	char s[55],s1[55];
	scanf("%s",s);
	int l=strlen(s);
	int t=0,i=(l+1)/2-1,j=(l+1)/2;
	if(l==1){
		printf("%s",s);
		return 0;
	}
	while(i>=0&&j<l){
		s1[t++]=s[i--];
		s1[t++]=s[j++];
	}
	if(i>=0){
		while(i>=0)
		s1[t++]=s[i--];
	}
	if(j<l){
		while(j<l)
		s1[t++]=s[j++];
	}
	for(int i=0;i<t;i++)	
	printf("%c",s1[i]);
}

B. Div Times Mod

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

Vasya likes to solve equations. Today he wants to solve (x div k)⋅(xmodk)=n(x div k)⋅(xmodk)=n, where divdiv and modmod stand for integer division and modulo operations (refer to the Notes below for exact definition). In this equation, kk and nn are positive integer parameters, and xx is a positive integer unknown. If there are several solutions, Vasya wants to find the smallest possible xx. Can you help him?

Input

The first line contains two integers nn and kk (1≤n≤1061≤n≤106, 2≤k≤10002≤k≤1000).

Output

Print a single integer xx — the smallest positive integer solution to (x div k)⋅(xmodk)=n(x div k)⋅(xmodk)=n. It is guaranteed that this equation has at least one positive integer solution.

Examples

input

6 3

output

11

input

1 2

output

3

input

4 6

output

10

Note

The result of integer division a div ba div b is equal to the largest integer cc such that b⋅c≤ab⋅c≤a. aa modulo bb (shortened amodbamodb) is the only integer cc such that 0≤c<b0≤c<b, and a−ca−c is divisible by bb.

In the first sample, 11 div 3=311 div 3=3 and 11mod3=211mod3=2. Since 3⋅2=63⋅2=6, then x=11x=11 is a solution to (x div 3)⋅(xmod3)=6(x div 3)⋅(xmod3)=6. One can see that 1919 is the only other positive integer solution, hence 1111 is the smallest one.

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n,k;
	scanf("%d%d",&n,&k);
	for(int i=k-1;i>=0;i--){
		if(n%i==0){
			printf("%d",i+(n/i)*k);
			return 0;
		}
	}
}

D. Minimum Diameter Tree

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a tree (an undirected connected graph without cycles) and an integer ss.

Vanya wants to put weights on all edges of the tree so that all weights are non-negative real numbers and their sum is ss. At the same time, he wants to make the diameter of the tree as small as possible.

Let's define the diameter of a weighed tree as the maximum sum of the weights of the edges lying on the path between two some vertices of the tree. In other words, the diameter of a weighed tree is the length of the longest simple path in the tree, where length of a path is equal to the sum of weights over all edges in the path.

Find the minimum possible diameter that Vanya can get.

Input

The first line contains two integer numbers nn and ss (2≤n≤1052≤n≤105, 1≤s≤1091≤s≤109) — the number of vertices in the tree and the sum of edge weights.

Each of the following n−1n−1 lines contains two space-separated integer numbers aiai and bibi (1≤ai,bi≤n1≤ai,bi≤n, ai≠biai≠bi) — the indexes of vertices connected by an edge. The edges are undirected.

It is guaranteed that the given edges form a tree.

Output

Print the minimum diameter of the tree that Vanya can get by placing some non-negative real weights on its edges with the sum equal to ss.

Your answer will be considered correct if its absolute or relative error does not exceed 10−610−6.

Formally, let your answer be aa, and the jury's answer be bb. Your answer is considered correct if |a−b|max(1,b)≤10−6|a−b|max(1,b)≤10−6.

Examples

input

4 3
1 2
1 3
1 4

output

2.000000000000000000

input

6 1
2 1
2 3
2 5
5 4
5 6

output

0.500000000000000000

input

5 5
1 2
2 3
3 4
3 5

output

3.333333333333333333

Note

In the first example it is necessary to put weights like this:

It is easy to see that the diameter of this tree is 22. It can be proved that it is the minimum possible diameter.

In the second example it is necessary to put weights like this:

#include <stdio.h>
int n,l,g[100001],i,a,b;
double x;
int main(){
    scanf("%d%lf",&n,&x);
    for(i=1;i<n;i++)scanf("%d%d",&a,&b),g[a]++,g[b]++;
    for(i=1;i<=n;i++)l+=!(g[i]-1);
    x=(2*x)/l;
    printf("%.7lf",x);
}

C. Connect Three

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

The Squareland national forest is divided into equal 1×11×1 square plots aligned with north-south and east-west directions. Each plot can be uniquely described by integer Cartesian coordinates (x,y)(x,y) of its south-west corner.

Three friends, Alice, Bob, and Charlie are going to buy three distinct plots of land A,B,CA,B,C in the forest. Initially, all plots in the forest (including the plots A,B,CA,B,C) are covered by trees. The friends want to visit each other, so they want to clean some of the plots from trees. After cleaning, one should be able to reach any of the plots A,B,CA,B,C from any other one of those by moving through adjacent cleared plots. Two plots are adjacent if they share a side.

For example, A=(0,0)A=(0,0), B=(1,1)B=(1,1), C=(2,2)C=(2,2). The minimal number of plots to be cleared is 55. One of the ways to do it is shown with the gray color.

Of course, the friends don't want to strain too much. Help them find out the smallest number of plots they need to clean from trees.

Input

The first line contains two integers xAxA and yAyA — coordinates of the plot AA (0≤xA,yA≤10000≤xA,yA≤1000). The following two lines describe coordinates (xB,yB)(xB,yB) and (xC,yC)(xC,yC) of plots BB and CC respectively in the same format (0≤xB,yB,xC,yC≤10000≤xB,yB,xC,yC≤1000). It is guaranteed that all three plots are distinct.

Output

On the first line print a single integer kk — the smallest number of plots needed to be cleaned from trees. The following kk lines should contain coordinates of all plots needed to be cleaned. All kk plots should be distinct. You can output the plots in any order.

If there are multiple solutions, print any of them.

Examples

input

Copy

0 0
1 1
2 2

output

Copy

5
0 0
1 0
1 1
1 2
2 2

input

Copy

0 0
2 0
1 1

output

Copy

4
0 0
1 0
1 1
2 0

Note

The first example is shown on the picture in the legend.

The second example is illustrated with the following image:

#include<bits/stdc++.h>
using namespace std;
int main(){
	int x[4],y[4],m[1010];
	for(int i=1;i<=3;i++){
		scanf("%d%d",&x[i],&y[i]);
		m[x[i]]=y[i];
	}
	sort(x+1,x+4);
	sort(y+1,y+4);
	printf("%d\n",x[3]-x[1]+y[3]-y[1]+1);
	for(int i=x[1];i<x[2];i++)
	printf("%d %d\n",i,m[x[1]]);
	for(int i=x[3];i>x[2];i--)
	printf("%d %d\n",i,m[x[3]]);
	for(int i=y[1];i<=y[3];i++)
	printf("%d %d\n",x[2],i);
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值