您的位置:首页 > 其它

sduacm16级寒假热身

2017-01-24 22:56 507 查看



A - Bachgold Problem

 CodeForces
- 749A

Bachgold problem is very easy to formulate. Given a positive integer n represent it as a sum of maximum possible number of prime numbers. One can prove that
such representation exists for any integer greater than 1.
Recall that integer k is called prime if it is greater than 1 and has exactly two positive integer divisors — 1 and k.

Input

The only line of the input contains a single integer n (2 ≤ n ≤ 100 000).

Output

The first line of the output contains a single integer k — maximum possible number of primes in representation.

The second line should contain k primes with their sum equal to n. You can print them in any order. If there are several optimal solution, print any of them.

Example

Input
5


Output
2
2 3


Input
6


Out
4000put
3
2 2 2


/*	给一个数,最多能够分解为多少个素数的和,第一行输出个数,第二行输出具体数字。
* prime number 是素数啊 = =。
* 	很容易想到拆成最小的,1不行,所以偶数全部拆成2,奇数加个3。
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
long n = sc.nextLong(); //不放心大小用了long
System.out.println(n/2); // 除以二得到个数
long i = 0;
while(i<n/2-1){
System.out.print(2+" ");  //前(n/2)-1个
n -=2;
}
if(n%2==1){   //最后判断一下 写2 还是3
System.out.println(3);
}
else{
System.out.println(2);
}
}
}
}



B - Parallelogram is Back

 CodeForces
- 749B 

Long time ago Alex created an interesting problem about parallelogram. The input data for this problem contained four integer points on the Cartesian plane, that defined the set of vertices of some non-degenerate (positive area) parallelogram. Points not
necessary were given in the order of clockwise or counterclockwise traversal.

Alex had very nice test for this problem, but is somehow happened that the last line of the input was lost and now he has only three out of four points of the original parallelogram. He remembers that test was so good that he asks you to restore it given
only these three points.

Input

The input consists of three lines, each containing a pair of integer coordinates xiand yi ( - 1000 ≤ xi, yi ≤ 1000).
It's guaranteed that these three points do not lie on the same line and no two of them coincide.

Output

First print integer k — the number of ways to add one new integer point such that the obtained set defines some parallelogram of positive area. There is no requirement for the points to be arranged in any special order
(like traversal), they just define the set of vertices.

Then print k lines, each containing a pair of integer — possible coordinates of the fourth point.

Example

Input
0 0
1 0
0 1


Output
3
1 -1
-1 1
1 1


Note

If you need clarification of what parallelogram is, please check Wikipedia page:
https://en.wikipedia.org/wiki/Parallelogram
/*	一个平行四边形 给三个点坐标 找第四个点坐标
*  第四个点有三种情况。利用中点即可, 即(x1+x1)/2 = (x3+x4)/2  解出x4,y4
*/
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc  = new Scanner(System.in);
// TODO Auto-generated method stub
int []x = new int[3];
int []y = new int [3];
while(sc.hasNext()){
for(int i = 0;i<3;i++){
x[i] = sc.nextInt();y[i] = sc.nextInt();
}
System.out.println(3);
System.out.print(x[1]+x[2]-x[0]+" ");
System.out.println(y[1]+y[2]-y[0]);
System.out.print(x[1]+x[0]-x[2]+" ");
System.out.println(y[1]+y[0]-y[2]);
System.out.print(x[0]+x[2]-x[1]+" ");
System.out.println(y[0]+y[2]-y[1]);

}
}

}



C - Lesha and array splitting

 CodeForces
- 754A 

One spring day on his way to university Lesha found an array A. Lesha likes to split arrays into several parts. This time Lesha decided to split the array A into several, possibly
one, new arrays so that the sum of elements in each of the new arrays is not zero. One more condition is that if we place the new arrays one after another they will form the old array A.

Lesha is tired now so he asked you to split the array. Help Lesha!

Input

The first line contains single integer n (1 ≤ n ≤ 100) — the number of elements in the array A.

The next line contains n integers a1, a2, ..., an ( - 103 ≤ ai ≤ 103) —
the elements of the array A.

Output

If it is not possible to split the array A and satisfy all the constraints, print single line containing "NO" (without quotes).

Otherwise in the first line print "YES" (without quotes). In the next line print single integer k — the number of new arrays. In each of the next k lines
print two integers li and ri which denote the subarray A[li... ri] of
the initial array A being the i-th new array. Integers li, ri should
satisfy the following conditions:

l1 = 1
rk = n
ri + 1 = li + 1 for each 1 ≤ i < k.
If there are multiple answers, print any of them.

Example

Input
3
1 2 -3


Output
YES
2
1 2
3 3


Input
8
9 -12 3 4 -4 -10 7 3


Output
YES
2
1 2
3 8


Input
1
0


Output
NO


Input
41 2 3 -5


Output
YES
4
1 1
2 2
3 3
4 4


package C_Lesha_and_array_splitting;
/*
*给出一个数列,拆成若干子序列保证每一个子序列sum!=0。要求第一个子序列第一个必须是第一位数
*最后一个子序列的最后一个必须是最后一位数(感觉像废话)。 每一个子序列结尾后,下一个子序列是该子序列的index+1开头。
*
*两种思路,一种是彻底拆开,前后有零的都带上。另一种是拆成两部分,第一部分有一个非零常数,剩余的一部分,和一定不为0.
*
* 一开始没看懂题目以为是输出内容,后来发现是输出index....wa了好几发
*/
import java.util.*;
public class Main {
public static void main (String []args){
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int []arrs = new int
;
int mark = 0;
int sum = 0;
boolean flag = true;
for(int i = 0;i<n;i++){
arrs[i]= sc.nextInt();
sum += arrs[i];
if(arrs[i]!=0&&flag){
mark = i+1; //记录第一个非零数
flag = false;
}
}
if(flag){  //全部是0
System.out.println("NO");
}
else{
System.out.println("YES");
if(sum!=0){ //和不为零 根本不需要拆(题目提出可以拆成一个 即不拆)
System.out.println(1);
System.out.print(1+" ");System.out.println(n);
}
else{
System.out.println(2); //和为零 拆成1到mark   mark+1 到 n
System.out.print(1+" ");System.out.println(mark);
System.out.print(mark+1+" ");System.out.println(n);
}
}
}
}
}



D - Ilya and tic-tac-toe game

 CodeForces
- 754B 

Ilya is an experienced player in tic-tac-toe on the 4 × 4 field. He always starts and plays with Xs. He played a lot of games today with his friend Arseny. The friends became tired and didn't finish the last game. It was Ilya's
turn in the game when they left it. Determine whether Ilya could have won the game by making single turn or not.

The rules of tic-tac-toe on the 4 × 4 field are as follows. Before the first turn all the field cells are empty. The two players take turns placing their signs into empty cells (the first player places Xs, the second player
places Os). The player who places Xs goes first, the another one goes second. The winner is the player who first gets three of his signs in a row next to each other (horizontal, vertical or diagonal).

Input

The tic-tac-toe position is given in four lines.

Each of these lines contains four characters. Each character is '.' (empty cell), 'x' (lowercase English letter x), or 'o'
(lowercase English letter o). It is guaranteed that the position is reachable playing tic-tac-toe, and it is Ilya's turn now (in particular, it means that the game is not finished). It is possible that all the cells are
empty, it means that the friends left without making single turn.

Output

Print single line: "YES" in case Ilya could have won by making single turn, and "NO" otherwise.

Example

Input
xx..
.oo.
x...
oox.


Output
YES


Input
x.ox
ox..
x.o.
oo.x


Output
NO


Input
x..x
..oo
o...
x.xo


Output
YES


Input
o.x.
o...
.x..
ooxx


Output
NO


Note

In the first example Ilya had two winning moves: to the empty cell in the left column and to the leftmost empty cell in the first row.

In the second example it wasn't possible to win by making single turn.

In the third example Ilya could have won by placing X in the last row between two existing Xs.

In the fourth example it wasn't possible to win by making single turn.

package D_Ilya_and_tic_tac_toe_game;
/*
* 给一个4x4的棋盘 再下一个x 能否连成三个 水平 竖直 对角线(horizontal, vertical or diagonal).
* 直接枚举所有情况即可。考虑全面,不要遗漏。
*/
import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
boolean flag=false;
String arrs[][] = new String [4][4];
for(int i = 0;i<4;i++){
String str = sc.nextLine();
for(int j = 0;j<4;j++){
arrs[i][j] = str.substring(j,j+1);
}
}
for(int i = 0;i<4;i++){
for(int j = 0;j<4;j++){
if(arrs[i][j].equals(".")){  //找到"." 看直线方向上是否有两个x。
if(j<2&&arrs[i][j+1].equals("x")&&arrs[i][j+2].equals("x"))
flag = true;
if(j>1&&arrs[i][j-1].equals("x")&&arrs[i][j-2].equals("x"))
flag = true;
if(i<2&&arrs[i+1][j].equals("x")&&arrs[i+2][j].equals("x"))
flag = true;
if(i>1&&arrs[i-1][j].equals("x")&&arrs[i-2][j].equals("x"))
flag = true;

if(i<2&&j<2&&arrs[i+1][j+1].equals("x")&&arrs[i+2][j+2].equals("x"))
flag = true;
if(i>1&&j>1&&arrs[i-1][j-1].equals("x")&&arrs[i-2][j-2].equals("x"))
flag = true;
if(i>1&&j<2&&arrs[i-1][j+1].equals("x")&&arrs[i-2][j+2].equals("x"))
flag = true;
if(i<2&&j>1&&arrs[i+1][j-1].equals("x")&&arrs[i+2][j-2].equals("x"))
flag = true;

if(i>0&&j>0&&i<3&&j<3&&( ( arrs[i-1][j-1].equals("x")&&arrs[i+1][j+1].equals("x") ) ||( arrs[i-1][j+1].equals("x")&&arrs[i+1][j-1].equals("x") ) ) )
flag = true;

if(i>0&&i<3&&arrs[i-1][j].equals("x")&&arrs[i+1][j].equals("x"))
flag = true;
if(j>0&&j<3&&arrs[i][j-1].equals("x")&&arrs[i][j+1].equals("x"))
flag = true;
}
}
}
if(flag)
System.out.println("YES");
else
System.out.println("NO");
}

}

}



E - New Year and Hurry

 CodeForces
- 750A 

Limak is going to participate in a contest on the last day of the 2016. The contest will start at 20:00 and will last four hours, exactly until midnight. There will ben problems, sorted by difficulty, i.e. problem 1 is
the easiest and problem n is the hardest. Limak knows it will take him 5·i minutes to solve the i-th problem.

Limak's friends organize a New Year's Eve party and Limak wants to be there at midnight or earlier. He needs k minutes to get there from his house, where he will participate in the contest first.

How many problems can Limak solve if he wants to make it to the party?

Input

The only line of the input contains two integers n and k (1 ≤ n ≤ 10, 1 ≤ k ≤ 240) — the number of
the problems in the contest and the number of minutes Limak needs to get to the party from his house.

Output

Print one integer, denoting the maximum possible number of problems Limak can solve so that he could get to the party at midnight or earlier.

Example

Input
3 222


Output
2


Input
4 190


Output
4


Input
7 1


Output
7


Note

In the first sample, there are 3 problems and Limak needs 222 minutes to get to the party. The three problems require 5, 10 and 15 minutes
respectively. Limak can spend5 + 10 = 15 minutes to solve first two problems. Then, at 20:15 he can leave his house to get to the party at 23:57 (after 222 minutes). In this scenario Limak would solve2 problems.
He doesn't have enough time to solve 3 problems so the answer is 2.

In the second sample, Limak can solve all 4 problems in 5 + 10 + 15 + 20 = 50 minutes. At 20:50 he will leave the house and go to the party. He will get there exactly at midnight.

In the third sample, Limak needs only 1 minute to get to the party. He has enough time to solve all 7 problems.

package E_NewYearandHurry;
/*
* Limak8-12点有个比赛,还有个party需要在12点之前到(去party的路上需要花费k分钟)。 热爱学习的他选择先做题- -。
* 第i个问题需要5*i分钟  (等差数列)  给出题目数量和从家到party的时间,计算出他能做多少题。
*
* 简单模拟
*/
import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt(),k = sc.nextInt();
int left = 240-k;
int i = 1;
while(i*5+i*(i-1)*5/2<=left){ //等差数列求和判断
i++;
}

if(i<=n)  //对i加一个判断
System.out.println(i-1);
else
System.out.println(n);
}

}

}


F - New Year and North Pole

 CodeForces
- 750B 

In this problem we assume the Earth to be a completely round ball and its surface a perfect sphere. The length of the equator and any meridian is considered to be exactly 40 000 kilometers. Thus, travelling from North Pole to
South Pole or vice versa takes exactly 20 000 kilometers.

Limak, a polar bear, lives on the North Pole. Close to the New Year, he helps somebody with delivering packages all around the world. Instead of coordinates of places to visit, Limak got a description how he should move, assuming that he starts from the
North Pole. The description consists of n parts. In the i-th part of his journey, Limak should move ti kilometers
in the direction represented by a stringdiri that is one of: "North", "South", "West",
"East".

Limak isn’t sure whether the description is valid. You must help him to check the following conditions:

If at any moment of time (before any of the instructions or while performing one of them) Limak is on the North Pole, he can move only to the South.
If at any moment of time (before any of the instructions or while performing one of them) Limak is on the South Pole, he can move only to the North.
The journey must end on the North Pole.
Check if the above conditions are satisfied and print "YES" or "NO" on a single line.

Input

The first line of the input contains a single integer n (1 ≤ n ≤ 50).

The i-th of next n lines contains an integer ti and a string diri (1 ≤ ti ≤ 106, 

) —
the length and the direction of the i-th part of the journey, according to the description Limak got.

Output

Print "YES" if the description satisfies the three conditions, otherwise print "NO", both without the quotes.

Example

Input
57500 South
10000 East
3500 North
4444 West
4000 North


Output
YES


Input
215000 South
4000 East


Output
NO


Input
520000 South
1000 North
1000000 West
9000 North
10000 North


Output
YES


Input
3
20000 South
10 East
20000 North


Output
NO


Input
21000 North
1000 South


Output
NO


Input
450 South
50 North
15000 South
15000 North


Output
YES


Note

Drawings below show how Limak's journey would look like in first two samples. In the second sample the answer is "NO" because he doesn't end on the North Pole.



package F_New_Year_and_North_Pole;
/*
* 北极熊送快递 NSWE四个方向。equator和任何一条meridian都是4w km。从N到S需要2w km。北极点只能向南走,南极点只能向北走。
* 最后熊熊送完快递需要回到北极点。
* 只需要考虑NS两个方向的移动,模拟即可。
* T T 第一次忘记考虑类似4w km South的情况。
*/
import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n =sc.nextInt();
int []dists = new int
;
String [] dirs = new String
;
for(int i = 0;i<n;i++){
dists[i] = sc.nextInt();
dirs[i] = sc.next();
}

if(!(dirs[0].equals("South"))){ //出发只能向南
System.out.println("NO");
return;
}

int sum = 0;
for(int i = 0;i<n;i++){
if(sum==20000&&!(dirs[i].equals("North"))){
System.out.println("NO");
return;
}
if(sum==0&&!(dirs[i].equals("South"))){
System.out.println("NO");
return;
}
if(dirs[i].equals("South")){   //南加北减  最后得零即回到北极。
sum+=dists[i];
}
if(dirs[i].equals("North")){
sum-=dists[i];

}
if(sum<0||sum>20000){  //第一次忘记这种情况,按照规则,根本不可能出现这种sum数据。
System.out.println("NO");
return;
}
}
if(sum==0){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}

}

}



G - Santa Claus and a Place in a Class

 CodeForces
- 752A 

Santa Claus is the first who came to the Christmas Olympiad, and he is going to be the first to take his place at a desk! In the classroom there are n lanes of m desks each, and
there are two working places at each of the desks. The lanes are numbered from 1 to n from the left to the right, the desks in a lane are numbered from 1 to mstarting
from the blackboard. Note that the lanes go perpendicularly to the blackboard, not along it (see picture).

The organizers numbered all the working places from 1 to 2nm. The places are numbered by lanes (i. e. all the places of the first lane go first, then all the places of the second lane,
and so on), in a lane the places are numbered starting from the nearest to the blackboard (i. e. from the first desk in the lane), at each desk, the place on the left is numbered before the place on the right.


 The picture illustrates the first and
the second samples.
Santa Clause knows that his place has number k. Help him to determine at which lane at which desk he should sit, and whether his place is on the left or on the right!

Input

The only line contains three integers n, m and k (1 ≤ n, m ≤ 10 000, 1 ≤ k ≤ 2nm) —
the number of lanes, the number of desks in each lane and the number of Santa Claus' place.

Output

Print two integers: the number of lane r, the number of desk d, and a character s, which stands for the side of the desk Santa Claus. The
character s should be "L", if Santa Clause should sit on the left, and "R" if his place is on the right.

Example

Input
4 3 9


Output
2 2 L


Input
4 3 24


Output
4 3 R


Input
2 4 4


Output
1 2 R


Note

The first and the second samples are shown on the picture. The green place corresponds to Santa Claus' place in the first example, the blue place corresponds to Santa Claus' place in the second example.

In the third sample there are two lanes with four desks in each, and Santa Claus has the fourth place. Thus, his place is in the first lane at the second desk on the right.

package G_Santa_Claus_and_a_Place_in_a_Class;
/*
* n列m行的桌子 给一个k 判断桌子所在行列以及座位是L还是R。
* 奇数L偶数R.  num=(k+1)/2为桌子序号 num/m是列  num%m是行
*/
import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner sc  = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt(),m=sc.nextInt(),k=sc.nextInt();
if(k%2==0){    //偶数情况
int num = k/2;
if(num%m==0){ //最后一行
System.out.print(num/m+" ");
System.out.print(m+" ");
System.out.println("R");
}
else{
System.out.print(num/m+1+" "); //非整除+1
System.out.print(num%m+" ");
System.out.println("R");
}
}
else{//奇数情况
k++;
int num = k/2;
if(num%m==0){
System.out.print(num/m+" ");
System.out.print(m+" ");
System.out.println("L");
}
else{
System.out.print(num/m+1+" ");
System.out.print(num%m+" ");
System.out.println("L");
}
}
}

}

}










#include <cstdio> //精简版
int main() {
int n, m, k;
scanf("%d%d%d", &n, &m, &k);
int d = (k + 1) / 2;
printf("%d %d %c\n", d / m + (d % m ? 1 : 0), d % m ? d % m : m, k % 2 ? 'L' : 'R');
}



H - Santa Claus and Keyboard Check

 CodeForces
- 752B 

Santa Claus decided to disassemble his keyboard to clean it. After he returned all the keys back, he suddenly realized that some pairs of keys took each other's place! That is, Santa suspects that each key is either on its place, or on the place of another
key, which is located exactly where the first key should be.

In order to make sure that he's right and restore the correct order of keys, Santa typed his favorite patter looking only to his keyboard.

You are given the Santa's favorite patter and the string he actually typed. Determine which pairs of keys could be mixed. Each key must occur in pairs at most once.

Input

The input consists of only two strings s and t denoting the favorite Santa's patter and the resulting string. s and t are
not empty and have the same length, which is at most 1000. Both strings consist only of lowercase English letters.

Output

If Santa is wrong, and there is no way to divide some of keys into pairs and swap keys in each pair so that the keyboard will be fixed, print «-1» (without quotes).

Otherwise, the first line of output should contain the only integer k (k ≥ 0) — the number of pairs of keys that should be swapped. The following k lines
should contain two space-separated letters each, denoting the keys which should be swapped. All printed letters must be distinct.

If there are several possible answers, print any of them. You are free to choose the order of the pairs and the order of keys in a pair.

Each letter must occur at most once. Santa considers the keyboard to be fixed if he can print his favorite patter without mistakes.

Example

Input
helloworld
ehoolwlroz


Output
3
h e
l o
d z


Input
hastalavistababy
hastalavistababy


Output
0


Input
merrychristmas
christmasmerry


Output
-1


package H_Santa_Claus_and_Keyboard_Check;
/*
* 键盘拆掉重组出现了问题, 现在给出一个正确序列和按相应键位出现的实际序列 判断是否键位两两对应
* 如果是,输出错误的对数和各自对应关系。否则-1
*
* 用一个二维数组储存初始键位 index作为正确键位序号  内容储存实际键位。判断是否对应。
*/
import java.util.*;
public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
String str1 = sc.nextLine(),str2 = sc.nextLine();
if(str1.equals(str2)){
System.out.println(0);
return;
}
int [] arrs = new int[26];
char [] a1 = new char[26],a2 = new char[26];
for(int i=0;i<26;i++){
arrs[i] = -1;  //全部先赋值-1。不能赋值为正确键位值 需要特殊考虑 例如a对应a又出现a对b的情况。
//如果一开始arrs[a]=a 发生关系改变后无法记录这种改变,因此需要赋值-1。
}
for(int i = 0;i<str1.length();i++){
char co = str1.charAt(i);
char ct = str2.charAt(i);
int c1 = co-'a', c2 = ct-'a'; //顺序取字母判断

if(arrs[c1]!=-1|arrs[c2]!=-1){ //有了对应但不相互对应
if(arrs[c1]!=c2|arrs[c2]!=c1){
System.out.println(-1);
return;
}
}
if(arrs[c1]==-1&&arrs[c2]==-1){ //均未有对应关系
int temp = c1;
arrs[c1] = c2;
arrs[c2] = temp;
}
}
int count = 0;
for(int i = 0;i<26;i++){
char a = 'a';
if(arrs[i]!=-1&&arrs[i]!=i){ //记录所有非正确键位的对应关系 两个数组分别储存。
a1[count] = (char) ('a'+i);
a2[count] = (char) ('a'+arrs[i]);
arrs[arrs[i]]=-1;  //记录以后就去掉 防止多次记录
arrs[i] = -1;
count++;
}
}
System.out.println(count);
for(int i = 0;i<count;i++){
System.out.print(a1[i]+" ");System.out.println(a2[i]);
}
}

}

}


I - Felicity is ComingCodeForces
- 757C     

题目大概看懂了但是没思路,第一次读这么长的题,然后因为放假各种原因一直在拖,有时间还是要a掉这道题啊,哪怕看着题解也要做一遍。必须要努力了,所有人都在进步,萌新不是借口,一直有小白的心态最后只能被淘汰。




 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: