codechef program #23 Three Way Communications;codechef program #24: Greedy puppy

COMM3


#1:PROBLEM STATEMENT:
The Chef likes to stay in touch with his staff. So, the Chef, the head server, and the sous-chef all carry two-way transceivers so they can stay in constant contact. Of course, these transceivers have a limited range so if two are too far apart, they cannot communicate directly.
The Chef invested in top-of-the-line transceivers which have a few advanced features. One is that even if two people cannot talk directly because they are out of range, if there is another transceiver that is close enough to both, then the two transceivers can still communicate with each other using the third transceiver as an intermediate device.
There has been a minor emergency in the Chef's restaurant and he needs to communicate with both the head server and the sous-chef right away. Help the Chef determine if it is possible for all three people to communicate with each other, even if two must communicate through the third because they are too far apart.

Input

The first line contains a single positive integer T ≤ 100 indicating the number of test cases to follow. The first line of each test case contains a positive integer R ≤ 1,000 indicating that two transceivers can communicate directly without an intermediate transceiver if they are at most R meters away from each other. The remaining three lines of the test case describe the current locations of the Chef, the head server, and the sous-chef, respectively. Each such line contains two integers X,Y (at most 10,000 in absolute value) indicating that the respective person is located at position X,Y.

Output

For each test case you are to output a single line containing a single string. If it is possible for all three to communicate then you should output "yes". Otherwise, you should output "no".
To be clear, we say that two transceivers are close enough to communicate directly if the length of the straight line connecting their X,Y coordinates is at most R.

Example


Input:
3
1
0 1
0 0
1 0
2
0 1
0 0
1 0
2
0 0
0 2
2 1


Output:
yes
yes
no



My Solution:


import java.util.Scanner;


class threeway
{

static boolean function(int x1,int x2,int y1,int y2,int r){
boolean value=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2) <= r * r;
return value;
}

public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int op=in.nextInt();
for (int a=0;a { int counter=0;
int r=in.nextInt();
int cx1=in.nextInt();
int cy1=in.nextInt();
int sx1=in.nextInt();
int sy1=in.nextInt();
int hx1=in.nextInt();
int hy1=in.nextInt();
if(function(cx1,sx1,cy1,sy1,r)) counter++;
if(function(hx1,sx1,hy1,sy1,r)) counter++;
if(function(cx1,hx1,cy1,hy1,r)) counter++;


if (counter>1) System.out.println("yes");
else {System.out.println("no");}

}
}

}






#2:PROBLEM STATEMENT:
Tuzik is a little dog. But despite the fact he is still a puppy he already knows about the pretty things that coins are. He knows that for every coin he can get very tasty bone from his master. He believes that some day he will find a treasure and have loads of bones.
And finally he found something interesting. A wooden chest containing N coins! But as you should remember, Tuzik is just a little dog, and so he can't open it by himself. Actually, the only thing he can really do is barking. He can use his barking to attract nearby people and seek their help. He can set the loudness of his barking very precisely, and therefore you can assume that he can choose to call any number of people, from a minimum of 1, to a maximum of K.
When people come and open the chest they divide all the coins between them in such a way that everyone will get the same amount of coins and this amount is maximal possible. If some coins are not used they will leave it on the ground and Tuzik will take them after they go away. Since Tuzik is clearly not a fool, he understands that his profit depends on the number of people he will call. While Tuzik works on his barking, you have to find the maximum possible number of coins he can get.

Input

The first line of the input contains an integer T denoting the number of test cases. Each of next T lines contains 2 space-separated integers: N and K, for this test case.

Output

For each test case output one integer - the maximum possible number of coins Tuzik can get.

Constraints

  • 1 ≤ T ≤ 50
  • 1 ≤ N, K ≤ 105

Example

Input:
2
5 2
11 3

Output:
1
2

Explanation

In the first example he should call two people. Each of them will take 2 coins and they will leave 1 coin for Tuzik.

In the second example he should call 3 people.


My Solution:



import java.util.Scanner;
import java.lang.*;

class GreedyPuppy
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int op=in.nextInt();
for (int a=0;a {
int b=in.nextInt();
int c=in.nextInt();
int e=0;
for(int i=1;i<=c;i++)
{
int d=b%i;
e=Math.max(d, e);
}
System.out.println(e);



}
}

}



Posted by lol ik

Comments

Post a Comment

Popular Posts