Showing posts with label math. Show all posts
Showing posts with label math. Show all posts

Sunday, April 13, 2014

Codeforces Round #206 (Div. 2) A Vasya and Digital Root

// Codeforces Round #206 (Div. 2) A Vasya and Digital Root
// Problem: http://codeforces.com/contest/355/problem/A

import java.io.*;
import java.math.*;
import java.util.*;

import org.omg.CORBA.Environment;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int k = in.nextInt();
int d = in.nextInt();
if (k == 1)
out.println(d);
else {
if (d == 0)
out.println("No solution");
else {
StringBuilder sb = new StringBuilder();
sb.append("1");
for (int i = 1; i < k - 1; i++) {
sb.append("0");
}
sb.append((d - 1));
out.println(sb);
}
}
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(
new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Codeforces Round #241 (Div. 2) A Guess a number!

// Codeforces Round #241 (Div. 2) A Guess a number!
// Problems: http://codeforces.com/contest/416/problem/A

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int ymin = Integer.MIN_VALUE;
int ymax = Integer.MAX_VALUE;
int n = in.nextInt();
for (int i = 0; i < n; i++) {
String eq = in.nextString();
int x = in.nextInt();
String yn = in.nextString();
if (yn.equals("Y")) {
if (eq.equals(">=")) {
if (x > ymin)
ymin = x;
}
else if (eq.equals("<")) {
if (x - 1 < ymax)
ymax = x - 1;
}
else if (eq.equals(">")) {
if (x + 1 > ymin)
ymin = x + 1;
}
else if (eq.equals("<=")) {
if (x < ymax)
ymax = x;
}
}
else {
if (eq.equals("<")) {
if (x > ymin)
ymin = x;
}
else if (eq.equals(">=")) {
if (x - 1 < ymax)
ymax = x - 1;
}
else if (eq.equals("<=")) {
if (x + 1 > ymin)
ymin = x + 1;
}
else if (eq.equals(">")) {
if (x < ymax)
ymax = x;
}
}
}
if (ymin <= ymax) {
ymin = Math.max(ymin, -2000000000);
ymin = Math.min(ymin, 2000000000);
out.println(ymin);
}
else
out.println("Impossible");

}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(
new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Saturday, April 12, 2014

Codeforces Round #205 (Div. 2) A Domino

// Codeforces Round #205 (Div. 2) A Domino
// Problems: http://codeforces.com/contest/353/problem/A

import java.io.*;
import java.math.*;
import java.util.*;

import org.omg.CORBA.Environment;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
int[] x = new int[n];
int[] y = new int[n];
int sum = 0;
int sumx = 0;
int sumy = 0;
for (int i = 0; i < n; i++) {
x[i] = in.nextInt();
y[i] = in.nextInt();
sumx += x[i];
sumy += y[i];
}
sum = sumx + sumy;
if (sum % 2 == 1)
out.println(-1);
else {
if (sumx % 2 == 0 && sumy % 2 == 0)
out.println(0);
else {
for (int i = 0; i < n; i++) {
if ((x[i] % 2 == 1 && y[i] % 2 == 0) ||
(x[i] % 2 == 0 && y[i] % 2 == 1)) {
out.println(1);
return;
}
}
out.println(-1);
}
}
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(
new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Saturday, April 5, 2014

Codeforces Round #204 (Div. 2) A Jeff and Digits

// Codeforces Round #204 (Div. 2) A Jeff and Digits
// Problem: http://codeforces.com/contest/352/problem/A

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
int n0 = 0;
int n5 = 0;
for (int i = 0; i < n; i++) {
int a = in.nextInt();
if (a == 0)
n0++;
else
n5++;
}
int k = (n5 / 9) * 9;
if (n0 == 0) {
out.println(-1);
}
else if (k == 0) {
out.println(0);
}
else {
for (int i = 0; i < k; i++) {
out.print(5);
}
for (int i = 0; i < n0; i++) {
out.print(0);
}
}
out.println();
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Friday, April 4, 2014

Codeforces Round #201 (Div. 2) B Fixed Points

// Codeforces Round #201 (Div. 2) B Fixed Points
// Problem statement: http://codeforces.com/contest/347/problem/B

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
int[] a = new int[n];
int[] pos = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
pos[a[i]] = i;
}
int fp = 0;
int maxAdd = 0;
for (int i = 0; i < a.length; i++) {
if (a[i] == i)
fp++;
else {
int add;
if (a[pos[i]] == i && a[i] == pos[i])
add = 2;
else
add = 1;
maxAdd = Math.max(maxAdd, add);
}
}
out.println(fp + maxAdd);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Codeforces Round #201 (Div. 2) A Difference Row

// Codeforces Round #201 (Div. 2) A Difference Row
// Problem statement: http://codeforces.com/contest/347/problem/A

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
int[] x = new int[n];
for (int i = 0; i < x.length; i++)
x[i] = in.nextInt();
Arrays.sort(x);
int tmp = x[0];
x[0] = x[n - 1];
x[n - 1] = tmp;
for (int i = 0; i < x.length; i++) {
if (i > 0) {
out.print(" ");
}
out.print(x[i]);
}
out.println();
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Sunday, March 30, 2014

Codeforces Round #200 (Div. 2) B Simple Molecules

// Codeforces Round #200 (Div. 2) B Simple Molecules
// Problem: http://codeforces.com/contest/344/problem/B

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int a = in.nextInt();
int b = in.nextInt();
int c = in.nextInt();
boolean valid = false;
for (int i = 0; i <= a; i++) {
int b12 = i;
int b31 = a - i;
int b23 = c - b31;
valid = (b12 >= 0 && b23 >= 0 && b31 >= 0 && b == (b12 + b23));
if (valid) {
out.println(b12 + " " + b23 + " " + b31);
return;
}
}
out.println("Impossible");
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Thursday, January 30, 2014

Codeforces Round #218 (Div. 2) B - Fox Dividing Cheese

// Codeforces Round #218 (Div. 2) B B - Fox Dividing Cheese

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
long a = in.nextInt();
long b = in.nextInt();
long g = GCD(a, b);
int ops = 0;
a = a / g;
while (a != 1) {
if (a % 5 == 0)
a = a / 5;
else if (a % 3 == 0)
a = a / 3;
else if (a % 2 == 0)
a = a / 2;
else {
ops = -1;
break;
}
ops++;
}
a = b / g;
if (ops != -1) {
while (a != 1) {
if (a % 5 == 0)
a = a / 5;
else if (a % 3 == 0)
a = a / 3;
else if (a % 2 == 0)
a = a / 2;
else {
ops = -1;
break;
}
ops++;
}
}
out.println(ops);
}

public static long GCD(long a, long b) {
long rem = a % b;
while (rem != 0) {
a = b;
b = rem;
rem = a % b;
}
return b;
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Sunday, August 25, 2013

Codeforces Round #195 (Div. 2) A Vasily the Bear and Triangle

// Codeforces Round #195 (Div. 2) A Vasily the Bear and Triangle

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int x = in.nextInt();
int y = in.nextInt();
int len = Math.abs(x) + Math.abs(y);
int x1;
int y1;
int x2;
int y2;
if (x < 0) {
x1 = -len;
y1 = 0;
x2 = 0;
if (y > 0)
y2 = len;
else
y2 = -len;
}
else {
x1 = 0;
x2 = len;
y2 = 0;
if (y > 0)
y1 = len;
else
y1 = -len;
}
out.println(x1 + " " + y1 + " " + x2 + " " + y2);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Friday, July 26, 2013

Topcoder SRM 582 DIV 2 L1 SemiPerfectSquare

// Topcoder SRM 582 DIV 2 L1 SemiPerfectSquare

import java.util.*;
import java.math.*;

//tc; rename the class name before submitting
public class SemiPerfectSquare {
public static void main(String[] args) {
SemiPerfectSquare obj = new SemiPerfectSquare();
System.out.println(
obj.check(
5
));
}

public String check(int N) {
int n = (int) Math.sqrt(N);
boolean yes = false;
for (int i = 2; i <= n; i++) {
int i2 = i * i;
for (int j = 1; j < i; j++) {
if (j * i2 == N)
yes = true;
}
}
if (yes)
return "Yes";
else
return "No";
}

}

Saturday, July 6, 2013

Codeforces Round #191 (Div. 2) B Hungry Sequence

// Codeforces Round #191 (Div. 2) B Hungry Sequence

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int n = in.nextInt();
int x = 5000001;
StringBuilder sb = new StringBuilder("");
sb.append(String.valueOf(x));
for (int i = 1; i < n; i++) {
x += 2;
sb.append(" " + String.valueOf(x));
}
out.println(sb);
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}

Codeforces Round #190 (Div. 2) C Ciel and Robot

// Codeforces Round #190 (Div. 2) C Ciel and Robot

import java.io.*;
import java.math.*;
import java.util.*;

//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;

private static void solve() throws IOException
{
int a = in.nextInt();
int b = in.nextInt();
String s = in.nextString();
int dx = 0;
int dy = 0;
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'L')
dx--;
else if (c == 'R')
dx++;
else if (c == 'D')
dy--;
else if (c == 'U')
dy++;
}
int repx = 0;
if (dx != 0)
repx = a / dx;
int repy = 0;
if (dy != 0)
repy = b / dy;
int rep = Math.max(repx, repy) - 100;
if (rep < 0)
rep = 0;
int px = rep * dx;
int py = rep * dy;
boolean located = false;
for (int k = 0; k < 200; k++) {
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (c == 'L')
px--;
else if (c == 'R')
px++;
else if (c == 'D')
py--;
else if (c == 'U')
py++;
if (px == a && py == b)
located = true;
}
}
if (a == 0 && b == 0)
located = true;
if (located)
out.println("Yes");
else
out.println("No");
}

public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
if (!cname.equals(""))
LOCAL_TEST = true;
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}

solve();
}

// =====================================
static class MyScanner {
BufferedReader bufReader;
StringTokenizer strTok;

public MyScanner() throws IOException
{
bufReader = new BufferedReader(new InputStreamReader(System.in));
strTok = new StringTokenizer("");
}

public MyScanner(String inputFile) throws IOException {
bufReader = new BufferedReader(new InputStreamReader(new FileInputStream(
inputFile)));
strTok = new StringTokenizer("");
}

String GetNextToken() throws IOException {
if (!strTok.hasMoreTokens())
strTok = new StringTokenizer(bufReader.readLine());
return strTok.nextToken();
}

public int nextInt() throws IOException {
return Integer.valueOf(GetNextToken());
}

public long nextLong() throws IOException {
return Long.valueOf(GetNextToken());
}

public double nextDouble() throws IOException {
return Double.valueOf(GetNextToken());
}

public String nextString() throws IOException {
return GetNextToken();
}

public String nextLine() throws IOException {
return bufReader.readLine();
}

public int countTokens() {
return strTok.countTokens();
}

public boolean hasMoreTokens() {
return strTok.hasMoreTokens();
}
}

}