// Codeforces Round #176 (Div. 2) B Pipeline
import java.io.*;
import java.io.ObjectInputStream.GetField;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
Long n = in.nextLong();
Long k = in.nextLong();
if (n == 1) {
out.println(0);
return;
}
Long left = 2L;
Long right = k;
Long totSplitter = 0L;
Long minAns = Long.MAX_VALUE;
Long mid;
while (true) {
mid = (left + right) / 2;
totSplitter = GetOut(mid, k);
if (totSplitter >= n) {
left = mid + 1;
minAns = Math.min(minAns, k - mid + 1);
}
else {
right = mid - 1;
}
if (left > right)
break;
}
if (minAns == Long.MAX_VALUE)
out.println(-1);
else
out.println(minAns);
}
static long GetOut(long k1, long k)
{
return k1 + (k1 + k - 1) * (k - k1) / 2;
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Some solution examples of problems in topcoder, codeeval, google code jam, interviewstreet, onlinejudge-uva, codeforces, projecteuler, etc
Sunday, March 31, 2013
Thursday, March 28, 2013
Codeforces Round #176 (Div. 2) A IQ Test
//Codeforces Round #176 (Div. 2) A IQ Test
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
String[] ss = new String[4];
for (int i = 0; i < ss.length; i++) {
ss[i] = in.nextString();
}
for (int i = 0; i < ss.length - 1; i++)
{
for (int j = 0; j < ss.length - 1; j++) {
int numw = 0;
if (ss[i].charAt(j) == '.')
numw++;
if (ss[i].charAt(j + 1) == '.')
numw++;
if (ss[i + 1].charAt(j) == '.')
numw++;
if (ss[i + 1].charAt(j + 1) == '.')
numw++;
if (numw != 2) {
out.println("YES");
return;
}
}
}
out.println("NO");
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
String[] ss = new String[4];
for (int i = 0; i < ss.length; i++) {
ss[i] = in.nextString();
}
for (int i = 0; i < ss.length - 1; i++)
{
for (int j = 0; j < ss.length - 1; j++) {
int numw = 0;
if (ss[i].charAt(j) == '.')
numw++;
if (ss[i].charAt(j + 1) == '.')
numw++;
if (ss[i + 1].charAt(j) == '.')
numw++;
if (ss[i + 1].charAt(j + 1) == '.')
numw++;
if (numw != 2) {
out.println("YES");
return;
}
}
}
out.println("NO");
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Wednesday, March 27, 2013
Codeforces Round #175 (Div. 2) B Find Marble
//Codeforces Round #175 (Div. 2) B Find Marble
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
int s = in.nextInt();
int t = in.nextInt();
int[] p = new int[n + 1];
for (int i = 1; i <= n; i++) {
p[i] = in.nextInt();
}
if (s == t) {
out.println(0);
}
else {
int ss = s;
for (int i = 1; i <= n; i++) {
ss = p[ss];
if (ss == s)
break;
if (ss == t) {
out.println(i);
return;
}
}
out.println(-1);
}
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
int s = in.nextInt();
int t = in.nextInt();
int[] p = new int[n + 1];
for (int i = 1; i <= n; i++) {
p[i] = in.nextInt();
}
if (s == t) {
out.println(0);
}
else {
int ss = s;
for (int i = 1; i <= n; i++) {
ss = p[ss];
if (ss == s)
break;
if (ss == t) {
out.println(i);
return;
}
}
out.println(-1);
}
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Codeforces Round #175 (Div. 2) A Slightly Decreasing Permutations
//Codeforces Round #175 (Div. 2) A Slightly Decreasing Permutations
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
int k = in.nextInt();
int p = n - k;
StringBuffer sb = new StringBuffer("");
for (int i = 1; i < p; i++) {
sb.append(i);
sb.append(" ");
}
for (int i = n; i >= p; i--) {
sb.append(i);
sb.append(" ");
}
out.println(sb.toString().trim());
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
int k = in.nextInt();
int p = n - k;
StringBuffer sb = new StringBuffer("");
for (int i = 1; i < p; i++) {
sb.append(i);
sb.append(" ");
}
for (int i = n; i >= p; i--) {
sb.append(i);
sb.append(" ");
}
out.println(sb.toString().trim());
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Topcoder SRM 574 DIV 2 L2 TheNumberGameDiv2
//Topcoder SRM 574 DIV 2 L2 TheNumberGameDiv2
import java.util.*;
import java.math.*;
import javax.crypto.spec.PSource;
public class TheNumberGameDiv2 {
public static void main(String[] args) {
System.out.println(
//
new TheNumberGameDiv2().minimumMoves(
218181918,
9181
));
}
HashSet<Integer> nums = new HashSet<Integer>();
int minmov;
public int minimumMoves(int A, int B) {
if (A == B)
return 0;
minmov = Integer.MAX_VALUE;
dfs(A, B, 0);
if (minmov == Integer.MAX_VALUE)
return -1;
else
return minmov;
}
private void dfs(int a, int b, int mov) {
if (a == b) {
minmov = Math.min(minmov, mov);
return;
}
if (a == 0)
return;
nums.add(a);
String rev = new StringBuffer(String.valueOf(a)).reverse().toString();
int arev = Integer.valueOf(rev);
if (!nums.contains(arev)) {
dfs(arev, b, mov + 1);
}
dfs(a / 10, b, mov + 1);
}
}
import java.util.*;
import java.math.*;
import javax.crypto.spec.PSource;
public class TheNumberGameDiv2 {
public static void main(String[] args) {
System.out.println(
//
new TheNumberGameDiv2().minimumMoves(
218181918,
9181
));
}
HashSet<Integer> nums = new HashSet<Integer>();
int minmov;
public int minimumMoves(int A, int B) {
if (A == B)
return 0;
minmov = Integer.MAX_VALUE;
dfs(A, B, 0);
if (minmov == Integer.MAX_VALUE)
return -1;
else
return minmov;
}
private void dfs(int a, int b, int mov) {
if (a == b) {
minmov = Math.min(minmov, mov);
return;
}
if (a == 0)
return;
nums.add(a);
String rev = new StringBuffer(String.valueOf(a)).reverse().toString();
int arev = Integer.valueOf(rev);
if (!nums.contains(arev)) {
dfs(arev, b, mov + 1);
}
dfs(a / 10, b, mov + 1);
}
}
Topcoder SRM 574 DIV 2 L1 CityMap
//Topcoder SRM 574 DIV 2 L1 CityMap
import java.util.*;
import java.math.*;
import javax.crypto.spec.PSource;
public class CityMap {
public static void main(String[] args) {
System.out.println(
//
new CityMap().getLegend(
new String[]
{},
new int[]
{}
));
}
public String getLegend(String[] cityMap, int[] POIs) {
int[] nchar = new int[26];
for (int i = 0; i < cityMap.length; i++) {
for (int j = 0; j < cityMap[i].length(); j++) {
char c = cityMap[i].charAt(j);
if (c != '.') {
nchar[c - 'A'] += 1;
}
}
}
String s = "";
for (int i = 0; i < POIs.length; i++) {
for (int j = 0; j < nchar.length; j++) {
if (POIs[i] == nchar[j]) {
char c = (char) ('A' + j);
s += String.valueOf(c);
}
}
}
return s;
}
}
import java.util.*;
import java.math.*;
import javax.crypto.spec.PSource;
public class CityMap {
public static void main(String[] args) {
System.out.println(
//
new CityMap().getLegend(
new String[]
{},
new int[]
{}
));
}
public String getLegend(String[] cityMap, int[] POIs) {
int[] nchar = new int[26];
for (int i = 0; i < cityMap.length; i++) {
for (int j = 0; j < cityMap[i].length(); j++) {
char c = cityMap[i].charAt(j);
if (c != '.') {
nchar[c - 'A'] += 1;
}
}
}
String s = "";
for (int i = 0; i < POIs.length; i++) {
for (int j = 0; j < nchar.length; j++) {
if (POIs[i] == nchar[j]) {
char c = (char) ('A' + j);
s += String.valueOf(c);
}
}
}
return s;
}
}
Topcoder SRM 573 DIV 2 2 TeamContestEasy
//Topcoder SRM 573 DIV 2 2 TeamContestEasy
import java.util.*;
import java.math.*;
import javax.crypto.spec.PSource;
public class TeamContestEasy {
public static void main(String[] args) {
System.out.println(
//
new TeamContestEasy().worstRank(
new int[]
{ 610297, 849870, 523999, 6557, 976530, 731458, 7404, 795100,
147040, 110947, 159692, 40785, 4949, 2903, 1688, 37278, 620703,
28156, 16823, 1159, 12132, 971379, 5916, 1159, 988589,
12215, 133, 1490, 911360, 920059, 544070, 40249, 514852, 852,
745070, 1105, 715897, 714696, 589133, 698317, 5683, 631612,
16453, 101000, 764881, 101, 2053, 754661 }
));
}
public int worstRank(int[] strength) {
int greater = 0;
int myScore = strength[0] + strength[1] + strength[2];
myScore -= Math.min(strength[0], Math.min(strength[1], strength[2]));
if (strength.length == 3)
return 1;
Integer[] other = new Integer[strength.length - 3];
for (int i = 0; i < other.length; i++) {
other[i] = strength[i + 3];
}
Arrays.sort(other, Collections.reverseOrder());
for (int i = 0; i < other.length / 3; i++) {
if (other[i] == -1)
continue;
int m1 = other[i];
other[i] = -1;
int m2 = -1;
int m3 = -1;
for (int j = other.length - 1; j >= 0; j--) {
if (other[j] == -1)
continue;
if (m1 + other[j] > myScore) {
m2 = other[j];
other[j] = -1;
for (int j2 = other.length - 1; j2 >= 0; j2--) {
if (other[j2] == -1)
continue;
other[j2] = -1;
break;
}
greater++;
break;
}
}
}
int maxRank = 1 + greater;
return maxRank;
}
}
import java.util.*;
import java.math.*;
import javax.crypto.spec.PSource;
public class TeamContestEasy {
public static void main(String[] args) {
System.out.println(
//
new TeamContestEasy().worstRank(
new int[]
{ 610297, 849870, 523999, 6557, 976530, 731458, 7404, 795100,
147040, 110947, 159692, 40785, 4949, 2903, 1688, 37278, 620703,
28156, 16823, 1159, 12132, 971379, 5916, 1159, 988589,
12215, 133, 1490, 911360, 920059, 544070, 40249, 514852, 852,
745070, 1105, 715897, 714696, 589133, 698317, 5683, 631612,
16453, 101000, 764881, 101, 2053, 754661 }
));
}
public int worstRank(int[] strength) {
int greater = 0;
int myScore = strength[0] + strength[1] + strength[2];
myScore -= Math.min(strength[0], Math.min(strength[1], strength[2]));
if (strength.length == 3)
return 1;
Integer[] other = new Integer[strength.length - 3];
for (int i = 0; i < other.length; i++) {
other[i] = strength[i + 3];
}
Arrays.sort(other, Collections.reverseOrder());
for (int i = 0; i < other.length / 3; i++) {
if (other[i] == -1)
continue;
int m1 = other[i];
other[i] = -1;
int m2 = -1;
int m3 = -1;
for (int j = other.length - 1; j >= 0; j--) {
if (other[j] == -1)
continue;
if (m1 + other[j] > myScore) {
m2 = other[j];
other[j] = -1;
for (int j2 = other.length - 1; j2 >= 0; j2--) {
if (other[j2] == -1)
continue;
other[j2] = -1;
break;
}
greater++;
break;
}
}
}
int maxRank = 1 + greater;
return maxRank;
}
}
Sunday, March 24, 2013
Codeforces Round #174 (Div. 2) B Cows and Poker Game
//Codeforces Round #174 (Div. 2) B Cows and Poker Game
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
char[] c = in.nextString().toCharArray();
int z = 0;
int na = 0;
int ni = 0;
for (int i = 0; i < c.length; i++) {
if (c[i] == 'A')
na++;
else if (c[i] == 'I')
ni++;
}
if (ni > 1)
z = 0;
else if (ni == 1)
z = 1;
else
z = na;
out.println(z);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
char[] c = in.nextString().toCharArray();
int z = 0;
int na = 0;
int ni = 0;
for (int i = 0; i < c.length; i++) {
if (c[i] == 'A')
na++;
else if (c[i] == 'I')
ni++;
}
if (ni > 1)
z = 0;
else if (ni == 1)
z = 1;
else
z = na;
out.println(z);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Codeforces Round #174 (Div. 2) A Cows and Primitive Roots
//Codeforces Round #174 (Div. 2) A Cows and Primitive Roots
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int p = in.nextInt();
int nx = 0;
for (int x = 2; x < p; x++) {
int xx = x;
boolean primroot = true;
for (int j = 1; j < p - 1; j++) {
if ((xx - 1) % p == 0) {
primroot = false;
break;
}
xx = xx * x % p;
}
if (primroot) {
if ((xx - 1) % p != 0) {
primroot = false;
}
}
if (primroot)
nx++;
}
if (p == 2)
nx = 1;
out.println(nx);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int p = in.nextInt();
int nx = 0;
for (int x = 2; x < p; x++) {
int xx = x;
boolean primroot = true;
for (int j = 1; j < p - 1; j++) {
if ((xx - 1) % p == 0) {
primroot = false;
break;
}
xx = xx * x % p;
}
if (primroot) {
if ((xx - 1) % p != 0) {
primroot = false;
}
}
if (primroot)
nx++;
}
if (p == 2)
nx = 1;
out.println(nx);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Friday, March 22, 2013
Codeforces Round #173 (Div. 2) C XOR and OR
//Codeforces Round #173 (Div. 2) C XOR and OR
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
String a = in.nextString();
String b = in.nextString();
if (a.length() != b.length()) {
out.println("NO");
return;
}
boolean ahas1 = a.contains("1");
boolean bhas1 = b.contains("1");
if (!ahas1 && !bhas1)
out.println("YES");
else if (ahas1 && bhas1)
out.println("YES");
else
out.println("NO");
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
String a = in.nextString();
String b = in.nextString();
if (a.length() != b.length()) {
out.println("NO");
return;
}
boolean ahas1 = a.contains("1");
boolean bhas1 = b.contains("1");
if (!ahas1 && !bhas1)
out.println("YES");
else if (ahas1 && bhas1)
out.println("YES");
else
out.println("NO");
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Codeforces Round #173 (Div. 2) B Painting Eggs
//Codeforces Round #173 (Div. 2) B Painting Eggs
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
long[] a = new long[n];
long[] g = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextLong();
g[i] = in.nextLong();
}
long suma = a[0];
long sumg = 0;
StringBuilder sb = new StringBuilder(n);
sb.append("A");
for (int i = 1; i < n; i++) {
long diffa = (suma + a[i]) - sumg;
long diffg = suma - (sumg + g[i]);
if (Math.abs(diffa) < Math.abs(diffg)) {
sb.append("A");
suma += a[i];
}
else {
sb.append("G");
sumg += g[i];
}
}
if (Math.abs(sumg - suma) <= 500) {
out.println(sb);
return;
}
suma = 0;
sumg = g[0];
sb = new StringBuilder(n);
sb.append("G");
for (int i = 1; i < n; i++) {
long diffa = (suma + a[i]) - sumg;
long diffg = suma - (sumg + g[i]);
if (Math.abs(diffa) < Math.abs(diffg)) {
sb.append("A");
suma += a[i];
}
else {
sb.append("G");
sumg += g[i];
}
}
if (Math.abs(sumg - suma) <= 500) {
out.println(sb);
return;
}
out.println(-1);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
long[] a = new long[n];
long[] g = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextLong();
g[i] = in.nextLong();
}
long suma = a[0];
long sumg = 0;
StringBuilder sb = new StringBuilder(n);
sb.append("A");
for (int i = 1; i < n; i++) {
long diffa = (suma + a[i]) - sumg;
long diffg = suma - (sumg + g[i]);
if (Math.abs(diffa) < Math.abs(diffg)) {
sb.append("A");
suma += a[i];
}
else {
sb.append("G");
sumg += g[i];
}
}
if (Math.abs(sumg - suma) <= 500) {
out.println(sb);
return;
}
suma = 0;
sumg = g[0];
sb = new StringBuilder(n);
sb.append("G");
for (int i = 1; i < n; i++) {
long diffa = (suma + a[i]) - sumg;
long diffg = suma - (sumg + g[i]);
if (Math.abs(diffa) < Math.abs(diffg)) {
sb.append("A");
suma += a[i];
}
else {
sb.append("G");
sumg += g[i];
}
}
if (Math.abs(sumg - suma) <= 500) {
out.println(sb);
return;
}
out.println(-1);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Codeforces Round #173 (Div. 2) A Bit++
//Codeforces Round #173 (Div. 2) A Bit++
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
int x = 0;
for (int i = 0; i < n; i++) {
String s = in.nextString();
if (s.contains("++"))
x++;
else
x--;
}
out.println(x);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
int x = 0;
for (int i = 0; i < n; i++) {
String s = in.nextString();
if (s.contains("++"))
x++;
else
x--;
}
out.println(x);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Codeforces Round #169 (Div. 2) B Little Girl and Game
//Codeforces Round #169 (Div. 2) B Little Girl and Game
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int[] acnt = new int[26];
char[] cs = in.nextString().toCharArray();
int len = cs.length;
Arrays.fill(acnt, 0);
for (int i = 0; i < cs.length; i++) {
if (cs[i] != '-')
acnt[cs[i] - 'a'] += 1;
}
int odd = 0;
for (int i = 0; i < acnt.length; i++) {
if (acnt[i] % 2 == 1)
odd++;
}
boolean player1win = false;
if (len % 2 == 0) {
if (odd == 0)
player1win = true;
}
else {
if (odd % 2 == 1)
player1win = true;
}
if (player1win)
out.println("First");
else
out.println("Second");
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int[] acnt = new int[26];
char[] cs = in.nextString().toCharArray();
int len = cs.length;
Arrays.fill(acnt, 0);
for (int i = 0; i < cs.length; i++) {
if (cs[i] != '-')
acnt[cs[i] - 'a'] += 1;
}
int odd = 0;
for (int i = 0; i < acnt.length; i++) {
if (acnt[i] % 2 == 1)
odd++;
}
boolean player1win = false;
if (len % 2 == 0) {
if (odd == 0)
player1win = true;
}
else {
if (odd % 2 == 1)
player1win = true;
}
if (player1win)
out.println("First");
else
out.println("Second");
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Codeforces Round #169 (Div. 2) A Lunch Rush
//Codeforces Round #169 (Div. 2) A Lunch Rush
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
long n = in.nextLong();
long k = in.nextLong();
long maxJoy = Long.MIN_VALUE;
for (int i = 0; i < n; i++) {
long f = in.nextLong();
long t = in.nextLong();
long j;
if (t > k) {
j = f - (t - k);
}
else {
j = f;
}
maxJoy = Math.max(maxJoy, j);
}
out.println(maxJoy);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
long n = in.nextLong();
long k = in.nextLong();
long maxJoy = Long.MIN_VALUE;
for (int i = 0; i < n; i++) {
long f = in.nextLong();
long t = in.nextLong();
long j;
if (t > k) {
j = f - (t - k);
}
else {
j = f;
}
maxJoy = Math.max(maxJoy, j);
}
out.println(maxJoy);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Codeforces Round #167 (Div. 2) B Dima and Sequence
//Codeforces Round #167 (Div. 2) B Dima and Sequence
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextLong();
}
long[] fa = new long[n];
for (int i = 0; i < n; i++) {
fa[i] = f(a[i]);
}
Arrays.sort(fa);
long pairs = 0;
long cnt = 1;
for (int i = 1; i < n; i++) {
if (fa[i] == fa[i - 1]) {
cnt++;
}
else {
if (cnt > 1) {
long newPairs = ((cnt * cnt) - cnt) / 2;
pairs += newPairs;
}
cnt = 1;
}
}
if (cnt > 1) {
long newPairs = ((cnt * cnt) - cnt) / 2;
pairs += newPairs;
}
out.println(pairs);
}
static long f(long x)
{
if (x == 0)
return 0;
else if (x % 2 == 0)
return f(x / 2);
else
return f(x / 2) + 1;
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextLong();
}
long[] fa = new long[n];
for (int i = 0; i < n; i++) {
fa[i] = f(a[i]);
}
Arrays.sort(fa);
long pairs = 0;
long cnt = 1;
for (int i = 1; i < n; i++) {
if (fa[i] == fa[i - 1]) {
cnt++;
}
else {
if (cnt > 1) {
long newPairs = ((cnt * cnt) - cnt) / 2;
pairs += newPairs;
}
cnt = 1;
}
}
if (cnt > 1) {
long newPairs = ((cnt * cnt) - cnt) / 2;
pairs += newPairs;
}
out.println(pairs);
}
static long f(long x)
{
if (x == 0)
return 0;
else if (x % 2 == 0)
return f(x / 2);
else
return f(x / 2) + 1;
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Codeforces Round #167 (Div. 2) A Dima and Friends
//Codeforces Round #167 (Div. 2) A Dima and Friends
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
int sum = 0;
for (int i = 0; i < n; i++) {
sum += in.nextInt();
}
int ways = 0;
for (int i = 1; i <= 5; i++) {
int tot = sum + i;
if (tot % (n + 1) != 1)
ways++;
}
out.println(ways);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
int sum = 0;
for (int i = 0; i < n; i++) {
sum += in.nextInt();
}
int ways = 0;
for (int i = 1; i <= 5; i++) {
int tot = sum + i;
if (tot % (n + 1) != 1)
ways++;
}
out.println(ways);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Codeforces Round #166 (Div. 2) B Prime Matrix
// Codeforces Round #166 (Div. 2) B Prime Matrix
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int n = in.nextInt();
int m = in.nextInt();
int[][] a = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
a[i][j] = in.nextInt();
}
}
int res = Integer.MAX_VALUE;
boolean[] isPrm = GetPrimeTable(200000);
int[] nextPrm = new int[200001];
int curPrm = 200000;
while (!isPrm[curPrm])
curPrm--;
for (int i = curPrm; i >= 1; i--) {
if (isPrm[i]) {
curPrm = i;
}
nextPrm[i] = curPrm;
}
for (int i = 0; i < n; i++) {
int step = 0;
for (int j = 0; j < m; j++) {
int num = a[i][j];
step += nextPrm[num] - num;
}
res = Math.min(res, step);
}
for (int j = 0; j < m; j++) {
int step = 0;
for (int i = 0; i < n; i++) {
int num = a[i][j];
step += nextPrm[num] - num;
}
res = Math.min(res, step);
}
out.println(res);
}
public static boolean[] GetPrimeTable(int maxNum) {
boolean[] isPrime = new boolean[maxNum + 1];
for (int i = 0; i < isPrime.length; i++) {
isPrime[i] = true;
}
isPrime[0] = isPrime[1] = false;
int sqrtMaxNum = (int) Math.sqrt(maxNum) + 1;
for (int i = 2; i <= sqrtMaxNum; i++) {
if (!isPrime[i])
continue;
int k = i * i;
while (k <= maxNum) {
isPrime[k] = false;
k += i;
}
}
return isPrime;
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Monday, March 18, 2013
Topcoder SRM 573 DIV 2 1 SkiResortsEasy
// Topcoder SRM 573 DIV 2 1 SkiResortsEasy
import java.util.*;
import java.math.*;
import javax.crypto.spec.PSource;
public class SkiResortsEasy {
public static void main(String[] args) {
System.out.println(
//
new SkiResortsEasy().minCost(
null
));
}
public int minCost(int[] altitude) {
int minCost = 0;
for (int i = 1; i < altitude.length; i++) {
if (altitude[i] > altitude[i - 1]) {
int diff = altitude[i] - altitude[i - 1];
altitude[i] -= diff;
minCost += diff;
}
}
return minCost;
}
}
import java.util.*;
import java.math.*;
import javax.crypto.spec.PSource;
public class SkiResortsEasy {
public static void main(String[] args) {
System.out.println(
//
new SkiResortsEasy().minCost(
null
));
}
public int minCost(int[] altitude) {
int minCost = 0;
for (int i = 1; i < altitude.length; i++) {
if (altitude[i] > altitude[i - 1]) {
int diff = altitude[i] - altitude[i - 1];
altitude[i] -= diff;
minCost += diff;
}
}
return minCost;
}
}
Sunday, March 17, 2013
Codeforces Round #166 (Div. 2) A Beautiful Year
//Codeforces Round #166 (Div. 2) A Beautiful Year
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int y = in.nextInt();
int yy = y + 1;
HashSet<Character> hs = new HashSet<Character>();
while (true) {
boolean distinct = true;
hs.clear();
char[] cc = String.valueOf(yy).toCharArray();
for (char c : cc) {
if (hs.contains(c)) {
distinct = false;
break;
}
else {
hs.add(c);
}
}
if (distinct)
break;
yy++;
}
out.println(yy);
}
static long compare(long a1, long b1, long a2, long b2) {
// compare a1/b1 with a2/b2
return (a1 * b2) - (a2 * b1);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int y = in.nextInt();
int yy = y + 1;
HashSet<Character> hs = new HashSet<Character>();
while (true) {
boolean distinct = true;
hs.clear();
char[] cc = String.valueOf(yy).toCharArray();
for (char c : cc) {
if (hs.contains(c)) {
distinct = false;
break;
}
else {
hs.add(c);
}
}
if (distinct)
break;
yy++;
}
out.println(yy);
}
static long compare(long a1, long b1, long a2, long b2) {
// compare a1/b1 with a2/b2
return (a1 * b2) - (a2 * b1);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Codeforces Round #172 (Div. 2) B Nearest Fraction
// Codeforces Round #172 (Div. 2) B Nearest Fraction
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int x = in.nextInt();
int y = in.nextInt();
int n = in.nextInt();
long a = 0;
long b = 0;
long minDistA = 1000000;
long minDistB = 1;
for (int i = 1; i <= n; i++) {
long bb = i;
long aa1 = x * bb / y;
long aa2 = aa1 + 1;
if (i == 3) {
n = n + 0;
}
long distA = Math.abs((x * bb) - (aa1 * y));
long distB = y * bb;
if (compare(distA, distB, minDistA, minDistB) < 0) {
minDistA = distA;
minDistB = distB;
a = aa1;
b = bb;
}
distA = Math.abs((x * bb) - (aa2 * y));
distB = y * bb;
if (compare(distA, distB, minDistA, minDistB) < 0) {
minDistA = distA;
minDistB = distB;
a = aa2;
b = bb;
}
}
out.println("" + a + "/" + b);
}
static long compare(long a1, long b1, long a2, long b2) {
// compare a1/b1 with a2/b2
return (a1 * b2) - (a2 * b1);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
public static void main(String[] args) throws IOException {
// helpers for input/output
boolean LOCAL_TEST = false;// change to false before submitting
out = System.out;
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin2.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();
}
private static void solve() throws IOException
{
int x = in.nextInt();
int y = in.nextInt();
int n = in.nextInt();
long a = 0;
long b = 0;
long minDistA = 1000000;
long minDistB = 1;
for (int i = 1; i <= n; i++) {
long bb = i;
long aa1 = x * bb / y;
long aa2 = aa1 + 1;
if (i == 3) {
n = n + 0;
}
long distA = Math.abs((x * bb) - (aa1 * y));
long distB = y * bb;
if (compare(distA, distB, minDistA, minDistB) < 0) {
minDistA = distA;
minDistB = distB;
a = aa1;
b = bb;
}
distA = Math.abs((x * bb) - (aa2 * y));
distB = y * bb;
if (compare(distA, distB, minDistA, minDistB) < 0) {
minDistA = distA;
minDistB = distB;
a = aa2;
b = bb;
}
}
out.println("" + a + "/" + b);
}
static long compare(long a1, long b1, long a2, long b2) {
// compare a1/b1 with a2/b2
return (a1 * b2) - (a2 * b1);
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
}
Subscribe to:
Posts (Atom)