exec_outcome stringclasses 1
value | code_uid stringlengths 32 32 | file_name stringclasses 111
values | prob_desc_created_at stringlengths 10 10 | prob_desc_description stringlengths 63 3.8k | prob_desc_memory_limit stringclasses 18
values | source_code stringlengths 117 65.5k | lang_cluster stringclasses 1
value | prob_desc_sample_inputs stringlengths 2 802 | prob_desc_time_limit stringclasses 27
values | prob_desc_sample_outputs stringlengths 2 796 | prob_desc_notes stringlengths 4 3k ⌀ | lang stringclasses 5
values | prob_desc_input_from stringclasses 3
values | tags listlengths 0 11 | src_uid stringlengths 32 32 | prob_desc_input_spec stringlengths 28 2.37k ⌀ | difficulty int64 -1 3.5k ⌀ | prob_desc_output_spec stringlengths 17 1.47k ⌀ | prob_desc_output_to stringclasses 3
values | hidden_unit_tests stringclasses 1
value |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
PASSED | 1ea3f71117d723aaf4c54e2bd31c5cf7 | train_002.jsonl | 1599575700 | Alexandra has an even-length array $$$a$$$, consisting of $$$0$$$s and $$$1$$$s. The elements of the array are enumerated from $$$1$$$ to $$$n$$$. She wants to remove at most $$$\frac{n}{2}$$$ elements (where $$$n$$$ — length of array) in the way that alternating sum of the array will be equal $$$0$$$ (i.e. $$$a_1 - a_2 + a_3 - a_4 + \dotsc = 0$$$). In other words, Alexandra wants sum of all elements at the odd positions and sum of all elements at the even positions to become equal. The elements that you remove don't have to be consecutive.For example, if she has $$$a = [1, 0, 1, 0, 0, 0]$$$ and she removes $$$2$$$nd and $$$4$$$th elements, $$$a$$$ will become equal $$$[1, 1, 0, 0]$$$ and its alternating sum is $$$1 - 1 + 0 - 0 = 0$$$.Help her! | 256 megabytes | import java.util.*;
import java.io.*;
public class Prb37 {
static FastReader sc = new FastReader();
// #JusticeForSSR
public static void main(String[] args) {
int t = sc.nextInt();
while(t-- != 0){
int n = sc.nextInt();
int z = 0;
int o = 0;
for(int i = 0 ; i < n ; i ++){
if(sc.nextInt() == 1){
o++;
}else{
z++;
}
}
if(z>=o){
System.out.println(z);
for(int i = 0 ; i < z ; i++){
if(i!=0){
System.out.print(" ");
}
System.out.print(0);
}
}
else{
if(o%2!=0){
o--;
}
System.out.println(o);
for(int i = 0 ; i < o ; i ++){
if(i!=0){
System.out.print(" ");
}
System.out.print(1);
}
}
System.out.println();
}
}
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;
}
}
} | Java | ["4\n2\n1 0\n2\n0 0\n4\n0 1 1 1\n4\n1 1 0 0"] | 1 second | ["1\n0\n1\n0\n2\n1 1\n4\n1 1 0 0"] | NoteIn the first and second cases, alternating sum of the array, obviously, equals $$$0$$$.In the third case, alternating sum of the array equals $$$1 - 1 = 0$$$.In the fourth case, alternating sum already equals $$$1 - 1 + 0 - 0 = 0$$$, so we don't have to remove anything. | Java 8 | standard input | [
"constructive algorithms",
"math"
] | eca92beb189c4788e8c4744af1428bc7 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \le t \le 10^3$$$). Description of the test cases follows. The first line of each test case contains a single integer $$$n$$$ ($$$2 \le n \le 10^3$$$, $$$n$$$ is even) — length of the array. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$0 \le a_i \le 1$$$) — elements of the array. It is guaranteed that the sum of $$$n$$$ over all test cases does not exceed $$$10^3$$$. | 1,100 | For each test case, firstly, print $$$k$$$ ($$$\frac{n}{2} \leq k \leq n$$$) — number of elements that will remain after removing in the order they appear in $$$a$$$. Then, print this $$$k$$$ numbers. Note that you should print the numbers themselves, not their indices. We can show that an answer always exists. If there are several answers, you can output any of them. | standard output | |
PASSED | 25cff5e439a9458cbfb3e797a4b16fbb | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes | import java.util.Scanner;
public class Prob2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int m = scanner.nextInt();
String[][] arr = new String[n][m];
String[] input = new String[n];
scanner.nextLine();
for (int i = 0; i < n; i++) {
input[i]=scanner.nextLine();
}
boolean k=true;
int starti=0;
int startj=0;
int endi=0;
int endj=0;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j]=""+input[i].charAt(j);
if(arr[i][j].equals("X") && k){
starti=i;
startj=j;
endi=i;
endj=j;
k=false;
}else if(arr[i][j].equals("X")){
endi=i;
endj=j;
}
}
}
for (int i = starti; i <=endi ; i++) {
for (int j = startj; j <=endj; j++) {
if(!arr[i][j].equals("X")){
System.out.println("NO");
return;
}
arr[i][j]=".";
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if(arr[i][j].equals("X")) {
System.out.println("NO");
return;
}
}
}
System.out.println("YES");
}
} | Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | 58cec351b24aaad63e61de826a0475a4 | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collector;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main {
public static void main(String[] args) {
File file = new File("in.txt");
InputStream inputStream = null;
// try {inputStream= new FileInputStream(file);} catch (FileNotFoundException ex){return;};
inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
List<String> map;
public void solve(int testNumber, InputReader in, PrintWriter out) {
Integer n = in.nextInt();
Integer m = in.nextInt();
map = new ArrayList<>();
for(int i=0; i<n; i++){
map.add(in.next());
}
Integer minX = m;
Integer maxX = -1;
Integer minY = n;
Integer maxY = -1;
Integer count = 0;
for(int i=0; i< n ;i++){
String row = map.get(i);
for(int j=0; j<m; j++){
Character ch = row.charAt(j);
if(ch == 'X'){
count +=1;
maxX = Math.max(maxX, j);
minX = Math.min(minX, j);
maxY = Math.max(maxY, i);
minY = Math.min(minY, i);
}
}
}
if((maxX-minX+1) * (maxY - minY+1) == count){
out.println("YES");
}
else{
out.println("NO");
}
}
}
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 String nextLine(){
try {
return reader.readLine();
} catch (IOException e){
throw new RuntimeException(e);
}
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() { return Long.parseLong(next()); }
}
class Pair<F, S> {
public final F first;
public final S second;
public Pair(F first, S second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof Pair)) {
return false;
}
Pair<?, ?> p = (Pair<?, ?>) o;
return Objects.equals(p.first, first) && Objects.equals(p.second, second);
}
@Override
public int hashCode() {
return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
}
@Override
public String toString() {
return "(" + first + ", " + second + ')';
}
}
| Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | e9a6b57f9cef302641f29bcb9e3fefef | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes | import java.util.*;
import java.math.*;
public class Main
{
public static void main( String[] args )
{
Scanner is = new Scanner( System.in );
int n = is.nextInt(), m = is.nextInt();
List<String> grid = new ArrayList<>();
for( int i = 0; i < n; i++ )
grid.add( is.next() );
int quanX = 0;
for( int i = 0; i < n; i++ )
for( int j = 0; j < m; j++ )
if( grid.get(i).charAt(j) == 'X' )
quanX++;
boolean answer = false;
for( int i = 0; i < n; i++ )
for( int j = 0; j < m; j++ )
if( grid.get(i).charAt(j) == 'X' )
{
// magic
int r = i, c = j;
while( c < m && grid.get( i ).charAt( c ) == 'X' )
c++;
while( r < n && grid.get( r ).charAt( j ) == 'X' )
r++;
int verify = 0;
for( int a = i; a < r; a++ )
for( int b = j; b < c; b++ )
if( grid.get(a).charAt( b ) == 'X' )
verify++;
else{
verify = -1;
a = r;
break;
}
if( quanX == verify )
answer = true;
i = n;
break;
}
System.out.println( ( answer ? "YES" : "NO" ) );
}
}
| Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | de62e2decd8439b8828d17428388bfce | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes |
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public void solve() throws IOException {
int n = nextInt(), m = nextInt();
int x1 = 0, y = 0, x2 = 0, k = 0;
boolean t = false;
char s[][] = new char[n][];
for (int i = 0; i < n; i++) {
s[i] = nextToken().toCharArray();
for (int j = 0; j < m; j++) {
if (s[i][j] == 'X') {
k++;
if (!t) {
x1 = j;
y = i;
t = true;
}
}
}
}
for (int i = m - 1; i >= 0; i--) {
if (s[y][i] == 'X') {
x2 = i;
break;
}
}
if(k % (x2 - x1 + 1) != 0) {
out.print("NO");
return;
}
else{
if(k / (x2 - x1 + 1) > n) {
out.print("NO");
return;
}
else for(int i = y; i < y + k / (x2 - x1 + 1); i++){
for(int j = x1; j <= x2; j++){
if(s[i][j] != 'X'){
out.print("NO");
return;
}
}
}
}
out.print("YES");
}
BufferedReader br;
StringTokenizer sc;
PrintWriter out;
public String nextToken() throws IOException {
while (sc == null || !sc.hasMoreTokens()) {
try {
sc = new StringTokenizer(br.readLine());
} catch (Exception e) {
return null;
}
}
return sc.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public boolean hasNext() {
while (sc == null || !sc.hasMoreTokens()) {
try {
String s = br.readLine();
if (s == null) {
return false;
}
sc = new StringTokenizer(s);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return sc.hasMoreTokens();
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
new Main().run();
}
public void run() {
try {
out = new PrintWriter(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader (new FileReader ("input.txt"));
// out = new PrintWriter (new File ("output.txt"));
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
}
| Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | 2e0e81f28110d05214a8726ea8f1cad8 | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes |
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.math.BigInteger;
import java.util.*;
public class random_num {
public static long x,y,d;
public static int mod= (int) (Math.pow(10,9)+7);
public static void main(String args[]){
InputReader s= new InputReader(System.in);
OutputStream outputStream= System.out;
PrintWriter out= new PrintWriter(outputStream);
int n=s.nextInt();
int m=s.nextInt();
TreeSet<Integer> x=new TreeSet();
TreeSet<Integer> y=new TreeSet();
int a[][]=new int[n][m];
for(int i=0;i<n;i++){
String str=s.nextLine();
for(int j=0;j<m;j++){
a[i][j]=str.charAt(j);
}
}
int f=0;
int mini=0,minj=0,flag=0;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(a[i][j]=='X'){
x.add(i);
y.add(j);
}
}
}
//System.out.println(x.first()+" "+x.last());
//System.out.println(y.first()+" "+y.last());
for(int i=x.first();i<=x.last();i++){
for(int j=y.first();j<=y.last();j++){
if(a[i][j]!='X'){
f=1;
break;
}
}
if(f==1){
break;
}
}
if(f==0)
System.out.println("YES");
else
System.out.println("NO");
out.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream inputstream) {
reader = new BufferedReader(new InputStreamReader(inputstream));
tokenizer = null;
}
public String nextLine(){
String fullLine=null;
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
fullLine=reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return fullLine;
}
return fullLine;
}
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 long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
public static HashMap<Integer,Long> getcatalan(int n){
HashMap<Integer,Long> a=new HashMap<Integer,Long>();
a.put(0,(long)1);
for(int i=1;i<=n+1;i++){
long m=2*(2*i-1);
long m1=m*a.get(i-1);
long m3=m1/(i+1);
//out.println(m+" "+m1+" "+m2+" "+m3);
a.put(i,m3%1000000007);
}
return a;
}
public static int getGdc(int[] x) {
// get the smallest of all number no need to check for higher values
int smallest = getSmallest(x);
for(int i = smallest; i >= 1; i--) {
int j;
for(j = 0; j < x.length; ++j) {
if(x[j] % i != 0)
break;
}
// if we pass through the array with all % == 0 return the value
if(j == x.length)
return i;
}
// so the only possible is 1
return 1;
}
// return smallest number of an array of int
public static int getSmallest(int[] x) {
int smallest = x[0];
for(int i = 1; i < x.length; ++i) {
if(x[i] < smallest)
smallest = x[i];
}
return smallest;
}
public static long gcd(long number1, long number2) {
if(number2 == 0){
return number1;
}
return gcd(number2, number1%number2);
}
public static void extendedEuclidean(long a,long b){
if(b == 0) {
d = a;
x = 1;
y = 0;
}
else {
extendedEuclidean(b, a%b);
long temp = x;
x = y;
y = temp - (a/b)*y;
}
}
public static int combinations(int n,int r){
if(n==r) return 1;
if(r==1) return n;
if(r==0) return 1;
return combinations(n-1,r)+ combinations(n-1,r-1);
}
public static long binomialCoeff(int n, int k)
{
long C[][]= new long[n+1][k+1];
int i, j;
// Caculate value of Binomial Coefficient in bottom up manner
for (i = 0; i <= n; i++)
{
for (j = 0; j <= Math.min(i, k); j++)
{
// Base Cases
if (j == 0 || j == i)
C[i][j] = 1;
// Calculate value using previosly stored values
else
C[i][j] = C[i-1][j-1] + C[i-1][j];
}
}
return C[n][k];
}
public static long expo(long a, long b){
if (b==1)
return a%mod;
if (b==2)
return (a%mod)*(a%mod);
if (b%2==0){
return expo(expo(a%mod,(b%mod)/2),2);
}
else{
return a*(expo(expo(a%mod,((b%mod-1%mod)%mod)/2),2));
}
}
public static int[] sieve(int N){
int arr[]= new int[N+1];
int b[]=new int[N+1];
for(int i=2;i<Math.sqrt(N);i++){
if(arr[i]==0){
for(int j= i*i;j<= N;j= j+i){
arr[j]=1;
}
}
}
return arr;
// All the i for which arr[i]==0 are prime numbers.
}
static class Pair implements Comparable<Pair>
{
int f,s;
Pair(int ii, int cc)
{
f=ii;
s=cc;
}
public int compareTo(Pair o)
{
return Integer.compare(this.f, o.f);
}
}
public static class company{
public int t;
public int d;
public company(int m,int f){
this.t=m;
this.d=f;
}
public int get_t(){
return this.t;
}
public int get_d(){
return this.d;
}
}
public static class pair_comparator implements Comparator<company>{
@Override
public int compare(company arg0, company arg1) {
// TODO Auto-generated method stub
return arg0.t-arg1.t;
}
}
} | Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | 713553ac123c38782dc5a6771781b91e | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes | import com.sun.org.apache.xpath.internal.operations.Bool;
import java.io.*;
import java.util.*;
import java.lang.*;
public final class Main {
private boolean isPrime(int n){
if(n == 1) return false;
for (int i = 2; i <= Math.sqrt(n); i++)
if(n % i == 0) return false;
return true;
}
private void solve() {
int n = in.nextInt();
int m = in.nextInt();
int maxx = -1;
int minx = n;
int maxy = -1;
int miny = m;
char[][] r = new char[n][];
for (int i = 0; i<n;i++){
r[i]=in.nextLine().toCharArray();
}
int count=0;
for (int i =0;i<n;i++){
for (int j = 0;j<m;j++){
if (r[i][j]=='X'){
maxx=Math.max(maxx,i);
minx=Math.min(minx,i);
maxy=Math.max(maxy,j);
miny=Math.min(miny,j);
count++;
}
}
}
if ((maxx-minx+1)*(maxy-miny+1)==count){
out.println("YES");
}else {
out.println("NO");
}
}
// -------------I/O------------- \\
void run() {
try {
in = new FScanner(new File("input.txt"));
out = new PrintWriter(new BufferedWriter(new FileWriter(new File("output.txt"))));
solve();
out.close();
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
void runIO() {
in = new FScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
solve();
out.close();
in.close();
}
public static void main(String[] args) {
new Main().runIO();
}
private FScanner in;
private PrintWriter out;
class FScanner {
private BufferedReader br;
private StringTokenizer st;
public FScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
void useString(String s) {
if (s != null) {
st = new StringTokenizer(s);
}
}
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 s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
void close() {
try {
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
| Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | ebbad926028aff00605acd49ab6898d3 | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes | import java.io.*;
import java.util.*;
public class B745 {
public static void main(String[] args) throws Exception{
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
boolean[][] grid = new boolean[n][k];
int minX = k;
int maxX = 0;
int minY = n;
int maxY = 0;
for(int i = 0;i<n;i++){
String s = f.readLine();
for(int j = 0;j<k;j++){
if(s.charAt(j)=='X'){
grid[i][j] = true;
minX = Math.min(minX, j);
maxX = Math.max(maxX, j);
minY = Math.min(minY, i);
maxY = Math.max(maxY, i);
}
}
}
for(int i = minX;i<=maxX;i++){
for(int j = minY;j<=maxY;j++){
if(!grid[j][i]){
System.out.println("NO");
System.exit(0);
}
}
}
System.out.println("YES");
}
}
| Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | 3214a1da8955e23c679f19b7f23be6bb | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes | import java.io.*;
import java.math.*;
import java.lang.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
Scanner sc = new Scanner(System.in);
int maxn = 1000 + 10;
char s[][]=new char[maxn][maxn];
int inf=1<<30;
int n, m;
n=sc.nextInt();m=sc.nextInt();
for (int i = 0; i < n; ++i)s[i]=sc.next().toCharArray();
int l = inf,r = -inf,u = inf,d = -inf;
for (int i = 0; i < n; ++i){
for (int j = 0; j < m; ++j){
if (s[i][j] == 'X'){
l = Math.min(l,j);
r= Math. max(r,j);
u = Math. min(u,i);
d = Math. max(d,i);
}
}
}
boolean ok = true;
for (int i = u; i <= d; ++i){
for (int j = l; j <= r; ++j){
if (s[i][j]!='X')ok = false;
}
}
if (ok)out.print("YES");
else out.print("NO");
out.flush();
}
} | Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | 92cb53bd01d569ba169e066b162d83a9 | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes | import java.io.*;
import java.util.*;
public class B {
static BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
static BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
public static void main(String[] args) throws IOException {
int n, m;
StringTokenizer nums = new StringTokenizer(input.readLine());
n = Integer.parseInt(nums.nextToken());
m = Integer.parseInt(nums.nextToken());
char[][] puzzle = new char[n][m];
for (int i = 0; i < n; i++) {
puzzle[i] = input.readLine().toCharArray();
}
if (checkIfRect(puzzle)) {
output.write("YES\n");
}
else output.write("NO\n");
output.close();
input.close();
}
static boolean checkIfRect(char[][] p) {
int x, a1, b1, a2, b2;
a1 = a2= b1= b2 = x = 0;
boolean first = false;
for (int i = 0; i < p.length; i++) {
for (int j = 0; j < p[0].length; j++) {
if (p[i][j] == 'X') {
x++;
if (!first) {
a1 = i;
b1= j;
first = true;
}
a2 = i;
b2 = j;
}
}
}
if ((a2 - a1 + 1)*(b2 - b1 + 1) == x) {
for (int i = a1; i <= a2; i++) {
for (int j = b1; j <= b2; j++) {
if (p[i][j] != 'X') return false;
}
}
return true;
}
else return false;
}
} | Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | a6d9fc9c4ce6f5a1c8cb989624dee534 | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class R385Div2B {
public static void main(String[] args) {
FastScanner in=new FastScanner();
int n=in.nextInt();
int m=in.nextInt();
char[][] a=new char[n][m];
for(int i=0;i<n;i++)
a[i]=in.nextToken().toCharArray();
int imin=n+1,imax=-1,jmin=m+1,jmax=-1;
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
if(a[i][j]=='X'){
imin=Math.min(imin,i);
imax=Math.max(imax,i);
jmin=Math.min(jmin,j);
jmax=Math.max(jmax,j);
}
}
}
for(int i=imin;i<=imax;i++)
for(int j=jmin;j<=jmax;j++)
if(a[i][j]!='X'){
System.out.println("NO");
return;
}
System.out.println("YES");
}
static class FastScanner{
BufferedReader br;
StringTokenizer st;
public FastScanner(){br=new BufferedReader(new InputStreamReader(System.in));}
String nextToken(){
while(st==null||!st.hasMoreElements())
try{st=new StringTokenizer(br.readLine());}catch(Exception e){}
return st.nextToken();
}
int nextInt(){return Integer.parseInt(nextToken());}
long nextLong(){return Long.parseLong(nextToken());}
double nextDouble(){return Double.parseDouble(nextToken());}
}
}
| Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | c14e5e58111211fdbf56ab07a1b11b0f | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes |
import java.io.*;
import java.util.*;
/**
*
* @author Sourav Kumar Paul (spaul100)
* NIT Silchar
*/
public class SolveB {
public static Reader in;
public static PrintWriter out;
public static long mod = 1000000007;
public static long inf = 100000000000000000l;
public static long fac[],inv[];
public static int union[];
public static void solve(){
int n = in.nextInt();
int m = in.nextInt();
int count = 0;
int minx= n, maxx=-1, miny = m, maxy = -1;
for(int i=0; i<n; i++)
{
char str[] = in.next().toCharArray();
for(int j=0; j<m; j++)
{
if(str[j] == 'X')
{
count ++;
minx = Math.min(i,minx);
maxx= Math.max(i,maxx);
miny = Math.min(j,miny);
maxy = Math.max(j,maxy);
}
}
}
if((maxx - minx + 1)*(maxy - miny + 1) == count)
out.println("Yes");
else
out.println("No");
}
/**
* ############################### Template ################################
*/
public static class Pair implements Comparable{
int x,y;
Pair(int x, int y)
{
this.x = x;
this.y = y;
}
@Override
public int compareTo(Object o)
{
Pair pp = (Pair)o;
if(pp.x == x)
return 0;
else if (x>pp.x)
return 1;
else
return -1;
}
}
public static void init()
{
for(int i=0; i<union.length; i++)
union[i] = i;
}
public static int find(int n)
{
return (union[n]==n)?n:(union[n]=find(union[n]));
}
public static void unionSet(int i ,int j)
{
union[find(i)]=find(j);
}
public static boolean connected(int i,int j)
{
return union[i]==union[j];
}
public static long gcd(long a, long b) {
long x = Math.min(a,b);
long y = Math.max(a,b);
while(x!=0)
{
long temp = x;
x = y%x;
y = temp;
}
return y;
}
public static long modPow(long base, long exp, long mod) {
base = base % mod;
long result =1;
while(exp > 0)
{
if(exp % 2== 1)
{
result = (result * base) % mod;
exp --;
}
else
{
base = (base * base) % mod;
exp = exp >> 1;
}
}
return result;
}
public static void cal()
{
fac = new long[1000005];
inv = new long[1000005];
fac[0]=1;
inv[0]=1;
for(int i=1; i<=1000000; i++)
{
fac[i]=(fac[i-1]*i)%mod;
inv[i]=(inv[i-1]*modPow(i,mod-2,mod))%mod;
}
}
public static long ncr(int n, int r)
{
return (((fac[n]*inv[r])%mod)*inv[n-r])%mod;
}
SolveB() throws IOException {
in = new Reader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
out.flush();
out.close();
}
public static void main(String args[]) {
new Thread(null, new Runnable() {
public void run() {
try {
new SolveB();
} catch (Exception e) {
e.printStackTrace();
}
}
}, "1", 1 << 26).start();
}
public static class Reader {
public BufferedReader reader;
public StringTokenizer st;
public Reader(InputStreamReader stream) {
reader = new BufferedReader(stream);
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public String nextLine() throws IOException{
return reader.readLine();
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
}
}
| Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | ca86e7e6989ffab85f3238391cd24988 | train_002.jsonl | 1481992500 | Hongcow likes solving puzzles.One day, Hongcow finds two identical puzzle pieces, with the instructions "make a rectangle" next to them. The pieces can be described by an n by m grid of characters, where the character 'X' denotes a part of the puzzle and '.' denotes an empty part of the grid. It is guaranteed that the puzzle pieces are one 4-connected piece. See the input format and samples for the exact details on how a jigsaw piece will be specified.The puzzle pieces are very heavy, so Hongcow cannot rotate or flip the puzzle pieces. However, he is allowed to move them in any directions. The puzzle pieces also cannot overlap.You are given as input the description of one of the pieces. Determine if it is possible to make a rectangle from two identical copies of the given input. The rectangle should be solid, i.e. there should be no empty holes inside it or on its border. Keep in mind that Hongcow is not allowed to flip or rotate pieces and they cannot overlap, i.e. no two 'X' from different pieces can share the same position. | 256 megabytes | import static java.lang.System.exit;
import java.io.*;
import java.util.StringTokenizer;
public class B {
static void solve() throws Exception {
int n = nextInt();
int m = nextInt();
char[][] pazzle = new char[n][m];
for (int i = 0; i < n; i++) {
pazzle[i] = next().toCharArray();
}
int left = 500, top = 0, right = 0, down = 500;
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
if (pazzle[i][j] == 'X') {
if (j < left) left = j;
if (i > top) top = i;
if (j > right) right = j;
if (i < down) down = i;
}
}
}
boolean isRect = true;
outer:
for (int i = down; i <= top; i++) {
for (int j = left; j <= right; j++) {
if (pazzle[i][j] != 'X') {
isRect = false;
break outer;
}
}
}
System.out.println(isRect ? "YES" : "NO");
/*System.out.println(left);
System.out.println(right);
System.out.println(down);
System.out.println(top);*/
}
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static String next() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
public static void main(String[] args) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
} | Java | ["2 3\nXXX\nXXX", "2 2\n.X\nXX", "5 5\n.....\n..X..\n.....\n.....\n....."] | 2 seconds | ["YES", "NO", "YES"] | NoteFor the first sample, one example of a rectangle we can form is as follows 111222111222For the second sample, it is impossible to put two of those pieces without rotating or flipping to form a rectangle.In the third sample, we can shift the first tile by one to the right, and then compose the following rectangle: .......XX................ | Java 8 | standard input | [
"implementation"
] | b395be2597f4cc0478bc45f774fa1c01 | The first line of input will contain two integers n and m (1 ≤ n, m ≤ 500), the dimensions of the puzzle piece. The next n lines will describe the jigsaw piece. Each line will have length m and will consist of characters '.' and 'X' only. 'X' corresponds to a part of the puzzle piece, '.' is an empty space. It is guaranteed there is at least one 'X' character in the input and that the 'X' characters form a 4-connected region. | 1,400 | Output "YES" if it is possible for Hongcow to make a rectangle. Output "NO" otherwise. | standard output | |
PASSED | c520a857595986f2f195fda97fdd911d | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.BufferedWriter;
import java.io.Writer;
import java.util.Scanner;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Nidala
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int x = in.nextInt();
int[] ar = new int[n + 1];
for (int i = 1; i <= n; ++i)
ar[i] = in.nextInt();
int[] pre = new int[n + 2];
int[] suf = new int[n + 2];
for (int i = 1; i <= n; ++i)
pre[i] = pre[i - 1] | ar[i];
for (int i = n; i >= 1; --i)
suf[i] = suf[i + 1] | ar[i];
long ret = 1;
while (k-- > 0)
ret = ret * x;//x^k
long ans = 0;
for (int i = 1; i <= n; ++i)
ans = Math.max(ans, pre[i - 1] | (ar[i] * ret) | suf[i + 1]);
out.println(ans);
}
}
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 close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
public InputReader(InputStream stream) {
this.stream = stream;
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | b205da0da8a7ba568db650bf7a6838d7 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
public class OrGame {
public static void main(String asd[])throws Exception{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int k=in.nextInt();
int x=in.nextInt();
long a[]=new long[n+1];
long pefix[]=new long[n+1];
long suf[]=new long[n+2];
long mul=1;
for(int i=1;i<=k;i++)
mul*=x;
for(int i=1;i<=n;i++)
a[i]=in.nextLong();
//Arrays.sort(a);
for(int i=1;i<=n;i++)
pefix[i]=pefix[i-1]|a[i];
for(int i=n;i>=1;i--)
suf[i]=suf[i+1]|a[i];
long c=0;
for(int i=1;i<=n;i++)
c=Math.max(c,pefix[i-1]|(a[i]*mul)|suf[i+1]);
System.out.println(c);
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 3bc5f971e479fff663e3eb689f36ecb7 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.*;
import java.util.*;
public class D {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
int n = sc.nextInt();
int k = sc.nextInt();
int x = sc.nextInt();
long coef = x;
for (int i = 1; i < k; i++) {
coef *= x;
}
long[] unique = new long[n];
List<Integer> l = new ArrayList<>(n);
long or = 0;
for (int i = 0; i < n; i++) {
int tmp = sc.nextInt();
or |= tmp;
l.add(tmp);
}
int pow = 1;
outer: while (pow <= 1000_000_000) {
if ((pow & or) == 0) {
pow *= 2;
continue;
}
boolean oneFound = false;
int index = -1;
for (int i = 0; i < n; i++) {
int el = l.get(i);
if ((pow & el) != 0) {
if (!oneFound) {
oneFound = true;
index = i;
} else {
pow *= 2;
continue outer;
}
}
}
if (oneFound) {
unique[index] += pow;
}
pow *= 2;
}
long biggest = 0;
for (int i = 0; i < n; i++) {
long big = l.get(i) * coef;
long cur = or & (~unique[i]);
long tmp = cur | big;
biggest = Math.max(tmp, biggest);
}
System.out.println(biggest);
}
// -----------MyScanner class for faster input thx Flatfoot----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (this.st == null || !this.st.hasMoreElements()) {
try {
this.st = new StringTokenizer(this.br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return this.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 = this.br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
// --------------------------------------------------------
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | f0de80d2e9b3908f6a81db612d54c921 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class ORgame {
static long []arr;
static int x;
static int n;
static int k;
static long[][] memo;
static long pre[];
static long post[];
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
k=sc.nextInt();
x=sc.nextInt();
arr=new long[n];
pre=new long[n];
post=new long[n];
for(int i=0;i<n;i++) {
arr[i]=sc.nextInt();
}
long max=0;
// System.out.println(sum);
pre[0]=0;
for(int i=1;i<n;i++) {
pre[i]=pre[i-1]|arr[i-1];
}
// post[n-1]=arr[n-1];
for(int i=n-2;i>=0;i--) {
post[i]=post[i+1]|arr[i+1];
}
for(int i=0;i<n;i++) {
long sum=(long) (arr[i]*Math.pow(x, k));
sum=sum|pre[i]|post[i];
max= Math.max(max, sum);
}
System.out.println(max);
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 2e6746c463babce3239b88ee4ec2ae43 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.*;
import java.util.*;
public class B578 {
public static void main(String[] args) throws IOException {
input.init(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = input.nextInt(), k = input.nextInt(), x = input.nextInt();
long[] a = new long[n];
for(int i = 0; i<n; i++) a[i] = input.nextInt();
long[] pref = new long[n], suff = new long[n];
for(int i = 0; i<n; i++) pref[i] = (a[i]) | (i == 0 ? 0 : pref[i-1]);
for(int i = n-1; i>=0; i--) suff[i] = a[i] | (i == n-1 ? 0 : suff[i+1]);
long max = 0;
for(int i = 0; i<n; i++)
{
long other = (i == 0 ? 0 : pref[i-1]) | (i == n-1 ? 0 : suff[i+1]);
for(int j = 0; j<=k; j++)
{
long cur = other | a[i];
max = Math.max(max, cur);
a[i] *= x;
}
}
out.println(max);
out.close();
}
public static class input {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | e6a33fe5c23ce83db58256a8a57d922e | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.util.StringTokenizer;
/**
* 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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int x = in.nextInt();
int[] a = new int[n];
int[] orLeft = new int[n];
int[] orRight = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
if (i == 0) {
orLeft[i] = a[i];
} else {
orLeft[i] = orLeft[i - 1] | a[i];
}
}
orRight[n - 1] = a[n - 1];
for (int i = n - 2; i >= 0; i--) {
orRight[i] = orRight[i + 1] | a[i];
}
long max = orLeft[n - 1];
for (int i = 0; i < n; i++) {
long left = i == 0 ? 0 : orLeft[i - 1];
long right = i == n - 1 ? 0 : orRight[i + 1];
long cur = left | right;
long num = a[i];
for (int h = 0; h < k; h++) {
num *= x;
max = Math.max(max, cur | num);
}
}
out.println(max);
}
}
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());
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 21563308f41a08a66410516be85d4dad | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.*;
import java.util.*;
public class Template implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException {
try {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
} catch (Exception e) {
String filename = "";
if (filename.isEmpty()) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader(filename + ".in"));
out = new PrintWriter(filename + ".out");
}
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine());
} catch (Exception e) {
return null;
}
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
int[] readIntArray(int size) throws IOException {
int[] res = new int[size];
for (int i = 0; i < size; i++) {
res[i] = readInt();
}
return res;
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
<T> List<T>[] createGraphList(int size) {
List<T>[] list = new List[size];
for (int i = 0; i < size; i++) {
list[i] = new ArrayList<>();
}
return list;
}
public static void main(String[] args) {
new Thread(null, new Template(), "", 1l * 200 * 1024 * 1024).start();
}
long timeBegin, timeEnd;
void time() {
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
long memoryTotal, memoryFree;
void memory() {
memoryFree = Runtime.getRuntime().freeMemory();
System.err.println("Memory = " + ((memoryTotal - memoryFree) >> 10)
+ " KB");
}
public void run() {
try {
timeBegin = System.currentTimeMillis();
memoryTotal = Runtime.getRuntime().freeMemory();
init();
solve();
out.close();
if (System.getProperty("ONLINE_JUDGE") == null) {
time();
memory();
}
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
void solve() throws IOException {
int n = readInt();
int k = readInt();
int x = readInt();
long[] pow = new long[k + 1];
pow[0] = 1;
for (int i = 1; i <= k; i++) {
pow[i] = pow[i - 1] * x;
}
int[] a = readIntArray(n);
int[] p = new int[n];
int[] s = new int[n];
for (int i = 1; i < n; i++) {
p[i] = a[i - 1] | p[i - 1];
}
for (int i = n - 2; i >= 0; i--) {
s[i] = a[i + 1] | s[i + 1];
}
long max = 0;
for (int i = 0; i < n; i++) {
for (int num = 0; num <= k; num++) {
max = Math.max(max, p[i] | s[i] | ((long) a[i] * pow[num]));
}
}
out.println(max);
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 5f2810d44c672c03a59bc9761fc5504c | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.util.*;
import org.omg.CORBA.INTERNAL;
import java.awt.List;
import java.io.*;
import java.lang.*;
import java.lang.reflect.Array;
public class code1
{
public static int mx = 1228228;
public static long[][] dp = new long[mx][21];
public static long[] a = new long[mx];
public static long[] b = new long[mx];
public static void main(String[] args)
{
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(System.out);
//Code starts..
int n = in.nextInt();
int k = in.nextInt();
int x = in.nextInt();
long mul = 1;
while(k-->0)
mul *= x;
long[] a= new long[n+1];
for(int i=0; i<n; i++)
a[i+1] = in.nextLong();
long[] pf = new long[n+2];
long[] sf = new long[n+2];
for(int i=1; i<=n; i++)
pf[i] = pf[i-1] | a[i];
for(int i=n; i>=0; i--)
sf[i] = sf[i+1] | a[i];
long ans = 0;
for(int i=1; i<=n; i++)
{
long tmp = pf[i-1] | (a[i]*mul) | sf[i+1];
ans = Math.max(ans, tmp);
// long tmp2 = pf[]
}
pw.println(ans);
//Code ends....
pw.flush();
pw.close();
}
static class InputReader
{
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int snext()
{
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars)
{
curChar = 0;
try
{
snumChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
int res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = snext();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n)
{
int a[] = new int[n];
for (int i = 0; i < n; i++)
{
a[i] = nextInt();
}
return a;
}
public String readString()
{
int c = snext();
while (isSpaceChar(c))
{
c = snext();
}
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine()
{
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c)
{
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static long c = 0;
public static long mod = 1000000007;
public static int d;
public static int p;
public static int q;
public static boolean flag;
public static long INF= Long.MAX_VALUE;
public static long fun(int[] a, int[] b, int m,int n) {
long result =0;
for(int i=0; i<m; i++)
for(int j=0; j<m; j++)
{
long[] fib = new long[Math.max(2, n+2)];
fib[1] = a[i];
fib[2] = b[j];
for(int k=3; k<=n; k++)
fib[k] = (fib[k-1]%mod + fib[k-2]%mod)%mod;
result = (result%mod + fib[n]%mod)%mod;
}
return result;
}
public static double slope(pair p1, pair p2)
{
double m = INF;
if((p1.x - p2.x)!=0)
m = p1.y - p2.y/(p1.x - p2.x);
return Math.abs(m);
}
public static int count(String[] s, int f)
{
int count = 0;
int n = s[0].length();
if(f==1)
{
for(int i = 0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i%2==0)
{
if(j%2==0 && s[i].charAt(j)=='0')
count++;
if(j%2==1 && s[i].charAt(j)=='1')
count++;
}
if(i%2==1)
{
if(j%2==1 && s[i].charAt(j)=='0')
count++;
if(j%2==0 && s[i].charAt(j)=='1')
count++;
}
}
}
}
else
{
count = 0;
for(int i = 0; i<n; i++)
{
for(int j=0; j<n; j++)
{
if(i%2==1)
{
if(j%2==0 && s[i].charAt(j)=='0')
count++;
if(j%2==1 && s[i].charAt(j)=='1')
count++;
}
if(i%2==0)
{
if(j%2==1 && s[i].charAt(j)=='0')
count++;
if(j%2==0 && s[i].charAt(j)=='1')
count++;
}
}
}
}
return count;
}
public static int gcd(int p2, int p22)
{
if (p2 == 0)
return (int) p22;
return gcd(p22%p2, p2);
}
public static int findGCD(int arr[], int n)
{
int result = arr[0];
for (int i=1; i<n; i++)
result = gcd(arr[i], result);
return result;
}
public static void nextGreater(long[] a, int[] ans)
{
Stack<Integer> stk = new Stack<>();
stk.push(0);
for(int i=1; i<a.length; i++)
{
if(!stk.isEmpty())
{
int s = stk.pop();
while(a[s]<a[i])
{
ans[s] = i;
if(!stk.isEmpty())
s = stk.pop();
else
break;
}
if(a[s]>=a[i])
stk.push(s);
}
stk.push(i);
}
return;
}
public static void nextGreaterRev(long[] a, int[] ans)
{
int n = a.length;
int[] pans = new int[n];
Arrays.fill(pans, -1);
long[] arev = new long[n];
for(int i=0; i<n; i++)
arev[i] = a[n-1-i];
Stack<Integer> stk = new Stack<>();
stk.push(0);
for(int i=1; i<n; i++)
{
if(!stk.isEmpty())
{
int s = stk.pop();
while(arev[s]<arev[i])
{
pans[s] = n - i-1;
if(!stk.isEmpty())
s = stk.pop();
else
break;
}
if(arev[s]>=arev[i])
stk.push(s);
}
stk.push(i);
}
//for(int i=0; i<n; i++)
//System.out.print(pans[i]+" ");
for(int i=0; i<n; i++)
ans[i] = pans[n-i-1];
return;
}
public static void nextSmaller(long[] a, int[] ans)
{
Stack<Integer> stk = new Stack<>();
stk.push(0);
for(int i=1; i<a.length; i++)
{
if(!stk.isEmpty())
{
int s = stk.pop();
while(a[s]>a[i])
{
ans[s] = i;
if(!stk.isEmpty())
s = stk.pop();
else
break;
}
if(a[s]<=a[i])
stk.push(s);
}
stk.push(i);
}
return;
}
public static long lcm(int[] numbers) {
long lcm = 1;
int divisor = 2;
while (true) {
int cnt = 0;
boolean divisible = false;
for (int i = 0; i < numbers.length; i++) {
if (numbers[i] == 0) {
return 0;
} else if (numbers[i] < 0) {
numbers[i] = numbers[i] * (-1);
}
if (numbers[i] == 1) {
cnt++;
}
if (numbers[i] % divisor == 0) {
divisible = true;
numbers[i] = numbers[i] / divisor;
}
}
if (divisible) {
lcm = lcm * divisor;
} else {
divisor++;
}
if (cnt == numbers.length) {
return lcm;
}
}
}
public static long fact(long n) {
long factorial = 1;
for(int i = 1; i <= n; i++)
{
factorial *= i;
}
return factorial;
}
public static void factSieve(int[] a, int n) {
for(int i=2; i<=n; i+=2)
a[i] = 2;
for(int i=3; i<=n; i+=2)
{
if(a[i]==0)
{
a[i] = i;
for(int j=i; j*i<=n; j++)
{
a[i*j] = i;
}
}
}
int k = 1000;
while(k!=1)
{
System.out.print(a[k]+" ");
k /= a[k];
}
}
public static int lowerLimit(int[] a, int n) {
int ans = 0;
int ll = 0;
int rl = a.length-1;
// System.out.println(a[rl]+" "+n);
if(a[0]>n)
return 0;
if(a[0]==n)
return 1;
else if(a[rl]<=n)
return rl+1;
while(ll<=rl)
{
int mid = (ll+rl)/2;
if(a[mid]==n)
{
ans = mid + 1;
break;
}
else if(a[mid]>n)
{
rl = mid-1;
}
else
{
ans = mid+1;
ll = mid+1;
}
}
return ans;
}
public static long choose(long total, long choose){
if(total < choose)
return 0;
if(choose == 0 || choose == total)
return 1;
return (choose(total-1,choose-1)+choose(total-1,choose))%mod;
}
public static int[] suffle(int[] a,Random gen)
{
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
return a;
}
public static long[] sort(long[] a)
{
Random gen = new Random();
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
long temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
Arrays.sort(a);
return a;
}
public static pair[] sort(pair[] a)
{
Random gen = new Random();
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
pair temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
Arrays.sort(a);
return a;
}
public static int[] sort(int[] a)
{
Random gen = new Random();
int n = a.length;
for(int i=0;i<n;i++)
{
int ind = gen.nextInt(n-i)+i;
int temp = a[ind];
a[ind] = a[i];
a[i] = temp;
}
Arrays.sort(a);
return a;
}
public static int floorSearch(int arr[], int low, int high, int x)
{
if (low > high)
return -1;
if (x > arr[high])
return high;
int mid = (low+high)/2;
if (mid > 0 && arr[mid-1] < x && x < arr[mid])
return mid-1;
if (x < arr[mid])
return floorSearch(arr, low, mid-1, x);
return floorSearch(arr, mid+1, high, x);
}
public static void swap(int a, int b){
int temp = a;
a = b;
b = temp;
}
public static ArrayList<Integer> primeFactorization(int n)
{
ArrayList<Integer> a =new ArrayList<Integer>();
for(int i=2;i*i<=n;i++)
{
while(n%i==0)
{
a.add(i);
n/=i;
}
}
if(n!=1)
a.add(n);
return a;
}
public static void sieve(boolean[] isPrime,int n)
{
for(int i=1;i<n;i++)
isPrime[i] = true;
isPrime[0] = false;
isPrime[1] = false;
for(int i=2;i*i<n;i++)
{
if(isPrime[i] == true)
{
for(int j=(2*i);j<n;j+=i)
isPrime[j] = false;
}
}
}
public static int lowerbound(ArrayList<Long> net, long c2) {
int i=Collections.binarySearch(net, c2);
if(i<0)
i = -(i+2);
return i;
}
public static int lowerboundArray(long[] psum, long c2) {
int i=Arrays.binarySearch(psum, c2);
if(i<0)
i = -(i+2);
return i;
}
public static int lowerboundArray(int[] psum, int c2) {
int i=Arrays.binarySearch(psum, c2);
if(i<0)
i = -(i+2);
return i;
}
public static int uperboundArray(long[] psum, long c2) {
int i=Arrays.binarySearch(psum, c2);
if(i<0)
i = -(i+1);
return i;
}
public static int uperbound(ArrayList<Long> net, long c2) {
int i=Collections.binarySearch(net, c2);
if(i<0)
i = -(i+1);
return i;
}
public static int GCD(int a,int b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static long GCD(long a,long b)
{
if(b==0)
return a;
else
return GCD(b,a%b);
}
public static void extendedEuclid(int A,int B)
{
if(B==0)
{
d = A;
p = 1 ;
q = 0;
}
else
{
extendedEuclid(B, A%B);
int temp = p;
p = q;
q = temp - (A/B)*q;
}
}
public static long LCM(long a,long b)
{
return (a*b)/GCD(a,b);
}
public static int LCM(int a,int b)
{
return (a*b)/GCD(a,b);
}
public static int binaryExponentiation(int x,int n)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=x*x;
n=n/2;
}
return result;
}
public static int[] countDer(int n)
{
int der[] = new int[n + 1];
der[0] = 1;
der[1] = 0;
der[2] = 1;
for (int i = 3; i <= n; ++i)
der[i] = (i - 1) * (der[i - 1] + der[i - 2]);
// Return result for n
return der;
}
static long binomialCoeff(int n, int k)
{
long C[][] = new long[n+1][k+1];
int i, j;
// Calculate value of Binomial Coefficient in bottom up manner
for (i = 0; i <= n; i++)
{
for (j = 0; j <= Math.min(i, k); j++)
{
// Base Cases
if (j == 0 || j == i)
C[i][j] = 1;
// Calculate value using previosly stored values
else
C[i][j] = C[i-1][j-1] + C[i-1][j];
}
}
return C[n][k];
}
public static long binaryExponentiation(long x,long n)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=result * x;
x=(x%mod * x%mod)%mod;
n=n/2;
}
return result;
}
public static int modularExponentiation(int x,int n,int M)
{
int result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result * x)%M;
x=(x%M*x%M)%M;
n=n/2;
}
return result;
}
public static long modularExponentiation(long x,long n,long M)
{
long result=1;
while(n>0)
{
if(n % 2 ==1)
result=(result %M* x%M)%M;
x=(x*x)%M;
n=n/2;
}
return result;
}
public static int modInverse(int A,int M)
{
return modularExponentiation(A,M-2,M);
}
public static long modInverse(long A,long M)
{
return modularExponentiation(A,M-2,M);
}
public static boolean checkYear(int year)
{
if (year % 400 == 0)
return true;
if (year % 100 == 0)
return false;
if (year % 4 == 0)
return true;
return false;
}
public static boolean isPrime(int n)
{
if (n <= 1) return false;
if (n <= 3) return true;
if (n%2 == 0 || n%3 == 0)
return false;
for (int i=5; i*i<=n; i=i+6)
{
if (n%i == 0 || n%(i+2) == 0)
return false;
}
return true;
}
static class pair implements Comparable<pair>
{
Long x;
Long y;
pair(long a,long l)
{
this.x=a;
this.y=l;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
return result;
}
public String toString()
{
return x+" "+y;
}
public boolean equals(Object o)
{
if (o instanceof pair)
{
pair p = (pair)o;
return p.x == x && p.y == y ;
}
return false;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode();
}
}
static class triplet implements Comparable<triplet>
{
Long x,y;
Integer z;
triplet(long l,long m,int z)
{
this.x = l;
this.y = m;
this.z = z;
}
public int compareTo(triplet o)
{
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
if(result==0)
result = z.compareTo(o.z);
return result;
}
public boolean equlas(Object o)
{
if(o instanceof triplet)
{
triplet p = (triplet)o;
return x==p.x && y==p.y && z==p.z;
}
return false;
}
public String toString()
{
return x+" "+y+" "+z;
}
public int hashCode()
{
return new Long(x).hashCode()*31 + new Long(y).hashCode() + new Long(z).hashCode();
}
}
/*static class node implements Comparable<node>
{
Integer x, y, z;
node(int x,int y, int z)
{
this.x=x;
this.y=y;
this.z=z;
}
public int compareTo(pair o) {
int result = x.compareTo(o.x);
if(result==0)
result = y.compareTo(o.y);
if(result==0)
result = z.compareTo(z);
return result;
}
@Override
public int compareTo(node o) {
// TODO Auto-generated method stub
return 0;
}
}
*/
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 90696fa31d51829cb078acdb2e10495c | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.*; //PrintWriter
import java.math.*; //BigInteger, BigDecimal
import java.util.*; //StringTokenizer, ArrayList
public class R320_Div2_D
{
FastReader in;
PrintWriter out;
public static void main(String[] args) {
new R320_Div2_D().run();
}
void run()
{
in = new FastReader(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
void solve()
{
int n = in.nextInt();
int k = in.nextInt();
int x = in.nextInt();
long[] a = new long[n];
long maxh = 0;
for (int i = 0; i < n; i++)
{
a[i] = in.nextInt();
long h = Long.highestOneBit(a[i]);
if (h > maxh)
maxh = h;
}
//Input: 5 1 3
//Input: 1 5 13 8 16
//Answer = 63, not 61
ArrayList<Integer> ind = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
//if (Long.highestOneBit(a[i]) == maxh || n < 6)
ind.add(i);
int[] bit = new int[64];
int[] bitCopy = new int[64];
for (int i = 0; i < n; i++)
{
long temp = a[i];
for (int j = 0; j < 64; j++)
{
long b = temp & 1L;
bit[j]+= b;
temp = temp >> 1;
}
}
for (int jj = 0; jj < 64; jj++)
bitCopy[jj] = bit[jj];
long max = 0;
for (int j = 0; j < ind.size(); j++)
{
for (int jj = 0; jj < 64; jj++)
bit[jj] = bitCopy[jj];
int aInd = ind.get(j);
long temp = a[aInd];
//subtract
for (int jj = 0; jj < 64; jj++)
{
long b = temp & 1L;
bit[jj]-= b;
temp = temp >> 1;
}
temp = a[aInd];
for (int i = 0; i < k; i++)
temp *= x;
for (int jj = 0; jj < 64; jj++)
{
long b = temp & 1L;
bit[jj]+= b;
temp = temp >> 1;
}
long bitOr = 0;
for (int i = 63; i >= 0; i--)
if (bit[i] > 0)
bitOr = bitOr * 2 + 1;
else
bitOr *= 2;
if (bitOr > max) max = bitOr;
}
out.println(max);
}
//-----------------------------------------------------
void runWithFiles() {
in = new FastReader(new File("input.txt"));
try {
out = new PrintWriter(new File("output.txt"));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
solve();
out.close();
}
class FastReader
{
BufferedReader br;
StringTokenizer tokenizer;
public FastReader(InputStream stream)
{
br = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public FastReader(File f) {
try {
br = new BufferedReader(new FileReader(f));
tokenizer = null;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens())
try {
tokenizer = new StringTokenizer(br.readLine());
}
catch (IOException e) {
throw new RuntimeException(e);
}
return tokenizer.nextToken();
}
public String nextLine() {
try {
return br.readLine();
}
catch(Exception e) {
throw(new RuntimeException());
}
}
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());
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 76d88a89ab69f8408d3127c9204fc136 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.*; //PrintWriter
import java.math.*; //BigInteger, BigDecimal
import java.util.*; //StringTokenizer, ArrayList
public class R320_Div2_D
{
FastReader in;
PrintWriter out;
public static void main(String[] args) {
new R320_Div2_D().run();
}
void run()
{
in = new FastReader(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
void solve()
{
int n = in.nextInt();
int k = in.nextInt();
int x = in.nextInt();
long[] a = new long[n];
long maxh = 0;
for (int i = 0; i < n; i++)
{
a[i] = in.nextInt();
long h = Long.highestOneBit(a[i]);
if (h > maxh)
maxh = h;
}
ArrayList<Integer> ind = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
if (Long.highestOneBit(a[i]) == maxh || n < 100)
ind.add(i);
int[] bit = new int[64];
int[] bitCopy = new int[64];
for (int i = 0; i < n; i++)
{
long temp = a[i];
for (int j = 0; j < 64; j++)
{
long b = temp & 1L;
bit[j]+= b;
temp = temp >> 1;
}
}
for (int jj = 0; jj < 64; jj++)
bitCopy[jj] = bit[jj];
long max = 0;
for (int j = 0; j < ind.size(); j++)
{
for (int jj = 0; jj < 64; jj++)
bit[jj] = bitCopy[jj];
int aInd = ind.get(j);
long temp = a[aInd];
//subtract
for (int jj = 0; jj < 64; jj++)
{
long b = temp & 1L;
bit[jj]-= b;
temp = temp >> 1;
}
temp = a[aInd];
for (int i = 0; i < k; i++)
temp *= x;
for (int jj = 0; jj < 64; jj++)
{
long b = temp & 1L;
bit[jj]+= b;
temp = temp >> 1;
}
long bitOr = 0;
for (int i = 63; i >= 0; i--)
if (bit[i] > 0)
bitOr = bitOr * 2 + 1;
else
bitOr *= 2;
if (bitOr > max) max = bitOr;
}
out.println(max);
}
//-----------------------------------------------------
void runWithFiles() {
in = new FastReader(new File("input.txt"));
try {
out = new PrintWriter(new File("output.txt"));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
solve();
out.close();
}
class FastReader
{
BufferedReader br;
StringTokenizer tokenizer;
public FastReader(InputStream stream)
{
br = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public FastReader(File f) {
try {
br = new BufferedReader(new FileReader(f));
tokenizer = null;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens())
try {
tokenizer = new StringTokenizer(br.readLine());
}
catch (IOException e) {
throw new RuntimeException(e);
}
return tokenizer.nextToken();
}
public String nextLine() {
try {
return br.readLine();
}
catch(Exception e) {
throw(new RuntimeException());
}
}
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());
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 76267ee59443571ea688cca43ccf257c | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.*; //PrintWriter
import java.math.*; //BigInteger, BigDecimal
import java.util.*; //StringTokenizer, ArrayList
public class R320_Div2_D
{
FastReader in;
PrintWriter out;
public static void main(String[] args) {
new R320_Div2_D().run();
}
void run()
{
in = new FastReader(System.in);
out = new PrintWriter(System.out);
solve();
out.close();
}
void solve()
{
int n = in.nextInt();
int k = in.nextInt();
int x = in.nextInt();
long[] a = new long[n];
long maxh = 0;
for (int i = 0; i < n; i++)
{
a[i] = in.nextInt();
long h = Long.highestOneBit(a[i]);
if (h > maxh)
maxh = h;
}
ArrayList<Integer> ind = new ArrayList<Integer>();
for (int i = 0; i < n; i++)
if (Long.highestOneBit(a[i]) == maxh || n < 6)
ind.add(i);
int[] bit = new int[64];
int[] bitCopy = new int[64];
for (int i = 0; i < n; i++)
{
long temp = a[i];
for (int j = 0; j < 64; j++)
{
long b = temp & 1L;
bit[j]+= b;
temp = temp >> 1;
}
}
for (int jj = 0; jj < 64; jj++)
bitCopy[jj] = bit[jj];
long max = 0;
for (int j = 0; j < ind.size(); j++)
{
for (int jj = 0; jj < 64; jj++)
bit[jj] = bitCopy[jj];
int aInd = ind.get(j);
long temp = a[aInd];
//subtract
for (int jj = 0; jj < 64; jj++)
{
long b = temp & 1L;
bit[jj]-= b;
temp = temp >> 1;
}
temp = a[aInd];
for (int i = 0; i < k; i++)
temp *= x;
for (int jj = 0; jj < 64; jj++)
{
long b = temp & 1L;
bit[jj]+= b;
temp = temp >> 1;
}
long bitOr = 0;
for (int i = 63; i >= 0; i--)
if (bit[i] > 0)
bitOr = bitOr * 2 + 1;
else
bitOr *= 2;
if (bitOr > max) max = bitOr;
}
out.println(max);
}
//-----------------------------------------------------
void runWithFiles() {
in = new FastReader(new File("input.txt"));
try {
out = new PrintWriter(new File("output.txt"));
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
solve();
out.close();
}
class FastReader
{
BufferedReader br;
StringTokenizer tokenizer;
public FastReader(InputStream stream)
{
br = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public FastReader(File f) {
try {
br = new BufferedReader(new FileReader(f));
tokenizer = null;
}
catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens())
try {
tokenizer = new StringTokenizer(br.readLine());
}
catch (IOException e) {
throw new RuntimeException(e);
}
return tokenizer.nextToken();
}
public String nextLine() {
try {
return br.readLine();
}
catch(Exception e) {
throw(new RuntimeException());
}
}
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());
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | be9b3a96580d4c1962fcc91151e0f63e | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | 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 xwchen
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int x = in.nextInt();
int[] a = in.nextIntArray1(n);
long[] prefix = new long[n + 2];
long[] suffix = new long[n + 2];
long mul = 1;
while (k-- > 0) {
mul *= x;
}
for (int i = 1; i <= n; ++i) {
prefix[i] = prefix[i - 1] | a[i];
}
for (int i = n; i >= 1; --i) {
suffix[i] = suffix[i + 1] | a[i];
}
long res = 0;
for (int i = 1; i <= n; ++i) {
res = Math.max(res, prefix[i - 1] | (a[i] * mul) | suffix[i + 1]);
}
out.println(res);
}
}
static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer = new StringTokenizer("");
public InputReader(InputStream inputStream) {
this.reader = new BufferedReader(
new InputStreamReader(inputStream));
}
public String next() {
while (!tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public int[] nextIntArray1(int n) {
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++)
a[i] = nextInt();
return a;
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 0fe8fc6d551e7271987d082ff3852197 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | 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 D {
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt(), k = sc.nextInt(), x = sc.nextInt();
int N = 1; while(N < n) N <<= 1;
int[] a = new int[N + 1]; for(int i = 1; i <= n; i++) a[i] = sc.nextInt();
SegmentTree st = new SegmentTree(a);
long ans = 0, pow = (long) Math.pow(x, k);
for(int i = 1; i <= n; i++)
ans = Math.max(ans, st.query(i+1, n) | st.query(1, i-1) | a[i] * pow);
out.println(ans);
out.flush();
out.close();
}
static class SegmentTree {
int N;
int[] array, sTree, lazy;
SegmentTree(int[] in)
{
array = in; N = in.length - 1;
sTree = new int[N<<1];
lazy = new int[N<<1];
build(1,1,N);
}
void build(int node, int b, int e) // O(n)
{
if(b == e)
sTree[node] = array[b];
else
{
int mid = b + e >> 1;
build(node<<1,b,mid);
build(node<<1|1,mid+1,e);
sTree[node] = sTree[node<<1] | sTree[node<<1|1];
}
}
int query(int i, int j)
{
return query(1,1,N,i,j);
}
int query(int node, int b, int e, int i, int j) // O(log n)
{
if(i>e || j <b)
return 0;
if(b>= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
int q1 = query(node<<1,b,mid,i,j);
int q2 = query(node<<1|1,mid+1,e,i,j);
return q1 | q2;
}
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream System){ br = new BufferedReader(new InputStreamReader(System)); }
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine()throws IOException{return br.readLine();}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public double nextDouble() throws IOException {return Double.parseDouble(next());}
public char nextChar()throws IOException{return next().charAt(0);}
public Long nextLong()throws IOException{return Long.parseLong(next());}
public boolean ready() throws IOException{return br.ready();}
public void waitForInput(){for(long i = 0; i < 3e9; i++);}
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 80fc5a37ad3a2ac1a6e9c22edce4a20a | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes |
//package busywithsolving;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class OrGame {
static BufferedReader br = new BufferedReader( new InputStreamReader(System.in) );
static int n,k,x;
public static void main( String[] args ) throws IOException
{
String[] str = br.readLine().split(" ");
n = Integer.parseInt(str[0]);
k = Integer.parseInt(str[1]);
x = Integer.parseInt(str[2]);
str = br.readLine().split(" ");
long Arr[] = new long[n+2];
for(int i=0;i<n;i++)
{
Arr[i] = Integer.parseInt(str[i]);
}
long Prfx[] = new long[n+2];
for(int i=0;i<n;i++)
{
Prfx[i+1] = Prfx[i]|Arr[i];
}
long Sffx[] = new long[n+2];
for(int i=n-1;i>=0;i--)
{
Sffx[i] = Sffx[i+1]|Arr[i];
}
int Now = (int) Math.pow(x, k);
long MX = 0;
for(int i=0;i<n;i++)
{
long vl = Prfx[i] | ( Arr[i]*Now ) | Sffx[i+1];
MX = Math.max( MX,vl );
}
System.out.println(MX);
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | bf5f541e64a250113bd58e019b2ad683 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
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 Alex
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
int BITS = 30;
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int operations = in.readInt(); // k
int multiplier = in.readInt(); // x
long bigmult = 1;
for (int i = 0; i < operations; i++) bigmult *= multiplier;
int[] a = IOUtils.readIntArray(in, n);
int[] bitcount = new int[BITS];
long ans = 0;
for (int val : a) {
for (int bit = 0; bit < BITS; bit++) {
if ((val & (1 << bit)) != 0) {
bitcount[bit]++;
}
}
}
for (int val : a) {
for (int bit = 0; bit < BITS; bit++) {
if ((val & (1 << bit)) != 0) {
bitcount[bit]--;
}
}
long tmp = bigmult * val;
for (int bit = 0; bit < BITS; bit++) {
if (bitcount[bit] > 0) {
tmp |= (1 << bit);
}
}
ans = Math.max(ans, tmp);
for (int bit = 0; bit < BITS; bit++) {
if ((val & (1 << bit)) != 0) {
bitcount[bit]++;
}
}
}
out.printLine(ans);
}
}
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 close() {
writer.close();
}
public void printLine(long i) {
writer.println(i);
}
}
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);
}
}
static class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.readInt();
return array;
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 5417b93bdb0583f5fda8223c9cc6082c | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
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 Alex
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
int MAXBITS = 30;
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt(), ops = in.readInt(), multiplier = in.readInt();
int[] a = IOUtils.readIntArray(in, n);
int[] count = new int[MAXBITS];
for(int val : a) {
int bitindex = 0;
while(val > 0) {
if(val % 2 == 1) count[bitindex]++;
bitindex++;
val /= 2;
}
}
long res = 0;
for(int val : a) {
int otherval = 0;
for(int i = 0; i < MAXBITS; i++) {
int num = (1 << i);
if(count[i] > 1 ||
(count[i] == 1 && ((num & val) != num))) {
otherval += num;
}
}
long thisval = val;
for(int i = 0; i < ops; i++) thisval *= multiplier;
res = Math.max(res, thisval | otherval);
}
out.printLine(res);
}
}
static class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for(int i = 0; i < size; i++)
array[i] = in.readInt();
return array;
}
}
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 close() {
writer.close();
}
public void printLine(long i) {
writer.println(i);
}
}
static 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);
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | f68db8481281e0d51a80ed2805f71dac | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes |
//package javaapplication1;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n, k;
long x;
n = in.nextInt();
k = in.nextInt();
x = in.nextLong();
long a[] = new long[n + 1];
for(int i = 1 ; i <= n ; i ++)
a[i] = in.nextLong();
long mul[] = new long[k + 1];
mul[0] = 1;
for(int i = 1 ; i <= k ; i ++)
mul[i] = mul[i - 1] * x;
long [][] dp = new long[2][];
for(int i = 0 ; i < 2 ; i ++)
dp[i] = new long[n + 2];
for(int i = 1 ; i <= n ; i ++)
dp[0][i] = dp[0][i - 1] | a[i];
for(int i = n ; i >= 1 ; i --)
dp[1][i] = dp[1][i + 1] | a[i];
long res = -1;
for(int i = 1 ; i <= n ; i ++)
for(int j = 1 ; j <= k ; j ++)
{
res = Math.max(res, dp[0][i - 1] | (a[i] * mul[j]) | dp[1][i + 1]);
}
System.out.println(res);
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 3facb9452c9508349fc6e6be9fef286c | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes |
import java.io.*;
import java.util.*;
public class B {
InputStream is;
int __t__ = 1;
int __f__ = 0;
int __FILE_DEBUG_FLAG__ = __f__;
String __DEBUG_FILE_NAME__ = "src/T";
FastScanner in;
PrintWriter out;
public void solve() {
int n = in.nextInt(), k = in.nextInt(), x = in.nextInt();
long[] a = in.nextLongArray(n);
int[] count = new int[64];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 64; j++) {
if ((a[i] & (1L << j)) != 0) count[j]++;
}
}
long res = 0;
for (int j = 0; j < 64; j++) {
if (count[j] > 0) res |= (1L << j);
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < 32; j++) {
if ((a[i] & (1L << j)) != 0) count[j]--;
}
long next = a[i];
for (int j = 0; j < k; j++) next *= x;
for (int j = 0; j < 64; j++) {
if ((next & (1L << j)) != 0) count[j]++;
}
long now = 0;
for (int j = 0; j < 64; j++) {
if (count[j] > 0) now |= (1L << j);
}
res = Math.max(now, res);
for (int j = 0; j < 64; j++) {
if ((next & (1L << j)) != 0) count[j]--;
}
for (int j = 0; j < 32; j++) {
if ((a[i] & (1L << j)) != 0) count[j]++;
}
}
System.out.println(res);
}
public void run() {
if (__FILE_DEBUG_FLAG__ == __t__) {
try {
is = new FileInputStream(__DEBUG_FILE_NAME__);
} catch (FileNotFoundException e) {
// TODO 自動生成された catch ブロック
e.printStackTrace();
}
System.out.println("FILE_INPUT!");
} else {
is = System.in;
}
in = new FastScanner(is);
out = new PrintWriter(System.out);
solve();
}
public static void main(String[] args) {
new B().run();
}
public void mapDebug(int[][] a) {
System.out.println("--------map display---------");
for (int i = 0; i < a.length; i++) {
for (int j = 0; j < a[i].length; j++) {
System.out.printf("%3d ", a[i][j]);
}
System.out.println();
}
System.out.println("----------------------------");
System.out.println();
}
public void debug(Object... obj) {
System.out.println(Arrays.deepToString(obj));
}
class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public FastScanner(InputStream stream) {
this.stream = stream;
//stream = new FileInputStream(new File("dec.in"));
}
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++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; i++)
array[i] = nextInt();
return array;
}
int[][] nextIntMap(int n, int m) {
int[][] map = new int[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextIntArray(m);
}
return map;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; i++)
array[i] = nextLong();
return array;
}
long[][] nextLongMap(int n, int m) {
long[][] map = new long[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextLongArray(m);
}
return map;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] array = new double[n];
for (int i = 0; i < n; i++)
array[i] = nextDouble();
return array;
}
double[][] nextDoubleMap(int n, int m) {
double[][] map = new double[n][m];
for (int i = 0; i < n; i++) {
map[i] = in.nextDoubleArray(m);
}
return map;
}
String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
String[] nextStringArray(int n) {
String[] array = new String[n];
for (int i = 0; i < n; i++)
array[i] = next();
return array;
}
String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 6a3b942df51f5f311462244884315b3e | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.util.*;
import java.io.*;
public class CF578B_DP{
public static int n, k;
public static long a[], x, dp[][], mul[];
public static void main(String[] args)throws Exception {
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
n = in.nextInt();
k = in.nextInt();
x = in.nextInt();
a = new long[n+5];
dp = new long[n+5][k+5];
mul = new long[k+5];
mul[0] = 1;
for(int i = 1; i <= k; i++){
mul[i] = mul[i-1]*x;
}
for(int i = 0; i <= n+4; i++) Arrays.fill(dp[i], -1);
for(int i = 1; i <= n; i++){
a[i] = in.nextLong();
}
long max1 = solve(1, 0);
for(int i = 1, j = n; i < j; i++, j--){
swap(i, j);
}
for(int i = 0; i <= n+4; i++) Arrays.fill(dp[i], -1);
long max2 = solve(1, 0);
pw.println(Math.max(max1, max2));
pw.close();
}
static void swap(int i, int j){
long temp = a[i];
a[i] = a[j];
a[j] = temp;
}
static long solve(int x, int y){
if(x > n) return 0;
if(dp[x][y] != -1) return dp[x][y];
long max = 0;
for(int i = 0; y+i <= k; i++){
max = Math.max(max, solve(x+1, y+i)|(a[x]*mul[i]));
}
return dp[x][y] = max;
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next()throws Exception {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
public String nextLine()throws Exception {
String line = null;
tokenizer = null;
line = reader.readLine();
return line;
}
public int nextInt()throws Exception {
return Integer.parseInt(next());
}
public double nextDouble() throws Exception{
return Double.parseDouble(next());
}
public long nextLong()throws Exception {
return Long.parseLong(next());
}
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | cce1c4099da5cd86ba561167c69c728e | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.util.*;
import java.io.*;
public class CF578B{
public static int n, k;
public static long max, a[], x, suffix[], prefix[];
public static void main(String[] args)throws Exception {
InputReader in = new InputReader(System.in);
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
n = in.nextInt();
k = in.nextInt();
x = in.nextInt();
a = new long[n+5];
prefix = new long[n+5];
suffix = new long[n+5];
max = 0;
for(int i = 1; i <= n; i++){
a[i] = in.nextInt();
prefix[i] = a[i] | prefix[i-1];
}
for(int i = n; i >= 1; i--){
suffix[i] = a[i] | suffix[i+1];
}
long mul = 1;
for(int i = 1; i <= k; i++){
mul *= x;
}
for(int i = 1; i <= n; i++){
max = Math.max(max, prefix[i-1] | (a[i]*mul) | suffix[i+1]);
}
pw.println(max);
pw.close();
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next()throws Exception {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
public String nextLine()throws Exception {
String line = null;
tokenizer = null;
line = reader.readLine();
return line;
}
public int nextInt()throws Exception {
return Integer.parseInt(next());
}
public double nextDouble() throws Exception{
return Double.parseDouble(next());
}
public long nextLong()throws Exception {
return Long.parseLong(next());
}
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 45811edaccbef6dfb78b49ce1dfe4f86 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
new Main().run();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
private void run() throws IOException {
if (new File("input.txt").exists())
in = new BufferedReader(new FileReader("input.txt"));
else
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
final int N = 1000 * 1000;
final double EPS = 1e-13;
private void solve() throws IOException {
int n = nextInt();
int k = nextInt();
int x = nextInt();
int a[] = new int[n];
int r[] = new int[n + 1];
int l[] = new int[n + 1];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
l[i + 1] = l[i] | a[i];
}
for (int i = n - 1; i > -1; i--) {
r[i] = r[i + 1] | a[i];
}
long y = 1L;
long ans = r[0];
for (int i = 0; i < k; i++)
y *= x;
for (int i = 0; i < n; i++) {
ans = max(ans, l[i] | (r[i + 1] | (a[i] * y)));
}
out.println(ans);
}
void chk(boolean b) {
if (b)
return;
System.out.println(new Error().getStackTrace()[1]);
exit(999);
}
void deb(String fmt, Object... args) {
System.out.printf(Locale.US, fmt + "%n", args);
}
String nextToken() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | fcf450b46f4fece9a3db05b38f1b74db | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | 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.StringTokenizer;
public class Main {
public static void main(String[] args) throws Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(in, out);
out.close();
}
}
class Task {
public void solve(InputReader in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int x = in.nextInt();
long[] a = new long[n];
int[] b = new int[64];
for(int i = 0; i < n; i++) {
a[i] = in.nextLong();
cal(b, a[i], 1);
}
int t = 1;
for(int i = 0; i < k; i++) {
t *= x;
}
long ans = 0;
for(int i = 0; i < n; i++) {
int[] bit = new int[64];
for(int j = 0; j < 64; j++) {
bit[j] = b[j];
}
long tmp = cal(bit, a[i], -1);
tmp = cal(bit, a[i] * t, 1);
if(tmp > ans) {
ans = tmp;
}
}
out.println(ans);
}
private long cal(int[] b, long x, int v) {
for(int i = 63; i >= 0; i--) {
if(((1L << i) & x) != 0) {
b[i] += v;
}
}
long ans = 0;
for(int i = 63; i >= 0; i--) {
if(b[i] > 0) {
ans |= (1L << i);
}
}
return ans;
}
}
class InputReader {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(nextLine());
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 411e7fe7eb573b498dde51ba9fc82200 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class TaskB {
static int x, y;
static ArrayList<Edge>[] adjList;
static int n, m;
static int INF = (int) 1e9;
static int[] dist;
static int dijkstra(int S) // O(E log E)
{
Arrays.fill(dist, INF);
PriorityQueue<Edge> pq = new PriorityQueue<Edge>();
dist[S] = 0;
pq.add(new Edge(S, 0)); // may add more in case of MSSP (Mult-Source)
while (!pq.isEmpty()) {
Edge cur = pq.remove();
if (cur.cost > dist[cur.node]) // lazy deletion
continue;
for (Edge nxt : adjList[cur.node])
if (cur.cost + nxt.cost < dist[nxt.node])
pq.add(new Edge(nxt.node, dist[nxt.node] = cur.cost + nxt.cost));
}
return -1;
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
long k = sc.nextLong();
long x = sc.nextLong();
long[] arr = new long[n];
long[] pre = new long[n];
long[] post = new long[n];
long x1 = 1;
while (k-- > 0)
x1 *= x;
if (n == 1)
System.out.println(x1*sc.nextLong());
else {
for (int i = 0; i < n; i++) {
arr[i] = sc.nextLong();
if (i == 0) {
pre[i] = arr[i];
} else {
pre[i] = arr[i] | pre[i - 1];
}
}
for (int i = n - 1; i >= 0; i--) {
if (i == n - 1) {
post[i] = arr[i];
} else {
post[i] = arr[i] | post[i + 1];
}
}
long max = Long.MIN_VALUE;
for (int i = 0; i < n; i++) {
long t = (arr[i] * x1);
max = Math.max(max,
(i == 0 ? t | post[i + 1] : i == n - 1 ? t | pre[i - 1] : (post[i + 1] | pre[i - 1] | t)));
}
System.out.println(max);
pw.close();
}
}
static class Pair implements Comparable<Pair> {
int x, y, i;
public Pair(int x, int y, int i) {
this.x = x;
this.y = y;
this.i = i;
}
@Override
public int compareTo(Pair o) {
if (o.x == x)
return y - o.y;
return x - o.x;
}
public String toString() {
return x + " " + y + " " + i;
}
}
static class Pair2 implements Comparable<Pair2> {
int x, y;
long i;
public Pair2(int x, int y, long i) {
this.x = x;
this.y = y;
this.i = i;
}
public int compareTo(Pair2 o) {
if (x == o.x)
if (y == o.y)
return Long.compare(i, o.i);
else
return y - o.y;
return x - o.x;
}
public String toString() {
return x + " " + y + " " + i;
}
}
static class Edge implements Comparable<Edge> {
int node, cost;
Edge(int a, int b) {
node = a;
cost = b;
}
public int compareTo(Edge e) {
return cost - e.cost;
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 76fd27f511c9294dd28bca02e4623b65 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.*;
import java.util.*;
import javax.swing.plaf.synth.SynthSpinnerUI;
public class tr1 {
static PrintWriter out;
static StringBuilder sb;
static int inf = (int) 1e9;
static long mod = (long) 1e9 + 7;
static int[] si;
static ArrayList<Integer> primes;
static HashSet<Integer> pr;
static int n, k, m;
static int[] in;
static HashMap<Integer, Integer> factors;
static HashSet<Integer> f;
static int[] fac, a, b;
static int[] l, r;
static int[][] memo;
static int[] numc;
static ArrayList<Integer>[] ad;
static HashMap<Integer, Integer> hm;
static pair[] ed;
static TreeSet<Integer>[] np;
static TreeMap<pair, Integer> tm;
static int[] le, re;
static char[] h;
static pair[] mm;
static boolean[] vis;
static int y;
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int n = sc.nextInt();
int k =sc.nextInt();
long x=sc.nextInt();
long l=1;
while(k-->0)
l*=x;
long ans=0;
long []a=new long[n];
long[]pre=new long[n];
long []suf=new long[n];
for(int i=0;i<n;i++) {
a[i]=sc.nextLong();
if(i==0)
pre[i]=a[i];
else
pre[i]=pre[i-1]|a[i];
}
suf[n-1]=a[n-1];
if(n==1) {
System.out.println(a[0]*l);
return;
}
for(int i=n-2;i>=0;i--)
suf[i]=suf[i+1]|a[i];
for(int i=0;i<n;i++)
if(i==0)
ans=a[i]*l|suf[i+1];
else if(i==n-1)
ans=Math.max(ans, a[i]*l|pre[i-1]);
else
ans=Math.max(ans, a[i]*l|pre[i-1]|suf[i+1]);
out.print(ans);
out.flush();
}
static class pair implements Comparable<pair> {
int x;
int y;
int z;
char d;
pair(int f, int t, int u, char r) {
x = f;
y = t;
z = u;
d = r;
}
public String toString() {
return x + " " + y + " " + z + " " + d;
}
@Override
public int compareTo(pair o) {
return z - o.z;
}
}
static class pair1 implements Comparable<pair1> {
int x;
int y;
int z;
pair1(int f, int t, int u) {
x = f;
y = t;
z = u;
// num = n;
}
public String toString() {
return x + " " + y;
}
@Override
public int compareTo(pair1 o) {
if (x == o.x)
return y - o.y;
return x - o.x;
}
}
static public class FenwickTree { // one-based DS
int n;
int[] ft;
FenwickTree(int size) {
n = size;
ft = new int[n + 1];
// for(int i=1;i<=n;i++)
// ft[i]=i;
}
int rsq(int b) // O(log n)
{
int sum = 0;
while (b > 0) {
sum += ft[b];
b -= b & -b;
} // min?
return sum;
}
int rsq(int a, int b) {
return rsq(b) - rsq(a - 1);
}
void point_update(int k, int val) // O(log n), update = increment
{
while (k <= n) {
// System.out.println(k+" "+val);
ft[k] += val;
k += k & -k;
} // min?
}
int point_query(int idx) // c * O(log n), c < 1
{
int sum = ft[idx];
if (idx > 0) {
int z = idx ^ (idx & -idx);
--idx;
while (idx != z) {
// System.out.println("oo");
sum -= ft[idx];
idx ^= idx & -idx;
}
}
return sum;
}
void scale(int c) {
for (int i = 1; i <= n; ++i)
ft[i] *= c;
}
int findIndex(int cumFreq) {
int msk = n;
while ((msk & (msk - 1)) != 0)
msk ^= msk & -msk; // msk will contain the MSB of n
int idx = 0;
while (msk != 0) {
int tIdx = idx + msk;
if (tIdx <= n && cumFreq >= ft[tIdx]) {
idx = tIdx;
cumFreq -= ft[tIdx];
}
msk >>= 1;
}
if (cumFreq != 0)
return -1;
return idx;
}
}
static public class SegmentTree { // 1-based DS, OOP
int N; // the number of elements in the array as a power of 2 (i.e. after padding)
long[] array, sTree, lazy;
SegmentTree(long[] in) {
array = in;
N = in.length - 1;
sTree = new long[N << 1]; // no. of nodes = 2*N - 1, we add one to cross out index zero
lazy = new long[N << 1];
build(1, 1, N);
}
void build(int node, int b, int e) // O(n)
{
if (b == e)
sTree[node] = array[b];
else {
int mid = b + e >> 1;
build(node << 1, b, mid);
build(node << 1 | 1, mid + 1, e);
sTree[node] = sTree[node << 1] + sTree[node << 1 | 1];
}
}
void update_point(int index, int val) // O(log n)
{
index += N - 1;
sTree[index] += val;
while (index > 1) {
index >>= 1;
sTree[index] = sTree[index << 1] + sTree[index << 1 | 1];
}
}
void update_range(int i, int j, long val) // O(log n)
{
update_range(1, 1, N, i, j, val);
}
void update_range(int node, int b, int e, int i, int j, long val) {
if (i > e || j < b)
return;
if (b >= i && e <= j) {
sTree[node] += (e - b + 1) * val;
lazy[node] += val;
} else {
int mid = b + e >> 1;
propagate(node, b, mid, e);
update_range(node << 1, b, mid, i, j, val);
update_range(node << 1 | 1, mid + 1, e, i, j, val);
sTree[node] = sTree[node << 1] + sTree[node << 1 | 1];
}
}
void propagate(int node, int b, int mid, int e) {
lazy[node << 1] += lazy[node];
lazy[node << 1 | 1] += lazy[node];
sTree[node << 1] += (mid - b + 1) * lazy[node];
sTree[node << 1 | 1] += (e - mid) * lazy[node];
lazy[node] = 0;
}
long query(int i, int j) {
return query(1, 1, N, i, j);
}
long query(int node, int b, int e, int i, int j) // O(log n)
{
if (i > e || j < b)
return 0;
if (b >= i && e <= j)
return sTree[node];
int mid = b + e >> 1;
propagate(node, b, mid, e);
long q1 = query(node << 1, b, mid, i, j);
long q2 = query(node << 1 | 1, mid + 1, e, i, j);
return q1 + q2;
}
}
static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public int[] merge(int[] d, int st, int e) {
if (st > e)
return null;
if (e == st) {
int[] ans = { d[e] };
return ans;
}
int mid = (st + e) / 2;
int[] ans = new int[e - st + 1];
int[] ll = merge(d, st, mid);
int[] rr = merge(d, mid + 1, e);
if (ll == null)
return rr;
if (rr == null)
return ll;
int iver = 0;
int idxl = 0;
int idxr = 0;
for (int i = st; i <= e; i++) {
if (ll[idxl] < rr[idxr]) {
}
}
return ans;
}
public static class pair2 implements Comparable<pair2> {
int a;
int idx;
pair2(int a, int i) {
this.a = a;
idx = i;
}
public String toString() {
return a + " " + idx;
}
@Override
public int compareTo(pair2 o) {
// TODO Auto-generated method stub
return idx - o.idx;
}
}
static long inver(long x) {
int a = (int) x;
long e = (mod - 2);
long res = 1;
while (e > 0) {
if ((e & 1) == 1) {
// System.out.println(res*a);
res = (int) ((1l * res * a) % mod);
}
a = (int) ((1l * a * a) % mod);
e >>= 1;
}
// out.println(res+" "+x);
return res % mod;
}
static int atMostSum(int arr[], int n, int k) {
int sum = 0;
int cnt = 0, maxcnt = 0;
for (int i = 0; i < n; i++) {
// If adding current element doesn't
// cross limit add it to current window
if ((sum + arr[i]) <= k) {
sum += arr[i];
cnt++;
}
// Else, remove first element of current
// window and add the current element
else if (sum != 0) {
sum = sum - arr[i - cnt] + arr[i];
}
// keep track of max length.
maxcnt = Math.max(cnt, maxcnt);
}
return maxcnt;
}
public static int[] longestSubarray(int[] inp) {
// array containing prefix sums up to a certain index i
int[] p = new int[inp.length];
p[0] = inp[0];
for (int i = 1; i < inp.length; i++) {
p[i] = p[i - 1] + inp[i];
}
// array Q from the description below
int[] q = new int[inp.length];
q[inp.length - 1] = p[inp.length - 1];
for (int i = inp.length - 2; i >= 0; i--) {
q[i] = Math.max(q[i + 1], p[i]);
}
int a = 0;
int b = 0;
int maxLen = 0;
int curr;
int[] res = new int[] { -1, -1 };
while (b < inp.length) {
curr = a > 0 ? q[b] - p[a - 1] : q[b];
if (curr >= 0) {
if (b - a > maxLen) {
maxLen = b - a;
res = new int[] { a, b };
}
b++;
} else {
a++;
}
}
return res;
}
static void factor(int n) {
if (si[n] == n) {
f.add(n);
return;
}
f.add(si[n]);
factor(n / si[n]);
}
static void seive() {
si = new int[100001];
primes = new ArrayList<>();
int N = 100001;
si[1] = 1;
for (int i = 2; i < N; i++) {
if (si[i] == 0) {
si[i] = i;
primes.add(i);
}
for (int j = 0; j < primes.size() && primes.get(j) <= si[i] && (i * primes.get(j)) < N; j++)
si[primes.get(j) * i] = primes.get(j);
}
}
static class unionfind {
int[] p;
int[] size;
int jum;
unionfind(int n) {
p = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
p[i] = i;
}
jum = n;
Arrays.fill(size, 1);
}
int findSet(int v) {
if (v == p[v])
return v;
return p[v] = findSet(p[v]);
}
boolean sameSet(int a, int b) {
a = findSet(a);
b = findSet(b);
if (a == b)
return true;
return false;
}
int max() {
int max = 0;
for (int i = 0; i < size.length; i++)
if (size[i] > max)
max = size[i];
return max;
}
void combine(int a, int b) {
a = findSet(a);
b = findSet(b);
if (a == b)
return;
jum--;
if (size[a] > size[b]) {
p[b] = a;
size[a] += size[b];
} else {
p[a] = b;
size[b] += size[a];
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public Scanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 3ff0db645785d2c22749e9f2371ced3b | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class OrGame
{
public static void main(String[] args) throws IOException
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), k = sc.nextInt(), x = sc.nextInt();
long [] a = new long[n+2];
long [] prefix = new long[n+2];
long [] suffix = new long[n+2];
for (int i = 1; i <= n; i++)
{
a[i] = sc.nextInt();
prefix[i] = prefix[i-1] | a[i];
}
for (int i = n; i > 0; i--)
{
suffix[i] = suffix[i+1] | a[i];
}
long mul = (long) Math.pow(x, k);
long ans = 0;
for (int i = 1; i <= n; i++)
{
ans = Math.max(ans, prefix[i-1] | (a[i]*mul) | suffix[i+1]);
}
System.out.println(ans);
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | b2188c384ae2a3866556af05e0c42c30 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* #
*
* @author pttrung
*/
public class B_Round_320_Div1 {
public static long MOD = 1000000007;
public static void main(String[] args) throws FileNotFoundException {
// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
// "output.txt")));
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
int n = in.nextInt();
int k = in.nextInt();
long x = in.nextInt();
long[] data = new long[n];
for (int i = 0; i < n; i++) {
data[i] = in.nextLong();
}
long[] pre = new long[n];
long[] post = new long[n];
for (int i = 0; i < n; i++) {
pre[i] |= data[i];
if (i > 0) {
pre[i] |= (pre[i - 1]);
}
}
for (int i = n - 1; i >= 0; i--) {
post[i] |= data[i];
if (i + 1 < n) {
post[i] |= post[i + 1];
}
}
long mul = 1;
for (int i = 0; i < k; i++) {
mul *= x;
}
long result = 0;
for (int i = 0; i < n; i++) {
long tmp = (i > 0 ? pre[i - 1] : 0) | (data[i] * mul)
| (i + 1 < n ? post[i + 1] : 0);
if (result < tmp) {
result = tmp;
}
}
out.println(result);
out.close();
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
public static class Point implements Comparable<Point> {
int x, y;
public Point(int start, int end) {
this.x = start;
this.y = end;
}
@Override
public int hashCode() {
int hash = 5;
hash = 47 * hash + this.x;
hash = 47 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Point other = (Point) obj;
if (this.x != other.x) {
return false;
}
if (this.y != other.y) {
return false;
}
return true;
}
@Override
public int compareTo(Point o) {
return x - o.x;
}
}
public static class FT {
long[] data;
FT(int n) {
data = new long[n];
}
public void update(int index, long value) {
while (index < data.length) {
data[index] += value;
index += (index & (-index));
}
}
public long get(int index) {
long result = 0;
while (index > 0) {
result += data[index];
index -= (index & (-index));
}
return result;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, long b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return val * val % MOD;
} else {
return val * (val * a % MOD) % MOD;
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new
// BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new
// FileInputStream(new File("input.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 5a34bec44e79af2789a7d3a0b003bf2d | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.BufferedReader;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class OrGame implements Closeable {
private InputReader in = new InputReader(System.in);
private PrintWriter out = new PrintWriter(System.out);
public void solve() {
int n = in.ni();
long k = in.nl(), x = in.nl();
long value = 1L;
while (k-- > 0) value *= x;
long[] a = new long[n + 2];
long[] prefix = new long[n + 2], suffix = new long[n + 2];
for (int i = 1; i <= n; i++) {
a[i] = in.nl();
prefix[i] = prefix[i - 1] | a[i];
}
for (int i = n; i >= 1; i--) {
suffix[i] = suffix[i + 1] | a[i];
}
long result = 0L;
for (int i = 1; i <= n; i++) {
long temp = a[i] * value;
temp |= (prefix[i - 1] | suffix[i + 1]);
if (temp > result) result = temp;
}
out.println(result);
}
@Override
public void close() throws IOException {
in.close();
out.close();
}
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 ni() {
return Integer.parseInt(next());
}
public long nl() {
return Long.parseLong(next());
}
public void close() throws IOException {
reader.close();
}
}
public static void main(String[] args) throws IOException {
try (OrGame instance = new OrGame()) {
instance.solve();
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | bcd242c7b4c444d99f11ee759be91d3f | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.awt.*;
import java.io.*;
import java.util.*;
public class Abc {
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
int n=sc.nextInt();long k=sc.nextInt(),x=sc.nextInt();
long a[]=new long[n];
for (int i=0;i<n;i++)a[i]=sc.nextInt();
long pow=(long) Math.pow(x,k);
long pref[]=new long[n+2];
long suff[]=new long[n+2];
for (int i=1;i<=n;i++)pref[i]=(pref[i-1]|a[i-1]);
for (int i=n;i>=1;i--)suff[i]=(suff[i+1]|a[i-1]);
long max=0;
for (int i=1;i<=n;i++){
max=Math.max((pref[i-1]|suff[i+1]|(a[i-1]*pow)),max);
}
System.out.println(max);
}
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;
}
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 7543b429bd2f0339808774b9e9c4b6fe | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.awt.Point;
import java.io.*;
import java.lang.Integer;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
import java.util.ArrayDeque;
import static java.lang.Math.*;
public class Main {
final boolean ONLINE_JUDGE = !new File("input.txt").exists();
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
void init() throws FileNotFoundException {
if (ONLINE_JUDGE) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
public static void main(String[] args) {
new Main().run();
// Sworn to fight and die
}
public void run() {
try {
long t1 = System.currentTimeMillis();
init();
solve();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
class LOL implements Comparable<LOL> {
int x;
int y;
public LOL(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(LOL o) {
if (o.x != x)
return (int) (o.x - x); // ---->
return (int) (o.y - y); // <----
}
}
int ans = 0;
long[] readLongArray (int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = readLong();
return a;
}
public void solve() throws IOException {
int n = readInt();
int k = readInt();
long x = readLong();
long[] a = readLongArray(n);
long[] prefOr = new long[n];
long[] sufOr = new long[n];
for(int i = 1; i < n; i++) prefOr[i] = prefOr[i - 1] | a[i - 1];
for(int i = n - 2; i >=0; i--) sufOr[i] = sufOr[i + 1] | a[i + 1];
long ans = 0;
long[] p = new long[k + 1];
p[0] = 1;
for (int i = 1; i < k + 1; i++) p[i] = p[i - 1] * x;
for (int i = 0; i < n; i++) ans = max(ans, (prefOr[i] | sufOr[i] | (a[i] * p[k])));
out.print(ans);
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | c43a53729261e38ab84466340129b003 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.util.Random;
import java.util.Scanner;
public class Application {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt(), k = scanner.nextInt(), x = scanner.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = scanner.nextInt();
System.out.println(solve(a, k, x));
// for (int i = 0; i < 1000; i++) {
// long[] arr = generate(200_000, new Random());
// int k = 9, x = 2;
//
// solve(arr, k, x);
// }
}
private static long[] generate(int len, Random rnd) {
long[] res = new long[len];
for (int i = 0; i < len; i++)
res[i] = rnd.nextInt(1_000_000_000);
return res;
}
private static long solve(long[] a, int k, int x) {
long aOr = 0;
long[] aForward = new long[a.length];
for (int i = 0; i < a.length; i++) {
aForward[i] = aOr;
aOr |= a[i];
}
aOr = 0;
long[] aBackward = new long[a.length];
for (int i = a.length - 1; i >= 0; i--) {
aBackward[i] = aOr;
aOr |= a[i];
}
long[] without = new long[a.length];
for (int i = 0; i < a.length; i++)
without[i] = aForward[i] | aBackward[i];
int xx = 1;
while (k > 0) {
xx *= x;
k--;
}
x = xx;
k = 1;
for (int i = 0; i < k; i++) {
long bestRes = without[0] | a[0];
int bestIdx = -1;
for (int j = 0; j < a.length; j++) {
long n = without[j] | (a[j] * x);
if (n > bestRes) {
bestRes = n;
bestIdx = j;
}
}
if (bestIdx != -1) {
long cleanup = ~(~without[bestIdx] & a[bestIdx]);
a[bestIdx] = a[bestIdx] * x;
for (int j = 0; j < a.length; j++) {
if (j != bestIdx)
without[j] = (without[j] & cleanup) | a[bestIdx];
}
}
}
return without[0] | a[0];
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | bc74592e4ad98afe1d299ebdc378d790 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | //package codeforces;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.StringTokenizer;
public class A implements Closeable {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
A() throws IOException {
// reader = new BufferedReader(new FileReader("input.txt"));
// writer = new PrintWriter(new FileWriter("output.txt"));
}
StringTokenizer stringTokenizer;
String next() throws IOException {
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
stringTokenizer = new StringTokenizer(reader.readLine());
}
return stringTokenizer.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
private int MOD = 1000 * 1000 * 1000 + 7;
int sum(int a, int b) {
a += b;
return a >= MOD ? a - MOD : a;
}
int product(int a, int b) {
return (int) (1l * a * b % MOD);
}
int pow(int x, int k) {
int result = 1;
while (k > 0) {
if (k % 2 == 1) {
result = product(result, x);
}
x = product(x, x);
k /= 2;
}
return result;
}
int inv(int x) {
return pow(x, MOD - 2);
}
void solve() throws IOException {
int n = nextInt(), k = nextInt(), x = nextInt();
int mul = 1;
for(int i = 0; i < k; i++) {
mul *= x;
}
int[] a = new int[n];
for(int i = 0; i < n; i++) {
a[i] = nextInt();
}
int[] bc = new int[30];
for (int number : a) {
for(int j = 0; j < 30; j++) {
bc[j] += (number >> j) % 2;
}
}
long answer = 0;
for (int number : a) {
int other = 0;
for(int j = 0; j < 30; j++) {
other += (bc[j] == (number >> j) % 2 ? 0 : 1) << j;
}
answer = Math.max(answer, other | (long)number * mul);
}
writer.println(answer);
}
public static void main(String[] args) throws IOException {
try (A a = new A()) {
a.solve();
}
}
@Override
public void close() throws IOException {
reader.close();
writer.close();
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | b1e15b4ce67c3bd3728855cd6843c4d0 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main
{
static int [][]arr;
static String ReadLn (int maxLg) // utility function to read from stdin
{
byte lin[] = new byte [maxLg];
int lg = 0, car = -1;
String line = "";
try
{
while (lg < maxLg)
{
car = System.in.read();
if ((car < 0) || (car == '\n')) break;
lin [lg++] += car;
}
}
catch (IOException e)
{
return (null);
}
if ((car < 0) && (lg == 0)) return (null); // eof
return (new String (lin, 0, lg));
}
public static void main (String args[]) // entry point from OS
{
Main myWork = new Main(); // create a dinamic instance
myWork.Begin(); // the true entry point
}
void Begin()
{
int n,k,x;
Scanner scan=new Scanner(System.in);
n =scan.nextInt();
k =scan.nextInt();
x =scan.nextInt();
long []s=new long [n];
for(int i=0;i<n;i++){
s[i]=scan.nextLong();
}
Arrays.sort(s);
long multi=(long) Math.pow(x, k);
long sum=0;
for(int i=0;i<n;i++){
sum=sum|s[i];
}
long result = 0;
for(int j=0;j<n;j++){
long temp=s[n-1-j];
s[n-1-j]*=multi;
// System.out.println(s[n-1-j]+" "+sum);
if((sum|s[n-1-j])>result){
long sum2=0;
for(int i=0;i<n;i++){
sum2=sum2|s[i];
}
result=sum2;
}
s[n-1-j]=temp;
}
System.out.println(result);
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | bca1293a8b418e24556fb322238916d1 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
/**
* Created by hama_du on 15/10/03.
*/
public class B {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int k = in.nextInt();
long x = in.nextInt();
long[] a = new long[n];
long allOr = 0;
for (int i = 0; i < n ; i++) {
a[i] = in.nextInt();
allOr |= a[i];
}
long multiplyFull = 1;
for (int i = 0; i < k ; i++) {
multiplyFull *= x;
}
int[] bitCount = new int[48];
long once = 0;
for (int i = 0; i < n ; i++) {
for (int j = 0; j < 48 ; j++) {
if ((a[i] & (1L<<j)) >= 1) {
bitCount[j]++;
}
}
}
for (int j = 0; j < 48 ; j++) {
if (bitCount[j] == 1) {
once |= 1L<<j;
}
}
long max = allOr;
for (int i = 0; i < n ; i++) {
long base = allOr ^ (once & a[i]);
long res = base | (a[i] * multiplyFull);
max = Math.max(max, res);
}
out.println(max);
out.flush();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int next() {
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 char nextChar() {
int c = next();
while (isSpaceChar(c))
c = next();
if ('a' <= c && c <= 'z') {
return (char) c;
}
if ('A' <= c && c <= 'Z') {
return (char) c;
}
throw new InputMismatchException();
}
public String nextToken() {
int c = next();
while (isSpaceChar(c))
c = next();
StringBuilder res = new StringBuilder();
do {
res.append((char) c);
c = next();
} while (!isSpaceChar(c));
return res.toString();
}
public int nextInt() {
int c = next();
while (isSpaceChar(c))
c = next();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c-'0';
c = next();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = next();
while (isSpaceChar(c))
c = next();
long sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c-'0';
c = next();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static void debug(Object... o) {
System.err.println(Arrays.deepToString(o));
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | cbf1afaa50f10c8c3b246f953876aadf | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | 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;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int x = in.nextInt();
long[] a = new long[n + 2];
long[] l = new long[n + 2];
long[] r = new long[n + 2];
for (int i = 1; i <= n; i++) {
a[i] = in.nextInt();
}
for (int i = 1; i <= n; i++) {
l[i] = l[i - 1] | a[i];
}
for (int i = n; i >= 1; i--) {
r[i] = r[i + 1] | a[i];
}
long ans = 0;
long y = 1;
for (int i = 0; i < k; i++) {
y *= x;
}
for (int i = 1; i <= n; i++) {
ans = Math.max(ans, (a[i] * y) | l[i - 1] | r[i + 1]);
}
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());
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | ddc2465cefa4b102b1eb6c130ebdff9e | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.ObjectInputStream.GetField;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Q1 {
static long MOD = 1000000007;
static boolean b[], b1[];
static ArrayList<Integer>[] amp, pa;
static ArrayList<Graph>[] amp1;
static int sum[],dist[],cnt[],arr[],start[],color[],parent[],prime[],size[];
static long ans = 0,k;
static int p = 0;
static FasterScanner sc = new FasterScanner(System.in);
static Queue<Integer> q = new LinkedList<>();
static PriorityQueue<Integer> pq;
static BufferedWriter log;
static HashSet<Integer> hs;
static HashMap<String,Integer> hm;
static HashMap<Pair,Integer> hmp;
static Stack<Integer> st;
static Pair prr[];
static long parent1[],parent2[],size1[],size2[],arr1[];
public static void main(String[] args) throws Exception {
new Thread(null, new Runnable() {
public void run() {
try {
soln();
} catch (Exception e) {
System.out.println(e);
}
}
}, "1", 1 << 26).start();
}
static class Node{
int x,len,val;
Node(int x, int len, int val){
this.x = x;
this.len = len;
this.val = val;
}
}
static int MAX = 2000000;
StringBuilder sb = new StringBuilder();
static String str;
static char ch[];
public static void soln() throws IOException {
//FasterScanner in = new FasterScanner(new FileInputStream("C:\\Users\\Admin\\Desktop\\Q3.in"));
//PrintWriter out = new PrintWriter("C:\\Users\\Admin\\Desktop\\A1.txt");
log = new BufferedWriter(new OutputStreamWriter(System.out));
int n = sc.nextInt(), k = sc.nextInt(), x = sc.nextInt();
long ans = (long) Math.pow(x, k);
long arr[] = sc.nextLongArray(n);
long dum[] = new long[n];
Arrays.sort(arr);
long an = 0;
for(int i = 0; i< n;i++){
dum[i] = arr[i]*ans;
an|=arr[i];
}
int cnt[] = new int[33];
for(int i = 0;i<n;i++){
String str = Long.toBinaryString(arr[i]);
int c = 1;
for(int j = str.length()-1;j>=0;j--){
cnt[c++]+=(str.charAt(j)-'0');
}
}
long fa = 0;
for(int i = 0; i< n;i++){
String str = Long.toBinaryString(arr[i]);
int c = 1;
long ans1 = (long) (Math.pow(2, 31)-1);
for(int j = str.length()-1;j>=0;j--){
if(str.charAt(j)=='1' && cnt[c]==1){
ans1-=Math.pow(2,c-1);
}
c++;
}
//System.out.println(dum[i]+" "+fa+" "+ans1);
fa = Math.max((an&ans1)|dum[i], fa);
}
System.out.println(fa);
log.close();
}
static void dfs(int x){
b[x] = true;
int cnt = 0;
while(cnt<2){
p++;
cnt++;
if(ch[p]=='n'){
dist[p] = dist[x]+1;
dfs(p);
}
else dist[p] = dist[x]+1;
}
}
public static void bfs(int x){
st = new Stack<>();
dist[x] = 0;
st.add(x);
while(!st.isEmpty()){
int y = st.pop();
int cnt = 0;
for(int i = y+1;i<ch.length;i++){
if(!b[i] && ch[i]=='n'){
st.add(i);
b[i] = true;
dist[i] = dist[y]+1;
cnt++;
}
if(cnt==2) break;
}
}
}
public static void seive(int n){
b = new boolean[(n+1)];
Arrays.fill(b, true);
b[1] = false;
for(int i = 2;i*i<=n;i++){
if(b[i]){
for(int p = 2*i;p<=n;p+=i){
b[p] = false;
prime[p] = i;
}
}
}
for(int i = 2;i<=n;i++){
if(b[i]) prime[i] = i;
}
}
long sum1 = 0;
public static long recur1(long n){
if(n==0) return 0;
if(n==1) return 1;
return 1+2*recur1(n/2);
}
static class Graph{
int vertex;
int weight;
Graph(int v, int w){
vertex = v;
weight = w;
}
}
static class Pair implements Comparable<Pair> {
int u;
int v;
public Pair(int u, int v) {
this.u = u;
this.v = v;
}
public int hashCode() {
int hu = (int) (u ^ (u >>> 32));
int hv = (int) (v ^ (v >>> 32));
return 31*hu + hv;
}
public boolean equals(Object o) {
Pair other = (Pair) o;
return ((u == other.u && v == other.v));
}
public int compareTo(Pair other) {
return Integer.compare(u, other.u) != 0 ? (Integer.compare(u, other.u)) : (Integer.compare(v, other.v));
}
public String toString() {
return "[u=" + u + ", v=" + v + "]";
}
}
public static void buildGraph(int n){
for(int i =0;i<n;i++){
int x = sc.nextInt()-1, y = sc.nextInt()-1;
prr[i] = new Pair(x,y);
int z = sc.nextInt();
hmp.put(prr[i],z);
hmp.put(new Pair(y,x),z);
amp[x].add(y);
amp[y].add(x);
}
}
public static int getParent(int x){
while(parent[x]!=x){
parent[ x] = parent[(int) parent[ x]];
x = parent[ x];
}
return x;
}
static long min(long a, long b, long c){
if(a<b && a<c) return a;
if(b<c) return b;
return c;
}
/*
static class Pair3{
int x, y ,z;
Pair3(int x, int y, int z){
this.x = x;
this.y = y;
this.z = z;
}
}*/
static int sum(int n){
String str = Integer.toString(n);
int cnt = 0;
for(int i = 0; i< str.length();i++){
cnt+=(str.charAt(i)-'0');
}
return cnt;
}
static void KMPSearch(String pat, String txt)
{
int M = pat.length();
int N = txt.length();
// create lps[] that will hold the longest
// prefix suffix values for pattern
int lps[] = new int[M];
int j = 0; // index for pat[]
// Preprocess the pattern (calculate lps[]
// array)
computeLPSArray(pat,M,lps);
int i = 0; // index for txt[]
while (i < N)
{
if (pat.charAt(j) == txt.charAt(i))
{
j++;
i++;
}
if (j == M)
{
// parent.add((i-j));
j = lps[j-1];
}
// mismatch after j matches
else if (i < N && pat.charAt(j) != txt.charAt(i))
{
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j-1];
else
i = i+1;
}
}
}
static void computeLPSArray(String pat, int M, int lps[])
{
// length of the previous longest prefix suffix
int len = 0;
int i = 1;
lps[0] = 0; // lps[0] is always 0
// the loop calculates lps[i] for i = 1 to M-1
while (i < M)
{
if (pat.charAt(i) == pat.charAt(len))
{
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
// This is tricky. Consider the example.
// AAACAAAA and i = 7. The idea is similar
// to search step.
if (len != 0)
{
len = lps[len-1];
// Also, note that we do not increment
// i here
}
else // if (len == 0)
{
lps[i] = len;
i++;
}
}
}
}
private static void permutation(String prefix, String str) {
int n = str.length();
if (n == 0); //hs.add(prefix);
else {
for (int i = 0; i < n; i++)
permutation(prefix + str.charAt(i), str.substring(0, i) + str.substring(i+1, n));
}
}
public static char give(char c1,char c2){
if(c1!='a' && c2!='a') return 'a';
if(c1!='b' && c2!='b') return 'b';
return 'c';
}
public static void buildTree(int n){
int arr[] = sc.nextIntArray(n);
for(int i = 0;i<n;i++){
int x = arr[i]-1;
amp[i+1].add(x);
amp[x].add(i+1);
}
}
static class SegmentTree {
long st[];
long lazy[];
SegmentTree(int n) {
int size = 4 * n;
st = new long[size];
//lazy = new long[size];
//build(0, n - 1, 1);
}
/*long[] build(int ss, int se, int si) {
if (ss == se) {
st[si][0] = 1;
st[si][1] = 1;
st[si][2] = 1;
return st[si];
}
int mid = (ss + se) / 2;
long a1[] = build(ss, mid, si * 2), a2[] = build(mid + 1, se,
si * 2 + 1);
long ans[] = new long[3];
if (arr[mid] < arr[mid + 1]) {
ans[1] = Math.max(a2[1], Math.max(a1[1], a1[2] + a2[0]));
if (a1[1] == (mid - ss + 1))
ans[0] = ans[1];
else
ans[0] = a1[0];
if (a2[2] == (se - mid))
ans[2] = ans[1];
else
ans[2] = a2[2];
} else {
ans[1] = Math.max(a1[1], a2[1]);
ans[0] = a1[0];
ans[2] = a2[2];
}
st[si] = ans;
return st[si];
}*/
void update(int si, int ss, int se, int idx, int val) {
if (ss == se) {
//arr[idx] += val;
st[si]+=val;
}
else {
int mid = (ss + se) / 2;
if(ss <= idx && idx <= mid)
{
update(2*si, ss, mid, idx, val);
}
else
{ update(2*si+1, mid+1, se, idx, val);
}
st[si] = st[2*si]+st[2*si+1];
}
}
long get(int qs, int qe, int ss, int se, int si){
if(qs>se || qe<ss) return 0;
if (qs <= ss && qe >= se) {
return st[si];
}
int mid = (ss+se)/2;
return get(qs, qe, ss, mid, si * 2)+get(qs, qe, mid + 1, se, si * 2 + 1);
}
void updateRange(int node, int start, int end, int l, int r, int val)
{
if(lazy[node] != 0)
{
// This node needs to be updated
st[node] += (end - start + 1) * lazy[node]; // Update it
if(start != end)
{
lazy[node*2] += lazy[node]; // Mark child as lazy
lazy[node*2+1] += lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start > end || start > r || end < l) // Current segment is not within range [l, r]
return;
if(start >= l && end <= r)
{
// Segment is fully within range
st[node] += (end - start + 1) * val;
if(start != end)
{
// Not leaf node
lazy[node*2] += val;
lazy[node*2+1] += val;
}
return;
}
int mid = (start + end) / 2;
updateRange(node*2, start, mid, l, r, val); // Updating left child
updateRange(node*2 + 1, mid + 1, end, l, r, val); // Updating right child
st[node] = st[node*2] + st[node*2+1]; // Updating root with max value
}
int queryRange(int node, int start, int end, int l, int r)
{
if(start > end || start > r || end < l)
return 0; // Out of range
if(lazy[node] != 0)
{
// This node needs to be updated
st[node] += (end - start + 1) * lazy[node]; // Update it
if(start != end)
{
lazy[node*2] += lazy[node]; // Mark child as lazy
lazy[node*2+1] += lazy[node]; // Mark child as lazy
}
lazy[node] = 0; // Reset it
}
if(start >= l && end <= r) // Current segment is totally within range [l, r]
return (int) st[node];
int mid = (start + end) / 2;
int p1 = queryRange(node*2, start, mid, l, r); // Query left child
int p2 = queryRange(node*2 + 1, mid + 1, end, l, r); // Query right child
return (p1 + p2);
}
void print() {
for (int i = 0; i < st.length; i++) {
System.out.print(st[i]+" ");
}
System.out.println();
}
}
static int convert(int x){
int cnt = 0;
String str = Integer.toBinaryString(x);
//System.out.println(str);
for(int i = 0;i<str.length();i++){
if(str.charAt(i)=='1'){
cnt++;
}
}
int ans = (int) Math.pow(3, 6-cnt);
return ans;
}
static class Node2{
Node2 left = null;
Node2 right = null;
Node2 parent = null;
int data;
}
static class BinarySearchTree{
Node2 root = null;
int height = 0;
int max = 0;
int cnt = 1;
ArrayList<Integer> parent = new ArrayList<>();
HashMap<Integer, Integer> hm = new HashMap<>();
public void insert(int x){
Node2 n = new Node2();
n.data = x;
if(root==null){
root = n;
}
else{
Node2 temp = root,temp2 = null;
while(temp!=null){
temp2 = temp;
if(x>temp.data) temp = temp.right;
else temp = temp.left;
}
if(x>temp2.data) temp2.right = n;
else temp2.left = n;
n.parent = temp2;
parent.add(temp2.data);
}
}
public Node2 getSomething(int x, int y, Node2 n){
if(n.data==x || n.data==y) return n;
else if(n.data>x && n.data<y) return n;
else if(n.data<x && n.data<y) return getSomething(x,y,n.right);
else return getSomething(x,y,n.left);
}
public Node2 search(int x,Node2 n){
if(x==n.data){
max = Math.max(max, n.data);
return n;
}
if(x>n.data){
max = Math.max(max, n.data);
return search(x,n.right);
}
else{
max = Math.max(max, n.data);
return search(x,n.left);
}
}
public int getHeight(Node2 n){
if(n==null) return 0;
height = 1+ Math.max(getHeight(n.left), getHeight(n.right));
return height;
}
}
static long findDiff(long[] arr, long[] brr, int m){
int i = 0, j = 0;
long fa = 1000000000000L;
while(i<m && j<m){
long x = arr[i]-brr[j];
if(x>=0){
if(x<fa) fa = x;
j++;
}
else{
if((-x)<fa) fa = -x;
i++;
}
}
return fa;
}
public static long max(long x, long y, long z){
if(x>=y && x>=z) return x;
if(y>=x && y>=z) return y;
return z;
}
static long modInverse(long a, long mOD2){
return power(a, mOD2-2, mOD2);
}
static long power(long x, long y, long m)
{
if (y == 0)
return 1;
long p = power(x, y/2, m) % m;
p = (p * p) % m;
return (y%2 == 0)? p : (x * p) % m;
}
static long power2(long x,BigInteger y,long m){
long ans = 1;
BigInteger two = new BigInteger("2");
while(y.compareTo(BigInteger.ZERO)>0){
if(y.getLowestSetBit()==y.bitCount()){
x = (x*x)%MOD;
y = y.divide(two);
}
else{
ans = (ans*x)%MOD;
y = y.subtract(BigInteger.ONE);
}
}
return ans;
}
static BigInteger power2(BigInteger x, BigInteger y, BigInteger m){
BigInteger ans = new BigInteger("1");
BigInteger one = new BigInteger("1");
BigInteger two = new BigInteger("2");
BigInteger zero = new BigInteger("0");
while(y.compareTo(zero)>0){
//System.out.println(y);
if(y.mod(two).equals(one)){
ans = ans.multiply(x).mod(m);
y = y.subtract(one);
}
else{
x = x.multiply(x).mod(m);
y = y.divide(two);
}
}
return ans;
}
static BigInteger power(BigInteger x, BigInteger y, BigInteger m)
{
if (y.equals(0))
return (new BigInteger("1"));
BigInteger p = power(x, y.divide(new BigInteger("2")), m).mod(m);
p = (p.multiply(p)).mod(m);
return (y.mod(new BigInteger("2")).equals(0))? p : (p.multiply(x)).mod(m);
}
static long d,x,y;
public static void extendedEuclidian(long a, long b){
if(b == 0) {
d = a;
x = 1;
y = 0;
}
else {
extendedEuclidian(b, a%b);
int temp = (int) x;
x = y;
y = temp - (a/b)*y;
}
}
public static long gcd(long n, long m){
if(m!=0) return gcd(m,n%m);
else return n;
}
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter writer;
static class FasterScanner {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
private SpaceCharFilter filter;
public FasterScanner(InputStream stream) {
this.stream = stream;
}
public int snext() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = snext();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = snext();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public long[] nextLongArray(int n) {
long a[] = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public String readString() {
int c = snext();
while (isSpaceChar(c)) {
c = snext();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = snext();
while (isSpaceChar(c))
c = snext();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = snext();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
} | Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | 8f474b5c408179419e8d676672859726 | train_002.jsonl | 1442416500 | You are given n numbers a1, a2, ..., an. You can perform at most k operations. For each operation you can multiply one of the numbers by x. We want to make as large as possible, where denotes the bitwise OR. Find the maximum possible value of after performing at most k operations optimally. | 256 megabytes | 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.util.ArrayList;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author beginner1010
*/
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);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int x = in.nextInt();
ArrayList<Long> arr = new ArrayList<>();
for (int i = 0; i < n; i++) {
long num;
num = in.nextInt();
arr.add(num);
}
long[] leftOr = new long[n];
long[] rightOr = new long[n + 1];
for (int i = 0; i < n; i++) {
leftOr[i] = arr.get(i) | (i == 0 ? 0 : leftOr[i - 1]);
rightOr[n - i - 1] = arr.get(n - i - 1) | rightOr[n - i];
}
long ans = 0;
for (int i = 0; i < n; i++) {
long num = arr.get(i);
for (int j = 0; j < k; j++) {
num *= x;
}
long or = (i == 0 ? 0 : leftOr[i - 1]) | rightOr[i + 1];
or |= num;
ans = Math.max(ans, or);
}
out.println(ans);
return;
}
}
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());
}
}
}
| Java | ["3 1 2\n1 1 1", "4 2 3\n1 2 4 8"] | 2 seconds | ["3", "79"] | NoteFor the first sample, any possible choice of doing one operation will result the same three numbers 1, 1, 2 so the result is . For the second sample if we multiply 8 by 3 two times we'll get 72. In this case the numbers will become 1, 2, 4, 72 so the OR value will be 79 and is the largest possible result. | Java 8 | standard input | [
"greedy",
"brute force"
] | b544f02d12846026f6c76876bc6bd079 | The first line contains three integers n, k and x (1 ≤ n ≤ 200 000, 1 ≤ k ≤ 10, 2 ≤ x ≤ 8). The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 109). | 1,700 | Output the maximum value of a bitwise OR of sequence elements after performing operations. | standard output | |
PASSED | eddca90e295da4d0de3cdd6c1d20924a | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
public final class Main {
//2^1 2^2 2^3 2^4 2^5 2^6 3^1 3^2 3^3 3^4 5^1 5^2 7^1 7^2
private void solve() {
int[] pirmes = {2, 3, 5, 7, 11,13,17,19,23,29,31,37,41,43,47};
int cnt = 0;
for (int i = 0; i < pirmes.length; i++) {
System.out.println(pirmes[i]);
System.out.flush();
if ("yes".equals(in.nextLine())) {
cnt++;
if (cnt > 1) {
System.out.println("composite");
System.out.flush();
return;
}
for (int j=pirmes[i]*pirmes[i]; j<=100;j*=pirmes[i]){
System.out.println(j);
System.out.flush();
if ("yes".equals(in.nextLine())){
cnt++;
if (cnt > 1) {
System.out.println("composite");
System.out.flush();
return;
}
}else{
break;
}
}
}
}
if (cnt > 1) {
System.out.println("composite");
} else
System.out.println("prime");
System.out.flush();
}
// -------------I/O------------- \\
void run() {
try {
in = new FScanner(new File("input.txt"));
out = new PrintWriter(new BufferedWriter(new FileWriter(new File("output.txt"))));
solve();
out.close();
in.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
void runIO() {
in = new FScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
solve();
out.close();
in.close();
}
public static void main(String[] args) {
new Main().runIO();
}
private FScanner in;
private PrintWriter out;
class FScanner {
private BufferedReader br;
private StringTokenizer st;
public FScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException ex) {
ex.printStackTrace();
}
}
void useString(String s) {
if (s != null) {
st = new StringTokenizer(s);
}
}
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 s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
void close() {
try {
br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
class LinkedList {
char c;
LinkedList next;
LinkedList prev;
LinkedList(char i) {
c = i;
next = this;
prev = this;
}
public char getC() {
return c;
}
public void setNext(LinkedList next) {
this.next = next;
}
public void setPrev(LinkedList prev) {
this.prev = prev;
}
public LinkedList getNext() {
return next;
}
public LinkedList getPrev() {
return prev;
}
public LinkedList add(LinkedList i) {
i.setNext(this.next);
this.next = i;
i.getNext().setPrev(i);
i.setPrev(this);
return this.next;
}
public LinkedList delete() {
this.next.setPrev(this.prev);
this.prev.setNext(this.next);
return this.next;
}
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 778f6376796f7b34bbb0871a45085ab4 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.util.Scanner;
public class C {
public static void main(String[] args) {
int[] prime = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47,53 };
boolean flag = true;
Scanner sc = new Scanner(System.in);
int count=0;
for (int i = 0; i < prime.length; i++) {
System.out.println(prime[i]);
System.out.flush();
String s = sc.next();
boolean pr=false;
if (s.equals("yes")) {
for (int j = i; j < prime.length; j++) {
int x= prime[j]*prime[i];
if(x>100){
pr=true;
break;
}
System.out.println(prime[j]*prime[i]);
System.out.flush();
s = sc.next();
if (s.equals("yes")) {
flag = false;
break;
}
}
if(!flag || pr)
break;
}
}
if (flag)
System.out.println("prime");
else
System.out.println("composite");
System.out.flush();
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 88ef4c8f1dbff8df95ef641fc98d9c72 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
public class C {
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
static boolean[] vis = new boolean[101];
static int[] prime = new int[50];
static int count = 0;
public static void main(String[] args) throws IOException {
Euler();
String que = null;
for (int i = 0; i < 20; i++) {
if (prime[i]*prime[i]>100) {
out.println("prime");
out.flush();
break;
} else {
out.println(prime[i]);
out.flush();
que = next();
if (que.equals("yes")){
int te=prime[i]*prime[i];
if(te>100){
out.println("prime");
out.flush();
break;
}else{
out.println(te);
out.flush();
que=next();
if(que.equals("yes")){
out.println("composite");
out.flush();
break;
}else{
int last=prime[i];
for(int j=i+1;j<20;j++){
if(prime[j]*last>100){
out.println("prime");
out.flush();
break;
}
out.println(prime[j]);
out.flush();
que=next();
if(que.equals("yes")){
out.println("composite");
out.flush();
break;
}
}
break;
}
}
}
}
}
}
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
static String next() throws IOException {
in.nextToken();
return in.sval;
}
static void Euler() {
Arrays.fill(vis, true);
vis[0] = false;
vis[1] = false;
vis[2] = true;
for (int i = 2; i < 101; i++) {
if (vis[i]) {
prime[count++] = i;
}
for (int j = 0; j < count && i * prime[j] < 101; j++) {
vis[i * prime[j]] = false;
if (i % prime[j] == 0) {
break;
}
}
}
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 8ddaa8168f900a10365b5433cf09516f | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Scanner;
import java.util.StringTokenizer;
public class C {
public static void main(String[] args) throws IOException{
Scanner in = new Scanner(System.in);
int[] a = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};
boolean check = false;
int x = 20;
for(int i=0;i<x;i++) {
System.out.println(a[i]);
System.out.flush();
String s = in.next();
if(s.equals("yes")) {
if(check) {
System.out.println("composite");
System.exit(0);
}
if(a[i]<10) {
x--;
check=true;
System.out.println(a[i]*a[i]);
System.out.flush();
s = in.next();
if(s.equals("yes")) {
System.out.println("composite");
System.exit(0);
}
}
}
}
System.out.println("prime");
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 58e3396cb31b59c9abd43fbec2548d2f | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class A {
static boolean isPrime(int n)
{
for(int i=2;i<n;i++)
if(n%i==0)
return false;
return true;
}
static ArrayList<Integer> multiples(int n)
{
ArrayList<Integer> arr = new ArrayList<Integer>();
for(int i=0;i<primes.length && n*primes[i]<=100 ;i++)
arr.add(n*primes[i]);
return arr;
}
static int[] primes;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
//int a = sc.nextInt();
int sum=0;
primes = new int[15];
int k=0;
for(int i=2;i<=47;i++)
if(isPrime(i))
primes[k++] = i;
int primesInd=0;
boolean prime=true;
ArrayList<Integer> arr=new ArrayList<Integer>() ;
//System.out.println(Arrays.toString(primes));
int arrInd=0;
for(int j=0;j<20;j++)
{
if(prime)
{
// System.out.println(primesInd);
if(primesInd==primes.length)
{
System.out.println("prime");
break;
}
System.out.println(primes[primesInd++]);
}
else
{
if(arrInd==arr.size())
{
System.out.println("prime");
break;
}
System.out.println(arr.get(arrInd++));
}
String s = sc.next();
if(s.equals("yes"))
{
if(prime)
{
arr = multiples(primes[primesInd-1]);
//System.out.println(arr);
prime=false;
}
else
{
System.out.println("composite");
break;
}
}
}
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 468decd111d765636cc53b4bca2584d8 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Arrays;
import java.util.Collections;
import java.util.StringTokenizer;
import java.io.*;
public class JavaApplication25 {
public static void main(String[] args) {
FastReader read = new FastReader();
int a[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 49, 4, 25, 9};
int count = 0;
for(int i = 0; i<a.length; i++){
System.out.println(a[i]);
System.out.flush();
String s = read.nextLine();
if(s.equals("yes")){
count++;
}
}
if(count>=2){
System.out.println("composite");
System.out.flush();
return;
}
System.out.println("prime");
System.out.flush();
}
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;
}
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | cfa3205c99530c08c34f9a14b3f771c0 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int pd[] = { 2, 3, 4, 5, 7, 9, 11, 13, 17, 19, 23, 25, 29, 31, 37, 41,
43, 47, 49 };
int cnt = 0;
for (int i = 0; i < pd.length; i++) {
System.out.println(pd[i]);
System.out.flush();
String line = Reader.nextLine();
if (line.equals("yes")) {
cnt++;
}
if (cnt >= 2) {
System.out.println("composite");
return;
}
}
System.out.println("prime");
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
// TODO add check for eof if necessary
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static String nextLine() throws IOException {
return reader.readLine();
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 989820e3a132c3cc78df1d3dc8af279b | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.Scanner;
public class P680_C {
private static StreamTokenizer inputReader = new StreamTokenizer(
new BufferedReader(new InputStreamReader(System.in)));
private static Scanner scaner = new Scanner(System.in);
static java.io.BufferedReader reader = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
public static int nextInt() {
int a = -1;
try {
inputReader.nextToken();
a = (int) inputReader.nval;
} catch (Exception e) {
}
return a;
}
public static void main(String[] args) {
boolean[] isPrime = new boolean[101];
for (int i = 0; i <= 100; i++) {
isPrime[i] = true;
}
for (int i = 2; i <= 10; i++) {
for (int j = 2; i * j <= 100; j++) {
isPrime[i * j] = false;
}
}
int c = 0;
for (int i = 2; i <= 100; i++) {
if (isPrime[i] && i >= 50) {
c++;
}
}
int asked = 0;
int prime = 1;
int counter = 0;
while (asked < 20) {
prime = getNextPRime(isPrime, prime);
System.out.println(prime);
System.out.flush();
String answer = scaner.nextLine();
if (answer.equals("yes")) {
counter++;
if (prime * prime <= 100) {
System.out.println(prime * prime);
System.out.flush();
answer = scaner.nextLine();
if (answer.equals("yes"))
counter++;
asked++;
}
}
if (counter >= 2) {
System.out.print("composite");
System.out.flush();
return;
}
if (prime > 50) {
System.out.print("prime");
System.out.flush();
return;
}
asked++;
}
}
private static int getNextPRime(boolean[] isPrimes, int index) {
for (int i = index + 1; i < isPrimes.length; i++) {
if (isPrimes[i]) {
return i;
}
}
return -1;
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 3e5924d0ec196e73fddb8389cbcaf0ef | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
public class BearPrime {
static StringTokenizer st;
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in), 325678);
static boolean[] pos, q = new boolean[101], prime = new boolean[101];
static int d = 0, qq = 0, primes = 0;
static ArrayList<Integer> possi = new ArrayList<Integer>();
//2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97
public static void main(String[] args) throws IOException {
ArrayList<Integer>[] div = new ArrayList[101];
prime = new boolean[101];
Arrays.fill(prime, true);
for (int i = 2; i <= 100; i++)
if (prime[i])
for (int j = i + i; j <= 100; j += i)
prime[j] = false;
pos = new boolean[101];
Arrays.fill(pos, true);
q = new boolean[101];
for (int i = 2; i <= 100; i++) div[i] = new ArrayList<Integer>();
for (int i = 2; i <= 100; i++) for (int j = i + i; j <= 100; j += i) div[j].add(i);
for (int i = 2; i <= 100; i++)
//System.out.println("i="+i+" pos="+pos[i] + " div="+div[i].toString());
if (pos[i])
for (int j = 0; j < div[i].size(); j++)
if (!q[div[i].get(j)])
if(query(div[i].get(j))) return;
updPoss();
//System.out.println(possi.toString());
while (possi.size() > 1 || primes == 0 || primes == possi.size()) {
for (int possib : possi)
if (pos[possib] && !q[possib])
if (query(possib)) return;
updPoss();
}
System.out.println(primes == possi.size() ? "prime" : "composite");
System.out.flush();
}
public static void updPoss() {
possi.clear();
primes = 0;
for (int j = 2; j <= 100; j++) {
if (pos[j]) {
if (prime[j]) primes++;
possi.add(j);
}
}
}
public static boolean query(int i) throws IOException {
q[i] = true;
qq++;
if (qq > 20) {
System.out.println("RIP");
System.exit(0);
}
System.out.println(i);
System.out.flush();
if (bf.readLine().equals("yes")) {
d++;
if (d == 2) {
System.out.println("composite");
System.out.flush();
return true;
}
for (int k = 2; k <= 100; k++)
if (k % i != 0) {
pos[k] = false;
}
} else
for (int k = i; k <= 100; k += i) {
pos[k] = false;
}
updPoss();
if (primes == 0) {
System.out.println("composite");
System.out.flush();
return true;
}else if (primes == possi.size()){
System.out.println("prime");
System.out.flush();
return true;
}
return false;
}
public static int in() throws IOException {
return Integer.parseInt(next());
}
public static String next() throws IOException {
if (st == null || !st.hasMoreTokens())
st = new StringTokenizer(bf.readLine());
return st.nextToken();
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | f1ba80b5280494b6476d80a0bd73ff84 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.util.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
public class Main
{
public static void main(String[] args) throws Exception
{
long startTime = System.nanoTime();
boolean [] primes=primeSieve(110);
ArrayList<Integer> div=new ArrayList<Integer>();
int j=0;
for(int i=2;i<=50;i++)
{
if(primes[i])
{
out.println(i);
out.flush();
String res=in.next();
//String res=verdict(num,i);
if(res.equals("yes"))
{
div.add(i);
}
}
}
if(div.size()==0)
{
out.println("prime");
exit(0);
}
else if(div.size()>1)
{
out.println("composite");
exit(0);
}
else
{
int t=div.get(0);
int d=t*t;
while(d<=100)
{
out.println(d);
out.flush();
String res=in.next();
//String res=verdict(num,d);
if(res.equals("yes"))
{
out.println("composite");
exit(0);
}
d*=t;
}
}
out.println("prime");
long endTime = System.nanoTime();
err.println("Execution Time : +" + (endTime-startTime)/1000000 + " ms");
exit(0);
}
static String verdict(int num,int i)
{
if(num%i==0)
return "yes";
else
return "no";
}
static boolean [] primeSieve(int n)
{
boolean [] primes=new boolean[n+1];
Arrays.fill(primes,true);
primes[0]=false;
primes[1]=false;
for(int i=2;i<=Math.sqrt(n);i++)
{
if(primes[i])
{
for(int j=i*i;j<=n;j+=i)
{
primes[j]=false;
}
}
}
return primes;
}
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 long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
}
static void exit(int a)
{
out.close();
err.close();
System.exit(a);
}
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static OutputStream errStream = System.err;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
static PrintWriter err = new PrintWriter(errStream);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 00e28563aa6fcfd36f0e469806955913 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.util.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
public class Main
{
public static void main(String[] args) throws Exception
{
long startTime = System.nanoTime();
boolean [] primes=primeSieve(110);
ArrayList<Integer> div=new ArrayList<Integer>();
for(int i=2;i<=50;i++)
{
if(primes[i])
{
out.println(i);
out.flush();
String res=in.next();
//String res=verdict(num,i);
if(res.equals("yes"))
{
div.add(i);
}
}
}
if(div.size()==0)
{
out.println("prime");
exit(0);
}
else if(div.size()>1)
{
out.println("composite");
exit(0);
}
else
{
int t=div.get(0);
int d=t*t;
while(d<=100)
{
out.println(d);
out.flush();
String res=in.next();
//String res=verdict(num,d);
if(res.equals("yes"))
{
out.println("composite");
exit(0);
}
d*=t;
}
}
out.println("prime");
long endTime = System.nanoTime();
err.println("Execution Time : +" + (endTime-startTime)/1000000 + " ms");
exit(0);
}
static String verdict(int num,int i)
{
if(num%i==0)
return "yes";
else
return "no";
}
static boolean [] primeSieve(int n)
{
boolean [] primes=new boolean[n+1];
Arrays.fill(primes,true);
primes[0]=false;
primes[1]=false;
for(int i=2;i<=Math.sqrt(n);i++)
{
if(primes[i])
{
for(int j=i*i;j<=n;j+=i)
{
primes[j]=false;
}
}
}
return primes;
}
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 long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
}
static void exit(int a)
{
out.close();
err.close();
System.exit(a);
}
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static OutputStream errStream = System.err;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
static PrintWriter err = new PrintWriter(errStream);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | bef7a134f24e3d22d0b74ecfdd6c44a0 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.util.*;
import java.awt.geom.*;
import java.io.*;
import java.math.*;
public class Main
{
public static void main(String[] args) throws Exception
{
long startTime = System.nanoTime();
boolean [] primes=primeSieve(110);
ArrayList<Integer> div=new ArrayList<Integer>();
for(int i=2;i<=50;i++)
{
if(primes[i])
{
out.println(i);
out.flush();
String res=in.next();
//String res=verdict(num,i);
if(res.equals("yes"))
{
div.add(i);
}
if(div.size()>1)
{
out.println("composite");
exit(0);
}
}
}
if(div.size()==0)
{
out.println("prime");
exit(0);
}
else if(div.size()>1)
{
out.println("composite");
exit(0);
}
else
{
int t=div.get(0);
int d=t*t;
while(d<=100)
{
out.println(d);
out.flush();
String res=in.next();
//String res=verdict(num,d);
if(res.equals("yes"))
{
out.println("composite");
exit(0);
}
d*=t;
}
}
out.println("prime");
long endTime = System.nanoTime();
err.println("Execution Time : +" + (endTime-startTime)/1000000 + " ms");
exit(0);
}
static String verdict(int num,int i)
{
if(num%i==0)
return "yes";
else
return "no";
}
static boolean [] primeSieve(int n)
{
boolean [] primes=new boolean[n+1];
Arrays.fill(primes,true);
primes[0]=false;
primes[1]=false;
for(int i=2;i<=Math.sqrt(n);i++)
{
if(primes[i])
{
for(int j=i*i;j<=n;j+=i)
{
primes[j]=false;
}
}
}
return primes;
}
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 long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
}
static void exit(int a)
{
out.close();
err.close();
System.exit(a);
}
static InputStream inputStream = System.in;
static OutputStream outputStream = System.out;
static OutputStream errStream = System.err;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(outputStream);
static PrintWriter err = new PrintWriter(errStream);
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | d40b515c49093b2b26e6d3305f8eccc5 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | 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.Random;
import java.util.StringTokenizer;
public class Main {
static long sum = 0;
static int n;
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
solver.solve(1, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int yes=0;
int []n= {2,4,3,9,5,25,7,49,11,13,17,19,23,29,31,37,41,43,47} ;
for (int i = 0; i < n.length; i++)
{
out.println(n[i]);
out.flush();
String x = in.next() ;
if (x.equals("yes"))
yes++;
if(yes==2)
{
System.out.println("composite");
return ;
}
}
System.out.println("prime");
return ;
}
int binarySearch(int arr[], int l, int r, int x, int n) {
if (r >= l)
{
int mid = l + (r - l) / 2;
if (arr[mid] <= x && ((mid + 1) <= n - 1 && arr[mid + 1] > x))
return mid + 1;
if (arr[mid] > x)
return binarySearch(arr, l, mid - 1, x, n);
return binarySearch(arr, mid + 1, r, x, n);
}
return r;
}
private boolean isPrime(int num) {
if (num < 2)
return false;
if (num == 2)
return true;
if (num % 2 == 0)
return false;
for (int i = 3; i * i <= num; i += 2)
if (num % i == 0)
return false;
return true;
}
public void shuffle(int c[]) {
for (int i = 0; i < c.length; i++)
{
int x = 0 + new Random().nextInt(c.length);
int temp = c[x];
c[x] = c[i];
c[i] = temp;
}
}
}
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());
}
}
static class Pair implements Comparable<Pair> {
char x;
int y;
boolean z;
public Pair(char x, int y, boolean z) {
this.x = x;
this.y = y;
this.z = z;
}
@Override
public int compareTo(Pair p) {
if (this.y < p.y)
return -1;
else if (this.y > p.y)
return 1;
else
return 0;
}
//
}
} | Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 5b9962ada42b1072afda5a16271f65c1 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.util.*;
import java.io.*;
public class Test {
public static void main(String at[]){
InputReader in=new InputReader(System.in);
PrintWriter out=new PrintWriter(System.out);
int t=1;
while(t-->0){
int cnt=0;
int prime[]={2,4,3,9,5,25,7,49,11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
for(int i=0;i<prime.length;i++){
System.out.println(prime[i]);
System.out.flush();
String s=in.next();
if(s.equals("yes")){
cnt++;
}
}
if(cnt>=2)
System.out.println("composite");
else
System.out.println("prime");
System.out.flush();
}
}
}
class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
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 & 15;
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c & 15;
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
//while (c != '\n' && c != '\r' && c != '\t' && c != -1)
//c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (c != '\n' && c != '\r' && c != '\t' && c != -1);
return res.toString();
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
} | Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 80352d6cd951af7788f238f8ba8a85cf | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.Queue;
import java.util.Scanner;
import java.util.SortedSet;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class Main {
static int n,count=0;
static final int N=(int) 1e5;
static int t[];
static int a[];
public static void main(String[] args) throws Exception{
int m;
//long start=System.nanoTime();
BufferedReader br =new BufferedReader(new InputStreamReader(System.in));
//StringTokenizer st=new StringTokenizer(br.readLine());
StringBuilder sb=new StringBuilder();
PrintStream out=System.out;
Scanner s=new Scanner(System.in);
int a[]=new int[101];
Arrays.fill(a, 1);
int count=1,query=1,x=-1,div=-1;
for(int i=2;query<=20;){
// System.out.println(i+" "+query);
if(a[i]==1){
query++;
System.out.println(i);
System.out.flush();
String str=s.nextLine();
if(str.equals("yes")){
count++;
div=i;
/*if(i%5==0 && i*i<=100){
x=i;
i=i*i;
}
else*/
i++;
}
else{
for(int j=i;j<=100;j+=i){
a[j]=0;
}
}
if(count>2){
System.out.println("composite");
System.exit(0);
}
while(a[i]!=1)
i++;
/* if(x!=-1){
i=x+1;
x=-1;
}*/
}
}
if(count<=2)
System.out.println("prime");
out.flush();
out.close();
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 42ff2b0ee5cc362ccc01503ccaf7c19b | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.util.*;
import java.io.*;
public class cfp{
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
boolean prime=true;
int[] ar={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
int i=0;
while(i<ar.length){
System.out.println(ar[i]);
System.out.flush();
String s=br.readLine();
if(s.equals("yes")){
int j=i+1;
int count=1;
while(j<ar.length){
System.out.println(ar[j]);
System.out.flush();
s=br.readLine();
if(s.equals("yes")){
count++;
}
j++;
}
int prod=ar[i]*ar[i];
while(prod<100){
System.out.println(prod);
System.out.flush();
s=br.readLine();
if(s.equals("yes")){
count++;
}
prod*=ar[i];
}
i=j;
if(count==1){
System.out.println("prime");
return;
}
else{
System.out.println("composite");
return;
}
}
i++;
}
System.out.println("prime");
}
} | Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | d4b9b4c090f2783cf532ffe5ef581142 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.util.*;
import java.io.*;
public class cdprac{
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int[] prime={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47};
int j=0;
int i=2;
int c=1;
while(i<=10){
System.out.println(i);
System.out.flush();
String s=br.readLine();
if((i==2||i==3||i==5||i==7)&& s.equals("yes")){
j=i;
if(i==2){
c=2;
}
//break;
}
else if(s.equals("yes")){
System.out.println("composite");
System.out.flush();
return;
}
else if(s=="no"){
continue;
}
i+=c;
}
int k=0;
if(j==2){
k=0;
}
if(j==3) k=1;
if(j==5) k=2;
else k=3;
if(j>0){
for( i=k;i<prime.length;i++){
if(prime[i]*j<=100){
System.out.println(prime[i]*j);
System.out.flush();
String s=br.readLine();
if(s.equals("yes")){
System.out.println("composite");
System.out.flush();
return;
}
}
}
}
System.out.println("prime");
System.out.flush();
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 627b287401d90714f46f3c93c1e3b1c4 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
static final int MAXN = 100005;
static final int INF = Integer.MAX_VALUE;
static class Solver {
int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 4, 9, 25, 49};
public void solve(int testNumber, InputReader in, PrintWriter out) {
int cnt = 0;
for (int query = 0; query < prime.length; query++) {
System.out.println(prime[query]);
System.out.flush();
String reply = in.next();
if (reply.equals("yes"))
cnt++;
}
if (cnt > 1) {
System.out.println("composite");
}
else {
System.out.println("prime");
}
System.out.flush();
}
}
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
solver.solve(1, in, out);
out.close();
}
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()); }
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | a7cb53905cee635c19a0ef855dd28a42 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes |
import java.util.Scanner;
/**
* Created by user on 01.06.2016.
*/
public class C {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int[] pr = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47};
int col = 0;
int del = 0;
for (int a : pr) {
System.out.println(a);
System.out.flush();
String s = scanner.next();
if (s.equals("yes")) {
col++;
del = a;
}
}
if (col == 1) {
int pros = del * del;
if (pros <= 100) {
System.out.println(pros);
System.out.flush();
String s = scanner.next();
if (s.equals("yes")) {
col++;
}
}
}
if (col > 1) {
System.out.println("composite");
System.out.flush();
} else {
System.out.println("prime");
System.out.flush();
}
}
}
| Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 6931c37e246df417be4f028fcdbbfdc3 | train_002.jsonl | 1465403700 | This is an interactive problem. In the output section below you will see the information about flushing the output.Bear Limak thinks of some hidden number — an integer from interval [2, 100]. Your task is to say if the hidden number is prime or composite.Integer x > 1 is called prime if it has exactly two distinct divisors, 1 and x. If integer x > 1 is not prime, it's called composite.You can ask up to 20 queries about divisors of the hidden number. In each query you should print an integer from interval [2, 100]. The system will answer "yes" if your integer is a divisor of the hidden number. Otherwise, the answer will be "no".For example, if the hidden number is 14 then the system will answer "yes" only if you print 2, 7 or 14.When you are done asking queries, print "prime" or "composite" and terminate your program.You will get the Wrong Answer verdict if you ask more than 20 queries, or if you print an integer not from the range [2, 100]. Also, you will get the Wrong Answer verdict if the printed answer isn't correct.You will get the Idleness Limit Exceeded verdict if you don't print anything (but you should) or if you forget about flushing the output (more info below). | 256 megabytes | import java.util.Scanner;
public class main {
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int arr[]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97};
int count=0;
for(int i=0;i<16;i++){
System.out.println(arr[i]);
String str=sc.nextLine();
if(str.equals("yes")){
count++;
if(i<4){
System.out.println(arr[i]*arr[i]);
str=sc.nextLine();
if(str.equals("yes")){
count++;
}
}
}
if(count>=2){
break;
}
}
if(count>=2){
System.out.println("composite");
System.out.flush();
}
else{
System.out.println("prime");
System.out.flush();
}
}
} | Java | ["yes\nno\nyes", "no\nyes\nno\nno\nno"] | 1 second | ["2\n80\n5\ncomposite", "58\n59\n78\n78\n2\nprime"] | NoteThe hidden number in the first query is 30. In a table below you can see a better form of the provided example of the communication process.The hidden number is divisible by both 2 and 5. Thus, it must be composite. Note that it isn't necessary to know the exact value of the hidden number. In this test, the hidden number is 30.59 is a divisor of the hidden number. In the interval [2, 100] there is only one number with this divisor. The hidden number must be 59, which is prime. Note that the answer is known even after the second query and you could print it then and terminate. Though, it isn't forbidden to ask unnecessary queries (unless you exceed the limit of 20 queries). | Java 8 | standard input | [
"math",
"constructive algorithms",
"number theory",
"interactive"
] | 8cf479fd47050ba96d21f3d8eb43c8f0 | After each query you should read one string from the input. It will be "yes" if the printed integer is a divisor of the hidden number, and "no" otherwise. | 1,400 | Up to 20 times you can ask a query — print an integer from interval [2, 100] in one line. You have to both print the end-of-line character and flush the output. After flushing you should read a response from the input. In any moment you can print the answer "prime" or "composite" (without the quotes). After that, flush the output and terminate your program. To flush you can use (just after printing an integer and end-of-line): fflush(stdout) in C++; System.out.flush() in Java; stdout.flush() in Python; flush(output) in Pascal; See the documentation for other languages. Hacking. To hack someone, as the input you should print the hidden number — one integer from the interval [2, 100]. Of course, his/her solution won't be able to read the hidden number from the input. | standard output | |
PASSED | 9371704580d0f198a4df291a284d0237 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.InputStreamReader;
public class D1176 {
private static int N = 3000000;
private static int[] div = getLowestDivisors(N);
private static int[] getLowestDivisors(int n) {
int[] ret = new int[n+1];
for (int i = 0; i <= n; i++) {
ret[i] = i;
}
for (int i = 2; i <= n; i++) {
if(ret[i]==i)
for (int j = i*2; j <= n; j+=i) {
if(ret[j]==j) ret[j] = i;
}
}
return ret;
}
public static void main(String[] args) throws Exception {
// BufferedReader br = new BufferedReader(new FileReader("F:/books/input.txt"));
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
Integer n = Integer.parseInt(br.readLine());
int[] a = new int[N];
String[] s = br.readLine().split(" ");
for (int i = 0; i < 2*n; i++) {
a[Integer.parseInt(s[i])]++;
}
int[] p = new int[200000];
for(int i=2,j=1;j<200000;i++) {
if(div[i]==i) p[j++]=i;
}
for(int i=N-1;i>1;i--) {
if(a[i]>0 && div[i]!=i) {
a[i/div[i]]-=a[i];
}
}
for(int i=2;i<N;i++) {
if(a[i]>0 && div[i]==i) a[p[i]]-=a[i];
}
StringBuilder sb = new StringBuilder();
for (int i = 2; i < N; i++) {
for (int j = 0; j < a[i]; j++) {
sb.append(i+" ");
}
}
System.out.println(sb);
}
}
| Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | db5ee6af40e0de41c42ae0f9c7f4f007 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class D {
static final int LIMIT = 2750140;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
boolean[] prime = new boolean[LIMIT];
int[] smallestPrimeDivisor = new int[LIMIT], index = new int[LIMIT];
Arrays.fill(prime, true);
int nxtIndex = 1;
for(int i = 2; i < LIMIT; ++i)
if(prime[i]) {
index[i] = nxtIndex++;
if(1l * i * i < LIMIT)
for(int j = i * i; j < LIMIT; j += i) {
prime[j] = false;
if (smallestPrimeDivisor[j] == 0)
smallestPrimeDivisor[j] = i;
}
}
int n = sc.nextInt();
Integer[] b = new Integer[n * 2];
for(int i = 0; i < n * 2; ++i)
b[i] = sc.nextInt();
Arrays.sort(b);
int[] set = new int[LIMIT];
for(int i = b.length - 1; i >= 0; --i)
if(set[b[i]] > 0)
set[b[i]]--;
else if(prime[b[i]]) {
set[index[b[i]]]++;
out.print(index[b[i]] + " ");
}
else {
set[b[i] / smallestPrimeDivisor[b[i]]]++;
out.print(b[i] + " ");
}
out.close();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready();}
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 0602f05a880b8e001fd8e9ccab52300d | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.*;
import java.util.*;
public class TaskD {
public void solve() {
FastReader in = new FastReader(System.in);
// FastReader in2 = new FastReader(new FileInputStream("input.txt"));
PrintWriter out = new PrintWriter(System.out);
// PrintWriter out2 = new PrintWriter(new FileOutputStream("output.txt"));
int n = in.nextInt();
int maxp = 2750131;
int[] b = new int[2 * n];
int[] cnt = new int[maxp + 1];
for (int i = 0; i < 2 * n; i++) {
b[i] = in.nextInt();
cnt[b[i]]++;
}
Arrays.sort(b);
for (int i = 0; i < n; i++) {
b[i] ^= b[2 * n - 1 - i];
b[2 * n - 1 - i] ^= b[i];
b[i] ^= b[2 * n - 1 - i];
}
int[] f = new int[maxp + 1];
boolean[] prime = new boolean[maxp + 1];
Arrays.fill(prime, 2, maxp + 1, true);
for (int i = 2, k = 0; i <= maxp; i++) {
if (prime[i]) {
f[i] = ++k;
if ((long) i * i <= maxp) {
for (int j = i * i; j <= maxp; j += i) {
if (prime[j]) {
prime[j] = false;
f[j] = i;
}
}
}
} else {
f[i] = i / f[i];
}
}
List<Integer> a = new ArrayList<>();
for (int i = 0; i < 2 * n; i++) {
int bi = b[i];
if (cnt[bi] != 0) {
cnt[bi]--;
cnt[f[bi]]--;
if (prime[bi])
a.add(f[bi]);
else
a.add(bi);
}
}
for (int ai : a)
out.print(ai + " ");
out.println();
out.close();
}
private class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
Integer nextInt() {
return Integer.parseInt(next());
}
Long nextLong() {
return Long.parseLong(next());
}
Double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(nextLine());
return st.nextToken();
}
String nextLine() {
String x = "";
try {
x = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return x;
}
}
public static void main(String[] args) {
new TaskD().solve();
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | ecc8b599062c8a863695d5aba492ed31 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | 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.util.HashMap;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.io.InputStream;
import java.io.Flushable;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Shurikat
*/
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);
TaskD solver = new TaskD();
solver.solve(1, in, out);
out.close();
}
static class TaskD {
private final Map<Integer, Integer> primeIndexes;
private final List<Integer> sortedPrimeFactors;
public TaskD() {
this.primeIndexes = new LinkedHashMap<>();
int n = 2750131;
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, 2, n + 1, true);
int index = 1;
for (int i = 2; i <= n; i++) {
if (prime[i]) {
primeIndexes.put(i, index++);
if ((long) i * i <= n) {
for (int j = i * i; j <= n; j += i) {
prime[j] = false;
}
}
}
}
this.sortedPrimeFactors = new ArrayList<>(primeIndexes.keySet());
this.sortedPrimeFactors.sort(Comparator.naturalOrder());
}
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int[] b = new int[n * 2];
Map<Integer, Integer> count = new HashMap<>();
for (int i = 0; i < 2 * n; i++) {
b[i] = in.nextInt();
count.merge(b[i], 1, Integer::sum);
}
Arrays.sort(b);
List<Integer> a = new ArrayList<>(n);
for (int i = 2 * n - 1; i >= 0; i--) {
if (count.get(b[i]) > 0) {
if (isPrime(b[i])) {
int index = primeIndexes.get(b[i]);
a.add(index);
count.put(index, count.get(index) - 1);
} else {
int greatestFactor = computeGreatestFactor(b[i]);
a.add(b[i]);
count.put(greatestFactor, count.get(greatestFactor) - 1);
}
count.put(b[i], count.get(b[i]) - 1);
}
}
for (int integer : a) {
out.print(integer);
out.print(' ');
}
}
public boolean isPrime(int val) {
return primeIndexes.containsKey(val);
}
public int computeGreatestFactor(int val) {
int greatestFactor = 1;
for (int i = 0; i < sortedPrimeFactors.size(); i++) {
int factor = sortedPrimeFactors.get(i);
if (val % factor == 0) {
return val / factor;
}
}
return greatestFactor;
}
}
static class InputReader implements AutoCloseable {
private final InputStream in;
private int capacity;
private byte[] buffer;
private int len = 0;
private int cur = 0;
public InputReader(InputStream stream) {
this(stream, 100_000);
}
public InputReader(InputStream stream, int capacity) {
this.in = stream;
this.capacity = capacity;
buffer = new byte[capacity];
}
private boolean update() {
if (cur >= len) {
try {
cur = 0;
len = in.read(buffer, 0, capacity);
if (len <= 0)
return false;
} catch (IOException e) {
throw new InputMismatchException();
}
}
return true;
}
private int read() {
if (update())
return buffer[cur++];
else return -1;
}
private boolean isSpace(int c) {
return c == '\n' || c == '\t' || c == '\r' || c == ' ';
}
private int readSkipSpace() {
int c;
do {
c = read();
} while (isSpace(c));
return c;
}
public int nextInt() {
int c = readSkipSpace();
if (c < 0)
throw new InputMismatchException();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c == -1) break;
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
if (res < 0)
throw new InputMismatchException();
c = read();
} while (!isSpace(c));
res *= sgn;
return res;
}
public void close() throws IOException {
in.close();
}
}
static class OutputWriter implements AutoCloseable, Flushable {
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 i) {
writer.print(i);
}
public void print(char i) {
writer.print(i);
}
public void flush() throws IOException {
writer.flush();
}
public void close() {
writer.close();
}
}
}
| Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 4f2236d9f313bae4022706c62c99963c | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util.Scanner;
import java.util.Vector;
import java.util.Arrays;
import java.util.Comparator;
public class D{
static int[] simples = new int[2750132];
static int max = 2750131;
static int[] coun = new int[max+1];
public static void erat(){
int num = -1;
int sqrt = (int)Math.sqrt(max)+1;
for(int i = 2; i<=max;++i){
if(simples[i] == 0 ){
simples[i] = num--;
if(i <= sqrt)
for(int j = i*i;j<=max;j+=i)
simples[j] = Math.max(j/i,simples[j]);
}
}
}
public static int search(Vector<Integer> b, int el){
int l = 0;
int r = b.size()-1;
while(l<=r){
int mid = (l+r)/2;
if(b.get(mid) == el) return mid;
if(Math.abs(b.get(mid))<el) r = mid-1;
else l = mid +1;
}
return (l >= b.size()) ? l-1:l;
}
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
Vector<Integer> a = new Vector<>();
Vector<Integer> b = new Vector<>();
for(int i = 0;i<2*n;++i){
b.add(in.nextInt());
coun[b.get(i)]++;
}
Comparator<Integer> comp = new Comparator<Integer>(){
public int compare(Integer a, Integer b){
return Integer.compare(b,a);
}};
b.sort(comp);
erat();
//System.out.println(b);
//System.out.println(Arrays.toString(Arrays.copyOf(simples,10)));
StringBuilder str = new StringBuilder();
for(int i = 0; i < 2*n;++i){
if (coun[b.get(i)] <= 0) continue;
//System.out.println(i + " " + b);
//System.out.println(simples[b.get(i)]);
if(simples[b.get(i)] < 0){
str.append(-simples[b.get(i)] + " ");
//int t = search(b,-simples[b.get(i)]);
//b.set(t,-b.get(t));
//b.set(i,-b.get(i));
coun[b.get(i)]--;
coun[-simples[b.get(i)]]--;
continue;
}
str.append(b.get(i) + " ");
coun[b.get(i)]--;
coun[simples[b.get(i)]]--;
}
System.out.println(str);
}
}
| Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 1407e49c4bbd743abc8d3e7a2f29811d | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | 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));
int n=Integer.parseInt(br.readLine());
String s1[]=br.readLine().split(" ");
int a[]=new int[2*n];
for(int i=0;i<2*n;i++)
a[i]=Integer.parseInt(s1[i]);
Arrays.sort(a);
boolean b[]=new boolean[2750132];
ArrayList<Integer> primes=new ArrayList<Integer>();
Arrays.fill(b,true);
for(int i=2;i<(int)Math.sqrt(2750132);i++)
{
if(b[i]==true)
{
for(int j=i*i;j<2750132;j+=i)
{
b[j]=false;
}
}
}
for(int i=2;i<2750132;i++)
if(b[i]==true)
primes.add(i);
int len=primes.size();
ArrayList<Integer> al=new ArrayList<Integer>();
ArrayList<Integer> a2=new ArrayList<Integer>();
ArrayList<Integer> fin=new ArrayList<Integer>();
HashMap<Integer,Integer> hm=new HashMap<Integer,Integer>();
for(int i=0;i<2*n;i++)
if(hm.containsKey(a[i]))
hm.put(a[i],1+hm.get(a[i]));
else
hm.put(a[i],1);
for(int i=2*n-1;i>=0;i--)
if(b[a[i]]==false)
{ al.add(a[i]); }
int m=al.size();
for(int i=0;i<m;i++)
{
int u=al.get(i);
int v=-1;
if(!hm.containsKey(u))
continue;
for(int j=0;j<len;j++)
if(u%primes.get(j)==0)
{ v=u/primes.get(j); break; }
fin.add(u);
if(hm.get(u)>1)
hm.put(u,hm.get(u)-1);
else
hm.remove(u);
//System.out.println(u+" "+v);
if(hm.get(v)>1)
hm.put(v,hm.get(v)-1);
else
hm.remove(v);
}
for(int i:hm.keySet())
{
for(int j=0;j<hm.get(i);j++)
a2.add(i);
}
Collections.sort(a2);
int size=a2.size();
//System.out.println(primes.size());
for(int i=0;i<size;i++)
{
int u=a2.get(i);
if(!hm.containsKey(a2.get(i)))
continue;
fin.add(a2.get(i));
if(hm.get(u)>1)
hm.put(u,hm.get(u)-1);
else
hm.remove(u);
int v=primes.get(u-1);
if(hm.get(v)>1)
hm.put(v,hm.get(v)-1);
else
hm.remove(v);
}
StringBuffer sb=new StringBuffer();
for(int i=0;i<n;i++)
sb.append(fin.get(i)).append(" ");
System.out.println(sb);
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 6b4a8cd07e3d3361e6cb6aa478a77b28 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.*;
import java.util.*;
public class D1176_RecoverIt {
static int LIM = 2750131;
public static void main(String[] args) {
InputReader in = new InputReader();
int n = in.nextInt();
ArrayList<Integer> list = new ArrayList<>();
for (int i=0; i<2*n; i++)
list.add(in.nextInt());
Collections.sort(list);
boolean[] isComposite = new boolean[LIM + 1];
Map<Integer, Integer> primes = new HashMap<>();
int[] maxDiv = new int[LIM + 1];
findPrimes(primes, isComposite, maxDiv);
calcMaxDivisors(maxDiv);
StringBuffer sb = new StringBuffer();
Map<Integer, Integer> used = new HashMap<>();
while (!list.isEmpty()) {
int num = list.remove(list.size()-1); // last element (largest)
if (used.containsKey(num)) {
if (used.get(num) > 1)
used.put(num, used.get(num) - 1);
else
used.remove(num);
continue;
}
if (isComposite[num]) {
sb.append(num + " ");
if (!used.containsKey(maxDiv[num]))
used.put(maxDiv[num], 1);
else
used.put(maxDiv[num], used.get(maxDiv[num]) + 1);
}
else {
sb.append(primes.get(num) + " ");
if (!used.containsKey(primes.get(num)))
used.put(primes.get(num), 1);
else
used.put(primes.get(num), used.get(primes.get(num)) + 1);
}
}
sb.append("\n");
System.out.println(sb);
}
public static void findPrimes(Map<Integer, Integer> primes, boolean[] isComposite, int[] maxDiv) {
int numPrimes = 0;
for (int i=2; i<=LIM; i++) if (!isComposite[i]) {
++numPrimes;
primes.put(i, numPrimes);
for (int j=i+i; j<LIM; j+=i) {
isComposite[j] = true;
if (maxDiv[j] == 0)
maxDiv[j] = i;
}
}
}
public static void calcMaxDivisors(int[] maxDiv) {
for (int i=0; i<=LIM; i++)
if (maxDiv[i] != 0)
maxDiv[i] = i/maxDiv[i];
}
static class InputReader {
public BufferedReader br;
public StringTokenizer st;
public InputReader() {
br = new BufferedReader(new InputStreamReader(System.in));
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | b08e9b8a18d4da91b1f619da8ae3dcc0 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util.*;
public class codeforces {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
for (int i = 0; i < 2 * n; i++) {
pq.add(scan.nextInt());
}
int p[] = new int[2750132];
for (int i = 0; i < p.length; i++) {
p[i] = i;
}
p[1] = 0;
for (int i = 2; i < p.length; i++) {
if (p[i] > 0) {
for (int j = i * 2; j < p.length; j += i) {
p[j] = 0;
}
}
}
TreeMap<Integer, Integer> prime = new TreeMap<>();
int cnt = 1;
for (int i = 0; i < p.length; i++) {
if (p[i] > 0) {
prime.put(i, cnt);
cnt++;
}
}
ArrayList<Integer> a = new ArrayList<>();
TreeMap<Integer, Integer> del = new TreeMap<>();
while (!pq.isEmpty()) {
int x = pq.poll();
if (del.containsKey(x)) {
del.put(x, del.get(x) - 1);
if (del.get(x) == 0) del.remove(x);
continue;
}
if (prime.containsKey(x)) {
a.add(prime.get(x));
del.put(prime.get(x), del.getOrDefault(prime.get(x), 0) + 1);
} else {
int d = x;
for (int i = 2; i < x; i++) {
if (x % i == 0) {
d = i;
break;
}
}
a.add(x);
del.put(x / d, del.getOrDefault(x / d, 0) + 1);
}
}
for (int x : a) System.out.print(x + " ");
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 1b8ef0f0004d61daac9a673aed775e09 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes |
import java.util.*;
import java.lang.*;
import java.lang.reflect.Array;
import java.io.*;
import java.math.*;
import java.text.DecimalFormat;
public class Temp{
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String rs() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static PrintWriter w = new PrintWriter(System.out);
public static class Key {
private final int x;
private final int y;
public Key(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Key)) return false;
Key key = (Key) o;
return x == key.x && y == key.y;
}
@Override
public int hashCode() {
int result = x;
result = 31 * result + y;
return result;
}
}
static long mod=1000000007L;
static ArrayList<Integer> arr[];
static int n;
static boolean prime[]=new boolean[(int)(1e7+1)];
static ArrayList<Integer> pri=new ArrayList<>();
static int maxD[]=new int[(int)3e6+1];
static void findMaxD(){
for(int i:pri){
if(i<=2)continue;
if(prime[i]){
for(int j=3;(j*i)<=(int)3e6;j+=2){
maxD[j*i]=Math.max(Math.max(i, j),maxD[j*i]);
}
}
}
for(int i=2;i<=(int)3e6;i+=2){
maxD[i]=i/2;
}
}
static void sieve() {
Arrays.fill(prime, true);
pri.add(-1);
prime[0] = prime[1] = false;
for(int i = 2 ; i <=(int)1e7 ; ++i) {
if(prime[i]) {
pri.add(i);
int j=2*i;
while(j<=(int)1e7){
prime[j]=false;
j+=i;
}
}
}
}
public static void main (String[] args)throws IOException{
InputReader sc=new InputReader(System.in);
sieve();
findMaxD();
int n=sc.ni();
//TreeMap<Integer,Integer> map=new TreeMap<>();
int co[]=new int[(int)3e6+1];
for(int i=0;i<2*n;i++){
int a=sc.ni();
co[a]++;
// if(map.containsKey(a))map.replace(a,map.get(a)+1);
// else map.put(a,1);
}
ArrayList<Integer> ans=new ArrayList<>();
for(int i=(int)3e6;i>2;i--){
if(co[i]>0&&!prime[i]){
int l=co[i];
for(int k=0;k<l&&co[maxD[i]]>0;k++){
ans.add(i);
co[i]--;
co[maxD[i]]--;
}
}
}
for(int i=2;i<=(int)3e6;i++){
if(co[i]>0&&prime[i]){
int l=co[i];
for(int k=0;k<l&&co[pri.get(i)]>0;k++){
co[i]--;
co[pri.get(i)]--;
ans.add(i);
}
}
}
for(int i:ans)w.print(i+" ");
w.println();
w.close();
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | e2106c7fd79e8c22201de7fbaedc22ee | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util.*;
import java.io.*;
public class RecoverIt {
static boolean[] prime;
public static void main(String[] args) {
FastScanner scanner = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int N = scanner.nextInt();
TreeMap<Integer, Integer> mapping = new TreeMap<>();
for(int i = 0; i < 2 * N; i++) {
int next = scanner.nextInt();
if (!mapping.containsKey(next)) mapping.put(next, 1);
else mapping.put(next, mapping.get(next) + 1);
}
ArrayList<Integer> primes = sieve(2750132);
int[] ans = new int[N];
int p = 0;
while(!mapping.isEmpty()) {
int maxKey = mapping.lastKey();
if (mapping.get(maxKey) > 1) mapping.put(maxKey, mapping.get(maxKey) -1);
else mapping.remove(maxKey);
if (prime[maxKey]) {
int ind = Collections.binarySearch(primes, maxKey);
ans[p++] = ind;
if (mapping.get(ind) > 1) mapping.put(ind, mapping.get(ind) -1);
else mapping.remove(ind);
}
else {
ans[p++] = maxKey;
for(int i= 2; i * i <= maxKey; i++) {
if (maxKey % i == 0) {
if (mapping.get(maxKey/i) > 1) mapping.put(maxKey/i, mapping.get(maxKey/i) -1);
else mapping.remove(maxKey/i);
break;
}
}
}
}
for(int i = 0; i < N; i++) {
if (i > 0) out.print(" ");
out.print(ans[i]);
}
out.flush();
}
static ArrayList<Integer> sieve(int p) {
prime = new boolean[p + 1];
Arrays.fill(prime, true);
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
prime[0] = prime[1] = false;
int i = 2;
for(; i * i <= p; i++) {
if (!prime[i]) continue;
list.add(i);
for(int j = i * i; j <= p; j+= i) {
prime[j] = false;
}
}
for(;i <= p; i++) if (prime[i]) list.add(i);
return list;
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(Reader in) {
br = new BufferedReader(in);
}
public FastScanner() {
this(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 readNextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readIntArray(int n) {
int[] a = new int[n];
for (int idx = 0; idx < n; idx++) {
a[idx] = nextInt();
}
return a;
}
}
}
| Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | b0a38a330978b395a1cf125549a3bc5f | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.InputMismatchException;
public class D565B {
static int[] great;
static boolean[] comp;
static int[] primes;
public static void main(String[] args) {
FastScanner in = new FastScanner(System.in);
int N = in.nextInt();
int[] count = new int[3000000];
ArrayList<Integer> list = new ArrayList<>();
for(int i = 0; i < N*2; i++) {
int x = in.nextInt();
list.add(x);
count[x]++;
}
Collections.sort(list);
ArrayList<Integer> aList = new ArrayList<>();
get();
for(int i = count.length; i >= 0; i--) {
if(i < comp.length && comp[i]) {
while(count[i] > 0) {
aList.add(i);
count[i]--;
count[great[i]]--;
}
}
}
for(int i = 0; i < count.length; i++) {
if(i < comp.length && !comp[i]) {
while(count[i] > 0) {
aList.add(i);
count[i]--;
count[primes[i]]--;
}
}
}
StringBuilder sb = new StringBuilder();
for(int i : aList) {
sb.append(i);
sb.append(" ");
}
System.out.println(sb.toString().trim());
}
public static void get() {
comp = new boolean[3000000];
great = new int[200005];
primes = new int[200000];
for(int i = 2; i < Math.sqrt(comp.length+1); i++) {
if(comp[i]) continue;
for(int j = i*i; j < comp.length; j += i) {
comp[j] = true;
if(j < great.length && great[j] == 0) {
great[j] = j / i;
}
}
}
int index = 1;
for(int i = 2; index < primes.length && i < comp.length; i++) {
if(!comp[i]) {
primes[index++] = i;
}
}
}
/**
* Source: Matt Fontaine
*/
static class FastScanner {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int chars;
public FastScanner(InputStream stream) {
this.stream = stream;
}
int read() {
if (chars == -1)
throw new InputMismatchException();
if (curChar >= chars) {
curChar = 0;
try {
chars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (chars <= 0)
return -1;
}
return buf[curChar++];
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
boolean isEndline(int c) {
return c == '\n' || c == '\r' || c == -1;
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String next() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isEndline(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndline(c));
return res.toString();
}
}
}
/*
3
3 5 2 3 2 4
outputCopy
3 4 2
inputCopy
1
2750131 199999
outputCopy
199999
inputCopy
1
3 6
outputCopy
6
*/ | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | c0e7493995d4163401894f899365f6fd | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util.*;
import java.io.*;
public class codeforces{
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader() {
reader = new BufferedReader(new InputStreamReader(System.in));
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 double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String s = null;
try {
s = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return s;
}
public String nextParagraph() {
String line = null;
String ans = "";
try {
while ((line = reader.readLine()) != null) {
ans += line;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return ans;
}
}
static int mod = 1000000007;
static void printArray(int ar[]){ for(int x:ar) System.out.print(x+" ");}
static void printArray(ArrayList<Integer> ar){ for(int x:ar) System.out.print(x+" ");}
static int gcd(int a, int b){ if (a == 0) return b; return gcd(b%a, a);}
static long power(long x, long 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 Data{
int e;
String val;
public Data(int e,String val){
this.e = e;
this.val = val;
}
}
static HashMap<Integer, Integer> pmap = new HashMap<Integer, Integer>();
static void sieveOfEratosthenes()
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
int size = 2750131;
boolean prime[] = new boolean[size+1];
for(int i=0;i<=size;i++)
prime[i] = true;
for(int p = 2; p*p <=size; p++)
{
// If prime[p] is not changed, then it is a prime
if(prime[p] == true)
{
// Update all multiples of p
for(int i = p*p; i <= size; i += p)
prime[i] = false;
}
}
int pr = 0;
for(int i=2;i<=size;i++){
if(prime[i]){
pr++;
pmap.put(i, pr);
}
}
return;
}
static int existsG(int g){
if(g%2 == 0)
return g/2;
for(int i=3;i<=Math.sqrt(g);i+=2){
if(g%i == 0)
return g/i;
}
return -1;
}
public static void main(String args[]){
InputReader sc = new InputReader();
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt(), i=0, j=0;
n<<=1;
int ar[] = new int[n];
int a[] = new int[n/2];
HashMap<Integer, Integer> nmap = new HashMap<Integer, Integer>();
sieveOfEratosthenes();
for(i=0;i<n;i++){
ar[i] = sc.nextInt();
nmap.put(ar[i], nmap.getOrDefault(ar[i], 0) + 1);
}
Arrays.sort(ar);
for(i=n-1;i>=0;i--){
int o = 0;
if(nmap.get(ar[i]) == 0)
continue;
if(pmap.containsKey(ar[i]) && pmap.containsKey(pmap.get(ar[i]))){
o = pmap.get(ar[i]);
a[j++] = o;
}else{
o = existsG(ar[i]);
a[j++] = ar[i];
}
nmap.put(o, nmap.get(o)-1);
nmap.put(ar[i], nmap.get(ar[i])-1);
}
for(int x:a)
pw.print(x+" ");
//pw.println("Done");
pw.close();
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | a0678129c05cd283b3c4d057c67b4632 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
private static int[] composite;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
Map<Integer, Integer> map = init();
int n = in.nextInt() * 2;
Integer[] arr = new Integer[n];
HashMap<Integer, Integer> count = new HashMap<>();
for (int i = 0; i < n; ++i) {
arr[i] = in.nextInt();
count.put(arr[i], count.getOrDefault(arr[i], 0) + 1);
}
Arrays.sort(arr);
for (int i = n - 1; i >= 0; --i) if (count.get(arr[i]) > 0) {
count.put(arr[i], count.get(arr[i]) - 1);
//System.out.println(arr[i] + " " + composite[arr[i]] + " " + map.get(arr[i]) + " " + count.get(arr[i]));
if (map.containsKey(arr[i]) && count.getOrDefault(map.get(arr[i]), 0) > 0) {
int x = map.get(arr[i]);
System.out.print(x + " ");
count.put(x, count.get(x) - 1);
} else {
// lets suppose we have the element
int x = arr[i] / composite[arr[i]];
System.out.print(arr[i] + " ");
count.put(x, count.getOrDefault(x, 0) - 1);
}
}
System.out.println();
}
private static Map<Integer, Integer> init() {
Map<Integer, Integer> map = new HashMap<>();
int maxv = 2750131;
composite = new int[maxv + 1];
for (int i = 4; i <= maxv; i += 2)
composite[i] = 2;
int cur = 2;
for (int i = 3; i <= maxv; i += 2) if (composite[i] == 0) {
for (int j = i + i; j <= maxv; j += i) if (composite[j] == 0)
composite[j] = i;
if (composite[cur] == 0) {
map.put(i, cur);
}
++cur;
}
return map;
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 406f7af3bc1240704052819f257166b6 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.HashMap;
public class Main
{
public static void main(String[] args)
{
FastReader fr =new FastReader(); PrintWriter op =new PrintWriter(System.out);
HashMap<Integer , Integer> hm =new HashMap<Integer , Integer>() ;
boolean nonprm[] =new boolean[2750132] ; int i ,j ,k ,l =2750131 ,n =fr.nextInt() ,arr[] =new int[2*n] ,frq[] =new int[l+1] ;
for (j =2 ; j<=l/2 ; ++j) nonprm[j*2] =true ;
for (i =3 ; i<(int)Math.sqrt(l) ; i += 2) {
if (nonprm[i]) continue;
for (j =i ; j<=l/i ; ++j) nonprm[j*i] =true ;
}
j =0 ;
for (i =2 ; i<=l ; i++) {
if (!nonprm[i]) hm.put (i , ++j) ;
}
for (i =0 ; i<2*n ; ++i) frq[(arr[i] =fr.nextInt())]++ ;
sort (arr , 0 , 2*n-1) ;
j =0 ; int ans[] =new int[n] ;
for (i =2*n-1 ; i>-1 ; i--) {
if (frq[arr[i]] == 0) continue;
if (hm.containsKey(arr[i])) {
ans[j++] =hm.get(arr[i]) ; frq[arr[i]]-- ; frq[hm.get(arr[i])]-- ;
}
else {
for (k =2 ; k<=(int)Math.sqrt(arr[i]) ; k++) {
if (arr[i]%k == 0) break;
}
ans[j++] =arr[i] ; frq[arr[i]]-- ; frq[arr[i]/k]-- ;
}
}
for (i =0 ; i<n ; ++i) op.print(ans[i]+" ") ;
op.flush(); op.close();
}
public static void sort(int[] arr , int l , int u) {
int m ;
if(l < u){
m =(l + u)/2 ;
sort(arr , l , m); sort(arr , m + 1 , u);
merge(arr , l , m , u);
}
}
public static void merge(int[] arr , int l , int m , int u) {
int[] low = new int[m - l + 1] ;
int[] upr = new int[u - m] ;
int i ,j =0 ,k =0 ;
for(i =l;i<=m;i++)
low[i - l] =arr[i] ;
for(i =m + 1;i<=u;i++)
upr[i - m - 1] =arr[i] ;
i =l;
while((j < low.length) && (k < upr.length))
{
if(low[j] < upr[k])
arr[i++] =low[j++] ;
else
arr[i++] =upr[k++] ;
}
while(j < low.length)
arr[i++] =low[j++] ;
while(k < upr.length)
arr[i++] =upr[k++] ;
}
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();
}
String nextLine() {
String str ="";
try
{
str =br.readLine();
}
catch(IOException e)
{
e.printStackTrace();
}
return str;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next()) ;
}
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | a4e1fad9addfeb22e484a105cc6be38a | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util.*;
public class D {
public static void main(String[] args) {
int maxn=3000_000;
boolean[] isPrime=new boolean[maxn];
Arrays.fill(isPrime, true);
for (int i=2; i<maxn; i++)
for (int j=i+i; j<maxn; j+=i)
isPrime[j]=false;
isPrime[0]=isPrime[1]=false;
int[] factOf=new int[maxn];
for (int i=2; i<maxn; i++)
if (isPrime[i])
for (int j=i; j<maxn; j+=i)
factOf[j]=i;
int[] primeIndex=new int[maxn];
int primeInd=1;
for (int i=2; i<maxn; i++)
if (isPrime[i])
primeIndex[i]=primeInd++;
Scanner fs=new Scanner(System.in);
int n=fs.nextInt();
int[] cs=new int[maxn];
for (int i=0; i<n+n; i++)
cs[fs.nextInt()]++;
for (int i=maxn-1; i>=0; i--) {
if (cs[i]==0) continue;
while (cs[i]>0){
cs[i]--;
if (isPrime[i]) {
cs[primeIndex[i]]--;
System.out.print(primeIndex[i]+" ");
//System.out.println("\ni: "+i+" "+primeIndex[i]+" "+cs[i]);
}
else {
int val=i;
int minFac=i;
while (val!=1) {
minFac=Math.min(minFac, factOf[val]);
val/=factOf[val];
}
cs[i/minFac]--;
System.out.print(i+" ");
}
}
}
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | fd2d912d631b46f620fa00a9cd20382d | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
import org.omg.CORBA.INTERNAL;
import org.omg.CORBA.MARSHAL;
import javax.swing.plaf.basic.BasicTreeUI;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
public class Main {
private static Scanner sc;
private static Printer pr;
static boolean []visited;
static int []color;
static int []pow=new int[(int)3e5+1];
static int []count;
static boolean check=true;
static boolean checkBip=true;
static TreeSet<Integer>[]list;
private static long aLong=(long)(Math.pow(10,9)+7);
static final long div=998244353;
static int answer=0;
static int[]countSubTree;
static int[]colorNode;
static boolean con=true;
static ArrayList<Integer>dfsPath=new ArrayList<>();
static ArrayList<Integer>ans=new ArrayList<>();
static boolean[]rec;
static int min=Integer.MAX_VALUE;
static ArrayList<Task>tasks=new ArrayList<>();
static boolean []checkpath;
static long []out;
private static void solve() throws IOException {
long maxN=2750132;
int n=sc.nextInt();
TreeMap<Long,Integer>map=new TreeMap<>(Collections.reverseOrder());
long []prime=new long[(int )maxN];
long []pidx=new long[(int )maxN];
for (int i=2;i<prime.length;++i){
if (prime[i]!=0)continue;
for (int j=2*i;j<prime.length;j+=i){
if (prime[j]==0)prime[j]=i;
}
}
//for (int i=2;i<=25;++i)pr.print(prime[i]+" ");
//pr.println();
int cnt=1;
for (int i=2;i<prime.length;++i){
if (prime[i]==0){
pidx[i]=cnt;
++cnt;
}
}
for (int i=0;i<(2*n);++i){
long in=sc.nextLong();
if (map.containsKey(in))map.put(in,map.get(in)+1);
else map.put(in,1);
}
ArrayList<Long>anss=new ArrayList<>();
while (map.size()!=0){
long maxEl=map.firstKey();
if (map.get(maxEl)>1)map.put(maxEl,map.get(maxEl)-1);
else map.remove(maxEl);
//pr.println("ma:"+maxEl);
if (prime[(int)maxEl]==0){
long idx=pidx[(int)maxEl];
//pr.println("idx:"+idx);
anss.add(idx);
if (map.get(idx)>1)map.put(idx,map.get(idx)-1);
else map.remove(idx);
}else {
anss.add(maxEl);
long gdiv=maxEl/prime[(int)maxEl];
if (map.get(gdiv)>1)map.put(gdiv,map.get(gdiv)-1);
else map.remove(gdiv);
}
}
//for (int i=anss.size()-1;i>=0;--i)pr.print(anss.get(i)+" ");
for (long s:anss)pr.print(s+" ");
}
public static class pp {
long val;
int idx;
public pp(long val,int idx) {
this.val = idx;
this.idx = idx;
}
}
public static class dsu{
int []rank;
int[]parent;
long []sum;
int n;
public dsu(int n) {
this.n = n;
parent=new int[n];
rank=new int[n];
sum=new long[n];
for (int i=0;i<n;++i){
rank[i]=1;
parent[i]=i;
sum[i]=0;
}
}
public int dad(int child){
if (parent[child]==child)return child;
else return dad(parent[child]);
}
public void merge(int u,int v){
int dadU=dad(u);
int dadV=dad(v);
if (dadU==dadV)return ;
if (u!=v){
if (rank[u]<rank[v]){
parent[u]=v;
sum[v]+=sum[u];
}
else if (rank[u]==rank[v]){
parent[u]=v;
sum[v]+=sum[u];
}
else{
parent[v]=u;
sum[u]+=sum[v];
}
}
}
}
public static long power(long x,long y,long mods){
if (y==0) return 1%mods;
long u=power(x,y/2,mods);
u=((u)*(u))%mods;
if (y%2==1) u=(u*(x%mods))%mods;
return u;
}
public static class Task implements Comparable<Task>{
long l,r;
public Task(long w,long v) {
this.l=w;
this.r=v;
}
@Override
public int compareTo(Task o) {
if (o.l<this.l)return 1;
else if (o.l>this.l)return -1;
else {
if (o.r>this.r)return 1;
else if (o.r<this.r)return -1;
}
return 0;
}
}
public static void printSolve(StringBuilder str, int[] colors, int n,int color){
for(int i = 1;i<=n;++i)
if(colors[i] == color)
str.append(i + " ");
str.append('\n');
}
public static class pairTask{
int val;
int size;
public pairTask(int x, int y) {
this.val = x;
this.size = y;
}
}
public static void subTree(int src,int parent,int x){
countSubTree[src]=1;
if (src==x) {
checkpath[src]=true;
//System.out.println("src:"+src);
}
else checkpath[src]=false;
for (int v:list[src]){
if (v==parent)
continue;
subTree(v,src,x);
countSubTree[src]+=countSubTree[v];
checkpath[src]|=checkpath[v];
}
}
public static boolean prime(long src){
for (int i=2;i*i<=src;i++){
if (src%i==0)
return false;
}
return true;
}
public static void bfsColor(int src){
Queue<Integer>queue = new ArrayDeque<>();
queue.add(src);
while (!queue.isEmpty()){
boolean b=false;
int vertex=-1,p=-1;
int poll=queue.remove();
for (int v:list[poll]){
if (color [v]==0){
vertex=v;
break;
}
}
for (int v:list[poll]){
if (color [v]!=0&&v!=vertex){
p=v;
break;
}
}
for (int v:list[p]){
if (color [v]!=0){
color[vertex]=color[v];
b=true;
break;
}
}
if (!b){
color[vertex]=color[poll]+1;
}
}
}
static int add(int a,int b ){
if (a+b>=div)
return (int)(a+b-div);
return (int)a+b;
}
public static int isBipartite(ArrayList<Integer>[]list,int src){
color[src]=0;
Queue<Integer>queue=new LinkedList<>();
int []ans={0,0};
queue.add(src);
while (!queue.isEmpty()){
ans[color[src=queue.poll()]]++;
for (int v:list[src]){
if (color[v]==-1){
queue.add(v);
color[v]=color[src]^1;
}else if (color[v]==color[src])
check=false;
}
}
return add(pow[ans[0]],pow[ans[1]]);
}
public static int powerMod(long b, long e){
long ans=1;
while (e-->0){
ans=ans*b%div;
}
return (int)ans;
}
public static int dfs(int s){
int ans=1;
visited[s]=true;
for (int k:list[s]){
if (!visited[k]){
ans+=dfs(k);
}
}
return ans;
}
public static int[] radixSort(int[] f) {
int[] to = new int[f.length];
{
int[] b = new int[65537];
for (int i = 0; i < f.length; i++) b[1 + (f[i] & 0xffff)]++;
for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
for (int i = 0; i < f.length; i++) to[b[f[i] & 0xffff]++] = f[i];
int[] d = f;
f = to;
to = d;
}
{
int[] b = new int[65537];
for (int i = 0; i < f.length; i++) b[1 + (f[i] >>> 16)]++;
for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
for (int i = 0; i < f.length; i++) to[b[f[i] >>> 16]++] = f[i];
int[] d = f;
f = to;
to = d;
}
return f;
}
public static long []primeFactor(int n){
long []prime=new long[n+1];
prime[1]=1;
for (int i=2;i<=n;i++)
prime[i]=((i&1)==0)?2:i;
for (int i=3;i*i<=n;i++){
if (prime[i]==i){
for (int j=i*i;j<=n;j+=i){
if (prime[j]==j)
prime[j]=i;
}
}
}
return prime;
}
public static StringBuilder binaryradix(long number){
StringBuilder builder=new StringBuilder();
long remainder;
while (number!=0) {
remainder = number % 2;
number >>= 1;
builder.append(remainder);
}
builder.reverse();
return builder;
}
public static int binarySearch(long[] a, int index,long target) {
int l = index;
int h = a.length - 1;
while (l<=h) {
int med = l + (h-l)/2;
if(a[med] - target <= target) {
l = med + 1;
}
else h = med - 1;
}
return h;
}
public static int val(char c){
return c-'0';
}
public static long gcd(long a,long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
private static class Pair implements Comparable<Pair> {
long x;
long y;
Pair() {
this.x = 0;
this.y = 0;
}
Pair(long x, long y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) return false;
Pair other = (Pair) obj;
if (this.x == other.x && this.y == other.y) {
return true;
}
return false;
}
@Override
public int compareTo(Pair other) {
if (this.x != other.x) return Long.compare(this.x, other.x);
return Long.compare(this.y*other.x, this.x*other.y);
}
}
public static void main(String[] args) throws IOException {
sc = new Scanner(System.in);
pr = new Printer(System.out);
solve();
pr.close();
// sc.close();
}
private static class Scanner {
BufferedReader br;
Scanner (InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
}
private boolean isPrintable(int ch) {
return ch >= '!' && ch <= '~';
}
private boolean isCRLF(int ch) {
return ch == '\n' || ch == '\r' || ch == -1;
}
private int nextPrintable() {
try {
int ch;
while (!isPrintable(ch = br.read())) {
if (ch == -1) {
throw new NoSuchElementException();
}
}
return ch;
} catch (IOException e) {
throw new NoSuchElementException();
}
}
String next() {
try {
int ch = nextPrintable();
StringBuilder sb = new StringBuilder();
do {
sb.appendCodePoint(ch);
} while (isPrintable(ch = br.read()));
return sb.toString();
} catch (IOException e) {
throw new NoSuchElementException();
}
}
int nextInt() {
try {
// parseInt from Integer.parseInt()
boolean negative = false;
int res = 0;
int limit = -Integer.MAX_VALUE;
int radix = 10;
int fc = nextPrintable();
if (fc < '0') {
if (fc == '-') {
negative = true;
limit = Integer.MIN_VALUE;
} else if (fc != '+') {
throw new NumberFormatException();
}
fc = br.read();
}
int multmin = limit / radix;
int ch = fc;
do {
int digit = ch - '0';
if (digit < 0 || digit >= radix) {
throw new NumberFormatException();
}
if (res < multmin) {
throw new NumberFormatException();
}
res *= radix;
if (res < limit + digit) {
throw new NumberFormatException();
}
res -= digit;
} while (isPrintable(ch = br.read()));
return negative ? res : -res;
} catch (IOException e) {
throw new NoSuchElementException();
}
}
long nextLong() {
try {
// parseLong from Long.parseLong()
boolean negative = false;
long res = 0;
long limit = -Long.MAX_VALUE;
int radix = 10;
int fc = nextPrintable();
if (fc < '0') {
if (fc == '-') {
negative = true;
limit = Long.MIN_VALUE;
} else if (fc != '+') {
throw new NumberFormatException();
}
fc = br.read();
}
long multmin = limit / radix;
int ch = fc;
do {
int digit = ch - '0';
if (digit < 0 || digit >= radix) {
throw new NumberFormatException();
}
if (res < multmin) {
throw new NumberFormatException();
}
res *= radix;
if (res < limit + digit) {
throw new NumberFormatException();
}
res -= digit;
} while (isPrintable(ch = br.read()));
return negative ? res : -res;
} catch (IOException e) {
throw new NoSuchElementException();
}
}
float nextFloat() {
return Float.parseFloat(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
try {
int ch;
while (isCRLF(ch = br.read())) {
if (ch == -1) {
throw new NoSuchElementException();
}
}
StringBuilder sb = new StringBuilder();
do {
sb.appendCodePoint(ch);
} while (!isCRLF(ch = br.read()));
return sb.toString();
} catch (IOException e) {
throw new NoSuchElementException();
}
}
void close() {
try {
br.close();
} catch (IOException e) {
// throw new NoSuchElementException();
}
}
}
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());
}
}
static class List {
String Word;
int length;
List(String Word, int length) {
this.Word = Word;
this.length = length;
}
}
private static class Printer extends PrintWriter {
Printer(PrintStream out) {
super(out);
}
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | c66c1a455871081d8c398d2fb40d9281 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util. * ;
public class recover_prime {
static boolean[] used = new boolean[3000000] ;
static int[] biggest_d = new int[3000000] ;
static HashMap<Integer, Integer> p_order = new HashMap<Integer, Integer>() ;
static void sieve() {
Arrays.fill(used, true);
for(int i=2;i<3000000;i++){
if(used[i] == true) {
p_order.put(i, p_order.size() + 1) ;
for(int j=2*i; j<3000000;j+=i) {
used[j] = false ;
if(biggest_d[j] == 0) {
biggest_d[j] = j / i ;
}
}
}
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner in = new Scanner(System.in) ;
int n = in.nextInt() ;
int[] a = new int[2*n] ;
HashMap<Integer, Vector<Integer>> v = new HashMap<Integer, Vector<Integer>>() ;
boolean[] used_a = new boolean[2*n] ;
for(int i=0;i<2*n;i++){
a[i] = in.nextInt() ;
}
sieve() ;
Arrays.sort(a);
for(int i=0;i<2*n;i++){
if(v.containsKey(a[i]) == false){
v.put(a[i], new Vector<Integer>()) ;
}
v.get(a[i]).addElement(i);
}
//for(int i=0;i<300;i++) {
// System.out.println(i + " " + biggest_d[i]);
//}
Vector<Integer> origin = new Vector<Integer>() ;
for(int i=2*n-1;i>=0;i--){
if(v.get(a[i]).size() > 0){
v.get(a[i]).remove(v.get(a[i]).size() -1) ;
if(used[a[i]] == true) {
int val = p_order.get(a[i]) ;
origin.add(val) ;
int pos_ = v.get(val).get(0) ;
v.get(val).remove(v.get(val).size() -1) ;
}
else
{
origin.add(a[i]) ;
int val = biggest_d[a[i]] ;
int pos_ = v.get(val).get(0) ;
v.get(val).remove(v.get(val).size() -1) ;
}
}
}
for(int x:origin) {
System.out.print(x+" ");
}
}
}
| Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 0b705971677f445f9f317e50b0283db8 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util.*;
public class RecoverIt {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
int a[] = new int[2*n];
for (int i=0; i<2*n; i++) {
a[i] = scanner.nextInt();
}
boolean[] isPrime = new boolean[2750132];
Arrays.fill(isPrime, true);
for (int p=2; p*p<isPrime.length; p++) {
if (isPrime[p]) {
for (int j=2*p; j<isPrime.length; j+=p) {
isPrime[j] = false;
}
}
}
int[] prime = new int[200001];
Map<Integer, Integer> primeOrd = new HashMap<>();
int idx = 1;
for (int i=2; i<isPrime.length; i++) {
if (isPrime[i]) {
prime[idx] = i;
primeOrd.put(i, idx);
idx++;
}
}
Arrays.sort(a);
for (int i=0; i<n; i++) {
int temp = a[i];
a[i] = a[2*n-i-1];
a[2*n-i-1] = temp;
}
StringBuilder sb = new StringBuilder();
Map<Integer, Integer> currOrig = new HashMap<>();
for (int i = 0; i < 2*n; i++) {
if (currOrig.get(a[i])!=null && currOrig.get(a[i]) > 0) {
currOrig.put(a[i], currOrig.get(a[i])-1);
} else if (primeOrd.containsKey(a[i])) {
currOrig.merge(primeOrd.get(a[i]), 1, (x, y) -> x + y);
sb.append(primeOrd.get(a[i])).append(' ');
} else {
for (int p=1; p<prime.length; p++) {
if (a[i]%prime[p] == 0) {
currOrig.merge(a[i]/prime[p], 1, (x,y) -> x+y);
break;
}
}
sb.append(a[i]).append(' ');
}
}
System.out.println(sb.toString());
}
}
| Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | a22d54d69326b49e5475c7379711aa7f | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.function.Function;
public class MainD {
static int N;
static int[] B;
public static void main(String[] args) {
FastScanner sc = new FastScanner(System.in);
N = sc.nextInt();
B = sc.nextIntArray(N*2);
List<Integer> ans = solve();
StringJoiner j = new StringJoiner(" ");
for (Integer an : ans) {
j.add(String.valueOf(an));
}
System.out.println(j.toString());
}
static int MAX = 2750131;
static List<Integer> solve() {
List<Integer> A = new ArrayList<>();
int[] primes = mkPrimes();
Map<Integer, Integer> REV = new HashMap<>();
for (int p : primes) {
if( p < 200000 ) {
int q = primes[p-1];
REV.put(q, p);
}
}
int[] cnt = new int[MAX+1];
for (int b : B) {
cnt[b]++;
}
for (int b = MAX; b >= 2; b--) {
if( cnt[b] == 0 ) continue;
// debug("b", b, cnt[b]);
if( REV.containsKey(b) ) {
// p -> P(p) の P(p)のほうなので逆引きする
int p = REV.get(b);
for (int i = 0; i < cnt[b]; i++) {
A.add(p);
}
cnt[p] -= cnt[b];
cnt[b] = 0;
} else {
// divisiable -> dividor
int dividor = maxDividor(b, primes);
for (int i = 0; i < cnt[b]; i++) {
A.add(b);
}
cnt[dividor] -= cnt[b];
cnt[b] = 0;
}
}
return A;
}
static int maxDividor(int n, int[] primes) {
for (int p : primes) {
if( n % p == 0 ) {
return n / p;
}
}
throw new IllegalArgumentException("wtf : " + n);
}
static int[] mkPrimes() {
boolean[] sieve = new boolean[MAX+1];
Arrays.fill(sieve, true);
sieve[0] = false;
sieve[1] = false;
for (int i = 2; i <= MAX; i++) {
if( sieve[i] ) {
for (int d = 2; d*i <= MAX; d++) {
sieve[i*d] = false;
}
}
}
int cnt = 0;
for (boolean prime : sieve) {
if( prime ) cnt++;
}
int[] primes = new int[cnt];
int idx = 0;
for (int num = 2; num < sieve.length; num++) {
if( sieve[num] ) {
primes[idx++] = num;
}
}
return primes;
}
@SuppressWarnings("unused")
static class FastScanner {
private BufferedReader reader;
private StringTokenizer tokenizer;
FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
long nextLong() {
return Long.parseLong(next());
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
int[] nextIntArray(int n, int delta) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt() + delta;
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
static <A> void writeLines(A[] as, Function<A, String> f) {
PrintWriter pw = new PrintWriter(System.out);
for (A a : as) {
pw.println(f.apply(a));
}
pw.flush();
}
static void writeLines(int[] as) {
PrintWriter pw = new PrintWriter(System.out);
for (int a : as) pw.println(a);
pw.flush();
}
static void writeLines(long[] as) {
PrintWriter pw = new PrintWriter(System.out);
for (long a : as) pw.println(a);
pw.flush();
}
static int max(int... as) {
int max = Integer.MIN_VALUE;
for (int a : as) max = Math.max(a, max);
return max;
}
static int min(int... as) {
int min = Integer.MAX_VALUE;
for (int a : as) min = Math.min(a, min);
return min;
}
static void debug(Object... args) {
StringJoiner j = new StringJoiner(" ");
for (Object arg : args) {
if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg));
else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg));
else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg));
else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg));
else j.add(arg.toString());
}
System.err.println(j.toString());
}
}
| Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | adcc96fed2e3517c4297d33d709d4981 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
/* abhi2601 */
public class Q1 implements Runnable{
//final static long mod = (long)1e9 + 7;
class Pair implements Comparable<Pair>{
int a,b;
Pair(int a, int b){
this.a=a;
this.b=b;
}
public int compareTo(Pair p){
return this.b-p.b;
}
}
public void run(){
InputReader sc = new InputReader(System.in);
//BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter w = new PrintWriter(System.out);
int n=sc.nextInt();
ArrayList<Integer>al=new ArrayList<>();
HashMap<Integer,Integer>hm=new HashMap<>();
for(int i=0;i<2*n;i++){
int q=sc.nextInt();
al.add(q);
if(hm.containsKey(q)) hm.put(q,hm.get(q)+1);
else hm.put(q,1);
}
Collections.sort(al);
int max=Collections.max(al);
boolean prime[] = new boolean[max+1];
Arrays.fill(prime,true);
for(int p=2;p*p<=max;p++)
{
if(prime[p])
{
for(int i=p*p;i<=max;i+=p)
prime[i]=false;
}
}
HashMap<Integer,Integer>pprime=new HashMap<>();
int c=1;
for(int i=2;i<=max;i++){
if(prime[i]){
pprime.put(i,c);
c++;
}
}
ArrayList<Integer>ans=new ArrayList<>();
for(int i=2*n-1;i>=0;i--){
int q=al.get(i);
if(hm.get(q).equals(0)) continue;
//w.println(q);
if(!prime[q]){
for(int j=2;j*j<=q;j++){
if(q%j==0){
int z=q/j;
ans.add(q);
hm.put(q,hm.get(q)-1);
hm.put(z,hm.get(z)-1);
//w.println("z "+z);
break;
}
}
}
else{
ans.add(pprime.get(q));
hm.put(q,hm.get(q)-1);
hm.put(pprime.get(q),hm.get(pprime.get(q))-1);
//w.println("pos "+pprime.get(q));
}
}
for(Integer i: ans) w.print(i+" ");
w.close();
}
/*static class Comp implements Comparator<String>{
public int compare(String x,String y){
return x.length()-y.length();
}
}*/
//static PrintWriter w;
/*File in=new File("input.txt");
File out=new File("output.txt");
Scanner sc= null;
try {
sc = new Scanner(in);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
w=new PrintWriter(out);
} catch (FileNotFoundException e) {
e.printStackTrace();
}*/
static 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 String nextLine()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
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 long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
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 res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception
{
new Thread(null, new Q1(),"q1",1<<26).start();
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 0594355a1a5ae384e2631f3c48509f96 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) throws Throwable {
Scanner sc = new Scanner();
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt() * 2;
int mx = 2750131;
int[] c = new int[mx + 1];
for (int i = 0; i < n; i++)
c[sc.nextInt()]++;
ArrayList<Integer> a = new ArrayList<>();
for (int i = mx; i >= 2; i--) {
int d = greatestDivisor(i);
if (d != -1) {
int mn = Math.min(c[i], c[d]);
c[i] -= mn;
c[d] -= mn;
while (mn-- > 0)
a.add(i);
}
}
int p = 1;
HashMap<Integer, Integer> map = new HashMap<>();
boolean[] isPrime = new boolean[mx + 1];
Arrays.fill(isPrime, true);
for (int i = 2; i <= mx; i++)
if (isPrime[i]) {
map.put(i, p++);
for (int j = 2 * i; j <= mx; j += i)
isPrime[j] = false;
}
for (int i = mx; i >= 2; i--) {
if (c[i] > 0 && map.containsKey(i)) {
int d = map.get(i);
int mn = Math.min(c[i], c[d]);
c[i] -= mn;
c[d] -= mn;
while (mn-- > 0)
a.add(d);
}
}
for (int x : a)
pw.print(x + " ");
pw.close();
}
static int greatestDivisor(int x) {
for (int i = 2; i * i <= x; i++)
if (x % i == 0)
return x / i;
return -1;
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
Scanner(String s) throws Throwable {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() throws Throwable {
if (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
int nextInt() throws Throwable {
return Integer.parseInt(next());
}
long nextLong() throws Throwable {
return Long.parseLong(next());
}
}
}
| Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 30b1cb34f3be4f139dd9e1486624dc54 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
import java.util.Stack;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;
import javax.sound.sampled.ReverbType;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Scanner;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
public class cf1{
static public int GCD(int a, int b) {
if (b==0) return a;
return GCD(b,a%b);
}
static boolean[]prime;
static ArrayList<Integer> primes;
static void sieveOfEratosthenes(int n)
{
prime = new boolean[n+1];
for(int i=0;i<=n;i++)
prime[i] = true;
for(int p = 2; p <=n; p++)
{
if(prime[p])
{
//ans[(int)p]=c;
primes.add(p);ps.add(p);
for(long i = p*1l*p; i <= n; i += p) {
//if(ans[i]System.out.println(i);
prime[(int)i] = false;
}
//c++;
}
}
}
static HashSet<Integer>ps=new HashSet<Integer>();
public static void main(String[] args) throws IOException {
MScanner sc=new MScanner(System.in);
//BufferedReader br=new BufferedReader((new InputStreamReader(System.in)));
PrintWriter pw=new PrintWriter(System.out);
int n=sc.nextInt();
int[]in=new int[2*n];
primes=new ArrayList<Integer>();
sieveOfEratosthenes((int)(1e7));
//System.out.println(primes.get(primes.size()-1));
HashMap<Integer,Integer>nums=new HashMap<Integer,Integer>();
for(int i=0;i<2*n;i++) {
in[i]=sc.nextInt();
nums.put(in[i],nums.getOrDefault(in[i],0)+1);
}
for(int i=0;i<n;i++) {
int x=(int)(Math.random()*n);
int tmp=in[i];
in[i]=in[x];
in[x]=tmp;
}
int[]ans=new int[n];int c=0;
Arrays.sort(in);
for(int i=(2*n)-1;i>=0;i--) {
//System.out.println(in[i]+" "+nums);
if(nums.containsKey(in[i])) {
nums.put(in[i],nums.get(in[i])-1);
if(nums.get(in[i])==0) {
nums.remove(in[i]);
}
boolean r=false;
if(ps.contains(in[i])) {
}
else {
int bigd=-1;
for(int j=0;j<primes.size();j++) {
if(in[i]%primes.get(j)==0) {
bigd=in[i]/primes.get(j);break;
}
}
if(nums.containsKey(bigd)) {
ans[c++]=in[i];r=true;
nums.put(bigd,nums.get(bigd)-1);
if(nums.get(bigd)==0) {
nums.remove(bigd);
}
}
}
if(!r) {
nums.put(in[i],nums.getOrDefault(in[i],0)+1);
}
if(c==n) {
break;
}
}
}
for(int i=0;i<n*2;i++) {
//System.out.println(in[i]+" "+nums);
if(nums.containsKey(in[i])) {
nums.put(in[i],nums.get(in[i])-1);
if(nums.get(in[i])==0) {
nums.remove(in[i]);
}
boolean r=false;
if(ps.contains(in[i])) {
if(nums.containsKey(primes.get(in[i]-1))) {
nums.put(primes.get(in[i]-1),nums.get(primes.get(in[i]-1))-1);
ans[c++]=in[i];r=true;
if(nums.get(primes.get(in[i]-1))==0) {
nums.remove(primes.get(in[i]-1));
}
}
}
else {
}
if(!r) {
nums.put(in[i],nums.getOrDefault(in[i],0)+1);
}
if(c==n) {
break;
}
}
}
for(int i:ans) {
pw.print(i+" ");
}
pw.flush();
}
static class tri implements Comparable<tri>{
int l;int idx;
tri(int x,int z){
l=x;idx=z;
}
@Override
public int compareTo(tri o) {
if(l!=o.l) {
if(l>o.l)return 1;
return -1;
}
return idx-o.idx;
}
public boolean equals(tri o) {
if(this.compareTo(o)==0)return true;
return false;
}
public String toString() {
return "("+l+" "+idx+")";
}
}
static class MScanner
{
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public boolean ready() throws IOException {return br.ready();}
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 9b8997b305bc7bfc3db96016a7b01dc8 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.FileReader;
import java.util.LinkedList;
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;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
DRecoverIt solver = new DRecoverIt();
solver.solve(1, in, out);
out.close();
}
static class DRecoverIt {
public void solve(int testNumber, Scanner sc, PrintWriter pw) {
int n = sc.nextInt();
boolean[] isPrime = new boolean[(int) 3e6];
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for (int i = 2; i * i <= isPrime.length; i++)
if (isPrime[i])
for (int j = i * i; j < isPrime.length; j += i)
isPrime[j] = false;
int idx = 1;
int map[] = new int[(int) 3e6];
ArrayList<Integer> primes = new ArrayList<>();
for (int i = 0; i < (int) 3e6; i++)
if (isPrime[i]) {
map[i] = idx++;
primes.add(i);
}
int[] arr = new int[n * 2];
int[] count = new int[(int) 3e6];
TreeSet<Integer> set = new TreeSet<>();
for (int i = 0; i < n * 2; i++) {
arr[i] = sc.nextInt();
set.add(arr[i]);
count[arr[i]]++;
}
List<Integer> ans = new LinkedList<>();
while (!set.isEmpty()) {
int x = set.last();
count[x]--;
if (count[x] == 0)
set.remove(x);
if (isPrime[x]) {
ans.add(map[x]);
count[map[x]]--;
if (count[map[x]] == 0)
set.remove(map[x]);
} else {
ans.add(x);
int i = 0, p = primes.get(i++);
while (p * p <= x) {
if (x % p == 0) {
x /= p;
break;
}
p = primes.get(i++);
}
count[x]--;
if (count[x] == 0)
set.remove(x);
}
}
for (int x : ans)
pw.print(x + " ");
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 232051c28e083a19e1d4cefcabc7649e | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util.*;
import java.io.*;
public class Codechef{
static boolean[]primes;
static int[]index;
static int[]fac;
static void sieve()
{
int n=2750131;
primes[1]=false;
for(int i=2;i*i<=n;i++)
{
if(primes[i]==false)
continue;
for(int j=2*i;j<=n;j=j+i)
{
primes[j]=false;
fac[j]=Math.max(i,Math.max(fac[j],j/i));
}
}
int cnt=1;
for(int i=2;i<=n;i++)
{
if(primes[i])
{
index[i]=cnt;
cnt++;
}
}
}
public static void main(String []args) throws Exception{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int t=1;
PrintWriter pr=new PrintWriter(new OutputStreamWriter(System.out));
while(t!=0)
{
t--;
String[]cmd=br.readLine().split(" ");
int n=Integer.valueOf(cmd[0]);
int[]b=new int[2*n];
cmd=br.readLine().split(" ");
int[]cnt=new int[2750132];
for(int i=0;i<2*n;i++)
{
b[i]=Integer.valueOf(cmd[i]);
cnt[b[i]]++;
}
primes=new boolean[2750132];
Arrays.fill(primes,true);
fac=new int[2750132];
Arrays.fill(fac,1);
index=new int[2750132];
sieve();
ArrayList<Integer> ans=new ArrayList<>();
for(int i=2750131;i>=1;i--)
{
while(cnt[i]>0)
{
if(!primes[i])
{
ans.add(i);
cnt[fac[i]]--;
}
else
{
ans.add(index[i]);
cnt[index[i]]--;
}
cnt[i]--;
}
}
for(int i=0;i<n;i++)
{
pr.write(ans.get(i)+" ");
}
//System.out.println(fac[2088]);
pr.write("\n");
}
pr.flush();
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 6cf4d8b27edcd24176ba643ff139e532 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.reflect.Array;
public class D {
static final int MOD = 1000000007; // 1e9 + 7
static final boolean AUTO_FLUSH = false; // slow if true
// int = num(); // long = ll();
// string = next(); // a string line = line();
// ---------------------------------- \
HashMap<Integer, Integer> map = new HashMap<>();
int[] primes = new int[200001];
int printed = 0;
void main() throws Exception {
BitSet vis = new BitSet();
int max = 2750159;
for (int i = 2; i <= max; i++) {
vis.set(i);
}
for (int i = 2; i*i <= max; i++) {
if (vis.get(i)) {
for (int j = i*i; j <= max; j += i) {
if (j < 0)
break;
vis.set(j, false);
}
}
}
int index = 1;
for (int i = 2; i <= max && index < 200001; i++)
if (vis.get(i))
primes[index++] = i;
int n = num();
int[] arr = new int[2*n];
for (int i = 0; i < 2*n; i++)
inc(arr[i] = num(), 1);
Arrays.sort(arr);
for (int i = 2*n-1; i >= 0; i--) {
int x = arr[i];
if (get(x) <= 0)
continue;
boolean is = false;
if (x <= 200000 && vis.get(x) && get(primes[x]) > 0) {
inc(primes[x], -1);
is = true;
}
if (!vis.get(x)) {
int divisor = 2;
while (x%divisor != 0)
divisor++;
divisor = x/divisor;
if (get(divisor) > 0) {
inc(divisor, -1);
is = true;
}
}
if (is) {
out(x + " ");
if (++printed >= n)
break;
inc(x, -1);
}
}
outln();
}
int get(int x) {
if (map.get(x) == null)
return 0;
return map.get(x);
}
void inc(int x, int cnt) {
map.put(x, get(x)+cnt);
}
// ---------------------------------- \
//#region
public static void main(String[] args) throws Exception {
new Thread(null, () -> {
try {
startTime();
new D().main();
boolean endsWithEnter = TO_BE_PRINTED.length() == 0 || TO_BE_PRINTED.charAt(TO_BE_PRINTED.length() - 1) == '\n';
flush();
if (!endsWithEnter)
log('\n');
logTime();
logMem();
} catch (Exception e) {
e.printStackTrace();
}
}, "Main", 1<<26).start();
}
static Random RAND = new Random();
static int random(int from, int to) {
return RAND.nextInt(to-from+1)+from;
}
static void logTime() {
log("Time: " + getTime() + "ms");
}
static void logMem() {
log("Memory (End): " + (int) (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / 1024 + " KB");
}
static long ll() {
return Long.parseLong(next());
}
static int num() {
return Integer.parseInt(next());
}
static void generateInputMode() throws Exception {
System.setOut(new PrintStream(new FileOutputStream("F://PROGRAMMING/input.txt")));
}
static String line() {
if (!tokenize())
return null;
INPUT_STREAM.setLength(0);
if (STRING_TOKENIZER.hasMoreTokens())
INPUT_STREAM.append(STRING_TOKENIZER.nextToken());
while (STRING_TOKENIZER.hasMoreTokens())
INPUT_STREAM.append(' ').append(STRING_TOKENIZER.nextToken());
return INPUT_STREAM.length() == 0 ? null : INPUT_STREAM.toString();
}
static void startTime() {
TIME_COMPLEXITY = System.currentTimeMillis();
}
static long getTime() {
return System.currentTimeMillis() - TIME_COMPLEXITY;
}
static void flush() {
System.out.print(TO_BE_PRINTED.toString());
TO_BE_PRINTED.setLength(0);
}
static void out(Object o) {
TO_BE_PRINTED.append(o.toString());
if (AUTO_FLUSH)
flush();
}
static void _log(String s) {
System.err.print(s);
}
static void _logln(String s) {
System.err.println(s);
}
static void _logln() {
System.err.println();
}
static void outln(Object o) {
out(o);
outln();
}
static void outln() {
out("\n");
}
static class Pair implements Comparable<Pair> {
public int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public static Pair of(int a, int b) {
return new Pair(a, b);
}
public int compareTo(Pair p) {
if (a == p.a)
return Integer.compare(b, p.b);
return Integer.compare(a, p.a);
}
public String toString() {
return "[" + a + "," + b + "]";
}
public boolean equals(Pair p) {
return p.a == a && p.b == b;
}
}
static void logArr(Object val) {
Class<?> valKlass = val.getClass();
Object[] outputArray = null;
for(Class<?> arrKlass : new Class<?>[] { int[].class, float[].class, double[].class, boolean[].class,
byte[].class, short[].class, long[].class, char[].class }) {
if (valKlass.isAssignableFrom(arrKlass)) {
int arrlength = Array.getLength(val);
outputArray = new Object[arrlength];
for(int i = 0; i < arrlength; ++i)
outputArray[i] = Array.get(val, i);
break;
}
}
if(outputArray == null)
outputArray = (Object[])val;
logArr0(outputArray);
}
static void logArr0(Object[] objs) {
_log("* " + objs[0]);
for (int i = 1; i < objs.length; i++)
_log(objs[i].toString().equals("\n") ? "\n>" : (" " + objs[i]));
_logln();
}
static void log(Object... objs) {
logArr0(objs);
}
static String next() {
return tokenize() ? STRING_TOKENIZER.nextToken() : null;
}
static boolean tokenize() {
if (STRING_TOKENIZER == null || !STRING_TOKENIZER.hasMoreTokens()) {
try {
STRING_TOKENIZER = new StringTokenizer(STREAM_READER.readLine());
} catch (Exception e) {
return false;
}
}
return true;
}
static long TIME_COMPLEXITY;
static BufferedReader STREAM_READER = new BufferedReader(new InputStreamReader(System.in), 32768);
static StringTokenizer STRING_TOKENIZER;
static StringBuilder INPUT_STREAM = new StringBuilder(), TO_BE_PRINTED = new StringBuilder();
//#endregion
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 0b75d74fc107f84976498a858a36fe3d | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes |
import java.io.ByteArrayInputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
// import java.util.Collections;
// import java.util.Comparator;
import java.util.List;
import java.util.Scanner;
public class D {
static boolean LOCAL = System.getProperty("ONLINE_JUDGE") == null;
public static void main(String[] args) throws Exception {
new D().go();
}
Scanner in;
String INPUT = "1\n" +
"3 6";
void go() {
if (LOCAL) {
System.setIn(new ByteArrayInputStream(INPUT.getBytes()));
}
in = new Scanner(System.in);
long startTime = System.currentTimeMillis();
solve();
if (LOCAL) {
System.out.printf("[%dms]", System.currentTimeMillis()-startTime);
}
in.close();
}
PrintWriter out = new PrintWriter(System.out);
StringBuffer sb = new StringBuffer();
int[] primes = new int[200000];
int pIndex = 2;
void solve() {
int n = in.nextInt();
List<Integer> b = new ArrayList<>();
for (int i = 0; i < 2*n; i++) {
b.add(in.nextInt());
}
primes[1] = 2;
for (int i = 3; i <= 2750131; i++) {
if (isPrime(i)) {
primes[pIndex++] = i;
}
}
// showFirst100Primes();
// Comparator c = Collections.reverseOrder();
// Collections.sort(b, c);
// boolean[] matched = new boolean[2*n];
// for (int i = 0; i < b.size(); i++) {
// int num = b.get(i);
// if (matched[i] && isPrime(num)) continue;
// matched[i] = true;
// System.out.println(num);
// }
int[] nums = new int[2750131 + 1];
for (int i = 0; i < 2*n; i++) {
nums[b.get(i)]++;
}
List<Integer> answer = new ArrayList<>();
for (int i = 2750131; i >= 2; i--) {
if (nums[i] > 0 && !isPrime(i)) {
int gd = gd(i);
while(nums[i] > 0) {
answer.add(i);
nums[i]--;
nums[gd]--;
}
}
}
for (int i = 2; i <= 2750131; i++) {
while(nums[i] > 0) {
answer.add(i);
nums[i]--;
nums[primes[i]]--;
}
}
StringBuffer str = new StringBuffer();
for (Integer a : answer) {
str.append(a+" ");
}
System.out.println(str);
} // end of solve
boolean isPrime(int n) {
// It is a great deal faster to check for primes *with* primes,
// note for speed also requires checking only primes <= Math.sqrt(n)
for (int i=1; i < pIndex && primes[i] <= Math.sqrt(n); i++) {
if (n%primes[i]==0) return false;
}
return true;
}
int gd(int n) {
for (int i=2; i <= Math.sqrt(n); i++) {
if (n%i==0) return n/i;
}
return 1;
}
void showFirst100Primes() {
for (int i = 0; i <= 100; i++) {
System.out.printf("%d: %d \n", i, primes[i]);
}
}
}
| Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 62a2e94843329425a9d695e2850f3ed7 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.List;
public class Recover
{
InputStream is;
PrintWriter out;
String INPUT = "";
private static final long M = 1000000007;
final int N = 2750132;
void solve() {
int n = ni();
int cnt[] = new int[N];
for (int i = 0; i < 2*n; i++)
cnt[ni()]++;
int[] factors = new int[N];
List<Integer> primes = new ArrayList<Integer>();
for (int p = 2; p < N; p++) {
if (factors[p] == 0) {
primes.add(p);
for (int i = p * 2; i < N; i += p)
if (factors[i] == 0)
factors[i] = i / p;
}
}
StringBuffer sb = new StringBuffer();
for (int i = N - 1; i >= 2; i--)
while (cnt[i] > 0 && factors[i] != 0) {
int d = factors[i];
cnt[d]--;
cnt[i]--;
sb.append(i).append(' ');
}
for (int i = 2; i < N; i++) {
while (cnt[i] > 0) {
cnt[primes.get(i - 1)]--;
cnt[i]--;
sb.append(i).append(' ');
}
}
out.println(sb);
}
void run() throws Exception {
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
if (!INPUT.isEmpty())
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args) throws Exception {
new Recover().run();
}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte() {
if (lenbuf == -1)
throw new InputMismatchException();
if (ptrbuf >= lenbuf) {
ptrbuf = 0;
try {
lenbuf = is.read(inbuf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private int skip() {
int b;
while ((b = readByte()) != -1 && isSpaceChar(b))
;
return b;
}
private double nd() {
return Double.parseDouble(ns());
}
private char nc() {
return (char) skip();
}
private String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while (!(isSpaceChar(b))) { // when nextLine, (isSpaceChar(b) && b != '
// ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n) {
char[] buf = new char[n];
int b = skip(), p = 0;
while (p < n) {
if (!(isSpaceChar(b)))
buf[p++] = (char) b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for (int i = 0; i < n; i++)
map[i] = ns(m);
return map;
}
private int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private int[][] na(int n, int m) {
int[][] a = new int[n][];
for (int i = 0; i < n; i++)
a[i] = na(m);
return a;
}
private int ni() {
int num = 0, b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private long nl() {
long num = 0;
int b;
boolean minus = false;
while ((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'))
;
if (b == '-') {
minus = true;
b = readByte();
}
while (true) {
if (b >= '0' && b <= '9') {
num = num * 10 + (b - '0');
} else {
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) {
System.out.println(Arrays.deepToString(o));
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | ca34334ef49a97d695187e4575500e81 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.*;
public class Main extends Thread {
boolean[] prime;
FastScanner sc;
PrintWriter pw;
MathF mf;
final class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
public long nlo() {
return Long.parseLong(next());
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
return st.nextToken();
}
public int ni() {
return Integer.parseInt(next());
}
public String nli() {
String line = "";
if (st.hasMoreTokens()) line = st.nextToken();
else try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
while (st.hasMoreTokens()) line += " " + st.nextToken();
return line;
}
public double nd() {
return Double.parseDouble(next());
}
}
final class MathF {
long grtdiv(long x)
{
if(isPrime(x))
return 1;
else
{
long ans=0;
long y=(long)Math.sqrt(x)+1;
for(;y>=2;y--)
{
if(x%y==0)
ans=Math.max(ans,Math.max(y,x/y));
}
return ans;
}
}
boolean isPrime(long x)
{
for(int i=2;i*i<=x;i++)
{
if(x%i==0)
{
return false;
}
}
return true;
}
long[][] mul(long[][] arr,long[][] brr,int a,int b,int c,int d)
{
long[][] crr=new long[a][d];
for(int i=0;i<a;i++)
{
for(int j=0;j<d;j++)
{
for(int k=0;k<b;k++)
{
crr[i][j]+=(((arr[i][k]%1000000007)*(brr[k][j]%1000000007))%1000000007);
}
}
}
return crr;
}
int sod(long a)
{
int ans=0;
while(a!=0)
{
ans+=(a%10);
a=a/10;
}
return ans;
}
long pow(long a,long b)
{
if(b==0)
return 1;
else
{
long x=pow(a,b/2);
if(b%2==1)
return (x*x*a);
else
return (x*x);
}
}
public long gcd(long a, long b) {
return (b == 0 ? a : gcd(b, a % b));
}
public long exponent(long a,long b)
{
if(b==0)
return 1;
else
{
long x=exponent(a,b/2);
x*=x;
if(b%2==1)
x*=a;
return x;
}
}
public int nod(long x) {
int i = 0;
while (x != 0) {
i++;
x = x / 10;
}
return i;
}
public int nob(long x) {
if(x==0)
return 1;
return (int) Math.floor(Math.log(x) / Math.log(2)) + 1;
}
public int[] FastradixSort(int[] f) {
int[] to = new int[f.length];
{
int[] b = new int[65537];
for (int i = 0; i < f.length; i++) b[1 + (f[i] & 0xffff)]++;
for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
for (int i = 0; i < f.length; i++) to[b[f[i] & 0xffff]++] = f[i];
int[] d = f;
f = to;
to = d;
}
{
int[] b = new int[65537];
for (int i = 0; i < f.length; i++) b[1 + (f[i] >>> 16)]++;
for (int i = 1; i <= 65536; i++) b[i] += b[i - 1];
for (int i = 0; i < f.length; i++) to[b[f[i] >>> 16]++] = f[i];
int[] d = f;
f = to;
to = d;
}
return f;
}
public void Primesieve(int n) {
prime=new boolean[n];
for (int i = 0; i < n; i++)
prime[i] = true;
for (int p = 2; p * p < n; p++) {
if (prime[p] == true) {
for (int i = p * p; i < n; i += p)
prime[i] = false;
}
}
}
}
public Main(ThreadGroup t,Runnable r,String s,long d )
{
super(t,r,s,d);
}
public void run()
{
sc=new FastScanner();
pw=new PrintWriter(System.out);
mf=new MathF();
solve();
pw.flush();
pw.close();
}
public static void main(String[] args)
{
new Main(null,null,"",1<<30).start();
}
/////////////------------------------------------/////////////
/////////////------------------------------------/////////////
////////////------------------MAIN-LOGIC---------/////////////
////////////-------------------------------------/////////////
///////////--------------------------------------/////////////
public void solve() {
int t=1;
while(t-->0){
mf.Primesieve(2750139);
ArrayList<Integer> list=new ArrayList();
int n=sc.ni();
for(int i=2;i<2750139;i++)
if(prime[i])
{
list.add(i);
}
int[] frr=new int[2750139];
Arrays.fill(frr,0);
int max=Integer.MIN_VALUE;
for(int i=0;i<2*n;i++)
{
int x=sc.ni();
max=Math.max(max,x);
frr[x]++;
}
ArrayList<Integer> ans=new ArrayList();
int i=0,k=max;
StringBuilder str=new StringBuilder("");
while(i<n)
{
if(k==2)
break;
if((frr[k]==0)||(mf.isPrime(k)))
{
k--;
continue;
}
frr[(int)mf.grtdiv(k)]-=frr[k];
i+=frr[k];
int j=frr[k];
frr[k]=0;
while(j!=0)
{
str.append(k+" ");
j--;
}
}
k=2;
while(i<n)
{
if(frr[k]==0||(!mf.isPrime(k)))
{
k++;
continue;
}
frr[list.get(k-1)]-=frr[k];
i+=frr[k];
while(frr[k]!=0)
{
str.append(k+" ");
frr[k]--;
}
}
pw.print(str);
}
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | a9302dd528f83c9e30c3cbf8e53e8136 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
public class Solution{
static InputReader sc;
static PrintWriter wc;
public static void main(String[] args) {
sc = new InputReader(System.in);
wc = new PrintWriter(System.out);
int n=sc.nextInt();
int m=2750131;
int i,j;
int isPrime[]=new int[m+1];
int primeIndex[]=new int[m+1],spf[]=new int[m+1];
int index=1;
for(i=2;i<=m;i++) isPrime[i]=1;
for(i=2;i<=m;i++){
if(isPrime[i]==1){
primeIndex[i]=index++;
for(j=2*i;j<=m;j+=i){
isPrime[j]=0;
if(spf[j]==0) spf[j]=i;
}
}
}
PriorityQueue<Integer> q=new PriorityQueue<>(new Comparator<Integer>(){
public int compare(Integer a, Integer b){
if(a==b) return 1;
return a>b?-1:1;
}
});
int freq[]=new int[m+1],temp;
for(i=0;i<2*n;i++){
temp=sc.nextInt();
freq[temp]++;
q.add(temp);
}
int next;
while(!q.isEmpty()){
next=q.remove();
//System.out.println(next);
if(freq[next]==0) continue;
freq[next]--;
if(isPrime[next]==1){
freq[primeIndex[next]]--;
wc.print(primeIndex[next]+" ");
}
else{
freq[next/spf[next]]--;
wc.print(next+" ");
}
//wc.println(next);
}
wc.close();
}
static 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 String nextLine()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
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 long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
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 res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 4ef01fc76979bc7c9d382800f0c6aae1 | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class TryB
{
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String rs() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static long mod=1000000007;
static BigInteger bigInteger = new BigInteger("1000000007");
static int n = (int)1e7;
static boolean[] prime;
static ArrayList<Integer> as;
static ArrayList<Integer> []as1;
static void sieve() {
Arrays.fill(prime , true);
prime[0] = prime[1] = false;
for(int i = 2 ; i * i <= n ; ++i) {
if(prime[i]) {
for(int k = i * i; k< n ; k+=i) {
prime[k] = false;
}
}
}
}
static PrintWriter w = new PrintWriter(System.out);
static int p = 0;
static char [][]sol;
static int x1;
static int y1;
static int x2;
static int y2;
public static void main(String[] args)
{
InputReader sc = new InputReader(System.in);
//PrintWriter w = new PrintWriter(System.out);
prime = new boolean[n + 1];
sieve();
prime[1] = false;
/*
as = new ArrayList<>();
for(int i=2;i<=1000000;i++)
{
if(prime[i])
as.add(i);
}
*/
/*
long a = sc.nl();
BigInteger ans = new BigInteger("1");
for (long i = 1; i < Math.sqrt(a); i++) {
if (a % i == 0) {
if (a / i == i) {
ans = ans.multiply(BigInteger.valueOf(phi(i)));
} else {
ans = ans.multiply(BigInteger.valueOf(phi(i)));
ans = ans.multiply(BigInteger.valueOf(phi(a / i)));
}
}
}
w.println(ans.mod(bigInteger));
*/
// MergeSort ob = new MergeSort();
// ob.sort(arr, 0, arr.length-1);
// Student []st = new Student[x];
// st[i] = new Student(i,d[i]);
//Arrays.sort(st,(p,q)->p.diff-q.diff);
//BigDecimal x = new BigDecimal(b[i]).multiply(new BigDecimal("-1")).divide(new BigDecimal(a[i]),100,RoundingMode.HALF_UP);
// PriorityQueue<Integer> pq=new PriorityQueue<Integer>(new multipliers());
int x = sc.ni();
int []a = sc.nia(2*x);
ArrayList<Integer> a1 = new ArrayList<>();
ArrayList<Integer> p = new ArrayList<>();
ArrayList<Integer> a2 = new ArrayList<>();
for(int i=2;i<10000000;i++)
{
if(prime[i])
{
p.add(i);
}
}
ArrayList<Integer> res = new ArrayList<>();
HashMap<Integer,Integer> hm = new HashMap<>();
for(int i=1;i<=2750131;i++)
{
hm.put(i, 0);
}
for(int i=0;i<2*x;i++)
{
a2.add(a[i]);
if(!hm.containsKey(a[i]))
hm.put(a[i], 1);
else
hm.put(a[i], hm.get(a[i])+1);
}
int []primei = new int[2750132];
// w.println(p.get(2));
Collections.sort(a2);
for(int i=2;i<=2750131;i++)
{
if(prime[i])
{
for(int j = 2;i*j<=2750131;j++)
{
if(primei[i*j] == 0)
primei[i*j] = i;
}
}
}
int []prime2 = new int[2750132];
int index = 1;
for(int i=2;i<=2750131;i++)
{
if(prime[i])
{
prime2[i] = index++;
}
}
//w.println(primei[6]);
for(int i = 2750131; i >= 2; i--)
{
if(hm.get(i) > 0)
{
if(prime[i])
{
for(int j = 0; j < hm.get(i); j++)
res.add(prime2[i]);
hm.put(prime2[i],hm.get(prime2[i])-hm.get(i));
hm.put(i, 0);
}
else
{
for(int j = 0; j < hm.get(i); j++)
res.add(i);
hm.put(i/primei[i],hm.get(i/primei[i])-hm.get(i));
hm.put(i, 0);
}
}
}
for(int i=0;i<res.size();i++)
{
w.print(res.get(i) + " ");
}
w.close();
}
static int []a;
static long a1,b;
public static int searchlow(int x) {
int lo=0, hi=a.length-1;
int res=Integer.MIN_VALUE;
while(lo<=hi){
int mid = (lo+hi)/2;
if(a[mid]==x){
res = mid;
hi = mid - 1;
}
else if(a[mid]>x){
hi = mid - 1;
}
else{
lo = mid + 1;
}
}
return res==Integer.MIN_VALUE?lo:res;
}
public static int searchhigh(int x) {
int lo=0, hi=a.length-1;
int res=Integer.MIN_VALUE;
while(lo<=hi){
int mid = (lo+hi)/2;
if(a[mid]==x){
res = mid;
lo=mid+1;
}
else if(a[mid]>x){
hi = mid - 1;
}
else{
lo = mid + 1;
}
}
return res==Integer.MIN_VALUE?hi:res;
}
public static long ct(int l, int r) {
int lo=searchlow(l), hi=searchhigh(r);
return hi-lo+1;
}
static long log2(long value) {
return Long.SIZE-Long.numberOfLeadingZeros(value);
}
static class Student
{
int id;
//int x;
int y;
int z;
Student(int id,int y,int z)
{
this.id = id;
//this.x = x;
//this.s = s;
this.y = y;
this.z = z;
}
}
public static long modMultiply(long one, long two) {
return (one % mod * two % mod) % mod;
}
static long fx(int m)
{
long re = 0;
for(int i=1;i<=m;i++)
{
re += (long) (i / gcd(i,m));
}
return re;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
static long phi(long nx)
{
// Initialize result as n
double result = nx;
// Consider all prime factors of n and for
// every prime factor p, multiply result
// with (1 - 1/p)
for (int p = 0; as.get(p) * as.get(p) <= nx; ++p)
{
// Check if p is a prime factor.
if (nx % as.get(p) == 0)
{
// If yes, then update n and result
while (nx % as.get(p) == 0)
nx /= as.get(p);
result *= (1.0 - (1.0 / (double) as.get(p)));
}
}
// If n has a prime factor greater than sqrt(n)
// (There can be at-most one such prime factor)
if (nx > 1)
result *= (1.0 - (1.0 / (double) nx));
return (long)result;
//return(phi((long)result,k-1));
}
public static void primeFactors(int n,int x)
{
as = new ArrayList<>();
// Print the number of 2s that divide n
while (n%2==0)
{
as.add(2);
//System.out.print(2 + " ");
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
// System.out.print(i + " ");
as.add(i);
n /= i;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n >= 2)
{
as.add(n);
}
}
static int digitsum(int x)
{
int sum = 0;
while(x > 0)
{
int temp = x % 10;
sum += temp;
x /= 10;
}
return sum;
}
static int countDivisors(int n)
{
int cnt = 0;
for (int i = 1; i*i <=n; i++)
{
if (n % i == 0 && i<=1000000)
{
// If divisors are equal,
// count only one
if (n / i == i)
cnt++;
else // Otherwise count both
cnt = cnt + 2;
}
}
return cnt;
}
static boolean isprime(long n)
{
if(n == 2)
return true;
if(n == 3)
return true;
if(n % 2 == 0)
return false;
if(n % 3 == 0)
return false;
long i = 5;
long w = 2;
while(i * i <= n)
{
if(n % i == 0)
return false;
i += w;
w = 6 - w;
}
return true;
}
static boolean binarysearch(int []arr,int p,int n)
{
//ArrayList<Integer> as = new ArrayList<>();
//as.addAll(0,at);
//Collections.sort(as);
boolean re = false;
int st = 0;
int end = n-1;
while(st <= end)
{
int mid = (st+end)/2;
if(p > arr[mid])
{
st = mid+1;
}
else if(p < arr[mid])
{
end = mid-1;
}
else if(p == arr[mid])
{
re = true;
break;
}
}
return re;
}
/* Java program for Merge Sort */
static class MergeSort
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
/* A utility function to print array of size n */
}
public static int ip(String s){
return Integer.parseInt(s);
}
public static String ips(int s){
return Integer.toString(s);
}
// Java program to print all permutations using
// Heap's algorithm
static class HeapAlgo
{
//static ArrayList<Integer> []ad;
//Prints the array
void printArr(int a[], int n)
{
for (int i=0; i<n; i++)
{
System.out.print(a[i] + " ");
as1[p].add(a[i]);
}
p++;
//System.out.println();
}
//Generating permutation using Heap Algorithm
void heapPermutation(int a[], int size, int n)
{
// if size becomes 1 then prints the obtained
// permutation
if (size == 1)
{
printArr(a,n);
}
for (int i=0; i<size; i++)
{
heapPermutation(a, size-1, n);
p++;
// if size is odd, swap first and last
// element
if (size % 2 == 1)
{
int temp = a[0];
a[0] = a[size-1];
a[size-1] = temp;
}
// If size is even, swap ith and last
// element
else
{
int temp = a[i];
a[i] = a[size-1];
a[size-1] = temp;
}
}
}
public static int lowerBound(int[] array, int length, int value) {
int low = 0;
int high = length-1;
while (low < high) {
final int mid = (low + high) / 2;
if (value <= array[mid]) {
high = mid;
} else {
low = mid + 1;
}
}
return low;
}
public static int upperBound(int[] array, int length, int value) {
int low = 0;
int high = length-1;
while (low < high) {
final int mid = (low + high) / 2;
if (value >= array[mid]) {
low = mid + 1;
} else {
high = mid;
}
}
return low;
}
// Driver code
}
// This code has been contributed by Amit Khandelwal.
// JAVA program for implementation of KMP pattern
// searching algorithm
static class KMP_String_Matching {
boolean KMPSearch(String pat, String txt)
{
int f = 0;
int M = pat.length();
int N = txt.length();
// create lps[] that will hold the longest
// prefix suffix values for pattern
int lps[] = new int[M];
int j = 0; // index for pat[]
// Preprocess the pattern (calculate lps[]
// array)
computeLPSArray(pat, M, lps);
int i = 0; // index for txt[]
while (i < N) {
if (pat.charAt(j) == txt.charAt(i)) {
j++;
i++;
}
if (j == M) {
/*
System.out.println("Found pattern "
+ "at index " + (i - j));
*/
f = 1;
// return true;
j = lps[j - 1];
}
// mismatch after j matches
else if (i < N && pat.charAt(j) != txt.charAt(i)) {
// Do not match lps[0..lps[j-1]] characters,
// they will match anyway
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
if(f==0)
return false;
else
return true;
}
void computeLPSArray(String pat, int M, int lps[])
{
// length of the previous longest prefix suffix
int len = 0;
int i = 1;
lps[0] = 0; // lps[0] is always 0
// the loop calculates lps[i] for i = 1 to M-1
while (i < M) {
if (pat.charAt(i) == pat.charAt(len)) {
len++;
lps[i] = len;
i++;
}
else // (pat[i] != pat[len])
{
// This is tricky. Consider the example.
// AAACAAAA and i = 7. The idea is similar
// to search step.
if (len != 0) {
len = lps[len - 1];
// Also, note that we do not increment
// i here
}
else // if (len == 0)
{
lps[i] = len;
i++;
}
}
}
}
// Driver program to test above function
/*
public static void main(String args[])
{
String txt = "ABABDABACDABABCABAB";
String pat = "ABABCABAB";
new KMP_String_Matching().KMPSearch(pat, txt);
}
*/
}
// This code has been contributed by Amit Khandelwal.
static class multipliers implements Comparator<Integer>{
public int compare(Integer a,Integer b) {
if(a<b)
return -1;
else if(b<a)
return 1;
else
return 0;
}
}
static class multipliers1 implements Comparator<Student>{
public int compare(Student a, Student b)
{
if(a.y < b.y)
return -1;
else if(a.y > b.y)
return 1;
else
{
if(a.z < b.z)
return -1;
else
return 1;
}
}
}
static void checkCollision(int a, int b, int c,
int x, int y, int radius)
{
// Finding the distance of line from center.
double dist = (Math.abs(a * x + b * y + c)) /
Math.sqrt(a * a + b * b);
// Checking if the distance is less than,
// greater than or equal to radius.
if (radius == dist)
{
double dis = (double)Math.sqrt(((x2-x1)*(x2-x1)) + ((y2-y1)*(y2-y1)));
DecimalFormat df = new DecimalFormat("0.000000");
w.println(df.format(dis));
//System.out.println(df.format(364565.1454));
}
else if (radius > dist)
{
double x11 = (double)x1;
double y11 = (double)y1;
double x22 = (double)x2;
double y22 = (double)y2;
double up = 2*x11*y11/(((x11*x11) - ((double)radius*(double)radius)));
up *= up;
double denm = ((y11*y11) - ((double)radius*(double)radius))/((x11*x11) - ((double)radius*(double)radius));
up -= (4*denm);
up = Math.sqrt(up);
double theta = 0.0;
denm += 1.0;
if(denm!=0.00000000)
theta = Math.atan(up/denm);
else
theta = Math.PI/(double)2;
//w.println(up);
//w.println(denm);
up = 2*x22*y22/(((x22*x22) - ((double)radius*(double)radius)));
up *= up;
denm = ((y22*y22) - ((double)radius*(double)radius))/((x22*x22) - ((double)radius*(double)radius));
up -= (4*denm);
up = Math.sqrt(up);
denm += 1.0;
double theta1 = 0.0;
// denm += 1.0;
if(denm!=0.00000000)
theta1 = Math.atan(up/denm);
else
theta1 = Math.PI/(double)2;
theta /= 2;
theta1 /= 2;
double re = theta +theta1;
re = re * (double) radius;
double eq1 = (x11*x11) + (y11*y11) - ((double)radius*(double)radius);
double eq2 = (x22*x22) + (y22*y22) - ((double)radius*(double)radius);
double t1 = Math.sqrt(eq1);
double t2 = Math.sqrt(eq2);
double res = re + t1 + t2;
// w.println(theta1);
//w.println(theta);
DecimalFormat df = new DecimalFormat("0.000000");
w.println(df.format(res));
}
else
{
double x11 = (double)x1;
double y11 = (double)y1;
double x22 = (double)x2;
double y22 = (double)y2;
double midx = (x11+x22)/4;
double midy = (y11+y22)/4;
double dis = (double)Math.sqrt(((x22-midx)*(x22-midx)) + ((y22-midy)*(y22-midy)));
double dis1 = (double)Math.sqrt(((x11-midx)*(x11-midx)) + ((y11-midy)*(y11-midy)));
dis += dis1;
DecimalFormat df = new DecimalFormat("0.000000");
w.println(df.format(dis));
}
}
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output | |
PASSED | 416cf74d9c6f9316a6084c0cf945497d | train_002.jsonl | 1560090900 | Authors guessed an array $$$a$$$ consisting of $$$n$$$ integers; each integer is not less than $$$2$$$ and not greater than $$$2 \cdot 10^5$$$. You don't know the array $$$a$$$, but you know the array $$$b$$$ which is formed from it with the following sequence of operations: Firstly, let the array $$$b$$$ be equal to the array $$$a$$$; Secondly, for each $$$i$$$ from $$$1$$$ to $$$n$$$: if $$$a_i$$$ is a prime number, then one integer $$$p_{a_i}$$$ is appended to array $$$b$$$, where $$$p$$$ is an infinite sequence of prime numbers ($$$2, 3, 5, \dots$$$); otherwise (if $$$a_i$$$ is not a prime number), the greatest divisor of $$$a_i$$$ which is not equal to $$$a_i$$$ is appended to $$$b$$$; Then the obtained array of length $$$2n$$$ is shuffled and given to you in the input. Here $$$p_{a_i}$$$ means the $$$a_i$$$-th prime number. The first prime $$$p_1 = 2$$$, the second one is $$$p_2 = 3$$$, and so on.Your task is to recover any suitable array $$$a$$$ that forms the given array $$$b$$$. It is guaranteed that the answer exists (so the array $$$b$$$ is obtained from some suitable array $$$a$$$). If there are multiple answers, you can print any. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main{
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
// Start writing your solution here. -------------------------------------
int n=sc.nextInt();
TreeMap<Integer,Integer>primeMap=new TreeMap<Integer,Integer>();
for(int i=2;i<2750132;i++){
boolean prime=true;
for(int j:primeMap.keySet()){
if(i%j==0){
prime=false;
break;
}
if(j>Math.sqrt(i))
break;
}
if(prime)
primeMap.put(i,primeMap.size()+1);
}
TreeMap<Integer,Integer>set=new TreeMap<Integer,Integer>();
for(int i=0;i<2*n;i++){
int x=sc.nextInt();
set.put(x,set.containsKey(x)?set.get(x)+1:1);
}
while(!set.isEmpty()){
int m=set.get(set.lastKey()),x=set.pollLastEntry().getKey();
if(primeMap.containsKey(x)){
int z=primeMap.get(x);
for(int i=0;i<m;i++)
out.print(z+" ");
remove(set,z,m);
}else{
for(int i=0;i<m;i++)
out.print(x+" ");
remove(set,bigFactor(x),m);
}
}
out.println();
// Stop writing your solution here. -------------------------------------
out.close();
}
private static void remove(TreeMap<Integer,Integer>set,int y,int size){
set.put(y,set.get(y)-size);
if(set.get(y)==0)
set.remove(y);
}
private static int bigFactor(int x){
for(int i=2;i<=x;i++)
if(x%i==0)
return x/i;
return -1235;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
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;
}
}
//--------------------------------------------------------
} | Java | ["3\n3 5 2 3 2 4", "1\n2750131 199999", "1\n3 6"] | 4 seconds | ["3 4 2", "199999", "6"] | null | Java 8 | standard input | [
"greedy",
"graphs",
"number theory",
"sortings",
"dfs and similar"
] | 07484b6a6915c5cb5fdf1921355f2a6a | The first line of the input contains one integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the number of elements in $$$a$$$. The second line of the input contains $$$2n$$$ integers $$$b_1, b_2, \dots, b_{2n}$$$ ($$$2 \le b_i \le 2750131$$$), where $$$b_i$$$ is the $$$i$$$-th element of $$$b$$$. $$$2750131$$$ is the $$$199999$$$-th prime number. | 1,800 | In the only line of the output print $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$2 \le a_i \le 2 \cdot 10^5$$$) in any order — the array $$$a$$$ from which the array $$$b$$$ can be obtained using the sequence of moves given in the problem statement. If there are multiple answers, you can print any. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.