src
stringlengths 95
64.6k
| complexity
stringclasses 7
values | problem
stringlengths 6
50
| from
stringclasses 1
value |
---|---|---|---|
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i) a[i] = in.nextInt();
boolean[] done = new boolean[n];
int res = 0;
while (true) {
int bi = -1;
for (int i = 0; i < n; ++i)
if (!done[i]) {
if (bi < 0 || a[i] < a[bi]) bi = i;
}
if (bi < 0) break;
++res;
for (int i = 0; i < n; ++i) if (!done[i] && a[i] % a[bi] == 0) done[i] = true;
}
out.println(res);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| quadratic | 1209_A. Paint the Numbers | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.stream.IntStream;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.charset.Charset;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author mikit
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
LightScanner in = new LightScanner(inputStream);
LightWriter out = new LightWriter(outputStream);
APaintTheNumbers solver = new APaintTheNumbers();
solver.solve(1, in, out);
out.close();
}
static class APaintTheNumbers {
public void solve(int testNumber, LightScanner in, LightWriter out) {
// out.setBoolLabel(LightWriter.BoolLabel.YES_NO_FIRST_UP);
int n = in.ints();
int[] a = in.ints(n);
IntroSort.sort(a);
boolean[] done = new boolean[n];
int ans = 0;
for (int i = 0; i < n; i++) {
if (done[i]) continue;
int d = a[i];
ans++;
for (int j = 0; j < n; j++) {
if (a[j] % d == 0) {
done[j] = true;
}
}
}
out.ans(ans).ln();
}
}
static class IntroSort {
private static int INSERTIONSORT_THRESHOLD = 16;
private IntroSort() {
}
static void sort(int[] a, int low, int high, int maxDepth) {
while (high - low > INSERTIONSORT_THRESHOLD) {
if (maxDepth-- == 0) {
HeapSort.sort(a, low, high);
return;
}
int cut = QuickSort.step(a, low, high);
sort(a, cut, high, maxDepth);
high = cut;
}
InsertionSort.sort(a, low, high);
}
public static void sort(int[] a) {
if (a.length <= INSERTIONSORT_THRESHOLD) {
InsertionSort.sort(a, 0, a.length);
} else {
sort(a, 0, a.length, 2 * BitMath.msb(a.length));
}
}
}
static final class ArrayUtil {
private ArrayUtil() {
}
public static void swap(int[] a, int x, int y) {
int t = a[x];
a[x] = a[y];
a[y] = t;
}
}
static class LightScanner {
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
public LightScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
}
public String string() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
return tokenizer.nextToken();
}
public int ints() {
return Integer.parseInt(string());
}
public int[] ints(int length) {
return IntStream.range(0, length).map(x -> ints()).toArray();
}
}
static final class BitMath {
private BitMath() {
}
public static int count(int v) {
v = (v & 0x55555555) + ((v >> 1) & 0x55555555);
v = (v & 0x33333333) + ((v >> 2) & 0x33333333);
v = (v & 0x0f0f0f0f) + ((v >> 4) & 0x0f0f0f0f);
v = (v & 0x00ff00ff) + ((v >> 8) & 0x00ff00ff);
v = (v & 0x0000ffff) + ((v >> 16) & 0x0000ffff);
return v;
}
public static int msb(int v) {
if (v == 0) {
throw new IllegalArgumentException("Bit not found");
}
v |= (v >> 1);
v |= (v >> 2);
v |= (v >> 4);
v |= (v >> 8);
v |= (v >> 16);
return count(v) - 1;
}
}
static class HeapSort {
private HeapSort() {
}
private static void heapfy(int[] a, int low, int high, int i, int val) {
int child = 2 * i - low + 1;
while (child < high) {
if (child + 1 < high && a[child] < a[child + 1]) {
child++;
}
if (val >= a[child]) {
break;
}
a[i] = a[child];
i = child;
child = 2 * i - low + 1;
}
a[i] = val;
}
static void sort(int[] a, int low, int high) {
for (int p = (high + low) / 2 - 1; p >= low; p--) {
heapfy(a, low, high, p, a[p]);
}
while (high > low) {
high--;
int pval = a[high];
a[high] = a[low];
heapfy(a, low, high, low, pval);
}
}
}
static class LightWriter implements AutoCloseable {
private final Writer out;
private boolean autoflush = false;
private boolean breaked = true;
public LightWriter(Writer out) {
this.out = out;
}
public LightWriter(OutputStream out) {
this(new BufferedWriter(new OutputStreamWriter(out, Charset.defaultCharset())));
}
public LightWriter print(char c) {
try {
out.write(c);
breaked = false;
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return this;
}
public LightWriter print(String s) {
try {
out.write(s, 0, s.length());
breaked = false;
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
return this;
}
public LightWriter ans(String s) {
if (!breaked) {
print(' ');
}
return print(s);
}
public LightWriter ans(int i) {
return ans(Integer.toString(i));
}
public LightWriter ln() {
print(System.lineSeparator());
breaked = true;
if (autoflush) {
try {
out.flush();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
return this;
}
public void close() {
try {
out.close();
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}
}
}
static class QuickSort {
private QuickSort() {
}
private static void med(int[] a, int low, int x, int y, int z) {
if (a[z] < a[x]) {
ArrayUtil.swap(a, low, x);
} else if (a[y] < a[z]) {
ArrayUtil.swap(a, low, y);
} else {
ArrayUtil.swap(a, low, z);
}
}
static int step(int[] a, int low, int high) {
int x = low + 1, y = low + (high - low) / 2, z = high - 1;
if (a[x] < a[y]) {
med(a, low, x, y, z);
} else {
med(a, low, y, x, z);
}
int lb = low + 1, ub = high;
while (true) {
while (a[lb] < a[low]) {
lb++;
}
ub--;
while (a[low] < a[ub]) {
ub--;
}
if (lb >= ub) {
return lb;
}
ArrayUtil.swap(a, lb, ub);
lb++;
}
}
}
static class InsertionSort {
private InsertionSort() {
}
static void sort(int[] a, int low, int high) {
for (int i = low; i < high; i++) {
for (int j = i; j > low && a[j - 1] > a[j]; j--) {
ArrayUtil.swap(a, j - 1, j);
}
}
}
}
}
| quadratic | 1209_A. Paint the Numbers | CODEFORCES |
// Working program using Reader Class
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
import java.util.StringTokenizer;
public class Main1
{
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws IOException
{
Reader s=new Reader();
int n = s.nextInt(), i, j, ans=0;
int[] a = new int[101];
for(i=0;i<n;i++){
a[s.nextInt()]++;
}
for(i=1;i<=100;i++){
if(a[i]>0){
ans++;
for(j=i;j<=100;j++){
if(j%i==0){
a[j]=0;
}
}
}
}
System.out.println(ans);
}
}
| quadratic | 1209_A. Paint the Numbers | CODEFORCES |
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
PrintWriter out = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer("");
String next() throws IOException {
if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); }
return tok.nextToken();
}
int ni() throws IOException { return Integer.parseInt(next()); }
long nl() throws IOException { return Long.parseLong(next()); }
void solve() throws IOException {
int n=ni();
int[]A=new int[n];
for (int x=0;x<n;x++) A[x]=ni();
Arrays.sort(A);
ArrayList<Integer>B=new ArrayList();
B.add(A[0]);
int ans=1;
Outer:
for (int x=1;x<n;x++) {
for (int y=0;y<B.size();y++) {
if (A[x]%B.get(y)==0) continue Outer;
}
ans++;
B.add(A[x]);
}
System.out.println(ans);
}
public static void main(String[] args) throws IOException {
new Main().solve();
}
} | quadratic | 1209_A. Paint the Numbers | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
boolean[] dead = new boolean[n];
int ans = 0;
for (int i = 0; i < n; i++) {
if (dead[i]) {
continue;
}
++ans;
for (int j = i; j < n; j++) {
if (a[j] % a[i] == 0) {
dead[j] = true;
}
}
}
out.println(ans);
}
}
static class FastScanner {
private BufferedReader in;
private StringTokenizer st;
public FastScanner(InputStream stream) {
in = new BufferedReader(new InputStreamReader(stream));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| quadratic | 1209_A. Paint the Numbers | CODEFORCES |
import java.util.*;
import java.io.*;
import java.util.Map.Entry;
public class Codeforces {
static int n;
static double max;
static int[] pre;
public static void findIntensity(int l){
for(int i = 0, j = i + l; j < n + 1; i++, j++){
double res = (pre[j] - pre[i]) / (double) l;
max = Math.max(max, res);
}
}
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] heat = new int[n];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < n; i++){
heat[i] = Integer.parseInt(st.nextToken());
}
max = 0;
pre = new int[n + 1];
pre[0] = 0;
for(int i = 0; i < n; i++){
pre[i + 1] = pre[i] + heat[i];
}
for(int i = k; i <= n; i++){
findIntensity(i);
}
System.out.println(max);
}
} | quadratic | 1003_C. Intense Heat | CODEFORCES |
//q4
import java.io.*;
import java.util.*;
import java.math.*;
public class q4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int query = in.nextInt();
while (query -- > 0) {
int n = in.nextInt();
int k = in.nextInt();
char[] arr = new char[n];
//slot all n into char array
String code = in.next();
for (int i = 0; i < n; i++) {
arr[i] = code.charAt(i);
}
//R, G, B cycle
int r = 0;
int g = 0;
int b = 0;
for (int i = 0; i < k; i++) {
if (i % 3 == 0) {
if (arr[i] == 'R') {g++; b++;}
else if (arr[i] == 'G') {r++; b++;}
else {r++; g++;} //if is 'B'
} else if (i % 3 == 1) {
if (arr[i] == 'G') {g++; b++;}
else if (arr[i] == 'B') {r++; b++;}
else {r++; g++;} //if is 'R'
} else { //if mod 3 is 2
if (arr[i] == 'B') {g++; b++;}
else if (arr[i] == 'R') {r++; b++;}
else {r++; g++;} //if is 'G'
}
}
//starting from kth position, if different then add 1, and check (j-k)th position
int rMin = r;
int gMin = g;
int bMin = b;
for (int j = k; j < n; j++) {
//R cycle
if ((j % 3 == 0 && arr[j] != 'R') ||
(j % 3 == 1 && arr[j] != 'G') ||
(j % 3 == 2 && arr[j] != 'B')) {
r++;
}
//R cycle
if (((j - k) % 3 == 0 && arr[j - k] != 'R') ||
((j - k) % 3 == 1 && arr[j - k] != 'G') ||
((j - k) % 3 == 2 && arr[j - k] != 'B')) {
r--;
}
rMin = Math.min(r, rMin);
//G cycle
if ((j % 3 == 0 && arr[j] != 'G') ||
(j % 3 == 1 && arr[j] != 'B') ||
(j % 3 == 2 && arr[j] != 'R')) {
g++;
}
if (((j - k) % 3 == 0 && arr[j - k] != 'G') ||
((j - k) % 3 == 1 && arr[j - k] != 'B') ||
((j - k) % 3 == 2 && arr[j - k] != 'R')) {
g--;
}
gMin = Math.min(gMin, g);
//B cycle
if ((j % 3 == 0 && arr[j] != 'B') ||
(j % 3 == 1 && arr[j] != 'R') ||
(j % 3 == 2 && arr[j] != 'G')) {
b++;
}
if (((j - k) % 3 == 0 && arr[j - k] != 'B') ||
((j - k) % 3 == 1 && arr[j - k] != 'R') ||
((j - k) % 3 == 2 && arr[j - k] != 'G')) {
b--;
}
bMin = Math.min(bMin, b);
}
System.out.println(Math.min(Math.min(rMin, gMin), bMin));
}
}
} | quadratic | 1196_D2. RGB Substring (hard version) | CODEFORCES |
//q4
import java.io.*;
import java.util.*;
import java.math.*;
public class q4 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out=new PrintWriter(new OutputStreamWriter(System.out));
int query = in.nextInt();
while (query -- > 0) {
int n = in.nextInt();
int k = in.nextInt();
char[] arr = new char[n];
//slot all n into char array
String code = in.next();
for (int i = 0; i < n; i++) {
arr[i] = code.charAt(i);
}
//R, G, B cycle
int r = 0;
int g = 0;
int b = 0;
for (int i = 0; i < k; i++) {
if (i % 3 == 0) {
if (arr[i] == 'R') {g++; b++;}
else if (arr[i] == 'G') {r++; b++;}
else {r++; g++;} //if is 'B'
} else if (i % 3 == 1) {
if (arr[i] == 'G') {g++; b++;}
else if (arr[i] == 'B') {r++; b++;}
else {r++; g++;} //if is 'R'
} else { //if mod 3 is 2
if (arr[i] == 'B') {g++; b++;}
else if (arr[i] == 'R') {r++; b++;}
else {r++; g++;} //if is 'G'
}
}
//starting from kth position, if different then add 1, and check (j-k)th position
int rMin = r;
int gMin = g;
int bMin = b;
for (int j = k; j < n; j++) {
//R cycle
if ((j % 3 == 0 && arr[j] != 'R') ||
(j % 3 == 1 && arr[j] != 'G') ||
(j % 3 == 2 && arr[j] != 'B')) {
r++;
}
//R cycle
if (((j - k) % 3 == 0 && arr[j - k] != 'R') ||
((j - k) % 3 == 1 && arr[j - k] != 'G') ||
((j - k) % 3 == 2 && arr[j - k] != 'B')) {
r--;
}
rMin = Math.min(r, rMin);
//G cycle
if ((j % 3 == 0 && arr[j] != 'G') ||
(j % 3 == 1 && arr[j] != 'B') ||
(j % 3 == 2 && arr[j] != 'R')) {
g++;
}
if (((j - k) % 3 == 0 && arr[j - k] != 'G') ||
((j - k) % 3 == 1 && arr[j - k] != 'B') ||
((j - k) % 3 == 2 && arr[j - k] != 'R')) {
g--;
}
gMin = Math.min(gMin, g);
//B cycle
if ((j % 3 == 0 && arr[j] != 'B') ||
(j % 3 == 1 && arr[j] != 'R') ||
(j % 3 == 2 && arr[j] != 'G')) {
b++;
}
if (((j - k) % 3 == 0 && arr[j - k] != 'B') ||
((j - k) % 3 == 1 && arr[j - k] != 'R') ||
((j - k) % 3 == 2 && arr[j - k] != 'G')) {
b--;
}
bMin = Math.min(bMin, b);
}
out.println(Math.min(Math.min(rMin, gMin), bMin));
}
out.flush();
}
} | quadratic | 1196_D2. RGB Substring (hard version) | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
import java.util.StringTokenizer;
import java.util.function.Function;
public class P1196D2 {
static boolean multipleIndependent = true;
void run() {
int n = in.nextInt();
int k = in.nextInt();
char[] s = in.next().toCharArray();
int[] dp = new int[3];
char[] c = {'R', 'G', 'B'};
int min = Integer.MAX_VALUE;
for (int i = 0; i < k; i++) {
dp[0] += s[i] == c[(i + 0) % 3] ? 0 : 1;
dp[1] += s[i] == c[(i + 1) % 3] ? 0 : 1;
dp[2] += s[i] == c[(i + 2) % 3] ? 0 : 1;
}
min = Math.min(Math.min(Math.min(dp[0], dp[1]), dp[2]), min);
// System.out.println(Arrays.toString(dp));
for (int i = k; i < n; i++) {
dp[0] += (s[i] == c[(i + 0) % 3] ? 0 : 1) - (s[i - k] == c[(i - k + 0) % 3] ? 0 : 1);
dp[1] += (s[i] == c[(i + 1) % 3] ? 0 : 1) - (s[i - k] == c[(i - k + 1) % 3] ? 0 : 1);
dp[2] += (s[i] == c[(i + 2) % 3] ? 0 : 1) - (s[i - k] == c[(i - k + 2) % 3] ? 0 : 1);
min = Math.min(Math.min(Math.min(dp[0], dp[1]), dp[2]), min);
// System.out.println(Arrays.toString(dp));
}
System.out.println(min);
}
/* -----: Template :----- */
static InputReader in = new InputReader(System.in);
public static void main(String[] args) {
P1196D2 p = new P1196D2();
int q = multipleIndependent ? in.nextInt() : 1;
while (q-- > 0) {
p.run();
}
}
int numLength(long n) {
int l = 0;
while (n > 0) {
n /= 10;
l++;
}
return l;
}
<R> long binarySearch(long lowerBound, long upperBound,
R value, Function<Long, R> generatorFunction, Comparator<R> comparator) {
if (lowerBound <= upperBound) {
long mid = (lowerBound + upperBound) / 2;
int compare = comparator.compare(generatorFunction.apply(mid), value);
if (compare == 0) {
return mid;
} else if (compare < 0) {
return binarySearch(mid + 1, upperBound, value, generatorFunction, comparator);
} else {
return binarySearch(lowerBound, mid - 1, value, generatorFunction, comparator);
}
} else {
return -1;
}
}
<T> Integer[] sortSimultaneously(T[] key, Comparator<T> comparator,
Object[]... moreArrays) {
int n = key.length;
for (Object[] array : moreArrays) {
if (array.length != n) {
throw new RuntimeException("Arrays must have equals lengths");
}
}
Integer[] indices = new Integer[n];
for (int i = 0; i < n; i++) {
indices[i] = i;
}
Comparator<Integer> delegatingComparator = (a, b) -> {
return comparator.compare(key[a], key[b]);
};
Arrays.sort(indices, delegatingComparator);
reorder(indices, key);
for (Object[] array : moreArrays) {
reorder(indices, array);
}
return indices;
}
void reorder(Integer[] indices, Object[] arr) {
if (indices.length != arr.length) {
throw new RuntimeException("Arrays must have equals lengths");
}
int n = arr.length;
Object[] copy = new Object[n];
for (int i = 0; i < n; i++) {
copy[i] = arr[indices[i]];
}
System.arraycopy(copy, 0, arr, 0, n);
}
int prodMod(int a, int b, int mod) {
return (int) (((long) a) * b % mod);
}
long prodMod(long a, long b, long mod) {
long res = 0;
a %= mod;
b %= mod;
while (b > 0) {
if ((b & 1) > 0) {
res = (res + a) % mod;
}
a = (a << 1) % mod;
b >>= 1;
}
return res;
}
long sumMod(int[] b, long mod) {
long res = 0;
for (int i = 0; i < b.length; i++) {
res = (res + b[i] % mod) % mod;
}
return res;
}
long sumMod(long[] a, long mod) {
long res = 0;
for (int i = 0; i < a.length; i++) {
res = (res + a[i] % mod) % mod;
}
return res;
}
long sumProdMod(int[] a, long b, long mod) {
long res = sumMod(a, mod);
return prodMod(res, b, mod);
}
long sumProdMod(long[] a, long b, long mod) {
long res = sumMod(a, mod);
return prodMod(res, b, mod);
}
long sumProdMod(int[] a, int[] b, long mod) {
if (a.length != b.length) {
throw new RuntimeException("Arrays must have equals lengths");
}
long res = 0;
for (int i = 0; i < a.length; i++) {
res = (res + prodMod(a[i], b[i], mod)) % mod;
}
return res;
}
long sumProdMod(long[] a, long[] b, long mod) {
if (a.length != b.length) {
throw new RuntimeException("Arrays must have equals lengths");
}
long res = 0;
for (int i = 0; i < a.length; i++) {
res = (res + prodMod(a[i], b[i], mod)) % mod;
}
return res;
}
int[] toPrimitive(Integer[] arr) {
int[] res = new int[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}
int[][] toPrimitive(Integer[][] arr) {
int[][] res = new int[arr.length][];
for (int i = 0; i < arr.length; i++) {
res[i] = toPrimitive(arr[i]);
}
return res;
}
long[] toPrimitive(Long[] arr) {
long[] res = new long[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}
long[][] toPrimitive(Long[][] arr) {
long[][] res = new long[arr.length][];
for (int i = 0; i < arr.length; i++) {
res[i] = toPrimitive(arr[i]);
}
return res;
}
Integer[] toWrapper(int[] arr) {
Integer[] res = new Integer[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}
Integer[][] toWrapper(int[][] arr) {
Integer[][] res = new Integer[arr.length][];
for (int i = 0; i < arr.length; i++) {
res[i] = toWrapper(arr[i]);
}
return res;
}
Long[] toWrapper(long[] arr) {
Long[] res = new Long[arr.length];
for (int i = 0; i < arr.length; i++) {
res[i] = arr[i];
}
return res;
}
Long[][] toWrapper(long[][] arr) {
Long[][] res = new Long[arr.length][];
for (int i = 0; i < arr.length; i++) {
res[i] = toWrapper(arr[i]);
}
return res;
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
public <T> T[] nextIntArray(int n, Function<Integer, T> function, Class<T> c) {
T[] arr = (T[]) Array.newInstance(c, n);
for (int i = 0; i < n; i++) {
arr[i] = function.apply(nextInt());
}
return arr;
}
public long[] nextLongArray(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
public <T> T[] nextLongArray(int n, Function<Long, T> function, Class<T> c) {
T[] arr = (T[]) Array.newInstance(c, n);
for (int i = 0; i < n; i++) {
arr[i] = function.apply(nextLong());
}
return arr;
}
public int[][] nextIntMap(int n, int m) {
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
map[i] = nextIntArray(m);
}
return map;
}
public long[][] nextLongMap(int n, int m) {
long[][] map = new long[n][m];
for (int i = 0; i < n; i++) {
map[i] = nextLongArray(m);
}
return map;
}
public char[][] nextCharMap(int n) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++) {
map[i] = next().toCharArray();
}
return map;
}
public void readColumns(Object[]... columns) {
int n = columns[0].length;
for (Object[] column : columns) {
if (column.length != n) {
throw new RuntimeException("Arrays must have equals lengths");
}
}
for (int i = 0; i < n; i++) {
for (Object[] column : columns) {
column[i] = read(column[i].getClass());
}
}
}
public <T> T read(Class<T> c) {
throw new UnsupportedOperationException("To be implemented");
}
}
}
| quadratic | 1196_D2. RGB Substring (hard version) | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author pandusonu
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
public void solve(int testNumber, InputReader in, PrintWriter out) {
// out.print("Case #" + testNumber + ": ");
int n = in.readInt();
int[] a = in.readIntArray(n);
int[][] sol = new int[n][n];
for (int i = 0; i < n; i++) {
sol[0][i] = a[i];
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
sol[i][j] = sol[i - 1][j] ^ sol[i - 1][j + 1];
}
}
for (int i = 1; i < n; i++) {
for (int j = 0; j < n - i; j++) {
sol[i][j] = Math.max(sol[i][j], Math.max(sol[i - 1][j], sol[i - 1][j + 1]));
}
}
int q = in.readInt();
for (int i = 0; i < q; i++) {
int l = in.readInt() - 1;
int r = in.readInt() - 1;
out.println(sol[r - l][l]);
}
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int read() {
try {
if (curChar >= numChars) {
curChar = 0;
numChars = stream.read(buf);
if (numChars <= 0)
return -1;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return buf[curChar++];
}
public int readInt() {
return (int) readLong();
}
public long readLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
if (c == -1) throw new RuntimeException();
}
boolean negative = false;
if (c == '-') {
negative = true;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += (c - '0');
c = read();
} while (!isSpaceChar(c));
return negative ? (-res) : (res);
}
public int[] readIntArray(int size) {
int[] arr = new int[size];
for (int i = 0; i < size; i++) arr[i] = readInt();
return arr;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
| quadratic | 983_B. XOR-pyramid | CODEFORCES |
import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
import jdk.management.cmm.SystemResourcePressureMXBean;
import java.awt.*;
import java.io.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.util.*;
import java.util.List;
import java.math.*;
public class Newbie {
static InputReader sc = new InputReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
solver s = new solver();
int t = 1;
while (t > 0) {
s.solve();
t--;
}
out.close();
}
/* static class descend implements Comparator<pair1> {
public int compare(pair1 o1, pair1 o2) {
if (o1.pop != o2.pop)
return (int) (o1.pop - o2.pop);
else
return o1.in - o2.in;
}
}*/
static class InputReader {
public BufferedReader br;
public StringTokenizer token;
public InputReader(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream), 32768);
token = null;
}
public String next() {
while (token == null || !token.hasMoreTokens()) {
try {
token = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return token.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static class card {
long a;
int cnt;
int i;
public card(long a, int cnt, int i) {
this.a = a;
this.cnt = cnt;
this.i = i;
}
}
static class ascend implements Comparator<pair> {
public int compare(pair o1, pair o2) {
return o1.a - o2.a;
}
}
static class extra {
static boolean v[] = new boolean[100001];
static List<Integer> l = new ArrayList<>();
static int t;
static void shuffle(long a[]) {
List<Long> l = new ArrayList<>();
for (int i = 0; i < a.length; i++)
l.add(a[i]);
Collections.shuffle(l);
for (int i = 0; i < a.length; i++)
a[i] = l.get(i);
}
static long gcd(long a, long b) {
if (b == 0)
return a;
else
return gcd(b, a % b);
}
static boolean valid(int i, int j, int r, int c) {
if (i >= 0 && i < r && j >= 0 && j < c)
return true;
else
return false;
}
static void seive() {
for (int i = 2; i < 100001; i++) {
if (!v[i]) {
t++;
l.add(i);
for (int j = 2 * i; j < 100001; j += i)
v[j] = true;
}
}
}
static int binary(long a[], long val, int n) {
int mid = 0, l = 0, r = n - 1, ans = 0;
while (l <= r) {
mid = (l + r) >> 1;
if (a[mid] == val) {
r = mid - 1;
ans = mid;
} else if (a[mid] > val)
r = mid - 1;
else {
l = mid + 1;
ans = l;
}
}
return (ans + 1);
}
static long fastexpo(int x, int y) {
long res = 1;
while (y > 0) {
if ((y & 1) == 1) {
res *= x;
}
y = y >> 1;
x = x * x;
}
return res;
}
static long lfastexpo(int x, int y, int p) {
long res = 1;
x = x % p;
while (y > 0) {
if ((y & 1) == 1) {
res = (res * x) % p;
}
y = y >> 1;
x = (x * x) % p;
}
return res;
}
}
static class pair {
int a;
int b;
public pair(int a, int i) {
this.a = a;
this.b = i;
}
}
static class pair1 {
pair p;
int in;
public pair1(pair a, int n) {
this.p = a;
this.in = n;
}
}
static long m = (long) 1e9 + 7;
static class solver {
void solve() {
int n = sc.nextInt();
int ans=0;
int a[]=new int[2*n];
for (int i = 0; i < 2 * n; i++) {
a[i]=sc.nextInt();
}
for(int i=0;i<2*n;i++)
{
if(a[i]>0)
{
int j=0;
for(j=i+1;a[i]!=a[j];j++)
{
if(a[j]>0)
ans++;
}
a[j]=0;
}
}
System.out.println(ans);
}
}
} | quadratic | 995_B. Suit and Tie | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class LogicalExpression {
int N = 256;
void solve() {
Expression[] E = new Expression[N];
for (int i = 0; i < N; i++) E[i] = new Expression();
E[Integer.parseInt("00001111", 2)].update_f("x");
E[Integer.parseInt("00110011", 2)].update_f("y");
E[Integer.parseInt("01010101", 2)].update_f("z");
for (int l = 2; l < 40; l++) {
for (int i = 0; i < N; i++) {
for (int j = 0; j < N; j++) {
if (E[i].e != null && E[j].t != null && E[i].e.length() + E[j].t.length() + 1 == l) {
E[i | j].update_e(E[i].e + '|' + E[j].t);
}
if (E[i].t != null && E[j].f != null && E[i].t.length() + E[j].f.length() + 1 == l) {
E[i & j].update_t(E[i].t + '&' + E[j].f);
}
}
if (E[i].f != null) E[i ^ (N - 1)].update_f('!' + E[i].f);
}
}
String[] res = new String[N];
for (int i = 0; i < N; i++) res[i] = E[i].calc_best();
int n = in.nextInt();
for (int i = 0; i < n; i++) {
int x = Integer.parseInt(in.nextToken(), 2);
out.println(res[x]);
}
}
static class Expression {
String e, t, f;
Expression() {
}
public Expression(String e, String t, String f) {
this.e = e;
this.t = t;
this.f = f;
}
String calc_best() {
String best = e;
if (compare(best, t) > 0) best = t;
if (compare(best, f) > 0) best = f;
return best;
}
void update_e(String ne) {
if (e == null || compare(e, ne) > 0) {
e = ne;
update_f('(' + e + ')');
}
}
void update_t(String nt) {
if (t == null || compare(t, nt) > 0) {
t = nt;
update_e(t);
}
}
void update_f(String nf) {
if (f == null || compare(f, nf) > 0) {
f = nf;
update_t(f);
}
}
int compare(String a, String b) {
if (a.length() != b.length()) return Integer.compare(a.length(), b.length());
return a.compareTo(b);
}
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new LogicalExpression().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| quadratic | 913_E. Logical Expression | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author gaidash
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, OutputWriter out) {
final int SIZE = 256;
final int UNDEF = -1;
int nPixels = in.nextInt();
int groupSize = in.nextInt();
int[] a = in.nextIntArray(nPixels);
boolean[] exists = new boolean[SIZE];
int[] left = new int[SIZE];
int[] right = new int[SIZE];
int[] ret = new int[nPixels];
Arrays.fill(ret, UNDEF);
for (int i = 0; i < nPixels; i++) {
for (int p = 0; p < SIZE; p++) {
if (exists[p] && left[p] <= a[i] && a[i] <= right[p]) {
ret[i] = left[p];
left[a[i]] = left[p];
right[a[i]] = right[p];
break;
}
}
if (ret[i] == UNDEF) {
int l = Math.max(a[i] - groupSize + 1, 0);
int r = l + groupSize - 1;
for (int p = a[i] - 1; p >= 0; p--) {
if (exists[p]) {
if (p >= l) {
int d = p - l;
l = p + 1;
r += d + 1;
}
if (right[p] >= l) {
right[p] = l - 1;
}
}
}
for (int p = a[i] + 1; p < SIZE; p++) {
if (exists[p] && left[p] <= r) {
r = left[p] - 1;
}
}
left[a[i]] = l;
right[a[i]] = r;
ret[i] = l;
}
exists[a[i]] = true;
}
// for (int p : a) {
// System.out.println("Segment for pixel " + p + " = " + "(" + left[p] + " , " + right[p] + ")");
// }
out.print(ret);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(array[i]);
}
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| quadratic | 980_C. Posterized | CODEFORCES |
import java.io.*;
import java.util.*;
public class Codeforces913F {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int a = input.nextInt();
int b = input.nextInt();
input.close();
final int mod = 998244353;
int frac = multiply(a, inverse(b, mod), mod);
int reverse = (mod+1-frac)%mod;
int[] fracpower = new int[n+1];
int[] reversepower = new int[n+1];
fracpower[0] = 1;
reversepower[0] = 1;
for (int i = 1; i <= n; i++) {
fracpower[i] = multiply(fracpower[i-1], frac, mod);
reversepower[i] = multiply(reversepower[i-1], reverse, mod);
}
int[][] dp1 = new int[n+1][n+1];
dp1[2][1] = 1;
for (int i = 3; i <= n; i++) {
for (int j = 1; j < i; j++) {
if (j == 1) {
dp1[i][j] = fracpower[i-1];
}
else {
dp1[i][j] = multiply(dp1[i-1][j-1], fracpower[i-j], mod);
}
if (j == i-1) {
dp1[i][j] += reversepower[i-1];
dp1[i][j] %= mod;
}
else {
dp1[i][j] += multiply(dp1[i-1][j], reversepower[j], mod);
dp1[i][j] %= mod;
}
}
}
int[][] dp2 = new int[n+1][n+1];
dp2[1][1] = 1;
dp2[2][1] = 1;
dp2[2][2] = 0;
for (int i = 3; i <= n; i++) {
int val = 0;
for (int j = 1; j < i; j++) {
dp2[i][j] = multiply(dp2[j][j], dp1[i][j], mod);
val += dp2[i][j];
val %= mod;
}
dp2[i][i] = (mod+1-val)%mod;
}
/*for (int i = 2; i <= n; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(dp2[i][j] + " ");
}
System.out.println();
}*/
int[] EV = new int[n+1];
EV[1] = 0;
EV[2] = 1;
for (int i = 3; i <= n; i++) {
int val = 0;
for (int j = 1; j < i; j++) {
int r = j*(i-j) + (j*(j-1))/2 + EV[i-j] + EV[j];
r %= mod;
val += multiply(dp2[i][j], r, mod);
val %= mod;
}
val += multiply((i*(i-1))/2, dp2[i][i], mod);
val %= mod;
int s = (mod+1-dp2[i][i])%mod;
EV[i] = multiply(val, inverse(s, mod), mod);
}
System.out.println(EV[n]);
}
public static int multiply(int a, int b, int mod) {
long x = (long)a*(long)b;
return (int) (x%mod);
}
public static int inverse (int a, int n) {
int m = n;
int r1 = 1;
int r2 = 0;
int r3 = 0;
int r4 = 1;
while ((a > 0) && (n > 0)) {
if (n >= a) {
r3 -= r1*(n/a);
r4 -= r2*(n/a);
n = n%a;
}
else {
int tmp = a;
a = n;
n = tmp;
tmp = r1;
r1 = r3;
r3 = tmp;
tmp = r2;
r2 = r4;
r4 = tmp;
}
}
if (a == 0) {
if (r3 >= 0)
return (r3%m);
else
return (m+(r3%m));
}
else {
if (r1 >= 0)
return (r1%m);
else
return (m+(r1%m));
}
}
} | quadratic | 913_F. Strongly Connected Tournament | CODEFORCES |
import java.util.*;
import java.lang.*;
import java.io.*;
public class java2 {
public static void main(String[] args) {
Scanner r = new Scanner(System.in);
int n=r.nextInt();
int []l=new int[1005];
int []ri=new int[1005];
int []candy=new int[1005];
for(int i=1;i<=n;++i)
{
l[i]=r.nextInt();
}
for(int i=1;i<=n;++i)
{
ri[i]=r.nextInt();
}
for(int i=1;i<=n;++i)
{
if(l[i]>i-1||ri[i]>n-i)
{
System.out.println("NO");
System.exit(0);
}
candy[i]=n-l[i]-ri[i];
}
for(int i=1;i<=n;++i)
{
int left=0,right=0;
for(int j=1;j<=i-1;++j)
{
if(candy[j]>candy[i])
{
++left;
}
}
for(int j=i+1;j<=n;++j)
{
if(candy[j]>candy[i])
{
++right;
}
}
if(left!=l[i]||right!=ri[i])
{
System.out.println("NO");
System.exit(0);
}
}
System.out.println("YES");
for(int i=1;i<=n;++i)
{
System.out.print(candy[i]+" ");
}
}
}
| quadratic | 1054_C. Candies Distribution | CODEFORCES |
import java.io.*;
import java.util.*;
public class Solution{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String args[] ) {
FastReader sc = new FastReader();
int n = sc.nextInt();
int m = sc.nextInt();
int[] arr = new int[105];
for(int i=0;i<m;i++){
int a = sc.nextInt();
arr[a]++;
}
for(int i=1;i<=1000;i++){
int sum=0;
for(int a:arr){
if(a!=0){
sum+=(a/i);
}
}
if(sum<n){
System.out.println(i-1);
return;
}
}
}
}
| quadratic | 1011_B. Planning The Expedition | CODEFORCES |
import java.io.*;
import java.util.*;
public class Solution{
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String args[] ) {
FastReader sc = new FastReader();
int n = sc.nextInt();
int m = sc.nextInt();
int[] arr = new int[105];
for(int i=0;i<m;i++){
int a = sc.nextInt();
arr[a]++;
}
for(int i=1;i<=1000;i++){
int sum=0;
for(int a:arr){
if(a!=0){
sum+=(a/i);
}
}
if(sum<n){
System.out.println(i-1);
return;
}
}
}
}
| quadratic | 1011_B. Planning The Expedition | CODEFORCES |
//package codeforces;
import java.util.Scanner;
public class ex5 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String S [] = new String[3];
int m=0,s=0,p=0;
int temp=0;
for (int i = 0; i < S.length; i++) {
S[i]=scan.next();
if(S[i].indexOf('m')!=-1) m++;
if(S[i].indexOf('s')!=-1) s++;
if(S[i].indexOf('p')!=-1) p++;
}
int n1 = Integer.parseInt(S[0].substring(0,1));
int n2 = Integer.parseInt(S[1].substring(0,1));
int n3 = Integer.parseInt(S[2].substring(0,1));
int d3 = Math.abs(n1-n2);
int d4 = Math.abs(n1-n3);
int d5 = Math.abs(n2-n3);
if(m==3||s==3||p==3) {
if(d3==1&d5==1&d4==2||d3==1&d4==1&d5==2||d5==1&d4==1&d3==2)
System.out.println(0);
else
if(d3==0&d4==0) System.out.println(0);
else
if(d3<d5&d3<d4) {
if(d3==1||d3==2||d3==0) System.out.println(1);
else System.out.println(2);
}
else if (d5<d4&d5<d3){
if(d5==1||d5==2||d5==0) System.out.println(1);
else System.out.println(2);
}
else if(d4<d5&d4<d3) {
if(d4==1||d4==2||d4==0) System.out.println(1);
else System.out.println(2);
}
else if(d3==2&d5==2||d4==2&d5==2||d3==2&d4==2||d3==1&d5==1||d4==1&d5==1||d3==2&d4==1)
System.out.println(1);
else System.out.println(2);
}
if(m==2||s==2||p==2) {
char c1 = S[0].charAt(1);
char c2 = S[1].charAt(1);
char c3 = S[2].charAt(1);
if(c1==c2) {
if(n1==n2) System.out.println(1);
else if(d3==1||d3==2) System.out.println(1);
else System.out.println(2);
}
if(c1==c3) {
if(n1==n3) System.out.println(1);
else if(d4==1||d4==2) System.out.println(1);
else System.out.println(2);
}
if(c2==c3) {
if(n2==n3) System.out.println(1);
else if(d5==1||d5==2) System.out.println(1);
else System.out.println(2);
}
}
if(m==1&s==1&p==1) System.out.println(2);
}
}
| constant | 1191_B. Tokitsukaze and Mahjong | CODEFORCES |
import java.io.PrintWriter;
import java.util.*;
import java.util.Arrays ;
import java .lang.String.* ;
import java .lang.StringBuilder ;
public class Test{
static int pos = 0 ;
static int arr[] ;
static LinkedList l1 = new LinkedList() ;
static void find(int p ,char[]x,int put[],String s){
int c= 0 ;
for (int i = 0; i < s.length(); i++) {
if(x[p]==s.charAt(i)){
c++ ; }
}
put[p] = c ;
}
static int mode(int m ,int[]x ){
int temp = 0 ;
for (int i = x.length-1; i >=0; i--) {
if(x[i]<=m){
temp= x[i] ;
/// break ;
return m-temp ;
}
}
return m-temp ;
}
static int mode2(int m ,int[]x ){
int temp = 0 ;
for (int i = x.length-1; i >=0; i--) {
if(x[i]<=m){
temp= x[i] ;
/// break ;
return x[i] ;
}
}
return 0 ;
}
static int find(int x[],int temp){
int j = 0 ;
for (int i = x.length-1; i >=0; i--) {
if(x[i]==temp) return j+1 ;
j++ ;
}
return -1 ;
}
static String ch(long[]x,long b){
for (int i = 0; i < x.length; i++) {
if(x[i]==b)return "YES" ;
}
return "NO" ;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in) ;
PrintWriter pw = new PrintWriter(System.out);
int k=in.nextInt(), n=in.nextInt(), s=in.nextInt(), p=in.nextInt() ;
int paper =n/s;
if(n%s!=0) paper++ ;
paper*=k ;
int fin = paper/p ;
if(paper%p!=0) fin++ ;
System.out.println( fin );
}
}
| constant | 965_A. Paper Airplanes | CODEFORCES |
import java.util.*;
import java.io.*;
import java.math.*;
public class loser
{
static class InputReader {
public BufferedReader br;
public StringTokenizer token;
public InputReader(InputStream stream)
{
br=new BufferedReader(new InputStreamReader(stream),32768);
token=null;
}
public String next()
{
while(token==null || !token.hasMoreTokens())
{
try
{
token=new StringTokenizer(br.readLine());
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
return token.nextToken();
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
}
static class card{
long a;
int i;
public card(long a,int i)
{
this.a=a;
this.i=i;
}
}
static class sort implements Comparator<pair>
{
public int compare(pair o1,pair o2)
{
if(o1.a!=o2.a)
return (int)(o1.a-o2.a);
else
return (int)(o1.b-o2.b);
}
}
static void shuffle(long a[])
{
List<Long> l=new ArrayList<>();
for(int i=0;i<a.length;i++)
l.add(a[i]);
Collections.shuffle(l);
for(int i=0;i<a.length;i++)
a[i]=l.get(i);
}
/*static long gcd(long a,long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}*/
/*static boolean valid(int i,int j)
{
if(i<4 && i>=0 && j<4 && j>=0)
return true;
else
return false;
}*/
static class pair{
int a,b;
public pair(int a,int b)
{
this.a=a;
this.b=b;
}
}
public static void main(String[] args)
{
InputReader sc=new InputReader(System.in);
int k=sc.nextInt();
int n=sc.nextInt();
int s=sc.nextInt();
int p=sc.nextInt();
long d=(long)Math.ceil((double)n/s);
if(d==0)
d=1;
d=k*d;
long ans=(long)Math.ceil((double)d/p);
System.out.println(ans);
}
} | constant | 965_A. Paper Airplanes | CODEFORCES |
import java.io.*;
import java.util.*;
public class Lcm
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw=new PrintWriter(System.out);
long n=Long.parseLong(br.readLine());
if(n<=2)
pw.println(n);
else
{
if(n%6==0)
{
pw.println(((n-1)*(n-2)*(n-3)));
}
else if(n%2==0)
{
pw.println((n*(n-1)*(n-3)));
}
else
{
pw.println((n*(n-1)*(n-2)));
}
}
pw.flush();
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
public class Task235A {
public static void main(String... args) throws NumberFormatException,
IOException {
Solution.main(System.in, System.out);
}
static class Scanner {
private final BufferedReader br;
private String[] cache;
private int cacheIndex;
Scanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
cache = new String[0];
cacheIndex = 0;
}
int nextInt() throws IOException {
if (cacheIndex >= cache.length) {
cache = br.readLine().split(" ");
cacheIndex = 0;
}
return Integer.parseInt(cache[cacheIndex++]);
}
long nextLong() throws IOException {
if (cacheIndex >= cache.length) {
cache = br.readLine().split(" ");
cacheIndex = 0;
}
return Long.parseLong(cache[cacheIndex++]);
}
String next() throws IOException {
if (cacheIndex >= cache.length) {
cache = br.readLine().split(" ");
cacheIndex = 0;
}
return cache[cacheIndex++];
}
void close() throws IOException {
br.close();
}
}
static class Solution {
public static void main(InputStream is, OutputStream os)
throws NumberFormatException, IOException {
PrintWriter pw = new PrintWriter(os);
Scanner sc = new Scanner(is);
long n = sc.nextInt();
if (n < 3) {
pw.println(n);
} else {
if (n % 2 != 0) {
pw.println(n * (n - 1) * (n - 2));
} else {
if (n % 3 != 0) {
pw.println(n * (n - 1) * (n - 3));
} else {
long cand1 = n * (n - 1) * (n - 2) / 2;
long cand2 = (n - 1) * (n - 2) * (n - 3);
pw.println(Math.max(cand1, cand2));
}
}
}
pw.flush();
sc.close();
}
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
public static long gcd(long a, long b)
{
return b==0? a:gcd(b, a%b);
}
public static long lcm(long a, long b, long c)
{
long d=a/gcd(a, b)*b;
return c/gcd(c, d)*d;
}
public static long max(long a, long b)
{
return a>b? a:b;
}
public static void main(String[] args)
{
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
long n=in.nextLong();
if(n<=2)
out.println(n);
else
out.println(max(lcm(n, n-1, n-2), max(lcm(n, n-1, n-3), lcm(n-1, n-2, n-3))));
out.close();
}
}
class InputReader
{
BufferedReader buf;
StringTokenizer tok;
InputReader()
{
buf = new BufferedReader(new InputStreamReader(System.in));
}
boolean hasNext()
{
while(tok == null || !tok.hasMoreElements())
{
try
{
tok = new StringTokenizer(buf.readLine());
}
catch(Exception e)
{
return false;
}
}
return true;
}
String next()
{
if(hasNext())
return tok.nextToken();
return null;
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
BigInteger nextBigInteger()
{
return new BigInteger(next());
}
BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.*;
/*Author LAVLESH*/
public class solution
{
static long gcd(long a,long b){
if(b==0) return a;
else
return gcd(b,a%b);
}
public static void main(String[]args){
Scanner in=new Scanner(System.in);
long n=in.nextLong();
long m1=0,m2=0;
if(n<3)m1=n;
else {
if((n&1)==1){
long lcm=n*(n-1)/gcd(n,n-1);
m1=lcm*(n-2)/gcd(lcm,n-2);
}
else{
long lcm=(n-1)*(n-2)/gcd(n-1,n-2);
m1=lcm*(n-3)/gcd(lcm,n-3);
lcm=n*(n-1)/gcd(n,n-1);
m2=lcm*(n-3)/gcd(lcm,n-3);
m1 = Math.max(m1,m2);
}
}
System.out.println(m1);
}}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static long GCF(long a, long b) {
if(b == 0) return a;
else return GCF(b, a%b);
}
public static long LCM(long a, long b){
return a*b / GCF(a, b);
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
if(n < 3) System.out.println(n);
else{
long t1 = LCM(n, n-1);
long t2 = LCM(n-2, n-3);
long l1 = LCM(t1, n-2);
long l2 = LCM(t1, n-3);
long l3 = LCM(n, t2);
long l4 = LCM(n-1, t2);
System.out.println(Math.max(l1, Math.max(l2, Math.max(l3, l4))));
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
//package NumberTheory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
*
* @author Sourav Kumar Paul
*/
public class A235 {
public static void main(String[] args) throws IOException{
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
long n = Long.parseLong(reader.readLine());
//int gcd = gcd(924,923);
//System.out.println(gcd);
// System.out.println(gcd(923,461));
if(n<=2)
System.out.println(n);
else if(n==3)
System.out.println("6");
else if(n % 2== 0)
{
if(n % 3 == 0)
{
System.out.println((n-3)*(n-1)*(n-2));
}
else
System.out.println(n * (n-1) * (n-3) );
}
else
System.out.println(n*(n-1)*(n-2));
}
private static int gcd(int i, int j) {
int a = Math.min(i,j);
int b = Math.max(i,j);
while(a != 0)
{
int temp = b % a;
b = a;
a = temp;
}
return b;
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.math.BigInteger;
import java.util.Scanner;
public class A235 {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long n = scan.nextInt();
BigInteger res = null;
if (n >= 3) {
if (n % 2 != 0) {
res = BigInteger.valueOf(n * (n - 1) * (n - 2));
} else if (n % 3 == 0) {
res = BigInteger.valueOf((n - 1) * (n - 2) * (n - 3));
} else {
res = BigInteger.valueOf(n * (n - 1) * (n - 3));
}
} else {
res = BigInteger.valueOf(n);
}
System.out.println(res);
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class LCM {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long n = scan.nextLong();
if (n <= 2)
System.out.println(n);
else if (n % 2 == 1)
System.out.println(n * (n - 1) * (n - 2));
else if (n % 3 == 0)
System.out.println((n - 1) * (n - 2) * (n - 3));
else
System.out.println(n * (n - 1) * (n - 3));
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class A235 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner sc = new Scanner(System.in);
long n = sc.nextInt();
if (n == 1) {
System.out.println(1);
return;
} else if (n == 2) {
System.out.println(2);
return;
} else if (n == 3) {
System.out.println(6);
return;
}
if (n % 2 == 0) {
if(n % 3 == 0)
System.out.println((n - 1) * (n - 2) * (n - 3));
else
System.out.println((n - 1) * n * (n - 3));
} else {
System.out.println(n * (n - 1) * (n - 2));
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.*;
public class Main
{
public static void main(String[]args)
{
Scanner input=new Scanner (System.in);
while(input.hasNext())
{
long n=input.nextLong();
if(n==1||n==2)
System.out.println(n);
else if(n%2==1)
System.out.println(n*(n-1)*(n-2));
else
if(n%3!=0)
System.out.println(Math.max(n*(n-1)*(n-3),n*(n-1)*(n-2)/2));
else
System.out.println(Math.max( Math.max(n*(n-1)*(n-3)/3,n*(n-1)*(n-2)/2) , Math.max((n-2)*(n-1)*(n-3),n*(n-2)*(n-3)/6) ));
}
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
long num=in.nextLong();
long lcm=1;
if(num==2){
System.out.println(2);
System.exit(0);
}//End if
else if(num%2==0&&num%3!=0)
lcm=(num)*(num-1)*(num-3);
else if(num%2==0&&num%3==0)
lcm=(num-1)*(num-2)*(num-3);
else if(num%2!=0&&num>2)
lcm=num*(num-1)*(num-2);
System.out.println(lcm);
}//End main()
}//End class
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
long num = input.nextLong();
if(num==0){
System.out.println(num);
}else if(num==1||num==2){
System.out.println(num);}
else if(num%2==0&&num>2&&num%3!=0){
System.out.println(num*(num-1)*(num-3));
}
else if(num%2==0&&num%3==0){
System.out.println((num-1)*(num-2)*(num-3));
}
else{
System.out.println(num*(num-1)*(num-2));}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.BufferedInputStream;
import java.util.Scanner;
/**
* Created by jizhe on 2016/1/29.
*/
public class LCMChallenge {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedInputStream(System.in));
long N = in.nextLong();
if( N == 1 || N == 2 )
{
System.out.printf("%d\n", N);
return;
}
if( (N&1) == 1 )
{
long lcm = N*(N-1)*(N-2);
System.out.printf("%d\n", lcm);
}
else
{
if( N == 4 )
{
System.out.printf("12\n");
}
else
{
long lcm;
if( N%3 == 0 )
{
lcm = (N-1)*(N-2)*(N-3);
}
else
{
lcm = N*(N-1)*(N-3);
}
System.out.printf("%d\n", lcm);
}
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.*;
public class maximus {
static long GCD(long a,long b){
if(b==0)return a;
return GCD(b,a%b);
}
public static void main(String [] args){
Scanner in=new Scanner(System.in);
long n=in.nextInt();
if(n<=2){
System.out.print(n);
return;
}
if(n%2==1){
System.out.print((n*(n-1)*(n-2)));
return;
}
if(n%2==0 && n<=6){
System.out.print(n*(n-1)*(n-2)/2);
return;
}
long temp=(n*(n-1)*(n-3))/GCD(n,n-3);
System.out.print(Math.max((n-1)*(n-2)*(n-3),temp));
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
long n;
long maxlcm;
void run(){
n = cin.nextInt();
if(n == 1 || n ==2)
maxlcm = n;
else if(n >= 3){
if(n % 2 != 0){
maxlcm = n * (n-1) * (n - 2);
}
else if(n%3 != 0)
maxlcm = n * (n - 1) * (n - 3);
else maxlcm = (n - 1) * (n - 2) * (n - 3);
}
System.out.println(maxlcm);
}
public static void main(String[] args) {
new Main().run();
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class LCM {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n=sc.nextLong();
if(n < 3) {
System.out.println(n);
}
else if(n % 2 != 0) {
System.out.println(n * (n-1) * (n-2));
}
else if(n % 3 == 0) {
System.out.println((n-1) * (n-2) * (n-3));
}
else {
System.out.println(n * (n-1) * (n-3));
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
long x = input.nextLong();
if(x==1||x==2){System.out.println(x);
}
else if(x%2==0&&x>2&&x%3!=0){
System.out.println((x)*(x-1)*(x-3));
}else if(x%2==0&&x%3==0){
System.out.println((x-1)*(x-2)*(x-3));
}
else {System.out.println(x*(x-1)*(x-2));}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.*;
import java.util.*;
public class LCMChallenge {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
long n = Long.parseLong(f.readLine());
if (n == 1 || n == 2)
System.out.println(n);
else if (n % 2 == 1)
System.out.println(n*(n-1)*(n-2));
else
{
long prod = n*(n-1);
long x = n-2;
while (x > 0 && gcd(n,x) > 1 || gcd(n-1,x) > 1)
x--;
prod *= x;
if ((n-1)*(n-2)*(n-3) > prod)
prod = (n-1)*(n-2)*(n-3);
System.out.println(prod);
}
}
public static long gcd(long a, long b)
{
if (b == 0)
return a;
return gcd(b, a%b);
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class test5{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
long x=in.nextLong();
if(x>=3){
if(x%2!=0)
System.out.println(x*(x-1)*(x-2));
else if(x%3==0)
System.out.println((x-3)*(x-1)*(x-2));
else
System.out.println(x*(x-1)*(x-3));
}
else System.out.println(x);
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.*;
import java.lang.*;
import java.io.*;
public class LCM {
public static long gcd(long a,long b) {
while(true) {
a=a%b;
if (a==0) return b;
b=b%a;
if (b==0) return a;
}
}
public static void main (String[] args) throws java.lang.Exception {
Scanner in=new Scanner(System.in);
long n=in.nextInt();
if (n>2) {
if (gcd(n,n-2)>1) {
if (gcd(n,n-3)>1) {
System.out.println((n-1)*(n-2)*(n-3));
}
else System.out.println(n*(n-1)*(n-3));
}
else System.out.println(n*(n-1)*(n-2));
}
else System.out.println(n);
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.*;
import java.util.*;
import java.math.*;
import static java.lang.Math.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Double.parseDouble;
import static java.lang.String.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//(new FileReader("input.in"));
StringBuilder out = new StringBuilder();
StringTokenizer tk;
//PrintWriter pw = new PrintWriter("output.out", "UTF-8");
long n = parseLong(in.readLine());
if(n <= 2) System.out.println(n);
else if(n%2 == 1)System.out.println(n*(n-1)*(n-2));
else {
long ans = (n-1)*(n-2)*(n-3);
if(gcd(n*(n-1),n-3)==1) ans = max(ans, n*(n-1)*(n-3));
System.out.println(ans);
}
}
static long gcd(long a,long b) {
return b==0 ? a : gcd(b, a%b);
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
public static Scanner scan = new Scanner(System.in);
public static boolean bg = true;
public static void main(String[] args) throws Exception {
long n1 = Integer.parseInt(scan.next());
if (n1==1){
System.out.println(1);
System.exit(0);
}
if (n1==2){
System.out.println(2);
System.exit(0);
}
if (n1==3){
System.out.println(6);
System.exit(0);
}
if (n1%2==0){
if (n1%3==0){
n1-=1;
n1 = n1*(n1-1)*(n1-2);
System.out.println(n1);
}
else {
n1 = n1*(n1-1)*(n1-3);
System.out.println(n1);
}
}
else {
n1 = n1*(n1-1)*(n1-2);
System.out.println(n1);
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class Prob235A {
public static void main(String[] Args) {
Scanner scan = new Scanner(System.in);
int x = scan.nextInt();
if (x < 3) {
if (x == 1)
System.out.println(1);
else
System.out.println(2);
} else {
long answer = x;
if (x % 2 == 1) {
answer *= x - 1;
answer *= x - 2;
} else if (x % 3 != 0) {
answer *= x - 1;
answer *= x - 3;
} else {
answer = x - 1;
answer *= x - 2;
answer *= x - 3;
}
System.out.println(answer);
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.PrintStream;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Reader;
import java.io.Writer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Nipuna Samarasekara
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
/////////////////////////////////////////////////////////////
public void solve(int testNumber, FastScanner in, FastPrinter out) {
int n=in.nextInt();
if(n==1||n==2){
out.println(n);
return;
}
if(n==4){
out.println(12);
return;
}
long nn=n;
if(n%2==1){
long ans=nn*(nn-1)*(nn-2);
out.println(ans);
}
else if(n%3==0){
nn--;
long ans=nn*(nn-1)*(nn-2);
out.println(ans);
}
else {
long ans=nn*(nn-1)*(nn-3);
out.println(ans);
}
}
}
class FastScanner extends BufferedReader {
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
// if (isEOF && ret < 0) {
// throw new InputMismatchException();
// }
// isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}
static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}
public int nextInt() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c
+ " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}
public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}
}
class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}
public FastPrinter(Writer out) {
super(out);
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.*;
import java.util.*;
public class Lcm
{
public static void main(String args[])throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
long n=Long.parseLong(br.readLine());
if(n<=2)
System.out.println(n);
else
{
if(n%6==0)
{
System.out.println(((n-1)*(n-2)*(n-3)));
}
else if(n%2==0)
{
System.out.println((n*(n-1)*(n-3)));
}
else
{
System.out.println((n*(n-1)*(n-2)));
}
}
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.*;
public class LCM235A
{
public static void main(String[] args)
{
// Set up scanner
Scanner sc = new Scanner(System.in);
// System.out.println("Enter n");
long n = sc.nextLong();
if (n==1)
{
System.out.println(1);
return;
}
if (n==2)
{
System.out.println(2);
return;
}
if (n==3)
{
System.out.println(6);
return;
}
if (n==4)
{
System.out.println(12);
return;
}
if (n%2 ==1) // Odd number easy
{
System.out.println(n*(n-1)*(n-2));
return;
}
// Even number is a bit harder
if (n%3 == 0)
{
System.out.println((n-1)*(n-2)*(n-3));
}
else
{
System.out.println(n*(n-1)*(n-3));
}
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author walker
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long n = in.readInt();
if(n < 3){
out.print(n);
}
else if(n % 2 != 0) {
out.print(n * (n-1) * (n-2));
}
else if(n % 3 == 0) {
out.print((n-1) * (n-2) * (n-3));
}
else {
out.print(n * (n-1) * (n-3));
}
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.BufferedInputStream;
import java.util.Scanner;
/**
* Created by jizhe on 2016/1/29.
*/
public class LCMChallenge {
public static void main(String[] args) {
Scanner in = new Scanner(new BufferedInputStream(System.in));
long N = in.nextLong();
if( N == 1 || N == 2 )
{
System.out.printf("%d\n", N);
return;
}
if( (N&1) == 1 )
{
long lcm = N*(N-1)*(N-2);
System.out.printf("%d\n", lcm);
}
else
{
long lcm;
if( N%3 == 0 )
{
lcm = (N-1)*(N-2)*(N-3);
}
else
{
lcm = N*(N-1)*(N-3);
}
System.out.printf("%d\n", lcm);
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Long N = sc.nextLong();
Long ans;
sc.close();
if(N <= 2)
System.out.println(N);
else{
if(N % 6 == 0){
ans = (N - 1) * (N - 2) * (N - 3);}
else if(N % 2 == 0){
ans = N * (N - 1) * (N - 3);
}
else{
ans = N * (N - 1) * (N - 2);
}
System.out.println(ans);
}
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class Solution {
public static void main(String [] args){
Scanner stdin = new Scanner(System.in);
long n = stdin.nextLong();
if(n<3) System.out.println(n);
else {
if(n%2==0){
long a=0,b=0;
if(n%3!=0) a = (n*(n-1)*(n-3));
n--;
b = (n*(n-1)*(n-2));
System.out.println(Math.max(a, b));
}
else System.out.println(n*(n-1)*(n-2));
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main {
Scanner cin = new Scanner(new BufferedInputStream(System.in));
long n;
long maxlcm;
void run(){
n = cin.nextInt();
if(n == 1 || n ==2)
maxlcm = n;
else if(n >= 3){
if(n % 2 != 0){
maxlcm = n * (n-1) * (n - 2);
}
else if(n%3 != 0)
maxlcm = n * (n - 1) * (n - 3);
else maxlcm = (n - 1) * (n - 2) * (n - 3);
}
System.out.println(maxlcm);
}
public static void main(String[] args) {
new Main().run();
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
if (n == 1 || n == 2) {
System.out.println(n);
} else if (n % 2 == 0) {
if (n % 3 == 0)
System.out.println((n - 1) * (n - 2) * (n - 3));
else
System.out.println(n * (n - 1) * (n - 3));
} else {
System.out.println(n * (n - 1) * (n - 2));
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.PrintStream;
import java.util.Scanner;
/**
* @author Roman Elizarov
*/
public class Round_146_A {
public static void main(String[] args) {
new Round_146_A().go();
}
void go() {
// read input
Scanner in = new Scanner(System.in);
int n = in.nextInt();
// solve
long result = solve(n);
// write result
PrintStream out = System.out;
out.println(result);
}
static long solve(int n) {
if (n == 1)
return 1;
if (n == 2)
return 2;
if (n % 2 == 0) {
if (n % 3 == 0)
return (long)(n - 1) * (n - 2) * (n - 3);
else
return (long)n * (n - 1) * (n - 3);
} else
return (long)n * (n - 1) * (n - 2);
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
//inputs
long n = in.nextLong();
if(n == 1)
System.out.println(1);
else if(n == 2)
System.out.println(2);
else if(n % 2 == 0){
int cnt = nPrime(n);
if(cnt == 1)
System.out.println((n) * (n-1) * (n-3));
else if(cnt > 1)
System.out.println((n-1) * (n-2) * (n-3));
}
else
System.out.println((n) * (n-1) * (n-2));
}
public static int nPrime(long n){
int cnt=1;
if(n % 3 == 0)
cnt++;
return cnt;
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.*;
import java.util.*;
public class lcm {
static int n;
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader in = new BufferedReader(new FileReader("lcm.in"));
n = Integer.parseInt(in.readLine());
System.out.println(lcm(n));
in.close();
}
static long lcm(int n) {
if (n == 1) return 1;
if (n == 2) return 2;
if(n%6==0) {
long ans = n-1;
ans *= n-2;
ans *= n-3;
return ans;
}
if (n%2==0) {
long ans = n;
ans *= n-1;
ans *= n-3;
return ans;
}
long ans = n;
ans *= n-1;
ans *= n-2;
return ans;
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
/** Oct 21, 2012 **/
import java.util.InputMismatchException;
import java.util.LinkedList;
/**
* @author DOAN Minh Quy
* @email [email protected]
*/
public class C236 {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new C236().run();
}
void run() {
InputScanner scanner = new InputScanner(System.in);
PrintStream printer = new PrintStream(System.out);
int n = scanner.nextInt();
long answer;
if ( n == 1 ){
answer = 1;
}else if ( n == 2 ){
answer = 2;
}else{
if ( (n & 1) != 0 ){
answer = (long)n * (long)(n-1) * (long)(n-2);
}else{
if ( n % 3 == 0 ){
answer = (long)(n-1) * (long)(n-2) * (long)(n-3);
}else{
answer = (long)(n) * (long)(n-1) * (long)(n-3);
}
}
}
printer.println(answer);
}
class InputScanner{
BufferedInputStream bis;
byte[] buffer = new byte[1024];
int currentChar;
int charCount;
public InputScanner(InputStream stream){
bis = new BufferedInputStream(stream);
}
public byte read() {
if (charCount == -1)
throw new InputMismatchException();
if (currentChar >= charCount) {
currentChar = 0;
try {
charCount = bis.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (charCount <= 0)
return -1;
}
return buffer[currentChar++];
}
public int nextInt(){
int c = read();
while (isSpaceChar(c)){
c = read();
}
int sign = 1;
if (c == '-') {
sign = -1;
c = read();
}
int rep = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
rep *= 10;
rep += c - '0';
c = read();
} while (!isSpaceChar(c));
return rep * sign;
}
public long nextLong(){
int c = read();
while (isSpaceChar(c)){
c = read();
}
int sign = 1;
if (c == '-') {
sign = -1;
c = read();
}
long rep = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
rep *= 10;
rep += c - '0';
c = read();
} while (!isSpaceChar(c));
return rep * (long)sign;
}
public String next(){
char c = (char)read();
while (isSpaceChar(c)){
c = (char)read();
}
StringBuilder build = new StringBuilder();
do{
build.append(c);
c = (char)read();
}while(!isSpaceChar(c));
return build.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public void close(){
try {
bis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.*;
import java.io.*;
public class LCMChallenge
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if(n < 3)
{
System.out.println(n);
}
else if(n % 2 == 1)
{
System.out.println((long)n * (n - 1) * (n - 2));
}
else
{
if(n % 3 != 0)
{
System.out.println((long)n * (n - 1) * (n - 3));
}
else
{
System.out.println((long)(n - 1) * (n - 2) * (n - 3));
}
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.math.BigInteger;
public class A235 {
public static void main(String args[]) throws Exception{
BufferedReader ip = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(ip.readLine());
int a,b,c;
int x = 0,y = 0,z = 0;
BigInteger l,t;
if(n-2 > 1)
{
a = n;
b = n-1;
c = n-2;
}
else
{
a = n;
if(n-1 > 1)
b = n-1;
else
b = 1;
c = 1;
System.out.println(a*b);
return;
}
if(n-3 > 1)
{
x = n-1;
y = n-2;
z = n-3;
}
if(n % 2 == 0)
if(n % 3 == 0)
l = BigInteger.valueOf(x).multiply(BigInteger.valueOf(y).multiply(BigInteger.valueOf(z)));
else
{
l = BigInteger.valueOf(a).multiply(BigInteger.valueOf(b).multiply(BigInteger.valueOf(c-1)));
t = BigInteger.valueOf(x).multiply(BigInteger.valueOf(y).multiply(BigInteger.valueOf(z)));
if(l.compareTo(t) < 0)
l = t;
}
else
l = BigInteger.valueOf(a).multiply(BigInteger.valueOf(b).multiply(BigInteger.valueOf(c)));
System.out.println(l);
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Nasko
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int N = in.nextInt();
if (N == 1) {
out.println(1);
} else if (N == 2) {
out.println(2);
} else if (N == 3) {
out.println(6);
} else {
long best = Long.MIN_VALUE;
best = Math.max(best, lcm(N, lcm(N - 1, N - 2)));
best = Math.max(best, lcm(N, lcm(N - 2, N - 3)));
best = Math.max(best, lcm(N, lcm(N - 1, N - 3)));
best = Math.max(best, lcm(N - 1, lcm(N - 2, N - 3)));
out.println(best);
}
}
private long lcm(long a, long b) {
return a * (b / gcd(a, b));
}
private long gcd(long a, long b) {
return b == 0 ? a : gcd(b, a % b);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////// SOLUTION ///////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
public class Solution{
// main();
public static void main(String[] args) throws IOException{
///input
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String[] str=br.readLine().split(" ");
long n=Long.parseLong(str[0]);
if(n<3)
System.out.println(n);
else if(n%2==1)
System.out.println(n*(n-1)*(n-2));
else
{
if(n%3!=0)
System.out.println(n*(n-1)*(n-3));
else
System.out.println((n-1)*(n-2)*(n-3));
}
}//void main
}//class main
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class A235 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long a = in.nextLong();
if (a % 2 == 0) {
long result = cal(a);
result = Math.max(result, cal(a + 1));
result = Math.max(result, cal2(a));
System.out.println(Math.max(result, a));
}
else {
long result = (a - 1) * (a - 2) * (a - 0);
System.out.println(Math.max(result, a));
}
}
static long cal(long a) {
long result = (a - 1) * (a - 2);
result /= gcd(a - 1, a - 2);
long gcd = gcd(result, a - 3);
result *= (a - 3);
result /= gcd;
return result;
}
static long cal2(long a) {
long result = (a) * (a - 1);
result /= gcd(a - 1, a);
long gcd = gcd(result, a - 3);
result *= (a - 3);
result /= gcd;
return result;
}
private static long gcd(long l, long i) {
if (l == 0 || i == 0) {
return 1;
}
if (l % i == 0) {
return i;
}
return gcd(i, l % i);
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.*;
import java.io.*;
public class Main {
boolean eof;
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return "-1";
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
void solve() {
long n = nextInt(), ans = n;
if (n % 6 == 0) {
ans = Math.max((n - 1) * (n - 2) * (n - 3), ans);
} else if (n % 2 == 0) {
ans = Math.max(ans, n * (n - 1) * (n - 3));
} else {
ans = Math.max(n * (n - 1) * (n - 2), ans);
}
out.print(ans);
}
BufferedReader br;
StringTokenizer st;
PrintWriter out;
void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
// br = new BufferedReader(new FileReader("input.txt"));
// out = new PrintWriter(new FileWriter("output.txt"));
solve();
br.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
new Main().run();
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
public class CF{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
long n = scan.nextLong();
TaskA t = new TaskA();
System.out.println(t.solve(n));
}
}
class TaskA{
public long solve(long n)
{
if(n < 3)
return n;
else if(n % 2 == 1)
return n * (n-1) * (n-2);
else if(n % 3 != 0)
return n * (n-1) * (n-3);
else
return (n-1) * (n-2) * (n-3);
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.lang.*;
import java.util.*;
import java.io.*;
public class Challenge {
public static void main(String[] args) throws java.lang.Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(in, out);
out.close();
}
}
class TaskA {
public void solve(InputReader in, PrintWriter out) {
int n = in.nextInt();
if (n == 1) {
out.println("1");
} else if (n == 2) {
out.println("2");
} else if (n == 3) {
out.println("6");
} else if (n%2 > 0) {
out.println(1L * n * (n-1) * (n-2));
} else if (n%3 == 0) {
out.println(1L * (n-1) * (n-2) * (n-3));
} else {
out.println(1L * n * (n-1) * (n-3));
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
/**
* 2013.07.27 No.1 235A LCM Challenge
* 数论 n%2 == 0? n%3 == 0?
* @author Administrator *
*/
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
if (n < 3)
System.out.println(n);
else if (n % 2 != 0)
System.out.println((long)n * (n - 1) * (n - 2));
else if(n % 3 != 0)
System.out.println((long)n * (n - 1) * (n - 3));
else
System.out.println((long)(n - 1) * (n - 2) * (n - 3));
in.close();
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.*;
public class LCMChallenge {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextInt();
if(n == 1l)
System.out.println(1);
else if(n == 2l)
System.out.println(2);
else
{
long c1 = n*(n-1)*(n-2);
long c2 = n*(n-1)*(n-3);
long c3 = (n-1)*(n-2)*(n-3);
if(n%2==0)
c1/=2;
else
c3/=2;
if(n%3==0)
c2/=3;
long ans = Math.max(c1, c2);
ans = Math.max(ans, c3);
System.out.println(ans);
}
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.*;
import java.io.*;
public class LCMChallenge {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int n = cin.nextInt();
if (n < 3) {
System.out.println(n);
} else if (n % 2 == 1) {
System.out.println((long) n * (n - 1) * (n - 2));
} else {
if (n % 3 != 0) {
System.out.println((long) n * (n - 1) * (n - 3));
} else {
System.out.println((long) (n - 1) * (n - 2) * (n - 3));
}
}
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main {
long n;
long maxlcm;
void run(){
n = cin.nextInt();
if(n == 1 || n ==2)
maxlcm = n;
else if(n >= 3){
if(n % 2 != 0){
maxlcm = n * (n-1) * (n - 2);
}
else if(n%3 != 0)
maxlcm = n * (n-1) * (n - 3);
else maxlcm = (n - 1) * (n - 2) * (n - 3);
}
System.out.println(maxlcm);
}
public static void main(String[] args) {
new Main().run();
}
Scanner cin = new Scanner(new BufferedInputStream(System.in));
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) throws IOException {
InputReader sc = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
if(n < 3)
out.println(n);
else
{
if((n & 1) == 1)
out.println(lcm(n, lcm(n - 1, n - 2)));
else
out.println(Math.max(lcm(n - 1, lcm(n - 2, n - 3)), lcm(n, lcm(n - 1, n - 3))));
}
out.flush();
out.close();
}
static long gcd(long a, long b)
{
while(b != 0)
{
a = a%b;
b ^= a;
a ^= b;
b ^= a;
}
return a;
}
static long lcm(long a, long b) { return a / gcd(a,b) * b; }
}
class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
} | constant | 235_A. LCM Challenge | CODEFORCES |
import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author BSRK Aditya ([email protected])
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
long n = in.nextLong();
if(n == 1) out.println("1");
else if(n == 2) out.println("2");
else if(n%2 == 1) out.println(n*(n-1)*(n-2));
else if(n%6 == 0) out.println((n-1)*(n-2)*(n-3));
else out.println(n*(n-1)*(n-3));
}
}
| constant | 235_A. LCM Challenge | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class AAA {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
String a="";
String b="";
for(int i=0;i<1129;i++) {
a+="1";
b+="8";
}
a+="9";
b+="1";
System.out.println(a);
System.out.println(b);
}
}
| constant | 1028_B. Unnatural Conditions | CODEFORCES |
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class B {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// int n = Integer.parseInt(bf.readLine());
StringTokenizer st = new StringTokenizer(bf.readLine());
// int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
StringBuilder ans1 = new StringBuilder();
StringBuilder ans2 = new StringBuilder();
for(int i=0; i<2229; i++) ans1.append('5');
ans1.append('6');
for(int i=0; i<2230; i++) ans2.append('4');
out.println(ans1.toString());
out.println(ans2.toString());
out.close(); System.exit(0);
}
}
| constant | 1028_B. Unnatural Conditions | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class AAA {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n=Integer.parseInt(st.nextToken());
int m=Integer.parseInt(st.nextToken());
String a="";
String b="";
for(int i=0;i<1129;i++) {
a+="1";
b+="8";
}
a+="9";
b+="1";
System.out.println(a);
System.out.println(b);
}
}
| constant | 1028_B. Unnatural Conditions | CODEFORCES |
import java.util.*;
import java.io.*;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1[]=br.readLine().split(" ");
int n=Integer.parseInt(s1[0]);
int S=Integer.parseInt(s1[1]);
if(S%n==0)
System.out.println(S/n);
else
System.out.println(S/n+1);
}
} | constant | 1061_A. Coins | CODEFORCES |
import java.io.*;
public class coins
{
public static void main(String args[])throws IOException
{
InputStreamReader read=new InputStreamReader(System.in);
BufferedReader in=new BufferedReader(read);
int i,k,n,v;
String a;
a=in.readLine();
for(i=0;i<a.length();i++)
{
if(a.charAt(i)==' ')
break;
}
n=Integer.parseInt(a.substring(0,i));
v=Integer.parseInt(a.substring(i+1));
k=v%n;
v=v/n;
if(k>0)
v++;
System.out.println(v);
}
}
| constant | 1061_A. Coins | CODEFORCES |
import java.util.*;
import java.io.*;
public class A
{
public static void main(String ar[]) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String s1[]=br.readLine().split(" ");
int n=Integer.parseInt(s1[0]);
int S=Integer.parseInt(s1[1]);
if(S%n==0)
System.out.println(S/n);
else
System.out.println(S/n+1);
}
} | constant | 1061_A. Coins | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
public static void main(String args[]) {
FastScanner scn = new FastScanner();
int n = scn.nextInt();
int s = scn.nextInt();
if (s <= n) {
System.out.println(1);
} else if (s > n) {
if(s%n == 0){
System.out.println(s/n);
} else {
System.out.println(s/n + 1);
}
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| constant | 1061_A. Coins | CODEFORCES |
import java.util.*;
public class A912 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
int A = scan.nextInt();
int B = scan.nextInt();
long x = scan.nextInt();
long y = scan.nextInt();
long z = scan.nextInt();
long requiredA = x * 2 + y;
long requiredB = y + z * 3;
long neededA = Math.max(0, requiredA - A);
long neededB = Math.max(0, requiredB - B);
System.out.print(neededA + neededB);
}
}
| constant | 912_A. Tricky Alchemy | CODEFORCES |
import java.io.*;
import java.util.StringTokenizer;
public class A267 {
static public long _solve(long a,long b){
long result = -1;
while(a!=0 && b!=0){
if(a>b){
result +=(a/b);
a = a%b;
}
else{
result +=(b/a);
b = b%a;
}
}
return result+1;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
long a,b;
while(t-- > 0){
a = in.nextLong();
b = in.nextLong();
System.out.println(_solve(a,b));
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() {
while (st == null || !st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Coder{
static class FastScanner{
BufferedReader s;
StringTokenizer st;
public FastScanner(){
st = new StringTokenizer("");
s = new BufferedReader(new InputStreamReader(System.in));
}
public int nextInt() throws IOException{
if(st.hasMoreTokens())
return Integer.parseInt(st.nextToken());
else{
st = new StringTokenizer(s.readLine());
return nextInt();
}
}
}
public static void main(String[] args) throws IOException{
FastScanner s = new FastScanner();
PrintWriter ww = new PrintWriter(new OutputStreamWriter(System.out));
int test = s.nextInt(); int cnt=0;
while(test-->0){
int a = s.nextInt();
int b = s.nextInt();
cnt=0;
while(a!=0 && b!=0){
int max = Math.max(a, b);
if(max == b){
int divi = b/a;
b -= divi*a;
cnt+=divi;
}else{
int divi = a/b;
a -= divi*b;
cnt+=divi;
}
// System.out.println(a+" "+b);
}
ww.println(cnt);
}
ww.close();
}
} | constant | 267_A. Subtractions | CODEFORCES |
import java.util.*;
public class Solution{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int pairs = in.nextInt();
while (pairs > 0){
in.nextLine();
int a = in.nextInt();
int b = in.nextInt();
int count = 0;
while (a != 0 && b != 0){
if (b >= a && a != 0){
count += b/a;
b = b%a;
}
if (a > b && b != 0){
count += a/b;
a = a%b;
}
}
System.out.println(count);
pairs--;
}
}
} | constant | 267_A. Subtractions | CODEFORCES |
import java.io.*;
import java.util.*;
public class Mai {
public static void main(String[] args) throws IOException{
Scanner cin = new Scanner(System.in);
int t, n, m;
t = cin.nextInt();
while(t > 0) {
t--;
int sum = 0;
n = cin.nextInt();
m = cin.nextInt();
while(n > 0 && m > 0) {
if(n < m) {
int k = n;
n = m;
m = k;
}
sum += n / m; n %= m;
}
System.out.println(sum);
}
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.util.Scanner;
/**
* Created by carolineshi on 3/30/17.
*/
public class Subtractions {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
while(t != 0) {
int f = scan.nextInt();
int s = scan.nextInt();
System.out.println(ops(f, s));
t--;
}
}
public static int ops(int f, int s) {
int ops = 0;
while((f > 0) && (s > 0)) {
if(f > s) {
ops += f/s;
f %= s;
} else {
//f <= s
ops += s/f;
s %= f;
}
}
return ops;
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class CodeForces {
public static void main(String[] args) throws IOException,NumberFormatException{
try {
FastScanner sc=new FastScanner();
int t=sc.nextInt();
while(t-->0) {
int a=sc.nextInt(),b=sc.nextInt();
int count=0;
while(a!=0&&b!=0) {
if(a>b) {
int temp=a;
a=b;
b=temp;
}
count+=(b/a);
b=b%a;
}
System.out.println(count);
}
}
catch(Exception e) {
return ;
}
}
public static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
} | constant | 267_A. Subtractions | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class CF267A {
public static void main(String[] args) {
int n=0, a, b;
BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
try {
n = Integer.parseInt(stdin.readLine());
} catch (IOException e) {
}
while(n-->0){
String[] row = null;
try {
row = stdin.readLine().split(" ");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
a = Integer.parseInt(row[0]);
b = Integer.parseInt(row[1]);
if(a<b) System.out.println(calc(a,b));
else System.out.println(calc(b,a));
}
}
static int calc(int a, int b){
if(a==0) return 0;
if(a==b) return 1;
if(a==1) return b;
else return b/a+calc(b%a, a);
}
}
| constant | 267_A. Subtractions | CODEFORCES |
//package A;
import java.util.Scanner;
public class Solution {
Scanner in = new Scanner(System.in);
void run() throws Exception {
int tests = in.nextInt();
while (tests > 0) {
--tests;
int a = in.nextInt();
int b = in.nextInt();
int res = 0;
while (a > 0 && b > 0) {
if (a >= b) {
res += a / b;
a %= b;
}
else {
res += b / a;
b %= a;
}
}
System.out.println(res);
}
}
public static void main(String args[]) throws Exception {
new Solution().run();
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.util.Scanner;
public class Question267A {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while (t--!=0){
int x=sc.nextInt();
int y=sc.nextInt();
int max=Math.max(x,y);
int min=Math.min(x,y);
int ans=0;
while (min>0 && max>0){
int temp=max;
ans+=temp/min;
max=min;
min=temp%min;
}
System.out.println(ans);
}
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.util.Scanner;
public class Main{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
int T = in.nextInt();
while(T!=0){
T--;
int a = in.nextInt();
int b = in.nextInt();
int ans=0;
while(a>0&&b>0){
if(a>b){
int c = a;
a = b;
b = c;
}
ans += (b-(b%a))/a;
b = b%a;
}
System.out.println(ans);
}
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Subtraction {
static long c=0;
public static void main(String[] args) throws IOException {
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
int n=Integer.parseInt(reader.readLine());
while (n-->0){
String l=reader.readLine();
String[] a=l.split(" ");
long A=Long.parseLong(a[0]);
long B=Long.parseLong(a[1]);
c=0;
gcd(A,B);
System.out.println(c);
}
}
private static void gcd(long a, long b) {
if (b==0)
return ;
c=c+a/b;
gcd(b,a%b);
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.util.*;
public class Subtractions {
public static void main(String[] args) {
Scanner kb = new Scanner(System.in);
int count = kb.nextInt();
while(count > 0) {
int smaller = kb.nextInt();
int larger = kb.nextInt();
int ops = 0;
while(smaller > 0 && larger > 0) {
if(smaller > larger) {
int temp = smaller;
smaller = larger;
larger = temp;
}
ops += larger/smaller;
larger = larger % smaller;
}
System.out.println(ops);
count--;
}
}
} | constant | 267_A. Subtractions | CODEFORCES |
import java.util.Scanner;
public class Sub
{
public static void main(String[] args)
{
Scanner scan=new Scanner(System.in);
int noOfPairs=scan.nextInt();
while(noOfPairs-->0)
{
int x=scan.nextInt();
int y=scan.nextInt();
int res=0;
while(x!=0&&y!=0)
{
if(x>y)
{
res+=x/y;
x=x%y;
}
else
{
res+=y/x;
y=y%x;
}
}
System.out.println(res);
}
scan.close();
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.util.Scanner;
public class Subtractions {
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int t=s.nextInt();
while(t--!=0){
int a=s.nextInt();
int b=s.nextInt();
int min=Math.min(a, b);
int max=Math.max(a, b);
int ops=0;
while(true){
int quo=max/min;
ops+=quo;
int rem=max%min;
max=Math.max(rem, min);
min=Math.min(min, rem);
if(rem==0) break;
}
System.out.println(ops);
}
}
}
| constant | 267_A. Subtractions | CODEFORCES |
//david alexander
import java.util.*;
public class Subtract {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int a,b;
String answer = "";
while(n!=0){
a = sc.nextInt();
b = sc.nextInt();
answer += solve(a,b) + "\n";
n--;
}
System.out.println(answer);
}
public static int solve(int a, int b){
int count = 0;
int div;
int mod;
while(true){
if(a >= b){
div = a/b;
mod = a%b;
count += div;
if(mod==0){
return count;
}
else{
a = mod;
}
}
else{
div = b/a;
mod = b%a;
count += div;
if(mod==0){
return count;
}
else{
b = mod;
}
}
}
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException{
Scanner cin = new Scanner(System.in);
int t, n, m;
t = cin.nextInt();
while(t > 0) {
t--;
int sum = 0;
n = cin.nextInt();
m = cin.nextInt();
while(n > 0 && m > 0) {
if(n < m) {
int k = n;
n = m;
m = k;
}
sum += n / m; n %= m;
}
System.out.println(sum);
}
}
}
| constant | 267_A. Subtractions | CODEFORCES |
/*
(7 10) 1 2 3
(3, 7)
(1, 3)
(3, 7)
(3, 4)
(3, 1)
(2, 1)
(1, 1)
1
*/
import java.util.*;
public class CodeForcesW8P2 {
public static void main(String [] args){
Scanner sc = new Scanner(System.in);
int tests = Integer.valueOf(sc.nextLine());
while(tests > 0){
int count = 0;
String [] input = sc.nextLine().split(" ");
int x = Integer.valueOf(input[0]);
int y = Integer.valueOf(input[1]);
if (x == y){
count += 1;
}
else {
if (x > y){
int temp = x;
x = y;
y = temp;
}
/*
(4, 17) 4 + 4
(1, 4)
(4, 16) 4
(4, 12)
(4, 8)
(4, 4)
*/
while (x != 1 && x != 0 && y != 1){
count += (y / x);
int temp = x;
x = (y % x);
y = temp;
}
if (x != 0)
count += y;
}
System.out.println(count);
tests --;
}
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
s.nextLine();
while(s.hasNext()) {
int first = s.nextInt();
int second = s.nextInt();
System.out.println(calculate(first,second));
}
}
public static int calculate(int first, int second) {
int operations = 0;
while(first != 0 && second != 0) {
int temp;
if(first < second) {
temp = second/first;
operations += temp;
second -= (first*temp);
}
else {
temp = first/second;
operations += temp;
first -= (second*temp);
}
}
return operations;
}
} | constant | 267_A. Subtractions | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int t = in.readInt();
while (t-- > 0) {
int[] a = new int[]{in.readInt(), in.readInt()};
Arrays.sort(a);
int ans = 0;
while (a[0] > 0) {
int x = a[1] / a[0];
ans += x;
a[1] -= a[0] * x;
Arrays.sort(a);
}
out.println(ans);
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.util.Scanner;
public class sub {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int num = in.nextInt();
while(num-->0) {
int a = in.nextInt();
int b = in.nextInt();
int res = 0;
while(a!=0 && b!=0) {
if(a>=b) {
res += a/b;
a %= b;
} else {
res += b/a;
b %= a;
}
}
System.out.println(res);
}
}
} | constant | 267_A. Subtractions | CODEFORCES |
import java.util.Scanner;
public class Subtractions {
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
int test=sc.nextInt();
while(test-->0){
long a=sc.nextLong();
long b=sc.nextLong();
long count=0;
long cnt=0;
while(a>0&&b>0){
count=0;
//System.out.println(a+" "+b);
if(a>b){
count+=(a-b)/b;
if(count!=0){
cnt+=count;
a-=b*count;}
else {
cnt++;
a-=b;
}
}
else{
count+=(b-a)/a;
if(count!=0){
cnt+=count;
b-=a*count;}
else {
cnt++;
b-=a;
}
}
}
System.out.println(cnt);
}
}
}
| constant | 267_A. Subtractions | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class StrangeAddition {
public static void main(String[] args) throws IOException {
PrintWriter out = new PrintWriter(System.out);
sc = new StringTokenizer(br.readLine());
int tc = nxtInt();
while (tc-- > 0) {
int a = nxtInt();
int b = nxtInt();
int ans = 0;
while (a != b) {
if (a == 0 || b == 0)
break;
if (a > b) {
int div = a / b;
a -= b * div;
ans += div;
} else {
int div = b / a;
b -= a * div;
ans += div;
}
}
out.println(ans + (a == b ? 1 : 0));
}
br.close();
out.close();
}
static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
static StringTokenizer sc;
static String nxtTok() throws IOException {
while (!sc.hasMoreTokens()) {
String s = br.readLine();
if (s == null)
return null;
sc = new StringTokenizer(s.trim());
}
return sc.nextToken();
}
static int nxtInt() throws IOException {
return Integer.parseInt(nxtTok());
}
static long nxtLng() throws IOException {
return Long.parseLong(nxtTok());
}
} | constant | 267_A. Subtractions | CODEFORCES |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.