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 | 57fa51221bf988d60366288ff3f38ef3 | train_003.jsonl | 1584018300 | You are given a tree consisting of $$$n$$$ vertices. A tree is a connected undirected graph with $$$n-1$$$ edges. Each vertex $$$v$$$ of this tree has a color assigned to it ($$$a_v = 1$$$ if the vertex $$$v$$$ is white and $$$0$$$ if the vertex $$$v$$$ is black).You have to solve the following problem for each vertex $$$v$$$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $$$v$$$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $$$cnt_w$$$ white vertices and $$$cnt_b$$$ black vertices, you have to maximize $$$cnt_w - cnt_b$$$. | 256 megabytes | import java.util.*;
public class HelloWorld
{
static TreeNode arr[];
static int ansarr[];
public static void main(String []args)
{
Scanner s=new Scanner(System.in);
int v=s.nextInt();
arr=new TreeNode[v+1];
for(int i=1 ; i<=v ; i++)
{
int colour=s.nextInt();
arr[i]=new TreeNode(i,colour,0);
}
for(int i=1 ; i<=v-1 ; i++)
{
int parent=s.nextInt();
int child=s.nextInt();
TreeNode p=arr[parent];
TreeNode c=arr[child];
p.childlist.add(child);
c.childlist.add(parent);
}
dfs(arr[1],0);
// StringBuilder str=new StringBuilder();
// for(int i=1 ; i<=v ; i++)
// {
// str.append(arr[i].dfsvalue+" ");
// }
// System.out.println(str);
ansarr=new int[v];
ansdfs(arr[1],0);
StringBuilder str1=new StringBuilder();
for(int i=0 ; i<v ; i++)
{
str1.append(ansarr[i]+" ");
}
System.out.println(str1);
}
public static void ansdfs(TreeNode root,int parent)
{
int ans=0;
for(int i=0 ; i<root.childlist.size() ; i++)
{
int child=root.childlist.get(i);
if(child==parent)
{
int net=ansarr[parent-1];
if(root.dfsvalue>0)
{
net-=root.dfsvalue;
}
ans=Math.max(ans,ans+net);
}
else
{
int temp=arr[child].dfsvalue;
ans=Math.max(ans,ans+temp);
}
}
if(root.colour==0)
{
ans--;
}
else
{
ans++;
}
ansarr[root.value-1]=ans;
for(int i=0 ; i<root.childlist.size() ; i++)
{
int child=root.childlist.get(i);
if(child!=parent)
{
ansdfs(arr[child],root.value);
}
}
return ;
}
public static int dfs(TreeNode root,int parent)
{
int ans=0;
for(int i=0 ; i<root.childlist.size() ; i++)
{
int child=root.childlist.get(i);
if(parent!=child)
{
int temp=dfs(arr[child],root.value);
ans=Math.max(ans,ans+temp);
}
}
if(root.colour==0)
{
ans--;
}
else
{
ans++;
}
root.dfsvalue=ans;
return root.dfsvalue;
}
static class TreeNode
{
int value,colour,dfsvalue;
ArrayList<Integer> childlist;
TreeNode(int v,int c,int d)
{
this.value=v;
this.colour=c;
this.dfsvalue=d;
this.childlist=new ArrayList<>();
}
}
} | Java | ["9\n0 1 1 1 0 0 0 0 1\n1 2\n1 3\n3 4\n3 5\n2 6\n4 7\n6 8\n5 9", "4\n0 0 1 0\n1 2\n1 3\n1 4"] | 2 seconds | ["2 2 2 2 2 1 1 0 2", "0 -1 1 -1"] | NoteThe first example is shown below:The black vertices have bold borders.In the second example, the best subtree for vertices $$$2, 3$$$ and $$$4$$$ are vertices $$$2, 3$$$ and $$$4$$$ correspondingly. And the best subtree for the vertex $$$1$$$ is the subtree consisting of vertices $$$1$$$ and $$$3$$$. | Java 8 | standard input | [
"dp",
"dfs and similar",
"trees",
"graphs"
] | cb8895ddd54ffbd898b1bf5e169feb63 | The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) β the number of vertices in the tree. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 1$$$), where $$$a_i$$$ is the color of the $$$i$$$-th vertex. Each of the next $$$n-1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by two integers $$$u_i$$$ and $$$v_i$$$, the labels of vertices it connects $$$(1 \le u_i, v_i \le n, u_i \ne v_i$$$). It is guaranteed that the given edges form a tree. | 1,800 | Print $$$n$$$ integers $$$res_1, res_2, \dots, res_n$$$, where $$$res_i$$$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $$$i$$$. | standard output | |
PASSED | 21b18322707898634fd352a97325b99a | train_003.jsonl | 1584018300 | You are given a tree consisting of $$$n$$$ vertices. A tree is a connected undirected graph with $$$n-1$$$ edges. Each vertex $$$v$$$ of this tree has a color assigned to it ($$$a_v = 1$$$ if the vertex $$$v$$$ is white and $$$0$$$ if the vertex $$$v$$$ is black).You have to solve the following problem for each vertex $$$v$$$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $$$v$$$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $$$cnt_w$$$ white vertices and $$$cnt_b$$$ black vertices, you have to maximize $$$cnt_w - cnt_b$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static LinkedList<Integer>[] adj;
static boolean[] vis;
static int[] a,count,ans;
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
StringBuilder sb=new StringBuilder();
int n=sc.nextInt();
adj=new LinkedList[n];
for(int i=0;i<n;i++)adj[i]=new LinkedList<>();
a =new int[n];
count=new int[n];
ans=new int[n];
Arrays.fill(ans,Integer.MAX_VALUE);
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
if(a[i]==0)a[i]--;
}
for(int i=0;i<n-1;i++){
int u=sc.nextInt()-1,v=sc.nextInt()-1;
adj[u].add(v);
adj[v].add(u);
}
dfs1(0,0);
dfs2(0,0,0);
for(int i=0;i<n;i++)sb.append(ans[i]+" ");
System.out.println(sb);
}
static void dfs1(int i,int par){
for(int node:adj[i]){
if(node!=par){
dfs1(node,i);
count[i]+=Math.max(count[node],0);
}
}
count[i]+=a[i];
}
static void dfs2(int i,int par,int v){
ans[i]=count[i]+v;
for(int node:adj[i]){
if(node!=par)dfs2(node,i,Math.max(ans[i]-Math.max(count[node],0),0));
}
}
}
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 | ["9\n0 1 1 1 0 0 0 0 1\n1 2\n1 3\n3 4\n3 5\n2 6\n4 7\n6 8\n5 9", "4\n0 0 1 0\n1 2\n1 3\n1 4"] | 2 seconds | ["2 2 2 2 2 1 1 0 2", "0 -1 1 -1"] | NoteThe first example is shown below:The black vertices have bold borders.In the second example, the best subtree for vertices $$$2, 3$$$ and $$$4$$$ are vertices $$$2, 3$$$ and $$$4$$$ correspondingly. And the best subtree for the vertex $$$1$$$ is the subtree consisting of vertices $$$1$$$ and $$$3$$$. | Java 8 | standard input | [
"dp",
"dfs and similar",
"trees",
"graphs"
] | cb8895ddd54ffbd898b1bf5e169feb63 | The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) β the number of vertices in the tree. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 1$$$), where $$$a_i$$$ is the color of the $$$i$$$-th vertex. Each of the next $$$n-1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by two integers $$$u_i$$$ and $$$v_i$$$, the labels of vertices it connects $$$(1 \le u_i, v_i \le n, u_i \ne v_i$$$). It is guaranteed that the given edges form a tree. | 1,800 | Print $$$n$$$ integers $$$res_1, res_2, \dots, res_n$$$, where $$$res_i$$$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $$$i$$$. | standard output | |
PASSED | d0301509e6d8ae0d16683a00aa85f8ac | train_003.jsonl | 1584018300 | You are given a tree consisting of $$$n$$$ vertices. A tree is a connected undirected graph with $$$n-1$$$ edges. Each vertex $$$v$$$ of this tree has a color assigned to it ($$$a_v = 1$$$ if the vertex $$$v$$$ is white and $$$0$$$ if the vertex $$$v$$$ is black).You have to solve the following problem for each vertex $$$v$$$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $$$v$$$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $$$cnt_w$$$ white vertices and $$$cnt_b$$$ black vertices, you have to maximize $$$cnt_w - cnt_b$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.StringTokenizer;
public class F {
static PrintWriter out = new PrintWriter(System.out);
static int N;
static ArrayList<Integer> adj[];
static int c[];
static int subVal[];
static int parVal[];
public static void main(String[] args) {
FS in = new FS();
N = in.nextInt();
c = in.NIA(N);
adj = new ArrayList[N];
for(int i = 0; i < N; i++) adj[i] = new ArrayList<Integer>();
for(int i = 0; i < N-1; i++) {
int a = in.nextInt()-1;
int b = in.nextInt()-1;
adj[a].add(b);
adj[b].add(a);
}
subVal = new int[N];
dfs(0, -1);
parVal = new int[N];
dfs2(0,-1);
for(int i = 0; i < N; i++) {
out.print((subVal[i]+parVal[i])+" ");
}
out.println();
out.close();
}
static void dfs(int node, int p) {
int val = (c[node] == 1 ? 1 : -1);
for(int ii : adj[node]) {
if(ii == p) continue;
dfs(ii, node);
val += Math.max(0, subVal[ii]);
}
subVal[node] = val;
}
static void dfs2(int node, int p) {
if(p == -1) {
parVal[node] = 0;
}
else {
int val = parVal[p] + subVal[p];
if(subVal[node] >= 0) val -= subVal[node];
parVal[node] = Math.max(0,val);
}
for(int ii : adj[node]) if(ii != p) dfs2(ii, node);
}
static class FS{
BufferedReader br;
StringTokenizer st;
public FS() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {st = new StringTokenizer(br.readLine());}
catch(Exception e) { throw null;}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next());}
double nextDouble() { return Double.parseDouble(next());}
long nextLong() { return Long.parseLong(next());}
int[] NIA(int n) {
int r[] = new int[n];
for(int i = 0; i < n; i++) r[i] = nextInt();
return r;
}
long[] NLA(int n) {
long r[] = new long[n];
for(int i = 0; i < n; i++) r[i] = nextLong();
return r;
}
char[][] grid(int r, int c){
char res[][] = new char[r][c];
for(int i = 0; i < r; i++) {
char l[] = next().toCharArray();
for(int j = 0; j < c; j++) {
res[i][j] = l[j];
}
}
return res;
}
}
}
| Java | ["9\n0 1 1 1 0 0 0 0 1\n1 2\n1 3\n3 4\n3 5\n2 6\n4 7\n6 8\n5 9", "4\n0 0 1 0\n1 2\n1 3\n1 4"] | 2 seconds | ["2 2 2 2 2 1 1 0 2", "0 -1 1 -1"] | NoteThe first example is shown below:The black vertices have bold borders.In the second example, the best subtree for vertices $$$2, 3$$$ and $$$4$$$ are vertices $$$2, 3$$$ and $$$4$$$ correspondingly. And the best subtree for the vertex $$$1$$$ is the subtree consisting of vertices $$$1$$$ and $$$3$$$. | Java 8 | standard input | [
"dp",
"dfs and similar",
"trees",
"graphs"
] | cb8895ddd54ffbd898b1bf5e169feb63 | The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) β the number of vertices in the tree. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 1$$$), where $$$a_i$$$ is the color of the $$$i$$$-th vertex. Each of the next $$$n-1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by two integers $$$u_i$$$ and $$$v_i$$$, the labels of vertices it connects $$$(1 \le u_i, v_i \le n, u_i \ne v_i$$$). It is guaranteed that the given edges form a tree. | 1,800 | Print $$$n$$$ integers $$$res_1, res_2, \dots, res_n$$$, where $$$res_i$$$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $$$i$$$. | standard output | |
PASSED | c875ee7fc4ea3ea046b6ad2e8dd49a1e | train_003.jsonl | 1584018300 | You are given a tree consisting of $$$n$$$ vertices. A tree is a connected undirected graph with $$$n-1$$$ edges. Each vertex $$$v$$$ of this tree has a color assigned to it ($$$a_v = 1$$$ if the vertex $$$v$$$ is white and $$$0$$$ if the vertex $$$v$$$ is black).You have to solve the following problem for each vertex $$$v$$$: what is the maximum difference between the number of white and the number of black vertices you can obtain if you choose some subtree of the given tree that contains the vertex $$$v$$$? The subtree of the tree is the connected subgraph of the given tree. More formally, if you choose the subtree that contains $$$cnt_w$$$ white vertices and $$$cnt_b$$$ black vertices, you have to maximize $$$cnt_w - cnt_b$$$. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class Main
{
static LinkedList<Integer> adj[];
static void DFS(int s,int b[],int w[],int parent,int a[]){
Iterator<Integer> itr=adj[s].listIterator();
while(itr.hasNext())
{
int n=itr.next();
if(n!=parent){
DFS(n,b,w,s,a);
}
}
if(a[s]==1)
w[s]++;
else b[s]++;
Iterator<Integer> itr1=adj[s].listIterator();
while(itr1.hasNext())
{
int n=itr1.next();
if(n!=parent){
if(w[n]>b[n])
{
w[s]=w[s]+w[n];
b[s]=b[s]+b[n];
}
}
}
}
static void solve(int s,int b[],int w[],int parent,int a[],int ans[]){
Iterator<Integer> itr=adj[s].listIterator();
while(itr.hasNext())
{
int n=itr.next();
if(n!=parent){
if(w[s]>b[s])
{
int x=b[s],y=w[s];
if(w[n]>b[n]){
x=b[s]-b[n];
y=w[s]-w[n];}
if(y>x){
b[n]=b[n]+x;
w[n]=w[n]+y;
}
}
ans[n]=w[n]-b[n];
solve(n,b,w,s,a,ans);
}
}
}
public static void main(String[] args)
{
FastReader s=new FastReader();
int n=s.nextInt();
int a[]=new int[n+1];
for(int i=1;i<=n;i++)
a[i]=s.nextInt();
adj=new LinkedList[n+1];
for(int i=0;i<=n;i++)
adj[i]=new LinkedList<>();
for(int i=0;i<n-1;i++){
int u=s.nextInt();
int v=s.nextInt();
adj[u].add(v);
adj[v].add(u);
}
int b[]=new int[n+1];
int w[]=new int[n+1];
DFS(1,b,w,-1,a);
int ans[]=new int[n+1];
ans[1]=w[1]-b[1];
solve(1,b,w,-1,a,ans);
for(int i=1;i<=n;i++)
System.out.print(ans[i]+" ");
}
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 | ["9\n0 1 1 1 0 0 0 0 1\n1 2\n1 3\n3 4\n3 5\n2 6\n4 7\n6 8\n5 9", "4\n0 0 1 0\n1 2\n1 3\n1 4"] | 2 seconds | ["2 2 2 2 2 1 1 0 2", "0 -1 1 -1"] | NoteThe first example is shown below:The black vertices have bold borders.In the second example, the best subtree for vertices $$$2, 3$$$ and $$$4$$$ are vertices $$$2, 3$$$ and $$$4$$$ correspondingly. And the best subtree for the vertex $$$1$$$ is the subtree consisting of vertices $$$1$$$ and $$$3$$$. | Java 8 | standard input | [
"dp",
"dfs and similar",
"trees",
"graphs"
] | cb8895ddd54ffbd898b1bf5e169feb63 | The first line of the input contains one integer $$$n$$$ ($$$2 \le n \le 2 \cdot 10^5$$$) β the number of vertices in the tree. The second line of the input contains $$$n$$$ integers $$$a_1, a_2, \dots, a_n$$$ ($$$0 \le a_i \le 1$$$), where $$$a_i$$$ is the color of the $$$i$$$-th vertex. Each of the next $$$n-1$$$ lines describes an edge of the tree. Edge $$$i$$$ is denoted by two integers $$$u_i$$$ and $$$v_i$$$, the labels of vertices it connects $$$(1 \le u_i, v_i \le n, u_i \ne v_i$$$). It is guaranteed that the given edges form a tree. | 1,800 | Print $$$n$$$ integers $$$res_1, res_2, \dots, res_n$$$, where $$$res_i$$$ is the maximum possible difference between the number of white and black vertices in some subtree that contains the vertex $$$i$$$. | standard output | |
PASSED | 0d15e23349aec57a20a2400f1bbee48f | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class spoj {
InputStream is;
PrintWriter out;
void solve()
{
int t=ni();
while(t-->0)
{
int n=ni();
HashSet<Integer> hs=new HashSet<Integer>();
int lt=(int)(Math.sqrt(n));
hs.add(0);
hs.add(1);
hs.add(n);
for(int i=1;i<=lt;i++)
{
hs.add(n/i);
hs.add(i);
}
out.println(hs.size());
ArrayList<Integer> l=new ArrayList<Integer>(hs);
Collections.sort(l);
for(int i=0;i<l.size();i++)
out.print(l.get(i)+" ");
out.println();
}
}
//---------- I/O Template ----------
public static void main(String[] args) { new spoj().run(); }
void run() {
is = System.in;
out = new PrintWriter(System.out);
solve();
out.flush();
}
byte input[] = new byte[1024];
int len = 0, ptr = 0;
int readByte() {
if(ptr >= len) { ptr = 0;
try { len = is.read(input); }
catch(IOException e) { throw new InputMismatchException(); }
if(len <= 0) { return -1; }
} return input[ptr++];
}
boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
int skip() {
int b = readByte();
while(b != -1 && isSpaceChar(b)) { b = readByte(); }
return b;
}
char nc() { return (char)skip(); }
String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); }
return sb.toString();
}
String nLine() {
int b = skip();
StringBuilder sb = new StringBuilder();
while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); }
return sb.toString();
}
int ni() {
int n = 0, b = readByte();
boolean minus = false;
while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
if(b == '-') { minus = true; b = readByte(); }
if(b == -1) { return -1; } //no input
while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
return minus ? -n : n;
}
long nl() {
long n = 0L; int b = readByte();
boolean minus = false;
while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
if(b == '-') { minus = true; b = readByte(); }
while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
return minus ? -n : n;
}
double nd() { return Double.parseDouble(ns()); }
float nf() { return Float.parseFloat(ns()); }
int[] na(int n) {
int a[] = new int[n];
for(int i = 0; i < n; i++) { a[i] = ni(); }
return a;
}
char[] ns(int n) {
char c[] = new char[n];
int i, b = skip();
for(i = 0; i < n; i++) {
if(isSpaceChar(b)) { break; }
c[i] = (char)b; b = readByte();
} return i == n ? c : Arrays.copyOf(c,i);
}
void pa(int a[])
{
for(int i=0;i<a.length;i++)
out.print(a[i]+" ");
out.println();
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | cc764ea79bed04bd00649b5a41f16b8e | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | /**
* @author derrick20
* Solved 1:12
*/
import java.io.*;
import java.util.*;
public class EveryoneWinner {
public static void main(String args[]) throws Exception {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int T = sc.nextInt();
while (T-->0) {
int N = sc.nextInt();
TreeSet<Integer> res = new TreeSet<>();
res.add(0);
res.add(1);
for (int i = 1; i <= (int) Math.ceil(Math.sqrt(N)); i++) {
res.add(N / i);
res.add(N / (N / i));
}
out.println(res.size());
for (int val : res) {
out.print(val + " ");
}
out.println();
}
out.close();
}
static class FastScanner {
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double cnt = 1;
BufferedInputStream in;
public FastScanner() {
in = new BufferedInputStream(System.in, BS);
}
public FastScanner(String s) {
try {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
catch (Exception e) {
in = new BufferedInputStream(System.in, BS);
}
}
private char getChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public int[] nextInts(int N) {
int[] res = new int[N];
for (int i = 0; i < N; i++) {
res[i] = (int) nextLong();
}
return res;
}
public long[] nextLongs(int N) {
long[] res = new long[N];
for (int i = 0; i < N; i++) {
res[i] = nextLong();
}
return res;
}
public long nextLong() {
cnt=1;
boolean neg = false;
if(c==NC)c=getChar();
for(;(c<'0' || c>'9'); c = getChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=getChar()) {
res = (res<<3)+(res<<1)+c-'0';
cnt*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/cnt;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=getChar();
while(c>32) {
res.append(c);
c=getChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=getChar();
while(c!='\n') {
res.append(c);
c=getChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=getChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 97f5eb6d14ce176d878f81cffb8cca7a | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes |
import java.util.*;
import java.io.*;
public class C {
static final boolean stdin = true;
static final String filename = "";
static FastScanner br;
static PrintWriter pw;
public static void main(String[] args) throws IOException {
if (stdin) {
br = new FastScanner();
pw = new PrintWriter(new OutputStreamWriter(System.out));
} else {
br = new FastScanner(filename + ".in");
pw = new PrintWriter(new FileWriter(filename + ".out"));
}
Solver solver = new Solver();
solver.solve(br, pw);
pw.close();
}
static class Solver {
static long mod = (long) (1e9);
public void solve(FastScanner br, PrintWriter pw) throws IOException {
int t = br.ni();
while (t-- > 0) {
int n = br.ni();
ArrayList<Integer> a = new ArrayList<Integer>();
HashSet<Integer> vis = new HashSet<Integer>();
for (int i = 1; i * i <= n; i++) {
if (!vis.contains(n / i)) {
a.add(n / i);
vis.add(n / i);
}
if (!vis.contains(n / (n / i))) {
a.add(n / (n / i));
vis.add(n / i);
}
}
a.add(0);
Collections.sort(a);
pw.println(a.size());
for (int i = 0; i < a.size(); i++) {
pw.print(a.get(i) + " ");
}
pw.println();
}
}
static long gcd(long a, long b) {
if (a > b)
return gcd(b, a);
if (a == 0)
return b;
return gcd(b % a, a);
}
static long lcm(long a, long b) {
return a * (b / gcd(a, b));
}
static long pow(long a, long b) {
if (b == 0)
return 1L;
long val = pow(a, b / 2);
if (b % 2 == 0)
return val * val % mod;
else
return val * val % mod * a % mod;
}
}
static class Point implements Comparable<Point> {
int a;
int b;
Point(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Point o) {
return this.a - o.a;
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
ArrayList<Integer>[] ng(int n, int e) {
ArrayList<Integer>[] adj = new ArrayList[n];
for (int i = 0; i < n; i++) {
adj[i] = new ArrayList<Integer>();
}
for (int i = 0; i < e; i++) {
int a = ni() - 1;
int b = ni() - 1;
adj[a].add(b);
adj[b].add(a);
}
return adj;
}
Integer[] nIa(int n) {
Integer[] arr = new Integer[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
int[] nia(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
Long[] nLa(int n) {
Long[] arr = new Long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
long[] nla(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
String[] nsa(int n) {
String[] arr = new String[n];
for (int i = 0; i < n; i++) {
arr[i] = nt();
}
return arr;
}
String nt() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(nt());
}
long nl() {
return Long.parseLong(nt());
}
double nd() {
return Double.parseDouble(nt());
}
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 375943173e9b8c0421b7a579633ab3e9 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.util.*;
import java.io.*;
public class Solution{
static class pair implements Comparable<pair>{
int a,b;
pair(int x,int y){
a=x;b=y;
}
public int compareTo(pair t){
if(t.a==this.a)
return this.b-t.b;
return this.a-t.a;
}
}
public static ArrayList<pair> bfs(String[] a,int r,int c){
ArrayList<pair> ans=new ArrayList<>();
Queue<pair> q=new LinkedList<>();
int[][] dxy={{-1,0,1,0},{0,-1,0,1}};
q.add(new pair(r,c));
HashSet<String> h=new HashSet<>();
while(!q.isEmpty()){
pair f=q.poll();
ans.add(f);h.add(f.a+" "+f.b);
for(int i=0;i<4;i++){
int dx=f.a+dxy[0][i];
int dy=f.b+dxy[1][i];
if(dx<0||dy<0||dx>=a.length||dy>=a.length||h.contains(dx+" "+dy)||
a[dx].charAt(dy)=='1')
continue;
q.add(new pair(dx,dy));
h.add(dx+" "+dy);
}
}
return ans;
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int t = s.nextInt();
while(t-- > 0) {
int n = s.nextInt();
HashSet<Integer> h=new HashSet<>();
h.add(0);
for(int i=1;i<=(int)Math.sqrt(n);i++){
h.add(n/i);
h.add(i);
}
ArrayList<Integer> a=new ArrayList<>();
for(int i:h){
a.add(i);
}
Collections.sort(a);
System.out.println(a.size());
StringBuilder st=new StringBuilder();
for(int i=0;i<a.size();i++)
st.append(a.get(i)+" ");
System.out.println(st);
}
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 52b35cd79a23a21aba6b05c8919c9e47 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class C {
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
FS in = new FS();
int T = in.nextInt();
for(int runs = 1; runs <= T; runs++) {
int x = in.nextInt();
TreeSet<Integer> ts = new TreeSet<Integer>();
ts.add(0);
int cur = 1;
while(cur <= x) {
ts.add(x/cur);
int lo = cur, hi = x;
int best = cur;
while(lo <= hi) {
int mid = (lo+hi)/2;
if(x/mid == x/cur) {
best = Math.max(best, mid);
lo = mid+1;
}
hi = mid-1;
}
cur = best+1;
}
out.println(ts.size());
for(int ii : ts) out.print(ii+" ");
out.println();
}
out.close();
}
static class FS{
BufferedReader br;
StringTokenizer st;
public FS() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(st == null || !st.hasMoreElements()) {
try {st = new StringTokenizer(br.readLine());}
catch(Exception e) { throw null;}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next());}
double nextDouble() { return Double.parseDouble(next());}
long nextLong() { return Long.parseLong(next());}
int[] NIA(int n) {
int r[] = new int[n];
for(int i = 0; i < n; i++) r[i] = nextInt();
return r;
}
long[] NLA(int n) {
long r[] = new long[n];
for(int i = 0; i < n; i++) r[i] = nextLong();
return r;
}
char[][] grid(int r, int c){
char res[][] = new char[r][c];
for(int i = 0; i < r; i++) {
char l[] = next().toCharArray();
for(int j = 0; j < c; j++) {
res[i][j] = l[j];
}
}
return res;
}
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | ca582dfed87a49f5cb011679743653a2 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 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.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.InputMismatchException;
import java.util.LinkedList;
import java.util.Queue;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.*;
@SuppressWarnings("unused")
public class template {
InputStream is;
PrintWriter out;
String INPUT = "";
static int min = 99999;
ArrayList<ArrayList<Integer>> x = new ArrayList<>();
void solve() {
int t = ni();
while(t-- > 0) {
int n = ni();
int l = 1 , h = n;
if(n == 1) {
out.println("2");
out.println("0 1");
}
else {
while(l < h) {
int mid = (l+h)/2;
int quo = n/mid;
if(quo < mid) {
h = mid;
}
else {
l = mid+1;
}
}
HashSet<Integer> hs = new HashSet<>();
for(int i = 1 ; i<l ; i++) {
hs.add(i);
hs.add(n/i);
}
hs.add(0);
out.println(hs.size());
//out.println(l);
int a[] = new int[hs.size()];
int ct = 0;
for(int i : hs) {
a[ct] = i;
ct++;
}
Arrays.sort(a);
for(int i : a) out.print(i+" ");
out.println();
}
}
}
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");
//tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new template().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 && !(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][m];
for(int i = 0;i < n;i++)
for(int j = 0 ; j<m ; j++) a[i][j] = ni();
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();
}
}
void display2D(int a[][]) {
for(int i[] : a) {
for(int j : i) {
out.print(j+" ");
}
out.println();
}
}
int[][] creategraph(int n , int m) {
int g[][] = new int[n+1][];
int from[] = new int[m];
int to[] = new int[m];
int ct[] = new int[n+1];
for(int i = 0 ; i<m; i++) {
from[i] = ni();
to[i] = ni();
ct[from[i]]++;
ct[to[i]]++;
}
int parent[] = new int[n+1];
for(int i = 0 ; i<n+1 ; i++) g[i] = new int[ct[i]];
for(int i = 0 ; i<m ; i++) {
g[from[i]][--ct[from[i]]] = to[i];
g[to[i]][--ct[to[i]]] = from[i];
}
return g;
}
static long __gcd(long a, long b)
{
if(b == 0)
{
return a;
}
else
{
return __gcd(b, a % b);
}
}
// To compute x^y under modulo m
static long power(long x, long y, long p)
{
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0)
{
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Function to find modular
// inverse of a under modulo m
// Assumption: m is prime
static long modInverse(long a, int m)
{
if (__gcd(a, m) != 1) {
//System.out.print("Inverse doesn't exist");
return -1;
}
else {
// If a and m are relatively prime, then
// modulo inverse is a^(m-2) mode m
// System.out.println("Modular multiplicative inverse is "
// +power(a, m - 2, m));
return power(a, m - 2, m);
}
}
static long nCrModPFermat(int n, int r,
int p , long fac[])
{
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
long t = (fac[n]* modInverse(fac[r], p))%p;
return ( (t* modInverse(fac[n-r], p))% p);
}
private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); }
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | d425f18d09085bef95c1cc268ebcba01 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.regex.*;
public class vk18
{
public static void main(String[]stp) throws Exception
{
Scanner scan=new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
//BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
//String []s;
int q=scan.nextInt();
while(q!=0)
{
TreeSet<Integer> ts=new TreeSet<>();
int n=scan.nextInt(),i;
ts.add(0);
for(i=1;i<=Math.sqrt(n);i++){ ts.add(i); ts.add(n/i); }
pw.println(ts.size());
//Iterator<Integer> itr=ts.iterator();
//System.out.print("0 ");
while(!ts.isEmpty()) pw.print(ts.pollFirst() + (ts.size()==0?"\n":" "));
//System.out.println();
q--;
}
pw.flush();
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 8d27d7492ae0745aa3a9cf6e8112f57a | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class a{
static int max=200001;
public static void main( String [] args) throws IOException{
FastScanner sc=new FastScanner();
int t=sc.nextInt();
while(t-->0){
int n=sc.nextInt();
int sq=(int)Math.sqrt(n);
TreeSet<Integer> tset=new TreeSet<>();
for(int i=1;i<=sq;i++){
tset.add(i);
tset.add(n/i);
}
tset.add(0);
tset.add(1);
System.out.println(tset.size());
StringBuilder sb=new StringBuilder("");
while(!tset.isEmpty())sb.append(tset.pollFirst()+" ");
System.out.println(sb.toString().trim());
}
}
}
class FastScanner{
private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public FastScanner() {
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public FastScanner( String file_name) throws IOException {
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException {
byte[] buf = new byte[64];
int cnt = 0, c;
while ((c = read()) != -1) {
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public String next() throws IOException{
byte c = read();
while(Character.isWhitespace(c)){
c = read();
}
StringBuilder builder = new StringBuilder();
builder.append((char)c);
c = read();
while(!Character.isWhitespace(c)){
builder.append((char)c);
c = read();
}
return builder.toString();
}
public int nextInt() throws IOException {
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public int[] nextIntArray( int n) throws IOException {
int arr[] = new int[n];
for(int i = 0; i < n; i++){
arr[i] = nextInt();
}
return arr;
}
public long nextLong() throws IOException {
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long[] nextLongArray( int n) throws IOException {
long arr[] = new long[n];
for(int i = 0; i < n; i++){
arr[i] = nextLong();
}
return arr;
}
public char nextChar() throws IOException{
byte c = read();
while(Character.isWhitespace(c)){
c = read();
}
return (char) c;
}
public double nextDouble() throws IOException {
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
public double[] nextDoubleArray( int n) throws IOException {
double arr[] = new double[n];
for(int i = 0; i < n; i++){
arr[i] = nextDouble();
}
return arr;
}
private void fillBuffer() throws IOException {
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException {
if (din == null)
return;
din.close();
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 1452ea0e17ab0effd116eb906bfab594 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
PrintWriter pw=new PrintWriter(System.out);
while(t-->0)
{
int n=sc.nextInt();
ArrayList<Integer> list=new ArrayList();
list.add(0);
TreeSet<Integer> set=new TreeSet();
set.add(0);
int k=1;
for(int i=1;i*i<=n;i++)
{
int x=n/i;
if(!set.contains(x))
{
set.add(x);
list.add(x);
k++;
}
x=n/x;
if(!set.contains(x))
{
set.add(x);
list.add(x);
k++;
}
}
pw.println(k);
Collections.sort(list);
for(int x:list)
pw.print(x+" ");
pw.println();
}
pw.flush();
pw.close();
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 52777efdcd48249b88265619894073eb | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | /* Rajkin Hossain */
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class C {
FastInput k = new FastInput(System.in);
//FastInput k = new FastInput("/home/rajkin/Desktop/input.txt");
FastOutput z = new FastOutput();
TreeSet<Integer> set;
void startProgram() {
int t = k.nextInt();
while(t-->0) {
int n = k.nextInt();
set = new TreeSet<Integer>();
for(int i = 1; i <= (int)Math.sqrt(n); i++) {
set.add(i);
set.add(n/i);
}
set.add(0);
z.println(set.size());
while(!set.isEmpty()) {
z.print(set.pollFirst()+" ");
}
z.println();
}
z.flush();
System.exit(0);
}
public static void main(String [] args) throws IOException {
new Thread(null, new Runnable(){
public void run(){
try{
new C().startProgram();
}
catch(Exception e){
e.printStackTrace();
}
}
},"Main",1<<28).start();
}
/* MARK: FastInput and FastOutput */
class FastInput {
BufferedReader reader;
StringTokenizer tokenizer;
FastInput(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
}
FastInput(String path){
try {
reader = new BufferedReader(new FileReader(path));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
tokenizer = null;
}
String next() {
return nextToken();
}
String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
boolean hasNext(){
try {
return reader.ready();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
String nextToken() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = null;
try {
line = reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
if (line == null) {
return null;
}
tokenizer = new StringTokenizer(line);
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.valueOf(nextToken());
}
double nextDouble() {
return Double.valueOf(nextToken());
}
}
class FastOutput extends PrintWriter {
FastOutput() {
super(new BufferedOutputStream(System.out));
}
public void debug(Object...obj) {
System.err.println(Arrays.deepToString(obj));
}
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | e15fefe8c7b3c3f446a24e578a365c30 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Solver {
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int q = nextInt();
for (int i = 0; i < q; i++) {
int n = nextInt();
int sqrt = (int)sqrt(n);
TreeSet<Integer>ts = new TreeSet<>();
ts.add(0);
ts.add(1);
for (int j = sqrt; j >= 1; j--) {
ts.add(n / j);
ts.add(j);
}
pw.println(ts.size());
for (int h = 0; h < ts.size();) {
pw.print(ts.pollFirst() + " ");
}
pw.println();
}
pw.close();
}
static StringTokenizer st = new StringTokenizer("");
static BufferedReader br;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
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());
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 37211d0f0148aae4c6c9a108464c3533 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | /**
* BaZ :D
*/
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
public class Main
{
static MyScanner scan;
static PrintWriter pw;
static long MOD = 1_000_000_007;
static long INF = 1_000_000_000_000_000_000L;
static long inf = 2_000_000_000;
public static void main(String[] args) {
new Thread(null, null, "BaZ", 1 << 27) {
public void run() {
try {
solve();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
}.start();
}
static void solve() throws IOException
{
//initIo(true);
initIo(false);
StringBuilder sb = new StringBuilder();
int t = ni();
while(t-->0) {
int n = ni();
ArrayList<Integer> list = new ArrayList<>();
list.add(0);
for(int i=1;i<=n;) {
int curr = n/i;
int till = n/curr;
list.add(curr);
i = till+1;
}
pl(list.size());
Collections.sort(list);
for(int e : list) {
p(e);
}
pl();
}
pw.flush();
pw.close();
}
static void initIo(boolean isFileIO) throws IOException {
scan = new MyScanner(isFileIO);
if(isFileIO) {
pw = new PrintWriter("/Users/amandeep/Desktop/output.txt");
}
else {
pw = new PrintWriter(System.out, true);
}
}
static int ni() throws IOException
{
return scan.nextInt();
}
static long nl() throws IOException
{
return scan.nextLong();
}
static double nd() throws IOException
{
return scan.nextDouble();
}
static String ne() throws IOException
{
return scan.next();
}
static String nel() throws IOException
{
return scan.nextLine();
}
static void pl()
{
pw.println();
}
static void p(Object o)
{
pw.print(o+" ");
}
static void pl(Object o)
{
pw.println(o);
}
static void psb(StringBuilder sb)
{
pw.print(sb);
}
static void pa(String arrayName, Object arr[])
{
pl(arrayName+" : ");
for(Object o : arr)
p(o);
pl();
}
static void pa(String arrayName, int arr[])
{
pl(arrayName+" : ");
for(int o : arr)
p(o);
pl();
}
static void pa(String arrayName, long arr[])
{
pl(arrayName+" : ");
for(long o : arr)
p(o);
pl();
}
static void pa(String arrayName, double arr[])
{
pl(arrayName+" : ");
for(double o : arr)
p(o);
pl();
}
static void pa(String arrayName, char arr[])
{
pl(arrayName+" : ");
for(char o : arr)
p(o);
pl();
}
static void pa(String listName, List list)
{
pl(listName+" : ");
for(Object o : list)
p(o);
pl();
}
static void pa(String arrayName, Object[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(Object o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, int[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(int o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, long[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(long o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, char[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(char o : arr[i])
p(o);
pl();
}
}
static void pa(String arrayName, double[][] arr) {
pl(arrayName+" : ");
for(int i=0;i<arr.length;++i) {
for(double o : arr[i])
p(o);
pl();
}
}
static class MyScanner
{
BufferedReader br;
StringTokenizer st;
MyScanner(boolean readingFromFile) throws IOException
{
if(readingFromFile) {
br = new BufferedReader(new FileReader("/Users/amandeep/Desktop/input.txt"));
}
else {
br = new BufferedReader(new InputStreamReader(System.in));
}
}
String nextLine()throws IOException
{
return br.readLine();
}
String next() throws IOException
{
if(st==null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.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());
}
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 153a3e72d37b3b7e0569d56678cc924a | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.util.*;
import java.io.*;
public class MyClass {
public static void main(String args[]) {
FastReader sc = new FastReader();
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while(t-->0){
long n = sc.nextLong();
TreeSet<Long> ts = new TreeSet<>();
for(long i=1;i<=Math.sqrt(n);i++){
ts.add(i);
ts.add(n/i);
}
sb.append((ts.size()+1)+"\n");
sb.append(0+" ");
for(long num : ts){
sb.append(num+" ");
}
sb.append("\n");
}
System.out.println(sb);
}
}
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\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 313d2e5d3c741c26cacfaf5623606b92 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.util.*;
import java.io.*;
public class winner {
int T, N;
winner() throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
T = Integer.parseInt(in.readLine());
for (int i = 0; i < T; i++) {
N = Integer.parseInt(in.readLine());
Queue<Integer> valid = new LinkedList<>();
valid.add(0);
int toDivide = N, last = 0;
while (last != N) {
// System.out.println(valid.peek());
last = N / toDivide;
valid.add(last);
toDivide = N / (last + 1);
}
out.println(valid.size());
while (!valid.isEmpty()) {
out.print(valid.poll());
if (valid.isEmpty()) break;
out.print(' ');
}
out.println();
}
in.close();
out.close();
}
public static void main(String[] args) throws IOException {
new winner();
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 66a162422143fd4e1e5815e483868216 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class C {
private static List<Integer> test(int a){
HashSet<Integer> set= new HashSet<>();
set.add(0);
for(int i=1;i<1000000;i++){
set.add(a/i);
}
List<Integer> l = new ArrayList<>(set);
Collections.sort(l);
return l;
}
private static void findVal(int a){
HashSet<Integer> set= new HashSet<>();
for(int i=1;i<Math.sqrt(a)+1;i++){
set.add(a/i);
}
HashSet<Integer> nSet = new HashSet<>();
for(Integer num:set){
nSet.add(num);
nSet.add(a/num);
}
nSet.add(0);
List<Integer> l=new ArrayList<>(nSet);
Collections.sort(l);
// List<Integer> tl = test(a);
// System.out.println(tl);
StringBuilder output = new StringBuilder();
int n2 = l.size();
output.append(n2+"\n");
for(int i=0;i<l.size()-1;i++) output.append(l.get(i)+" ");
output.append(l.get(l.size()-1));
System.out.println(output.toString());
}
public static void main(String[] args){
// File file = new File("/Users/chihohar/IdeaProjects/CodeForce/input/input_c.txt");
// Scanner scan = null;
// try {
// scan = new Scanner(file);
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
Scanner scan = new Scanner(System.in);
int t = scan.nextInt();
for (int i = 0; i < t; i++) {
int a = scan.nextInt();
findVal(a);
}
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | a08012c6e97f641b7f741db786f89263 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int q = scan.nextInt();
scan.nextLine();
for (int i = 0; i < q; i++) {
handleQ(scan);
}
}
private static void handleQ(Scanner scan) {
int n = scan.nextInt();
StringBuilder res = new StringBuilder().append("0 ");
List<Integer> l = new ArrayList<>();
int counter = 1;
for (int i = 1; i <= Math.sqrt(n); i++) {
counter++;
res.append(i + " ");
l.add(n / i);
}
int checkI = l.get(l.size() - 1);
if (checkI != n / checkI) {
counter++;
res.append(checkI + " ");
}
for (int i = l.size() - 2; i >= 0; i--) {
counter++;
res.append(l.get(i) + " ");
}
System.out.println(counter);
System.out.println(res);
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | afc41a336456566a5c901fb809969132 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.util.TreeSet;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Pranay2516
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskC solver = new TaskC();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskC {
public void solve(int testNumber, FastReader in, PrintWriter out) {
int n = in.nextInt();
TreeSet<Integer> a = new TreeSet<>();
a.add(0);
for (int i = 1; i <= Math.sqrt(n); ++i) {
a.add(n / i);
a.add(i);
}
out.println(a.size());
for (int e : a) out.print(e + " ");
out.println();
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private FastReader.SpaceCharFilter filter;
public FastReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public 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 boolean isSpaceChar(int c) {
if (filter != null) return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 0d958ce211ade7d8fc027cc9278a4919 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void solve(InputReader in , OutputWriter out) {
int n = in.readInt();
TreeSet<Integer> set = new TreeSet<Integer>();
set.add(0);
for(int i = 1; i<n; i++) {
int x = n/i;
int y = n/x;
if(set.contains(y)) break;
set.add(x);
set.add(y);
}
set.add(1);
out.println(set.size());
for(Integer i : set) {
out.print(i + " ");
}
out.println();
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int t = in.readInt();
while (t-- > 0)
solve(in , out);
out.flush();
out.close();
}
}
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 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 long readLong() {
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 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);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | af8fe4124ed189481bff8d9ba67b5ee1 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void solve(InputReader in , OutputWriter out) {
long n = in.readLong();
TreeSet<Long> set = new TreeSet<>();
set.add(0l);
for(long i = 1; i*i <=n; i++) {
set.add(n/i);
set.add(n / (n/i));
}
out.println(set.size());
for(Long i : set) {
out.print(i + " ");
}
out.println();
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int t = in.readInt();
while (t-- > 0)
solve(in , out);
out.flush();
out.close();
}
}
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 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 long readLong() {
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 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);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 22cdd5cee6c2952ccee29784490aa8c8 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | // @uthor -: puneet
// TimeStamp -: 9:03 PM - 29/11/19
import javax.swing.*;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Q33 implements Runnable {
public void solve() {
int t=in.ni();
while(t-->0){
int n=in.ni();
TreeMap<Integer,Integer> map=new TreeMap<>();
for(int i=1;i*i<=n;++i){
int temp=n/i;
int temp2=n/temp;
map.put(temp,1);
map.put(temp2,1);
}
map.put(0,1);
out.println(map.size());
for(int i:map.keySet()){
out.print(i+" ");
}
out.println();
}
}
/* -------------------- Templates and Input Classes -------------------------------*/
@Override
public void run() {
try {
init();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
solve();
out.flush();
}
private FastInput in;
private PrintWriter out;
public static void main(String[] args) throws Exception {
new Thread(null, new Q33(), "Main", 1 << 27).start();
}
private void init() throws FileNotFoundException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
try {
if (System.getProperty("user.name").equals("puneet")) {
outputStream = new FileOutputStream("/home/puneet/Desktop/output.txt");
inputStream = new FileInputStream("/home/puneet/Desktop/input.txt");
}
} catch (Exception ignored) {
}
out = new PrintWriter(outputStream);
in = new FastInput(inputStream);
}
private void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
private void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (long object : arr) list.add(object);
Collections.sort(list);
for (int i = 0; i < list.size(); ++i) arr[i] = list.get(i);
}
public long ModPow(long x, long y, long MOD) {
long res = 1L;
x = x % MOD;
while (y >= 1) {
if ((y & 1) > 0) res = (res * x) % MOD;
x = (x * x) % MOD;
y >>= 1;
}
return res;
}
public int gcd(int a, int b) {
if (a == 0) return b;
return gcd(b % a, a);
}
public long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
static class FastInput {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastInput(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 peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 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 {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
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 {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String ns() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public BigInteger readBigInteger() {
try {
return new BigInteger(ns());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nc() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nd() {
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, ni());
}
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, ni());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String next() {
return ns();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] intarr(int n) {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = ni();
}
return arr;
}
public double[] doublearr(int n) {
double arr[] = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = nd();
}
return arr;
}
public double[][] doublearr(int n, int m) {
double[][] arr = new double[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = nd();
}
}
return arr;
}
public int[][] intarr(int n, int m) {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = ni();
}
}
return arr;
}
public long[][] longarr(int n, int m) {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = nl();
}
}
return arr;
}
public long[] longarr(int n) {
long arr[] = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nl();
}
return arr;
}
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 6f7f9ce2ebf2f614c9378ae4580ad3af | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.PriorityQueue;
import java.util.TreeSet;
public class C_Practice {
static Reader r = new Reader();
static PrintWriter out = new PrintWriter(System.out);
private static void solve1() throws IOException {
int n = r.nextInt();
int arr[] = r.nextIntArray(n);
for (int i = 0; i < n; i++) {
if (arr[i] > i + 1) {
out.println(-1);
out.close();
return;
}
}
int cnt = 1;
int ans[] = new int[n];
Arrays.fill(ans, -1);
boolean vis[] = new boolean[(int) 1e6 + 1];
ArrayList<Integer> av = new ArrayList<Integer>();
vis[arr[0]] = true;
for (int i = 1; i < n; i++) {
if (arr[i] != arr[i - 1]) {
ans[i] = arr[i - 1];
vis[arr[i]] = true;
} else {
cnt++;
}
}
for (int i = 0; i <= 1000000 && cnt > 0; i++) {
if (!vis[i]) {
av.add(i);
cnt--;
}
}
int idx = 0;
for (int i = 0; i < n; i++) {
if (ans[i] == -1) {
ans[i] = av.get(idx++);
}
}
for (int i = 0; i < n; i++) {
out.print(ans[i] + " ");
}
out.close();
}
static ArrayList<Integer> tree[];
static int one, more;
private static void BFS(int node, int depth, int parent) {
if (depth == 1) {
one++;
} else if (depth > 1) {
more++;
}
for (int child : tree[node]) {
if (child != parent) {
BFS(child, depth + 1, node);
}
}
}
private static void solve2() throws IOException {
int t = r.nextInt();
StringBuilder res = new StringBuilder();
while (t-- > 0) {
int n = r.nextInt();
int x = r.nextInt();
one = 0;
more = 0;
tree = new ArrayList[n + 1];
for (int i = 1; i <= n; i++) {
tree[i] = new ArrayList<Integer>();
}
for (int i = 1; i <= n - 1; i++) {
int u = r.nextInt();
int v = r.nextInt();
tree[u].add(v);
tree[v].add(u);
}
BFS(x, 0, -1);
String ans = "";
if (one <= 1) {
ans = "Ayush";
} else {
int chance = (more & 1) == 0 ? 0 : 1; // 0 --> Ayush || 1 --> Ashish //
if ((one & 1) == 0) {
chance = (chance + 1) % 2;
}
ans = (chance == 0) ? "Ayush" : "Ashish";
}
res.append(ans).append("\n");
}
out.print(res);
out.close();
}
private static boolean isOn(long num, int bit) {
if ((num & (1L << bit)) == 0) {
return false;
}
return true;
}
private static void solve3() throws IOException {
int t = r.nextInt();
StringBuilder res = new StringBuilder();
while (t-- > 0) {
long n = r.nextLong();
long ans = 2 * n - Long.bitCount(n);
res.append(ans).append("\n");
}
out.print(res);
out.close();
}
private static void solve4() throws IOException {
int t = r.nextInt();
StringBuilder res = new StringBuilder();
while (t-- > 0) {
int n = r.nextInt();
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
arr.add(r.nextInt());
}
Collections.sort(arr);
int ans = -1;
ArrayList<Integer> temp;
for (int i = 1; i <= 1024; i++) {
temp = new ArrayList<Integer>();
for (int j = 0; j < n; j++) {
temp.add((arr.get(j) ^ i));
}
Collections.sort(temp);
if (temp.equals(arr)) {
ans = i;
break;
}
}
res.append(ans).append("\n");
}
out.print(res);
out.close();
}
private static void solve5() throws IOException {
int t = r.nextInt();
StringBuilder res = new StringBuilder();
while (t-- > 0) {
int x1 = r.nextInt();
int y1 = r.nextInt();
int x2 = r.nextInt();
int y2 = r.nextInt();
res.append((long) (x2 - x1) * (y2 - y1) + 1).append("\n");
}
out.print(res);
out.close();
}
private static void solve6() throws IOException {
int n = r.nextInt();
int s = r.nextInt();
if ((s / n <= 1)) {
out.print("NO");
} else {
int last = (s / n) + (s % n);
int rem = (s / n);
out.println("YES");
for (int i = 1; i <= n - 1; i++) {
out.print(rem + " ");
}
out.println(last);
out.print(1);
}
out.close();
}
static class Pair implements Comparable<Pair> {
int fi, se;
public Pair(int fi, int se) {
this.fi = fi;
this.se = se;
}
@Override
public boolean equals(Object o) {
if (this == o)
return true;
if (o == null || getClass() != o.getClass())
return false;
Pair pair = (Pair) o;
return fi == pair.fi && se == pair.se;
}
@Override
public int hashCode() {
return Objects.hash(fi, se);
}
@Override
public int compareTo(Pair o) {
if (o.se - o.fi + 1 == this.se - this.fi + 1) {
return this.fi - o.fi;
}
return (o.se - o.fi + 1) - (this.se - this.fi + 1);
}
}
private static void solve7() throws IOException {
int t = r.nextInt();
while (t-- > 0) {
int n = r.nextInt();
PriorityQueue<Pair> pq = new PriorityQueue<>();
pq.add(new Pair(0, n - 1));
int cnt = 0;
int ans[] = new int[n];
while (!pq.isEmpty()) {
Pair p = pq.poll();
int lt = p.fi;
int rt = p.se;
int idx = (lt + rt) / 2;
ans[idx] = ++cnt;
if (lt < idx)
pq.add(new Pair(lt, idx - 1));
if (rt > idx)
pq.add(new Pair(idx + 1, rt));
}
for (int i = 0; i < n; i++) {
out.print(ans[i] + " ");
}
out.println();
}
out.close();
}
public static class Pair1 {
int x, y, prevX, prevY;
public Pair1(int a, int b, int c, int d) {
x = a;
y = b;
prevX = c;
prevY = d;
}
@Override
public int hashCode() {
return prevX + prevY + x + y;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (this.getClass() != obj.getClass())
return false;
Pair1 other = (Pair1) obj;
if (x == other.x && y == other.y && prevX == other.prevX && prevY == other.prevY)
return true;
if (x == other.prevX && y == other.prevY && prevX == other.x && prevY == other.y)
return true;
return false;
}
}
private static void solve8() throws IOException {
int t = r.nextInt();
while (t-- > 0) {
char str[] = r.next().toCharArray();
HashSet<Pair1> set = new HashSet<>();
long ans = 0;
int x = 0, y = 0;
for (int i = 0; i < str.length; i++) {
int fi = x, se = y;
if (str[i] == 'N')
y++;
else if (str[i] == 'S')
y--;
else if (str[i] == 'E')
x++;
else
x--;
int th = x, fo = y;
Pair1 p = new Pair1(fi, se, th, fo);
if (set.contains(p)) {
ans += 1;
} else {
ans += 5;
set.add(p);
}
}
out.println(ans);
}
out.close();
}
private static void solve9() throws IOException {
int n = r.nextInt();
int arr[] = new int[n];
int vis[] = new int[n + 1];
for (int i = 0; i < n; i++) {
arr[i] = r.nextInt();
vis[arr[i]] = 1;
}
TreeSet<Integer> av = new TreeSet<Integer>();
for (int i = 1; i <= n; i++) {
if (vis[i] == 0)
av.add(i);
}
for (int i = 0; i < n; i++) {
if (arr[i] == 0 && av.contains(i + 1)) {
if (av.higher(i + 1) != null) {
arr[i] = av.higher(i + 1);
av.remove(arr[i]);
} else {
arr[i] = av.lower(i + 1);
av.remove(arr[i]);
}
}
}
for (int i = 0; i < n; i++) {
if (arr[i] == 0) {
if (av.higher(i + 1) != null) {
arr[i] = av.higher(i + 1);
av.remove(arr[i]);
} else {
arr[i] = av.lower(i + 1);
av.remove(arr[i]);
}
}
out.print(arr[i] + " ");
}
out.close();
}
static class Day implements Comparable<Day> {
int a, b;
public Day(int a, int b) {
this.a = a;
this.b = b;
}
@Override
public int compareTo(Day o) {
if (a == o.a) {
return (b - o.b);
}
return (a - o.a);
}
}
private static void solve10() throws IOException {
int n = r.nextInt();
Day arr[] = new Day[n];
for (int i = 0; i < n; i++) {
arr[i] = new Day(r.nextInt(), r.nextInt());
}
Arrays.sort(arr);
int ans = Integer.MIN_VALUE;
for (int i = 0; i < n; i++) {
if (Math.min(arr[i].a, arr[i].b) >= ans) {
ans = Math.max(ans, Math.min(arr[i].a, arr[i].b));
} else {
ans = Math.max(ans, Math.max(arr[i].a, arr[i].b));
}
}
out.print(ans);
out.close();
}
static class Point implements Comparable<Point> {
int x, y;
public Point(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public int compareTo(Point o) {
if (x == o.x) {
return (y - o.y);
}
return (x - o.x);
}
}
private static void solve11() throws IOException {
int rebel = r.nextInt();
int bases = r.nextInt();
Point a1[] = new Point[rebel];
Point a2[] = new Point[bases];
for (int i = 0; i < rebel; i++) {
a1[i] = new Point(r.nextInt(), r.nextInt());
}
for (int i = 0; i < bases; i++) {
a2[i] = new Point(r.nextInt(), r.nextInt());
}
Arrays.sort(a1);
Arrays.sort(a2);
boolean flag = (a1.length == a2.length) ? true : false;
out.print(flag ? "Yes" : "No");
out.close();
}
private static void solve12() throws IOException {
int t = r.nextInt();
while (t-- > 0) {
int n = r.nextInt();
HashSet<Integer> set = new HashSet<Integer>();
set.add(0);
for (int i = 1; i * i <= n; i++) {
set.add(i);
set.add((n / i));
}
ArrayList<Integer> pal = new ArrayList<Integer>();
pal.addAll(set);
Collections.sort(pal);
out.println(set.size());
for (int ele : pal) {
out.print(ele + " ");
}
out.println();
}
out.close();
}
public static void main(String[] args) throws IOException {
// solve1();
// solve2();
// solve3();
// solve4();
// solve5();
// solve6();
// solve7();
// solve8();
// solve9();
// solve10();
// solve11();
solve12();
}
static class Reader {
final private int BUFFER_SIZE = 1 << 12;
boolean consume = false;
private byte[] buffer;
private int bufferPointer, bytesRead;
private boolean reachedEnd = false;
public Reader() {
buffer = new byte[BUFFER_SIZE];
bufferPointer = 0;
bytesRead = 0;
}
public boolean hasNext() {
return !reachedEnd;
}
private void fillBuffer() throws IOException {
bytesRead = System.in.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) {
buffer[0] = -1;
reachedEnd = true;
}
}
private void consumeSpaces() throws IOException {
while (read() <= ' ' && reachedEnd == false)
;
bufferPointer--;
}
private byte read() throws IOException {
if (bufferPointer == bytesRead) {
fillBuffer();
}
return buffer[bufferPointer++];
}
public String next() throws IOException {
StringBuilder sb = new StringBuilder();
consumeSpaces();
byte c = read();
do {
sb.append((char) c);
} while ((c = read()) > ' ');
if (consume) {
consumeSpaces();
}
;
if (sb.length() == 0) {
return null;
}
return sb.toString();
}
public String nextLine() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String str = br.readLine();
return str;
}
public int nextInt() throws IOException {
consumeSpaces();
int ret = 0;
byte c = read();
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (consume) {
consumeSpaces();
}
if (neg) {
return -ret;
}
return ret;
}
public long nextLong() throws IOException {
consumeSpaces();
long ret = 0;
byte c = read();
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10L + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (consume) {
consumeSpaces();
}
if (neg) {
return -ret;
}
return ret;
}
public double nextDouble() throws IOException {
consumeSpaces();
double ret = 0;
double div = 1;
byte c = read();
boolean neg = (c == '-');
if (neg) {
c = read();
}
do {
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (c == '.') {
while ((c = read()) >= '0' && c <= '9') {
ret += (c - '0') / (div *= 10);
}
}
if (consume) {
consumeSpaces();
}
if (neg) {
return -ret;
}
return ret;
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
public int[][] nextIntMatrix(int n, int m) throws IOException {
int[][] grid = new int[n][m];
for (int i = 0; i < n; i++) {
grid[i] = nextIntArray(m);
}
return grid;
}
public char[][] nextCharacterMatrix(int n) throws IOException {
char[][] a = new char[n][];
for (int i = 0; i < n; i++) {
a[i] = next().toCharArray();
}
return a;
}
public void close() throws IOException {
if (System.in == null) {
return;
} else {
System.in.close();
}
}
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 45c0b6afbf86f9c20e305ea199349282 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.util.*;
import java.io.*;
public class R603C2 {
public static void main(String[] args) {
JS scan = new JS();
PrintWriter out = new PrintWriter(System.out);
int t = scan.nextInt();
int max = 10_000_001;
while(t-->0) {
int n = scan.nextInt();
TreeSet<Integer> ans = new TreeSet<>();
for(int i = 1;i*i<=n;i++) {
int div = n/i;
ans.add(div);
}
TreeSet<Integer> ans1 = new TreeSet<>();
for(Integer curr:ans) {
int div = n/curr;
ans1.add(div);
}
for(Integer curr:ans1) {
ans.add(curr);
}
out.println(ans.size()+1);
out.print(0+" ");
for(Integer curr:ans) {
out.print(curr+" ");
}
out.println();
//definitely binary search
}
out.flush();
out.close();
/*
15
0 1 2 3 5 7 15
*/
}
static class JS{
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public JS() {
in = new BufferedInputStream(System.in, BS);
}
public JS(String s) throws FileNotFoundException {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | ed6358260253fb31fb229d673b43e285 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.*;
import java.util.*;
public class codeforces {
static class pair{
int x;
int y;
public pair(int x,int y){
this.x=x;
this.y=y;
}
}
public static long sqrt(long x){
long s=0,e=x,root=0;
while(s<=e){
long mid=(s+e)/2;
if(mid*mid==x)
return mid;
if(mid*mid<x)
{
root=mid;
s=mid+1;
}
else
e=mid-1;
}
return root;
}
public static long power(int a,int b){
if(a==0 || b==0)
return 1;
long ans=power(a,b/2);
ans*=ans;
if(b%2==1)
ans*=a;
return ans;
}
public static int max(int a,int b){
return Math.max(a,b);
}
public static int min(int a,int b){
return Math.min(a,b);
}
public static void main(String args[] ) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuilder str=new StringBuilder();
PrintWriter out=new PrintWriter(System.out);
// String[] l=br.readLine().split(" ");
int t=Integer.parseInt(br.readLine());
while(t-->0){
long n=Long.parseLong(br.readLine());
long root=sqrt(n);
long count=2*root;
if(root!=(n/root))
count++;
str.append(count+"\n");
for(long i=0;i<=root;i++)
str.append(i+" ");
if(root!=(n/root))
str.append(n/root+" ");
for(long i=root-1;i>=1;i--)
str.append(n/i+" ");
str.append("\n");
}
out.print(str);
out.close();
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 91e2e1aec107e53316e048a7b06744c7 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.*;
import java.util.*;
public class codeforces {
static class pair{
int x;
int y;
public pair(int x,int y){
this.x=x;
this.y=y;
}
}
public static long power(int a,int b){
if(a==0 || b==0)
return 1;
long ans=power(a,b/2);
ans*=ans;
if(b%2==1)
ans*=a;
return ans;
}
public static int max(int a,int b){
return Math.max(a,b);
}
public static int min(int a,int b){
return Math.min(a,b);
}
public static void main(String args[] ) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuilder str=new StringBuilder();
PrintWriter out=new PrintWriter(System.out);
// String[] l=br.readLine().split(" ");
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
int root=(int) Math.sqrt(n);
int count=2*root;
if(root!=(n/root))
count++;
str.append(count+"\n");
for(int i=0;i<=root;i++)
str.append(i+" ");
if(root!=(n/root))
str.append(n/root+" ");
for(int i=root-1;i>=1;i--)
str.append(n/i+" ");
str.append("\n");
}
out.print(str);
out.close();
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | e6a240f79d5a98c8001ee686d36341a1 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.*;
import java.util.*;
public class codeforces {
static class pair{
int x;
int y;
public pair(int x,int y){
this.x=x;
this.y=y;
}
}
public static long power(int a,int b){
if(a==0 || b==0)
return 1;
long ans=power(a,b/2);
ans*=ans;
if(b%2==1)
ans*=a;
return ans;
}
public static int max(int a,int b){
return Math.max(a,b);
}
public static int min(int a,int b){
return Math.min(a,b);
}
public static void main(String args[] ) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringBuilder str=new StringBuilder();
PrintWriter out=new PrintWriter(System.out);
// String[] l=br.readLine().split(" ");
int t=Integer.parseInt(br.readLine());
while(t-->0){
int n=Integer.parseInt(br.readLine());
int root=(int) Math.sqrt(n);
LinkedHashSet<Integer> a=new LinkedHashSet<>();
for(int i=0;i<=root;i++)
a.add(i);
for(int i=root;i>=1;i--)
a.add(n/i);
str.append(a.size()+"\n");
for(int x:a)
str.append(x+" ");
str.append("\n");
}
out.print(str);
out.close();
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 9c8e2e901e8739d3087e12ea7b7f9fd6 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
public class Rextester{
public static void main(String[] args)throws IOException{
int max=100000;
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = new Integer(br.readLine());
StringBuffer sb = new StringBuffer();
while(t-->0){
int n = new Integer(br.readLine());
int[] array = new int[max];
int p=1,q=1;
while(p<=n&&(n/p)>=(n/(n/p))){
int x = n/p;
int z = n/(n/p);
if(x==z){
array[q++]=x;
}
else{
array[q++]=x;
array[q++]=z;
}
p++;
}
sb.append(q).append("\n");
Arrays.sort(array);
for(int i=max-q;i<max;i++){
sb.append(array[i]).append(" ");
}
sb.append("\n");
}
System.out.println(sb);
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | d9452583a4522642615fa3caf29addd5 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.*;
import java.util.*;
@SuppressWarnings("unused")
public class Main {
FastScanner in;
PrintWriter out;
int MOD = (int)1e9+7;
long ceil(long a, long b){return (a + b - 1) / b;}
long gcd(long a, long b){return b == 0 ? a : gcd(b, a % b);}
long lcm(long a, long b){return a / gcd(a, b) * b;}
void solve() {
int t = in.nextInt();
for(int i = 0; i < t; i++){
solveMain();
}
}
void solveMain(){
int n = in.nextInt();
TreeSet<Integer> set = new TreeSet<>();
for(int i = 0; i <= Math.sqrt(n); i++) set.add(i);
for(int i = 1; i <= Math.sqrt(n); i++) set.add(n / i);
out.println(set.size());
for(int e : set) out.print(e + " ");
out.println();
}
public static void main(String[] args) {
new Main().m();
}
private void m() {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
/*
try {
String path = "output.txt";
out = new PrintWriter(new BufferedWriter(new FileWriter(new File(path))));
}catch (IOException e){
e.printStackTrace();
}
*/
solve();
out.flush();
in.close();
out.close();
}
static class FastScanner {
private Reader input;
public FastScanner() {this(System.in);}
public FastScanner(InputStream stream) {this.input = new BufferedReader(new InputStreamReader(stream));}
public void close() {
try {
this.input.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public int nextInt() {return (int) nextLong();}
public long nextLong() {
try {
int sign = 1;
int b = input.read();
while ((b < '0' || '9' < b) && b != '-' && b != '+') {
b = input.read();
}
if (b == '-') {
sign = -1;
b = input.read();
} else if (b == '+') {
b = input.read();
}
long ret = b - '0';
while (true) {
b = input.read();
if (b < '0' || '9' < b) return ret * sign;
ret *= 10;
ret += b - '0';
}
} catch (IOException e) {
e.printStackTrace();
return -1;
}
}
public double nextDouble() {
try {
double sign = 1;
int b = input.read();
while ((b < '0' || '9' < b) && b != '-' && b != '+') {
b = input.read();
}
if (b == '-') {
sign = -1;
b = input.read();
} else if (b == '+') {
b = input.read();
}
double ret = b - '0';
while (true) {
b = input.read();
if (b < '0' || '9' < b) break;
ret *= 10;
ret += b - '0';
}
if (b != '.') return sign * ret;
double div = 1;
b = input.read();
while ('0' <= b && b <= '9') {
ret *= 10;
ret += b - '0';
div *= 10;
b = input.read();
}
return sign * ret / div;
} catch (IOException e) {
e.printStackTrace();
return Double.NaN;
}
}
public char nextChar() {
try {
int b = input.read();
while (Character.isWhitespace(b)) {
b = input.read();
}
return (char) b;
} catch (IOException e) {
e.printStackTrace();
return 0;
}
}
public String nextStr() {
try {
StringBuilder sb = new StringBuilder();
int b = input.read();
while (Character.isWhitespace(b)) {
b = input.read();
}
while (b != -1 && !Character.isWhitespace(b)) {
sb.append((char) b);
b = input.read();
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
public String nextLine() {
try {
StringBuilder sb = new StringBuilder();
int b = input.read();
while (b != -1 && b != '\n') {
sb.append((char) b);
b = input.read();
}
return sb.toString();
} catch (IOException e) {
e.printStackTrace();
return "";
}
}
public int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
public int[] nextIntArrayDec(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt() - 1;
}
return res;
}
public int[] nextIntArray1Index(int n) {
int[] res = new int[n + 1];
for (int i = 0; i < n; i++) {
res[i + 1] = nextInt();
}
return res;
}
public long[] nextLongArray(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
public long[] nextLongArrayDec(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong() - 1;
}
return res;
}
public long[] nextLongArray1Index(int n) {
long[] res = new long[n + 1];
for (int i = 0; i < n; i++) {
res[i + 1] = nextLong();
}
return res;
}
public double[] nextDoubleArray(int n) {
double[] res = new double[n];
for (int i = 0; i < n; i++) {
res[i] = nextDouble();
}
return res;
}
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | e0de90ca8413d7f3ce2e9fb18d2d8511 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.*;
import java.util.LinkedList;
public class EveryoneIsAWinner2 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int wheel = Integer.parseInt(br.readLine());
while (wheel-- > 0) {
int n = Integer.parseInt(br.readLine());
LinkedList<Integer> stack = new LinkedList<Integer>();
int threshold = (int) Math.sqrt(n);
int divide = 1;
while (n / divide > threshold) {
stack.push(n / divide);
divide++;
}
out.println(stack.size() + threshold + 1);
for (int i = 0; i <= threshold; i++) {
out.print(i + " ");
}
while (!stack.isEmpty()) {
out.print(stack.pop() + " ");
}
out.println();
}
out.flush();
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | fb7daea2902ac4066cf3f9d6c350d4e0 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes |
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
//import java.math.*;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
static int MD= 1000000007 ;
public static void main(String[] args) throws IOException {
OutputStream outputStream = System.out;
InputReader in = new InputReader();
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) throws IOException {
int t=in.nextInt();
while(t-->0) {
int n=in.nextInt();
ArrayList<Integer> ans=new ArrayList<>();
for(int i=1;;) {
ans.add(n/i);
if(n/i==0) {
break;
}
i=(n/(n/i))+1;
}
int l=ans.size();
out.println(l);
for(int i=l-1;i>=0;i--) {
out.print(ans.get(i)+" ");
}
out.println();
}
}
}
static int[] dijkstra(int graph[][], int src,int V) {
int[] dist=new int[V];
// Arrays.fill(dist,-1);
Boolean[] vis =new Boolean[V];
for(int i=0;i<V;i++) {
dist[i]=Integer.MAX_VALUE;
vis[i]=false;
}
dist[src]=0;
for(int i=0;i<V;i++) {
int min=Integer.MAX_VALUE , minIndex=-1;
for(int j=0;j<V;j++) {
if(vis[j]==false && dist[j]<min) {
min=dist[j];
minIndex=j;
}
}
int u=minIndex;
vis[i]=true;
for(int j=0;j<V;j++) {
if(vis[j]==false && graph[u][j]!=0 && dist[u]+graph[u][j]<dist[j]) {
dist[j]=dist[u]+graph[u][j];
}
}
}
return dist;
}
static class InputReader
{
BufferedReader br;
StringTokenizer st;
public InputReader()
{
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;
}
}
static void sort(int[] a, int low, int high)
{
int N = high - low;
if (N <= 1)
return;
int mid = low + N/2;
sort(a, low, mid);
sort(a, mid, high);
int[] temp = new int[N];
int i = low, j = mid;
for (int k = 0; k < N; k++)
{
if (i == mid)
temp[k] = a[j++];
else if (j == high)
temp[k] = a[i++];
else if (a[j]<a[i])
temp[k] = a[j++];
else
temp[k] = a[i++];
}
for (int k = 0; k < N; k++)
a[low + k] = temp[k];
}
public static long fexp(long x, long y, long 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 int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static long lgcd(long a, long b)
{
if (b == 0)
return a;
return lgcd(b, a % b);
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | c4851e9f620b49b982c2fd35e7ce086f | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class hello1
{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;
}
}
static String sum (String s)
{
String s1 = "";
if(s.contains("a"))
s1+="a";
if(s.contains("e"))
s1+="e";
if(s.contains("i"))
s1+="i";
if(s.contains("o"))
s1+="o";
if(s.contains("u"))
s1+="u";
return s1;
}
public static HashMap<String, Integer> sortByValue(HashMap<String, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<String, Integer> > list =
new LinkedList<Map.Entry<String, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<String, Integer> >() {
public int compare(Map.Entry<String, Integer> o1,
Map.Entry<String, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<String, Integer> temp = new LinkedHashMap<String, Integer>();
for (Map.Entry<String, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
public static void main(String args[])
{
FastReader sc = new FastReader();
int t = sc.nextInt();
StringBuilder sb = new StringBuilder();
while(t-->0){
long n = sc.nextLong();
TreeSet<Long> ts = new TreeSet<>();
for(long i=1;i<=Math.sqrt(n);i++){
ts.add(i);
ts.add(n/i);
}
sb.append((ts.size()+1)+"\n");
sb.append(0+" ");
for(long num : ts){
sb.append(num+" ");
}
sb.append("\n");
}
System.out.println(sb);
}
static boolean find(int a[],int A,int B)
{
if( root(a,A)==root(a,B) ) //if A and B have the same root, it means that they are connected.
return true;
else
return false;
}
static void union(int Arr[ ],int size[ ],int A,int B)
{
int root_A = root(Arr,A);
int root_B = root(Arr,B);
if(root_A == root_B)
{
return;
}
if(size[root_A] < size[root_B] )
{
Arr[ root_A ] = Arr[root_B];
size[root_B] += size[root_A];
size[root_A] = 0;
}
else
{
Arr[ root_B ] = Arr[root_A];
size[root_A] += size[root_B];
size[root_B] = 0;
}
}
static int root (int Arr[ ] ,int i)
{
while(Arr[ i ] != i)
{
i = Arr[ i ];
}
return i;
}
}
class Pair
{
int a;
int b;
Pair(int a,int b)
{
this.a=a;
this.b=b;
}
} | Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 3aa45a43a40ce871083dccc055d98097 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Contest {
static ArrayList<Integer> prime=new ArrayList();
/*public static void PrimeArray(int n)
{
for(int i=2;i<=n;i++)
{
boolean b=isprime(i);
if(b)
{
prime.add(i);
}
}
}
*/
public static long gcd(long n1, long n2)
{
if (n2 != 0)
return gcd(n2, n1 % n2);
else
return n1;
}
//////////////////
public static class trie {
int x,y,z;
trie (int x,int y,int z){
this.x=x;
this.y=y;
this.z=z;
}
}
public static class pair {
int x,y;
pair (int x,int y){
this.x=x;
this.y=y;
}
public boolean equals(Object o) {
if(o instanceof pair) {
pair p = (pair)o;
return (x == p.x && y == p.y);
}
return false;
}
public int hashCode() {
return (Integer.hashCode(x) * 3 + Integer.hashCode(y) );
}
}
//////////////////
static class Cmp implements Comparator<pair>
{
public int compare(pair a,pair b)
{
if(a.x==b.x)
{
return(a.y-b.y);
}
return(a.x-b.x);
}
}
//////////////////
//////////////////
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
//////////////////////
//public static ArrayList<Integer> a=new ArrayList();
public static long Divisors(long n)
{
// Note that this loop runs till square root
long sum=0;
for (long i=1; i*i<=(n); i++)
{
if (n%i == 0)
{
// If divisors are equal, print only one
if (n/i == i)
{
// printf("%d ", i);
sum=sum+1;
// a.add(i);
}
else // Otherwise print both
{
//printf("%d %d ", i, n/i);
sum=sum+2;
// a.add(i);
//a.add(n/i);
}
}
}
return(sum);
}
/////////////////////
public static long SmallestPrimeDivisor(long n)
{
// if divisible by 2
if (n % 2 == 0)
return 2;
// iterate from 3 to sqrt(n)
for (long i = 3; i * i <= n; i += 2) {
if (n % i == 0)
return i;
}
return n;
}
////////////////////
public static boolean isprime(long n)
{
int count=0;
for(long i=2;i*i<=n;i++)
{
if(n%i==0)
{
return(false);
}
}
return(true);
}
//////////////////////
// (x^n)%1000000007
public static long modularExponentiation(long x,long n,long M)
{
if(n==0)
return 1;
else if(n%2 == 0) //n is even
return modularExponentiation((x*x)%M,n/2,M);
else //n is odd
return (x*modularExponentiation((x*x)%M,(n-1)/2,M))%M;
}
public static long nCr(int n,int k)
{
if(k==0)
return 1;
return(n*nCr(n-1,k-1)/k);
}
public static long factorial(int n)
{
if(n==1)
return 1;
return((n%1000000007)*factorial(n-1));
}
public static void debug(Object...o) {
System.out.println( Arrays.deepToString(o) );
}
public static void main(String []args) throws Exception
{
FastReader in =new FastReader();
PrintWriter pw = new PrintWriter(System.out);
//Scanner sc=new Scanner(System.in)
// int x=in.nextInt();
int t=in.nextInt();
while(t-->0)
{
int n=in.nextInt();
TreeSet<Integer> h=new TreeSet();
int x=(int)Math.sqrt(n);
for(int i=0;i<=x;i++)
{
h.add(i);
}
for(int i=1;i<=x;i++)
{
h.add(n/i);
}
pw.println(h.size());
for(Integer y : h)
{
pw.print(y+" ");
}
pw.println("");
}
pw.flush();
pw.close();
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | e2317ea2147bf79ebc77cf4939679f04 | train_003.jsonl | 1575038100 | On the well-known testing system MathForces, a draw of $$$n$$$ rating units is arranged. The rating will be distributed according to the following algorithm: if $$$k$$$ participants take part in this event, then the $$$n$$$ rating is evenly distributed between them and rounded to the nearest lower integer, At the end of the drawing, an unused rating may remainΒ β it is not given to any of the participants.For example, if $$$n = 5$$$ and $$$k = 3$$$, then each participant will recieve an $$$1$$$ rating unit, and also $$$2$$$ rating units will remain unused. If $$$n = 5$$$, and $$$k = 6$$$, then none of the participants will increase their rating.Vasya participates in this rating draw but does not have information on the total number of participants in this event. Therefore, he wants to know what different values of the rating increment are possible to get as a result of this draw and asks you for help.For example, if $$$n=5$$$, then the answer is equal to the sequence $$$0, 1, 2, 5$$$. Each of the sequence values (and only them) can be obtained as $$$\lfloor n/k \rfloor$$$ for some positive integer $$$k$$$ (where $$$\lfloor x \rfloor$$$ is the value of $$$x$$$ rounded down): $$$0 = \lfloor 5/7 \rfloor$$$, $$$1 = \lfloor 5/5 \rfloor$$$, $$$2 = \lfloor 5/2 \rfloor$$$, $$$5 = \lfloor 5/1 \rfloor$$$.Write a program that, for a given $$$n$$$, finds a sequence of all possible rating increments. | 256 megabytes | //package codeforces.div2;//package codeforces.div3;
import java.io.*;
import java.util.*;
public class EveryoneisaWinner {
static PrintWriter pw = new PrintWriter(System.out);
static public void print(Integer n) {
ArrayList<Integer> list = new ArrayList<>();
list.add(0);
//HashMap<Integer, Integer> map = new HashMap<>();
if(n>=1) {
list.add(1);
}
//HashMap<Integer, Integer> m = new HashMap<>();
for(int i = 2; i*i <= n ; i++) {
int diff = n/i;
list.add(i);
if(diff!=i) {
list.add(diff);
}
}
Collections.sort(list);
if(n!=1) {
list.add(n);
}
pw.println(list.size());
for(int i = 0; i < list.size(); i++) {
pw.print(list.get(i) + " ");
}
pw.println();
}
public static void main(String []args) {
FastReader sc = new FastReader();
int n = sc.nextInt();
while (n > 0) {
Integer m = sc.nextInt();
print(m);
n--;
}
pw.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(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 str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["4\n5\n11\n1\n3"] | 1 second | ["4\n0 1 2 5 \n6\n0 1 2 3 5 11 \n2\n0 1 \n3\n0 1 3"] | null | Java 8 | standard input | [
"binary search",
"meet-in-the-middle",
"number theory",
"math"
] | 1fea14a5c21bf301981656cbe015864d | The first line contains integer number $$$t$$$ ($$$1 \le t \le 10$$$)Β β the number of test cases in the input. Then $$$t$$$ test cases follow. Each line contains an integer $$$n$$$ ($$$1 \le n \le 10^9$$$)Β β the total number of the rating units being drawn. | 1,400 | Output the answers for each of $$$t$$$ test cases. Each answer should be contained in two lines. In the first line print a single integer $$$m$$$Β β the number of different rating increment values that Vasya can get. In the following line print $$$m$$$ integers in ascending orderΒ β the values of possible rating increments. | standard output | |
PASSED | 7b542c305bad5053de6a93e197090625 | train_003.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size nβΓβn and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly nΒ·(nβ-β1)β/β2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
public class CODEFORCES
{
private InputStream is;
private PrintWriter out;
class q implements Comparable<q>
{
int l, d, t;
long ans;
q(int a, int b, int ti)
{
l = a;
this.d = b;
ans = 0;
t = ti;
}
@Override
public int compareTo(q o)
{
return Integer.compare(l, o.l) != 0 ? Integer.compare(l, o.l)
: (Integer.compare(d, o.d) != 0 ? Integer.compare(d, o.d) : Integer.compare(t, o.t));
}
}
class qi
{
int l, d, r, u;
long ans = 0;
q b[] = new q[8];
qi(int a, int b, int c, int d)
{
l = a;
this.d = b;
r = c;
u = d;
ini();
}
void ini()
{
b[0] = new q(r, u, 1);
b[1] = new q(r, d - 1, 1);
b[2] = new q(l - 1, u, 1);
b[3] = new q(l - 1, d - 1, 1);
b[4] = new q(n, u, 1);
b[5] = new q(n, d - 1, 1);
b[6] = new q(r, n, 1);
b[7] = new q(l - 1, n, 1);
}
void ans()
{
long tmp = n - b[4].ans;
ans += comb(tmp) + comb(b[5].ans) + comb(n - b[6].ans) + comb(b[7].ans);
tmp = n + b[0].ans - b[6].ans - b[4].ans;
ans -= comb(tmp);
ans -= comb(b[5].ans - b[1].ans);
ans -= comb(b[7].ans - b[2].ans);
ans -= comb(b[3].ans);
ans = comb(n) - ans;
}
}
long comb(long p)
{
return (p * (p - 1)) >> 1;
}
int arr[], m, n;
void update(int x)
{
x++;
while (x <= m)
{
arr[x]++;
x += (x & -x);
}
}
int ans(int x)
{
int an = 0;
x++;
while (x > 0)
{
an += arr[x];
x -= (x & -x);
}
return an;
}
void solve()
{
n = ni();
int q = ni();
int l = n + 8 * q;
q a[] = new q[l];
for (int i = 1; i <= n; i++)
a[i - 1] = new q(i, ni(), 0);
qi b[] = new qi[q];
for (int i = 0; i < q; i++)
{
b[i] = new qi(ni(), ni(), ni(), ni());
a[n + 8 * i] = b[i].b[0];
a[n + 8 * i + 1] = b[i].b[1];
a[n + 8 * i + 2] = b[i].b[2];
a[n + 8 * i + 3] = b[i].b[3];
a[n + 8 * i + 4] = b[i].b[4];
a[n + 8 * i + 5] = b[i].b[5];
a[n + 8 * i + 6] = b[i].b[6];
a[n + 8 * i + 7] = b[i].b[7];
}
Arrays.sort(a);
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int bb[] = new int[l];
for (int i = 0; i < bb.length; i++)
bb[i] = a[i].d;
Arrays.sort(bb);
int cnt = 0;
for (int i = 0; i < l; i++)
{
if (!map.containsKey(bb[i]))
map.put(bb[i], cnt++);
}
arr = new int[cnt + 1];
m = cnt;
for (int i = 0; i < bb.length; i++)
{
if (a[i].t == 1)
a[i].ans = ans(map.get(a[i].d));
else
{
update(map.get(a[i].d));
a[i].ans = ans(map.get(a[i].d));
}
}
for (int i = 0; i < q; i++)
{
b[i].ans();
out.println(b[i].ans);
}
}
void soln()
{
is = System.in;
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis() - s + "ms");
}
public static void main(String[] args)
{
new CODEFORCES().soln();
}
// To Get Input
// Some Buffer Methods
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 && !(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 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 boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o)
{
if (!oj)
System.out.println(Arrays.deepToString(o));
}
} | Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2ββ€βnββ€β200β000, 1ββ€βqββ€β200β000)Β β the size of the grid and the number of query rectangles. The second line contains n integers p1,βp2,β...,βpn, separated by spaces (1ββ€βpiββ€βn, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l,βd,βr,βu (1ββ€βlββ€βrββ€βn, 1ββ€βdββ€βuββ€βn), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | 6b82df7f85ded0dc9f2ff1bda7f98d7e | train_003.jsonl | 1504702500 | Ilya is sitting in a waiting area of Metropolis airport and is bored of looking at time table that shows again and again that his plane is delayed. So he took out a sheet of paper and decided to solve some problems.First Ilya has drawn a grid of size nβΓβn and marked n squares on it, such that no two marked squares share the same row or the same column. He calls a rectangle on a grid with sides parallel to grid sides beautiful if exactly two of its corner squares are marked. There are exactly nΒ·(nβ-β1)β/β2 beautiful rectangles.Ilya has chosen q query rectangles on a grid with sides parallel to grid sides (not necessarily beautiful ones), and for each of those rectangles he wants to find its beauty degree. Beauty degree of a rectangle is the number of beautiful rectangles that share at least one square with the given one.Now Ilya thinks that he might not have enough time to solve the problem till the departure of his flight. You are given the description of marked cells and the query rectangles, help Ilya find the beauty degree of each of the query rectangles. | 512 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
import java.lang.*;
public class Main {
private static FastReader sc = new FastReader(System.in);
private static OutputWriter out = new OutputWriter(System.out);
private static HashMap<Integer, ArrayList<Integer>> leftIdx, rightIdx;
public static void main(String[] args) throws Exception {
int n = sc.nextInt();
int q = sc.nextInt();
int[] row = new int[n + 1];
for (int i = 1; i <= n; i++) {
row[i] = sc.nextInt();
}
SegmentTree st = new SegmentTree(n);
long[] ans = new long[q];
int[] up = new int[q];
int[] down = new int[q];
leftIdx = new HashMap<>();
rightIdx = new HashMap<>();
for (int i = 0; i <= n; i++) {
leftIdx.put(i, new ArrayList<>());
rightIdx.put(i, new ArrayList<>());
}
for (int i = 0; i < q; i++) {
int l = sc.nextInt();
int d = sc.nextInt();
int r = sc.nextInt();
int u = sc.nextInt();
ans[i] = (((long) (l - 1)) * (l - 2)) / 2 + (((long) (d - 1)) * (d - 2)) / 2 + (((long) (n - r)) * (n - 1 - r)) / 2 + (((long) (n - u)) * (n - 1 - u)) / 2;
leftIdx.get(l - 1).add(i);
rightIdx.get(r).add(i);
up[i] = u;
down[i] = d;
}
long[] upLeft = new long[q];
long[] downLeft = new long[q];
long[] upRight = new long[q];
long[] downRight = new long[q];
for (int i = 1; i <= n; i++) {
st.update(row[i]);
for (int idx : leftIdx.get(i)) {
long points = st.query(1, down[idx] - 1);
downLeft[idx] += points;
points = st.query(up[idx] + 1, n);
upLeft[idx] += points;
}
for (int idx : rightIdx.get(i)) {
long points = st.query(1, down[idx] - 1);
downRight[idx] -= points;
points = st.query(up[idx] + 1, n);
upRight[idx] -= points;
}
if (i == n) {
for (int idx = 0; idx < q; idx++) {
long points = st.query(1, down[idx] - 1);
downRight[idx] += points;
points = st.query(up[idx] + 1, n);
upRight[idx] += points;
}
}
}
for (int i = 0; i < q; i++) {
ans[i] -= (upLeft[i] * (upLeft[i] - 1)) / 2;
ans[i] -= (upRight[i] * (upRight[i] - 1)) / 2;
ans[i] -= (downLeft[i] * (downLeft[i] - 1)) / 2;
ans[i] -= (downRight[i] * (downRight[i] - 1)) / 2;
out.printLine((((long) n) * (n - 1)) / 2 - ans[i]);
}
out.close();
}
}
class SegmentTree {
int[] st;
int n;
public SegmentTree(int n) {
this.n = n;
st = new int[4 * n + 1];
}
public void update(int pos) {
update(1, 1, n, pos);
}
private void update(int node, int sl, int sh, int pos) {
if (sl == sh) {
st[node] = 1;
return;
}
int mid = (sl + sh) / 2;
if (pos <= mid) update(2 * node, sl, mid, pos);
else update(2 * node + 1, mid + 1, sh, pos);
st[node] = st[2 * node] + st[2 * node + 1];
}
public int query(int ql, int qh) {
return query(1, 1, n, ql, qh);
}
private int query(int node, int sl, int sh, int ql, int qh) {
if (qh < sl || ql > sh || ql > qh) return 0;
if (ql <= sl && qh >= sh) return st[node];
int mid = (sl + sh) / 2;
return query(2 * node, sl, mid, ql, qh) + query(2 * node + 1, mid + 1, sh, ql, qh);
}
}
class FastReader {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastReader(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 peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
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 == ',') {
c = read();
}
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 String nextString() {
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 isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String nextLine() {
String s = readLine0();
while (s.trim().length() == 0)
s = readLine0();
return s;
}
public String nextLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return nextLine();
} else {
return readLine0();
}
}
public BigInteger nextBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
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 boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1)
read();
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
} | Java | ["2 3\n1 2\n1 1 1 1\n1 1 1 2\n1 1 2 2", "4 2\n1 3 2 4\n4 1 4 4\n1 1 2 3"] | 2 seconds | ["1\n1\n1", "3\n5"] | NoteThe first sample test has one beautiful rectangle that occupies the whole grid, therefore the answer to any query is 1.In the second sample test the first query rectangle intersects 3 beautiful rectangles, as shown on the picture below: There are 5 beautiful rectangles that intersect the second query rectangle, as shown on the following picture: | Java 8 | standard input | [
"data structures"
] | bc834c0aa602a8ba789b676affb0b33a | The first line of input contains two integers n and q (2ββ€βnββ€β200β000, 1ββ€βqββ€β200β000)Β β the size of the grid and the number of query rectangles. The second line contains n integers p1,βp2,β...,βpn, separated by spaces (1ββ€βpiββ€βn, all pi are different), they specify grid squares marked by Ilya: in column i he has marked a square at row pi, rows are numbered from 1 to n, bottom to top, columns are numbered from 1 to n, left to right. The following q lines describe query rectangles. Each rectangle is described by four integers: l,βd,βr,βu (1ββ€βlββ€βrββ€βn, 1ββ€βdββ€βuββ€βn), here l and r are the leftmost and the rightmost columns of the rectangle, d and u the bottommost and the topmost rows of the rectangle. | 2,100 | For each query rectangle output its beauty degree on a separate line. | standard output | |
PASSED | ca34c6d51ac3826c0590a7ad70fbb95b | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes |
import java.util.*;
public class B
{
public static void main(String[] args)
{
new B(new Scanner(System.in));
}
double EPS = 1e-15;
line[] rs;
void swap(int i, int j)
{
line tmp = rs[i];
rs[i] = rs[j];
rs[j] = tmp;
}
boolean resolve()
{
for (int i=0; i<3; i++)
if (rs[i].e1.eq(rs[i].e2))
return false;
for (int i=0; i<3; i++)
for (int j=i+1; j<3; j++)
{
vect cp = rs[i].comm(rs[j]);
if (cp != null)
{
swap(0, i);
swap(1, j);
if (cp.x == 1)
rs[0].flip();
if (cp.y == 1)
rs[1].flip();
return true;
}
}
return false;
}
boolean check2()
{
vect v1 = rs[0].getVect();
vect v2 = rs[1].getVect();
if ((v1.dot(v2) >= 0)&&(v1.cross(v2) != 0))
return true;
return false;
}
boolean checkRatio()
{
//System.out.printf("START TEST 3%n");
if (!rs[0].onLine(rs[2].e1))
{
//System.out.printf("FAILED MATCH ONE%n");
// Try the other way
rs[2].flip();
if (!rs[0].onLine(rs[2].e1))
{
//System.out.printf("FAILED MATCH ONE%n");
return false;
}
}
//System.out.printf("TRYING LINE 1%n");
if (!rs[1].onLine(rs[2].e2))
return false;
double u = rs[0].scaleProj(rs[2].e1);
double v = 1-u;
double p = u/v;
double q = v/u;
//System.out.printf("TESTING RATIO 1 -> %.6f %.6f%n", p, q);
if ((u < 0.1)||(u > 0.9))
return false;
if ((4*u < (v-EPS))||(4*v < (u-EPS)))
return false;
u = rs[1].scaleProj(rs[2].e2);
v = 1-u;
//System.out.printf("TESTING RATIO 2 -> %.6f %.6f%n", p, q);
if ((u < 0.1)||(u > 0.9))
return false;
//if ((p < (0.25-EPS))||(q < (0.25-EPS)))
if ((4*u < (v-EPS))||(4*v < (u-EPS)))
return false;
return true;
}
boolean test()
{
//System.out.printf("TESTING%n");
if (!resolve())
return false;
//System.out.printf("PASS 1%n");
if (!check2())
return false;
//System.out.printf("PASS 2%n");
return checkRatio();
}
public B(Scanner in)
{
int T = in.nextInt();
while (T-->0)
{
rs = new line[3];
for (int i=0; i<3; i++)
{
vect a = new vect(in.nextLong(), in.nextLong());
vect b = new vect(in.nextLong(), in.nextLong());
rs[i] = new line(a, b);
}
String res = "NO";
if (test())
res = "YES";
System.out.println(res);
}
}
}
class line
{
vect e1, e2;
public line(vect a, vect b)
{
e1 = a; e2 = b;
}
vect comm(line rhs)
{
if (e1.eq(rhs.e1))
return new vect(0, 0);
if (e2.eq(rhs.e1))
return new vect(1, 0);
if (e2.eq(rhs.e2))
return new vect(1, 1);
if (e1.eq(rhs.e2))
return new vect(0, 1);
return null;
}
void flip()
{
vect tmp = e1;
e1 = e2;
e2 = tmp;
}
boolean onLine(vect rhs)
{
vect v1 = getVect();
vect v2 = rhs.sub(e1);
if (v1.cross(v2) == 0)
return true;
return false;
}
vect getVect()
{
return e2.sub(e1);
}
double scaleProj(vect p)
{
vect v1 = getVect();
vect v2 = p.sub(e1);
return (v1.dot(v2)*1.0)/v1.mag2();
}
public String toString()
{
return e1+"<->"+e2;
}
}
class vect
{
long x, y;
public vect(long i, long j)
{
x=i; y=j;
}
boolean eq(vect rhs)
{
if (x != rhs.x) return false;
return (y == rhs.y);
}
vect add(vect rhs)
{
return new vect(x+rhs.x, y+rhs.y);
}
vect sub(vect rhs)
{
return new vect(x-rhs.x, y-rhs.y);
}
long dot(vect rhs)
{
return x*rhs.x+y*rhs.y;
}
long cross(vect rhs)
{
return x*rhs.y-y*rhs.x;
}
long mag2()
{
return x*x+y*y;
}
public String toString()
{
return String.format("(%d, %d)", x, y);
}
}
| Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | c6bd79ae1909336a8e36412eeb8b50f2 | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class B {
public static boolean intersect(int x, int y, long[][] L) {
boolean can = false;
long v1X = 0, v1Y = 0, v2X = 0, v2Y = 0;
for (int i = 0; i < 4; i += 2)
for (int j = 0; j < 4; j += 2) {
if (L[x][i] == L[y][j] && L[x][i + 1] == L[y][j + 1]) {
can = true;
v1X = L[x][(i + 2) % 4] - L[x][i];
v1Y = L[x][(i + 3) % 4] - L[x][i + 1];
v2X = L[y][(j + 2) % 4] - L[y][j];
v2Y = L[y][(j + 3) % 4] - L[y][j + 1];
}
}
if (!can)
return false;
long dot = v1X * v2X + v1Y * v2Y;
return dot >= 0;
}
public static boolean split(int x, int y, long[][] L) {
for (int i = 0; i < 4; i += 2) {
if (L[y][i] >= Math.min(L[x][0], L[x][2])
&& L[y][i] <= Math.max(L[x][0], L[x][2])
&& L[y][i + 1] >= Math.min(L[x][1], L[x][3])
&& L[y][i + 1] <= Math.max(L[x][1], L[x][3])) {
long dx1 = L[x][0] - L[x][2];
long dy1 = L[x][1] - L[x][3];
long dx2 = L[y][i] - L[x][0];
long dy2 = L[y][i + 1] - L[x][1];
if (dy1 * dx2 == dy2 * dx1) {
double hyp = Math.sqrt(dx1 * dx1 + dy1 * dy1);
double len = Math.sqrt(dx2 * dx2 + dy2 * dy2);
hyp -= len;
if (4 * len >= hyp && len <= 4 * hyp)
return true;
}
}
}
return false;
}
public static void main(String[] args) throws NumberFormatException,
IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(in.readLine());
int[][] P = { { 0, 1, 2 }, { 0, 2, 1 }, { 1, 2, 0 } };
loop: while (t-- > 0) {
long[][] L = new long[3][4];
for (int i = 0; i < 3; i++) {
String[] S = in.readLine().split(" ");
for (int j = 0; j < 4; j++)
L[i][j] = Integer.parseInt(S[j]);
}
for (int i = 0; i < 3; i++) {
if (intersect(P[i][0], P[i][1], L)
&& split(P[i][0], P[i][2], L)
&& split(P[i][1], P[i][2], L)) {
System.out.println("YES");
continue loop;
}
}
System.out.println("NO");
}
}
} | Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | 90f8b5bae2c21f0102325fd3719e145b | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | import java.io.*;
import java.text.*;
import java.util.*;
public class LetterA {
static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static String LINE() throws Exception { return stdin.readLine(); }
static String TOKEN() throws Exception {
while (st == null || !st.hasMoreTokens())st = new StringTokenizer(LINE());
return st.nextToken();
}
static int INT() throws Exception {return Integer.parseInt(TOKEN());}
static long LONG() throws Exception {return Long.parseLong(TOKEN());}
static double DOUBLE() throws Exception {return Double.parseDouble(TOKEN());}
static DecimalFormat DF = new DecimalFormat("0.000",new DecimalFormatSymbols(Locale.ENGLISH));
public static final double EPSILON = 1E-13;
public static void main(String[] args) throws Exception {
int cases = INT();
while(cases-->0) {
long[][] A = new long[][] {{LONG(),LONG()},{LONG(),LONG()}};
long[][] B = new long[][] {{LONG(),LONG()},{LONG(),LONG()}};
long[][] C = new long[][] {{LONG(),LONG()},{LONG(),LONG()}};
new LetterA().go(A,B,C);
}
}
public void go(long[][] A, long[][] B, long[][] C) {
boolean ok = false;
if(check(A,B,C))ok = true;
else if(check(A,C,B))ok = true;
else if(check(B,A,C))ok = true;
else if(check(B,C,A))ok = true;
else if(check(C,A,B))ok = true;
else if(check(C,B,A))ok = true;
if(ok)System.out.println("YES");
else System.out.println("NO");
}
public boolean check(long[][] A, long[][] B, long[][] C) {
if(!divides(A,B,C))return false;
if(A[0][0]==B[0][0] && A[0][1]==B[0][1]) {
long dxa = A[1][0]-A[0][0], dya = A[1][1]-A[0][1];
long dxb = B[1][0]-B[0][0], dyb = B[1][1]-B[0][1];
if(angleok(dxa,dya,dxb,dyb))return true;
}
if(A[0][0]==B[1][0] && A[0][1]==B[1][1]) {
long dxa = A[1][0]-A[0][0], dya = A[1][1]-A[0][1];
long dxb = B[0][0]-B[1][0], dyb = B[0][1]-B[1][1];
if(angleok(dxa,dya,dxb,dyb))return true;
}
if(A[1][0]==B[0][0] && A[1][1]==B[0][1]) {
long dxa = A[0][0]-A[1][0], dya = A[0][1]-A[1][1];
long dxb = B[1][0]-B[0][0], dyb = B[1][1]-B[0][1];
if(angleok(dxa,dya,dxb,dyb))return true;
}
if(A[1][0]==B[1][0] && A[1][1]==B[1][1]) {
long dxa = A[0][0]-A[1][0], dya = A[0][1]-A[1][1];
long dxb = B[0][0]-B[1][0], dyb = B[0][1]-B[1][1];
if(angleok(dxa,dya,dxb,dyb))return true;
}
return false;
}
public boolean angleok(long dxa, long dya, long dxb, long dyb) {
double anga = Math.atan2(dya,dxa);
double angb = Math.atan2(dyb,dxb);
double angdiff = Math.abs(anga-angb);
return (angdiff>0-EPSILON && angdiff<Math.PI/2+EPSILON) || (angdiff<2*Math.PI+EPSILON && angdiff>3*Math.PI/2-EPSILON);
}
public boolean divides(long[][] A, long[][] B, long[][] C) {
if(pointOnLine(A[0],A[1],C[0]) && pointOnLine(B[0], B[1], C[1])) {
if(okSplit(A[0],A[1],C[0]) && okSplit(B[0],B[1],C[1]))
return true;
}
if(pointOnLine(A[0],A[1],C[1]) && pointOnLine(B[0], B[1], C[0])) {
if(okSplit(A[0],A[1],C[1]) && okSplit(B[0],B[1],C[0]))
return true;
}
return false;
}
public boolean okSplit(long[] A1, long[] A2, long[] splitpoint) {
long dxa = Math.abs(splitpoint[0]-A1[0]);
long dya = Math.abs(splitpoint[1]-A1[1]);
long dxb = Math.abs(splitpoint[0]-A2[0]);
long dyb = Math.abs(splitpoint[1]-A2[1]);
long lena = dxa*dxa+dya*dya;
long lenb = dxb*dxb+dyb*dyb;
if(lena>16*lenb || lenb>16*lena)return false;
return true;
}
public static boolean pointOnLine(long[] PA, long[] PB, long[] X) {
long A = PB[1] - PA[1];
long B = PA[0] - PB[0];
long C = A * PA[0] + B * PA[1];
if (A * X[0] + B * X[1] != C)return false;
return (((X[0] <= PA[0] && X[0] >= PB[0]) || (X[0] >= PA[0] && X[0] <= PB[0]))
&& ((X[1] <= PA[1] && X[1] >= PB[1]) || (X[1] >= PA[1] && X[1] <= PB[1])));
}
}
| Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | b8709f3daac674ea1da121bb6e64268b | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | import java.io.*;
import java.text.*;
import java.util.*;
public class LetterA {
static BufferedReader stdin = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static String LINE() throws Exception { return stdin.readLine(); }
static String TOKEN() throws Exception {
while (st == null || !st.hasMoreTokens())st = new StringTokenizer(LINE());
return st.nextToken();
}
static int INT() throws Exception {return Integer.parseInt(TOKEN());}
static long LONG() throws Exception {return Long.parseLong(TOKEN());}
static double DOUBLE() throws Exception {return Double.parseDouble(TOKEN());}
static DecimalFormat DF = new DecimalFormat("0.000",new DecimalFormatSymbols(Locale.ENGLISH));
public static final double EPSILON = 1E-13;
public static void main(String[] args) throws Exception {
int cases = INT();
while(cases-->0) {
long[][] A = new long[][] {{LONG(),LONG()},{LONG(),LONG()}};
long[][] B = new long[][] {{LONG(),LONG()},{LONG(),LONG()}};
long[][] C = new long[][] {{LONG(),LONG()},{LONG(),LONG()}};
new LetterA().go(A,B,C);
}
}
public void go(long[][] A, long[][] B, long[][] C) {
boolean ok = false;
if(check(A,B,C))ok = true;
else if(check(A,C,B))ok = true;
else if(check(B,A,C))ok = true;
else if(check(B,C,A))ok = true;
else if(check(C,A,B))ok = true;
else if(check(C,B,A))ok = true;
if(ok)System.out.println("YES");
else System.out.println("NO");
}
public boolean check(long[][] A, long[][] B, long[][] C) {
if(!divides(A,B,C))return false;
if(A[0][0]==B[0][0] && A[0][1]==B[0][1]) {
long dxa = A[1][0]-A[0][0], dya = A[1][1]-A[0][1];
long dxb = B[1][0]-B[0][0], dyb = B[1][1]-B[0][1];
if(angleok(dxa,dya,dxb,dyb))return true;
}
if(A[0][0]==B[1][0] && A[0][1]==B[1][1]) {
long dxa = A[1][0]-A[0][0], dya = A[1][1]-A[0][1];
long dxb = B[0][0]-B[1][0], dyb = B[0][1]-B[1][1];
if(angleok(dxa,dya,dxb,dyb))return true;
}
if(A[1][0]==B[0][0] && A[1][1]==B[0][1]) {
long dxa = A[0][0]-A[1][0], dya = A[0][1]-A[1][1];
long dxb = B[1][0]-B[0][0], dyb = B[1][1]-B[0][1];
if(angleok(dxa,dya,dxb,dyb))return true;
}
if(A[1][0]==B[1][0] && A[1][1]==B[1][1]) {
long dxa = A[0][0]-A[1][0], dya = A[0][1]-A[1][1];
long dxb = B[0][0]-B[1][0], dyb = B[0][1]-B[1][1];
if(angleok(dxa,dya,dxb,dyb))return true;
}
return false;
}
public boolean angleok(long dxa, long dya, long dxb, long dyb) {
double anga = Math.atan2(dya,dxa);
double angb = Math.atan2(dyb,dxb);
double angdiff = Math.abs(anga-angb);
return (angdiff>0-EPSILON && angdiff<Math.PI/2+EPSILON) || (angdiff<2*Math.PI+EPSILON && angdiff>3*Math.PI/2-EPSILON);
}
public boolean divides(long[][] A, long[][] B, long[][] C) {
if(pointOnLine(A[0],A[1],C[0]) && pointOnLine(B[0], B[1], C[1])) {
if(okSplit(A[0],A[1],C[0]) && okSplit(B[0],B[1],C[1]))
return true;
}
if(pointOnLine(A[0],A[1],C[1]) && pointOnLine(B[0], B[1], C[0])) {
if(okSplit(A[0],A[1],C[1]) && okSplit(B[0],B[1],C[0]))
return true;
}
return false;
}
public boolean okSplit(long[] A1, long[] A2, long[] splitpoint) {
long dxa = Math.abs(splitpoint[0]-A1[0]);
long dya = Math.abs(splitpoint[1]-A1[1]);
long dxb = Math.abs(splitpoint[0]-A2[0]);
long dyb = Math.abs(splitpoint[1]-A2[1]);
long lena = dxa*dxa+dya*dya;
long lenb = dxb*dxb+dyb*dyb;
if(lena>16*lenb || lenb>16*lena)return false;
return true;
}
public static boolean pointOnLine(long[] PA, long[] PB, long[] X) {
long A = PB[1] - PA[1];
long B = PA[0] - PB[0];
long C = A * PA[0] + B * PA[1];
if (A * X[0] + B * X[1] != C)return false;
return (((X[0] <= PA[0] && X[0] >= PB[0]) || (X[0] >= PA[0] && X[0] <= PB[0]))
&& ((X[1] <= PA[1] && X[1] >= PB[1]) || (X[1] >= PA[1] && X[1] <= PB[1])));
}
}
| Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | 56eb39c29b340c2fcd85689edae87ac0 | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | import java.util.Scanner;
public class ProblemB {
private class Segment{
public long x1;
public long y1;
public long x2;
public long y2;
public int common; // ΠΊΠ°ΠΊΠ°Ρ Π²Π΅ΡΡΠΈΠ½Π° 1 ΠΈΠ»ΠΈ 2
public boolean isBinding=false;
public Segment() {
}
public Segment(long x1, long y1, long x2, long y2) {
super();
this.x1 = x1;
this.y1 = y1;
this.x2 = x2;
this.y2 = y2;
}
public String toString() {
return "x1="+x1+" y1="+y1+"x2="+x2+" y2="+y2;
}
public final boolean isMemberOfSegment(Segment s,long x,long y){
// ΠΏΡΠΈΠ½Π°Π΄Π»Π΅ΠΆΠ½ΠΎΡΡΡ ΡΠΎΡΠΊΠΈ ΠΎΡΡΠ΅Π·ΠΊΡ
boolean res=false;
long a=s.y1-s.y2;
long b=s.x2-s.x1;
long c=s.x1*s.y2-s.y1*s.x2;
if (a*x+b*y+c==0)
if ((Math.min(s.x1, s.x2)<=x&&x<=Math.max(s.x1, s.x2))&&(Math.min(s.y1, s.y2)<=y&&y<=Math.max(s.y1, s.y2))){
res=true;
}
// ΡΡΡ ΠΆΠ΅ ΠΏΡΠΎΠ²Π΅ΡΡΠ΅ΠΌ Π½Π° ΡΠΎΠΎΡΠ½ΠΎΡΠ΅Π½ΠΈΠ΅ Π΄Π΅Π»Π΅Π½ΠΈΡ ΠΎΡΡΠ΅Π·ΠΊΠ°
if (res){
double l=Math.sqrt(Math.pow(s.x1-s.x2,2)+Math.pow(s.y1-s.y2,2));
double l1=Math.sqrt(Math.pow(s.x1-x,2)+Math.pow(s.y1-y,2));
double l2=Math.sqrt(Math.pow(s.x2-x,2)+Math.pow(s.y2-y,2));
res=Math.min(l1, l2)/Math.max(l1, l2)>=0.25;
//System.out.println("l1="+l1);
//System.out.println("l2="+l2);
//System.out.println("l="+Math.min(l1, l2)/Math.max(l1, l2));
}
return res;
}
public final double getDegrees(Segment s1,Segment s2){
// ΡΠ³ΠΎΠ» ΠΌΠ΅ΠΆΠ΄Ρ ΠΎΡΡΠ΅Π·ΠΊΠ°ΠΌΠΈ
long x1=0;
long y1=0;
long x2=0;
long y2=0;
long x3=0;
long y3=0;
if (s1.common==1){
x2=s1.x1; y2=s1.y1;
x1=s1.x2; y1=s1.y2;
}else if (s1.common==2){
x2=s1.x2; y2=s1.y2;
x1=s1.x1; y1=s1.y1;
}
if (s2.common==1){
x3=s2.x2; y3=s2.y2;
}else if (s2.common==2){
x3=s2.x1; y3=s2.y1;
}
long dx1=x1-x2;
long dy1=y1-y2;
long dx2=x3-x2;
long dy2=y3-y2;
long a=dx1*dy2-dy1*dx2;
long b=dx1*dx2+dy1*dy2;
return Math.abs(Math.toDegrees(Math.atan2(a, b)));
}
public String checkA(Segment[] a){
boolean res=false;
int i1=0;
int i2=0;
int i3=0;
// ΠΏΡΠΎΠ²Π΅ΡΡΠ΅ΠΌ Π½Π° ΠΎΠ±ΡΡΡ ΡΠΎΡΠΊΡ
for (int i = 0; i < 3; i++) {
for (int j = i + 1; j < 3; j++) {
if (a[i].x1 == a[j].x1 && a[i].y1 == a[j].y1) {
a[i].isBinding = true;a[i].common=1;
a[j].isBinding = true;a[j].common=1;
i1=i;
i2=j;
res = true;
} else if (a[i].x1 == a[j].x2 && a[i].y1 == a[j].y2) {
a[i].isBinding = true;a[i].common=1;
a[j].isBinding = true;a[j].common=2;
i1=i;
i2=j;
res = true;
} else if (a[i].x2 == a[j].x1 && a[i].y2 == a[j].y1) {
a[i].isBinding = true;a[i].common=2;
a[j].isBinding = true;a[j].common=1;
i1=i;
i2=j;
res = true;
} else if (a[i].x2 == a[j].x2 && a[i].y2 == a[j].y2) {
a[i].isBinding = true;a[i].common=2;
a[j].isBinding = true;a[j].common=2;
i1=i;
i2=j;
res = true;
}
}
}
//System.out.println("Common point ="+res);
for(int i = 0; i < 3; i++) {
if (i!=i1&&i!=i2)
i3=i;
}
if (res){
// ΠΏΡΠΎΠ²Π΅ΡΡΠ΅ΠΌ Π½Π° ΡΠΎΠ΅Π΄ΠΈΠ½Π΅Π½ΠΈΠ΅ Π΄Π²ΡΡ
ΠΎΡΡΠ΅Π·ΠΊΠΎΠ²
if (a[0].isBinding&&a[1].isBinding&&a[2].isBinding)
res=false;
else
for(int i = 0; i < 3; i++) {
if (!a[i].isBinding){
res=isMemberOfSegment(a[i1],a[i].x1,a[i].y1)||isMemberOfSegment(a[i1],a[i].x2,a[i].y2);
if (res)
res=res&&isMemberOfSegment(a[i2],a[i].x1,a[i].y1)||isMemberOfSegment(a[i2],a[i].x2,a[i].y2);
}
}
}
if (res){
// ΠΏΡΠΎΠ²Π΅ΡΡΠ΅ΠΌ ΡΠ³Π»Ρ
double angel=getDegrees(a[i1], a[i2]);
if (angel>90||angel<=0){
res=false;
//System.out.println("Π£Π³ΠΎΠ» ΡΠ»ΠΈΡΠΊΠΎΠΌ Π±ΠΎΠ»ΡΡΠΎΠΉ");
}
}
if (res)
return "YES";
else
return "NO";
}
}
void run(){
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
boolean res=false;
Segment[][] a=new Segment[n][3];
for (int i = 0; i < n; i++) {
for (int j = 0; j < 3; j++) {
a[i][j]=new Segment(sc.nextLong(),sc.nextLong(),sc.nextLong(),sc.nextLong());
}
}
// ΠΏΡΠΎΠ²Π΅ΡΡΠ΅ΠΌ Π½Π° ΠΎΠ±ΡΡΡ ΡΠΎΡΠΊΡ
for (int i = 0; i < n; i++) {
System.out.println(new Segment().checkA(a[i]));
}
sc.close();
}
public static void main(String[] args) {
new ProblemB().run();
}
}
| Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | 7cbc9d368f314c16ec627a5683cc919f | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
public class ProblemB {
public static void main(String[] args) throws IOException {
BufferedReader s = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.valueOf(s.readLine());
for (int c = 0 ; c < t ; c++) {
long[][] pt = new long[3][4];
for (int i = 0 ; i < 3 ; i++) {
pt[i] = readLine(s.readLine());
}
if (solve(pt)) {
out.println("YES");
} else {
out.println("NO");
}
}
out.flush();
}
private static boolean solve(long[][] pt) {
long same = 0;
long px = 0, py = 0;
long ax = 0, ay = 0, bx = 0, by = 0;
int third = 0;
for (int i = 0 ; i < 3 ; i++) {
for (int k = 0 ; k <= 1 ; k++) {
for (int j = i+1 ; j < 3 ; j++) {
for (int l = 0 ; l <= 1 ; l++) {
if (pt[i][2*k] == pt[j][2*l] && pt[i][2*k+1] == pt[j][2*l+1]) {
same++;
third = 3 - i - j;
px = pt[i][2*k];
py = pt[i][2*k+1];
ax = pt[i][2*(1-k)];
ay = pt[i][2*(1-k)+1];
bx = pt[j][2*(1-l)];
by = pt[j][2*(1-l)+1];
}
}
}
}
}
if (same != 1) {
return false;
}
if (!checkDegree(ax - px, ay - py, bx - px, by - py)) {
return false;
}
boolean[] on = new boolean[2];
for (int k = 0 ; k <= 1 ; k++) {
long qx = pt[third][2*k];
long qy = pt[third][2*k+1];
if (isonline(qx - px, qy - py, ax - px, ay - py)) {
long dq = (qx - px) * (qx - px) + (qy - py) * (qy - py);
long da = (ax - px) * (ax - px) + (ay - py) * (ay - py);
if (da <= dq * 25 && dq * 25 <= da * 16) {
on[0] = true;
}
} else if (isonline(qx - px, qy - py, bx - px, by - py)) {
long dq = (qx - px) * (qx - px) + (qy - py) * (qy - py);
long db = (bx - px) * (bx - px) + (by - py) * (by - py);
if (db <= dq * 25 && dq * 25 <= db * 16) {
on[1] = true;
}
}
}
return (on[0] && on[1]);
}
private static boolean isonline(long ax, long ay, long bx, long by) {
long nai = ax * bx + ay * by;
if (nai < 0) {
return false;
}
BigInteger n = BigInteger.valueOf(nai);
n = n.multiply(n);
long al = ax * ax + ay * ay;
long bl = bx * bx + by * by;
if (n.mod(BigInteger.valueOf(al)).compareTo(BigInteger.ZERO) == 0) {
n = n.divide(BigInteger.valueOf(al));
if (n.compareTo(BigInteger.valueOf(bl)) == 0) {
return true;
}
}
return false;
}
private static boolean checkDegree(long ax, long ay, long bx, long by) {
long nai = ax * bx + ay * by;
if (nai < 0) {
return false;
}
BigInteger n = BigInteger.valueOf(nai);
n = n.multiply(BigInteger.valueOf(nai));
long al = ax * ax + ay * ay;
long bl = bx * bx + by * by;
if (n.mod(BigInteger.valueOf(al)).compareTo(BigInteger.ZERO) == 0) {
n = n.divide(BigInteger.valueOf(al));
if (n.compareTo(BigInteger.valueOf(bl)) == 0) {
return false;
}
}
return true;
}
private static long[] readLine(String readLine) {
long[] r = new long[4];
String[] line = readLine.split(" ");
for (int i = 0 ; i <= 3 ; i++) {
r[i] = Long.valueOf(line[i]);
}
return r;
}
public static void debug(Object... os){
System.err.println(Arrays.deepToString(os));
}
} | Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | d54bdf339456b9ed79bb13e75ac1ec2d | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | //package round13;
import java.io.BufferedOutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.Arrays;
import java.util.Scanner;
public class B {
private Scanner in;
private PrintWriter out;
public void solve()
{
double[][] p = new double[3][4];
for(int i = 0;i < 3;i++){
for(int j = 0;j < 4;j++){
p[i][j] = in.nextDouble();
}
}
double[][] c = new double[3][4];
for(int s = 0;s <= 2;s+=2){
for(int t = 0;t <= 2;t+=2){
for(int u = 0;u <= 2;u+=2){
for(int i = 0;i < 4;i++){
c[0][i] = p[0][(s+i)%4];
c[1][i] = p[1][(t+i)%4];
c[2][i] = p[2][(u+i)%4];
}
if(check(c)){
out.println("YES"); return;
}
}
}
}
for(int s = 0;s <= 2;s+=2){
for(int t = 0;t <= 2;t+=2){
for(int u = 0;u <= 2;u+=2){
for(int i = 0;i < 4;i++){
c[0][i] = p[0][(s+i)%4];
c[1][i] = p[2][(t+i)%4];
c[2][i] = p[1][(u+i)%4];
}
if(check(c)){
out.println("YES"); return;
}
}
}
}
for(int s = 0;s <= 2;s+=2){
for(int t = 0;t <= 2;t+=2){
for(int u = 0;u <= 2;u+=2){
for(int i = 0;i < 4;i++){
c[0][i] = p[1][(s+i)%4];
c[1][i] = p[2][(t+i)%4];
c[2][i] = p[0][(u+i)%4];
}
if(check(c)){
out.println("YES"); return;
}
}
}
}
out.println("NO");
}
private boolean check(double[][] c)
{
if(!(c[0][0] == c[1][0] && c[0][1] == c[1][1]))return false;
double ip = (c[0][2] - c[0][0]) * (c[1][2] - c[1][0]) + (c[0][3] - c[0][1]) * (c[1][3] - c[1][1]);
double L0 = (c[0][2] - c[0][0]) * (c[0][2] - c[0][0]) + (c[0][3] - c[0][1]) * (c[0][3] - c[0][1]);
double L1 = (c[1][2] - c[1][0]) * (c[1][2] - c[1][0]) + (c[1][3] - c[1][1]) * (c[1][3] - c[1][1]);
if(!(ip >= 0 && ip * ip < L0 * L1))return false;
{
double aa = c[0][0] - c[0][2];
double cc = c[0][1] - c[0][3];
double bb = -c[2][0] + c[2][2];
double dd = -c[2][1] + c[2][3];
double xx = c[2][2] - c[0][2];
double yy = c[2][3] - c[0][3];
double det = aa * dd - bb * cc;
if(det == 0)return false;
double t = (double)(dd * xx - bb * yy) / det;
double u = (double)(-cc * xx + aa * yy) / det;
if(!((u == 0 || u == 1) && t >= 0.2 && t <= 0.8))return false;
}
{
double aa = c[1][0] - c[1][2];
double cc = c[1][1] - c[1][3];
double bb = -c[2][0] + c[2][2];
double dd = -c[2][1] + c[2][3];
double xx = c[2][2] - c[1][2];
double yy = c[2][3] - c[1][3];
double det = aa * dd - bb * cc;
if(det == 0)return false;
double t = (double)(dd * xx - bb * yy) / det;
double u = (double)(-cc * xx + aa * yy) / det;
if(!((u == 0 || u == 1) && t >= 0.2 && t <= 0.8))return false;
}
return true;
}
public void run() throws Exception
{
// in = new Scanner(new StringReader("3 4 4 6 0 4 1 5 2 4 0 4 4 0 0 0 6 0 6 2 -4 1 1 0 1 0 0 0 5 0 5 2 -1 1 2 0 1"));
in = new Scanner(System.in);
System.setOut(new PrintStream(new BufferedOutputStream(System.out)));
out = new PrintWriter(System.out);
int n = in.nextInt();
for(int i = 1;i <= n;i++){
long t = System.currentTimeMillis();
solve();
out.flush();
// System.err.printf("%04d/%04d %7d%n", i, n, System.currentTimeMillis() - t);
}
}
public static void main(String[] args) throws Exception
{
new B().run();
}
private int ni() { return Integer.parseInt(in.next()); }
private static void tr(Object... o) { System.out.println(o.length == 1 ? o[0] : Arrays.toString(o)); }
private void tra(int[] a) {System.out.println(Arrays.toString(a));}
private void tra(int[][] a)
{
for(int[] e : a){
System.out.println(Arrays.toString(e));
}
}
}
| Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | ea4e64cce1b909bd7aa228f56eb352ad | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | import java.util.Scanner;
public class B {
static class Line {
long x1, y1, x2, y2;
public Line(int x1, int y1, int x2, int y2) {
this.x1 = x1;
this.x2 = x2;
this.y1 = y1;
this.y2 = y2;
}
public boolean sharePoint(Line l) {
if (this.x1 == l.x1 && this.y1 == l.y1 && !this.passBy(l.x2, l.y2)
&& !l.passBy(x2, y2) || this.x2 == l.x2 && this.y2 == l.y2
&& !this.passBy(l.x1, l.y1) && !l.passBy(x1, y1)
|| this.x1 == l.x2 && this.y1 == l.y2
&& !this.passBy(l.x1, l.y1) && !l.passBy(x2, y2)
|| this.x2 == l.x1 && this.y2 == l.y1
&& !this.passBy(l.x2, l.y2) && !l.passBy(x1, y1))
return true;
return false;
}
public boolean passBy(long x, long y) {
long a = y1 - y2;
long b = x2 - x1;
long c = y1 * (x1 - x2) + x1 * (y2 - y1);
if (a * x + b * y + c == 0 && x <= Math.max(x1, x2)
&& x >= Math.min(x1, x2) && y <= Math.max(y1, y2)
&& y >= Math.min(y1, y2))
return true;
return false;
}
}
public static long getSqDist(long x1, long y1, long x2, long y2) {
return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
}
public static long dotProduct(Line l1, Line l2) {
if(l1.x1==l2.x1&&l1.y1==l2.y1)
return (l1.x2 - l1.x1) * (l2.x2 - l2.x1) + (l1.y2 - l1.y1)
* (l2.y2 - l2.y1);
else if(l1.x1==l2.x2&&l1.y1==l2.y2)
return (l1.x2 - l1.x1) * (l2.x1 - l2.x2) + (l1.y2 - l1.y1)
* (l2.y1 - l2.y2);
else if(l1.x2==l2.x1&&l1.y2==l2.y1)
return (l1.x1 - l1.x2) * (l2.x2 - l2.x1) + (l1.y1 - l1.y2)
* (l2.y2 - l2.y1);
else return (l1.x1 - l1.x2) * (l2.x1 - l2.x2) + (l1.y1 - l1.y2)
* (l2.y1 - l2.y2);
}
public static boolean valid(Line l1, Line l2, Line l3) {
if (l1.passBy(l3.x1, l3.y1) && l2.passBy(l3.x2, l3.y2)) {
long min, max;
min = Math.min(getSqDist(l1.x1, l1.y1, l3.x1, l3.y1), getSqDist(
l1.x2, l1.y2, l3.x1, l3.y1));
max = Math.max(getSqDist(l1.x1, l1.y1, l3.x1, l3.y1), getSqDist(
l1.x2, l1.y2, l3.x1, l3.y1));
if (min * 16 < max) {
return false;
}
min = Math.min(getSqDist(l2.x1, l2.y1, l3.x2, l3.y2), getSqDist(
l2.x2, l2.y2, l3.x2, l3.y2));
max = Math.max(getSqDist(l2.x1, l2.y1, l3.x2, l3.y2), getSqDist(
l2.x2, l2.y2, l3.x2, l3.y2));
if (min * 16 < max) {
return false;
}
return true;
} else if (l2.passBy(l3.x1, l3.y1) && l1.passBy(l3.x2, l3.y2)) {
long min, max;
min = Math.min(getSqDist(l2.x1, l2.y1, l3.x1, l3.y1), getSqDist(
l2.x2, l2.y2, l3.x1, l3.y1));
max = Math.max(getSqDist(l2.x1, l2.y1, l3.x1, l3.y1), getSqDist(
l2.x2, l2.y2, l3.x1, l3.y1));
if (min * 16 < max) {
return false;
}
min = Math.min(getSqDist(l1.x1, l1.y1, l3.x2, l3.y2), getSqDist(
l1.x2, l1.y2, l3.x2, l3.y2));
max = Math.max(getSqDist(l1.x1, l1.y1, l3.x2, l3.y2), getSqDist(
l1.x2, l1.y2, l3.x2, l3.y2));
if (min * 16 < max) {
return false;
}
return true;
}
return false;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int cases = sc.nextInt();
for (int i = 0; i < cases; i++) {
Line l1 = new Line(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc
.nextInt());
Line l2 = new Line(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc
.nextInt());
Line l3 = new Line(sc.nextInt(), sc.nextInt(), sc.nextInt(), sc
.nextInt());
if (l1.sharePoint(l2)) {
if (l1.sharePoint(l3) || l2.sharePoint(l3)
|| dotProduct(l1, l2) < 0) {
System.out.println("NO");
continue;
}
if (valid(l1, l2, l3)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
} else if (l2.sharePoint(l3)) {
if (l1.sharePoint(l3) || dotProduct(l2, l3) < 0) {
System.out.println("NO");
continue;
}
if (valid(l2, l3, l1)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
} else if (l3.sharePoint(l1)) {
if (dotProduct(l3, l1) < 0) {
System.out.println("NO");
continue;
}
if (valid(l3, l1, l2)) {
System.out.println("YES");
} else {
System.out.println("NO");
}
} else {
System.out.println("NO");
}
}
}
} | Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | ebcbe7ab576b6c356675dff1ee1f4865 | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | import java.util.*;
public class A {
static double EPS = 1e-9;
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
int n = reader.nextInt();
while(n --> 0){
Seg a = new Seg(reader);
Seg b = new Seg(reader);
Seg c = new Seg(reader);
if(a.shares(b)){
eval(a,b,c);
}else if(a.shares(c)){
eval(a,c,b);
}else if(b.shares(c)){
eval(b,c,a);
}else{
System.out.println("NO");
}
}
}
public static void eval(Seg a, Seg b, Seg c){
// System.out.println(a + " " + b + " " + c);
boolean good = a.commonAngle(b);
if(good && a.on(c.s) && b.on(c.t)){
// System.out.println("check 1");
double L1 = a.getL(c.s); L1 /= (1-L1);
double L2 = b.getL(c.t); L2 /= (1-L2);
// System.out.println(L1 + " " + L2);
good &= L1 >= .25 && L1 <= 4 && L2 >= .25 && L2 <= 4;
}else if(good && a.on(c.t) && b.on(c.s)){
// System.out.println("check 2");
double L1 = a.getL(c.t); L1 /= (1-L1);
double L2 = b.getL(c.s); L2 /= (1-L2);
good &= L1 >= .25 && L1 <= 4 && L2 >= .25 && L2 <= 4;
}else if(good)
good = false;
System.out.println(good?"YES":"NO");
}
public static class Seg{
Point s,t;
public Seg(Scanner in){
s = new Point(in.nextInt(), in.nextInt());
t = new Point(in.nextInt(), in.nextInt());
if(s.compareTo(t) > 0){
Point x = s;
s = t;
t = x;
}
}
public double getL(Point p){
return p.sub(s).mag()/t.sub(s).mag();
}
public boolean on(Point p){
return Math.abs(p.sub(s).cross(t.sub(s))) < EPS && p.sub(s).dot(t.sub(s)) >= 0 && p.sub(t).dot(s.sub(t)) >= 0;
}
public boolean shares(Seg x){
return s.equals(x.s) || s.equals(x.t) || t.equals(x.s) || t.equals(x.t);
}
public boolean commonAngle(Seg x){
double a = 2*Math.PI;
if(x.s.equals(s)){
a = t.sub(s).angBetween(x.t.sub(x.s));
}else if(x.t.equals(s)){
a = t.sub(s).angBetween(x.s.sub(x.t));
}else if(t.equals(x.s)){
a = s.sub(t).angBetween(x.t.sub(x.s));
}else if(t.equals(x.t)){
a = s.sub(t).angBetween(x.s.sub(x.t));
}
return a <= Math.PI/2+EPS;
}
public String toString(){
return "["+s+", "+t+"]";
}
}
public static class Point implements Comparable<Point>{
double x,y;
public Point(double _x, double _y){
x = _x; y = _y;
}
public Point sub(Point p){
return new Point(x-p.x, y-p.y);
}
public Point add(Point p){
return new Point(x+p.x, y+p.y);
}
public Point scale(double L){
return new Point(x*L, y*L);
}
public Point rot(double t){
double c = Math.cos(t);
double s = Math.sin(t);
return new Point(x*c-y*s, x*s+y*c);
}
public Point norm(){
return new Point(x/mag(), y/mag());
}
public Point project(Point p){
return p.scale(dot(p)/p.dot(p));
}
public double dot(Point p){
return x*p.x + y*p.y;
}
public double cross(Point p){
return x*p.y-y*p.x;
}
public double angBetween(Point p){
double d = Math.abs(ang()-p.ang());
return Math.min(d, 2*Math.PI-d);
}
public double mag(){
return Math.abs(Math.sqrt(x*x + y*y));
}
public double ang(){
double a = Math.atan2(y,x);
if(a<0) a+=2*Math.PI;
return a;
}
public double dis(Point p){
return sub(p).mag();
}
public int compareTo(Point p){
if(p.x == x){
return (int)Math.signum(y-p.y);
}
return (int)Math.signum(x-p.x);
}
public boolean equals(Point p){
return compareTo(p)==0;
}
public String toString(){
return "("+x+", "+y+")";
}
}
}
| Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | 1844a4566def2c4c7452b5ace25dfd00 | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | //package round13;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class B {
static StreamTokenizer in =
new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static int nextInt() throws IOException{
in.nextToken();
return (int)in.nval;
}
static PrintWriter out = new PrintWriter(System.out);
static boolean check1(int x[][], int y[][], int i, int j){
if (x[i][0]==x[j][1] && y[i][0]==y[j][1])
swp(x, y, j);
if (x[i][0]!=x[j][0] || y[i][0]!=y[j][0]) return false;
long a = x[i][0]-x[i][1],
b = y[i][0]-y[i][1],
c = -(a*x[i][0]+b*y[i][0]);
if (Math.signum(a*x[i][1]+b*y[i][1]+c)*Math.signum(a*x[j][1]+b*y[j][1]+c) < 0) return false;
a = y[i][0]-y[i][1];
b = x[i][1]-x[i][0];
c = -(a*x[i][0]+b*y[i][0]);
if (a*x[j][1]+b*y[j][1]+c == 0) return false;
return true;
}
static void swap(int x[][], int y[][], int i, int j){
for (int k=0; k<2; k++){
int p = x[j][k]; x[j][k] = x[i][k]; x[i][k] = p;
p = y[j][k]; y[j][k] = y[i][k]; y[i][k] = p;
}
}
static void swp(int x[][], int y[][], int i){
int p = x[i][0]; x[i][0] = x[i][1]; x[i][1] = p;
p = y[i][0]; y[i][0] = y[i][1]; y[i][1] = p;
}
static double dist(long x1, long y1, long x2, long y2){
return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}
static boolean check2(int x[][], int y[][], int i, int j){
long a = y[i][0]-y[i][1],
b = x[i][1]-x[i][0],
c = -(a*x[i][0]+b*y[i][0]);
if (a*x[j][1]+b*y[j][1]+c == 0)
swp(x, y, j);
if (a*x[j][0]+b*y[j][0]+c != 0) return false;
if (a*x[j][1]+b*y[j][1]+c == 0) return false;
if (!(x[j][0]>=x[i][0] && x[j][0]<=x[i][1] || x[j][0]>=x[i][1] && x[j][0]<=x[i][0])) return false;
if (!(y[j][0]>=y[i][0] && y[j][0]<=y[i][1] || y[j][0]>=y[i][1] && y[j][0]<=y[i][0])) return false;
double d1 = dist(x[i][0], y[i][0], x[j][0], y[j][0]);
double d2 = dist(x[i][1], y[i][1], x[j][0], y[j][0]);
if (Math.min(d1, d2)/Math.max(d1, d2)+0.000000001 < 0.25) return false;
return true;
}
public static void main(String[] args) throws IOException{
int n = nextInt(),
x[][] = new int[3][2],
y[][] = new int[3][2];
for (int q=0; q<n; q++){
for (int i=0; i<3; i++)
for (int j=0; j<2; j++){
x[i][j] = nextInt();
y[i][j] = nextInt();
}
if (!check1(x, y, 0, 1)){
swp(x, y, 0);
if (check1(x, y, 0, 1)){
}
else if (!check1(x, y, 0, 2)){
swp(x, y, 0);
if (check1(x, y, 0, 2))
swap(x, y, 1, 2);
else if (!check1(x, y, 1, 2)){
swp(x, y, 1);
if (check1(x, y, 1, 2))
swap(x, y, 0, 2);
else{
out.println("NO");
continue;
}
}
else swap(x, y, 0, 2);
}
else swap(x, y, 1, 2);
}
if (!check2(x, y, 0, 2) || !check2(x, y, 1, 2)) out.println("NO");
else out.println("YES");
}
out.flush();
}
}
| Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | 0ff325b4c352c63cfbb3688e1bf6e715 | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | import java.util.*;
import java.awt.Point;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.io.*;
import java.math.BigInteger;
public class Main implements Runnable {
class Point {
long x;
long y;
public Point(long x, long y) {
this.x = x;
this.y = y;
}
@Override
public boolean equals(Object obj) {
Point other = (Point) obj;
return x == other.x && y == other.y;
}
public long getX() {
return x;
}
public long getY() {
return y;
}
public double distance(Main.Point p) {
return Point2D.distance(x, y, p.x, p.y);
}
}
class Line {
Point p1;
Point p2;
public Line(long x1, long y1, long x2, long y2) {
p1 = new Point(x1, y1);
p2 = new Point(x2, y2);
}
public Point getP1() {
return p1;
}
public Point getP2() {
return p2;
}
public double dist(Point p) {
if (vecMul(p1.x - p2.x, p1.y - p2.y, p.x - p2.x, p.y - p2.y) != 0) {
return 1;
} else {
// return Line2D.ptSegDist(p1.x, p1.y, p2.x, p2.y, p.x, p.y);
return 0;
}
}
private long vecMul(long x, long y, long x2, long y2) {
return x * y2 - x2 * y;
}
}
public void solution() throws IOException {
int ts = in.nextInt();
while (ts-- > 0) {
Line a = new Line(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt());
Line b = new Line(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt());
Line c = new Line(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt());
boolean ok = false;
ok |= check(a, b, c);
ok |= check(b, c, a);
ok |= check(c, a, b);
out.println(ok ? "YES" : "NO");
}
}
private boolean check(Line a, Line b, Line c) {
boolean ok = false;
ok |= a.getP1().equals(b.getP1()) && checkAngle(a.getP1(), a.getP2(), b.getP2());
ok |= a.getP1().equals(b.getP2()) && checkAngle(a.getP1(), a.getP2(), b.getP1());
ok |= a.getP2().equals(b.getP1()) && checkAngle(a.getP2(), a.getP1(), b.getP2());
ok |= a.getP2().equals(b.getP2()) && checkAngle(a.getP2(), a.getP1(), b.getP1());
if (a.dist(c.getP1()) < EPS && b.dist(c.getP2()) < EPS) {
ok &= checkQuater(a, c.getP1());
ok &= checkQuater(b, c.getP2());
} else if (a.dist(c.getP2()) < EPS && b.dist(c.getP1()) < EPS) {
ok &= checkQuater(a, c.getP2());
ok &= checkQuater(b, c.getP1());
} else {
ok = false;
}
return ok;
}
final double EPS = 1e-7;
private boolean checkAngle(Point a, Point b, Point c) {
long x1 = b.getX() - a.getX();
long y1 = b.getY() - a.getY();
long x2 = c.getX() - a.getX();
long y2 = c.getY() - a.getY();
return x1 * x2 + y1 * y2 >= 0 && x1 * y2 != x2 * y1;
}
private boolean checkQuater(Line a, Point p) {
if (a.getP1().getX() == a.getP2().getX()) {
return check(a.p1.y, a.p2.y, p.y);
} else {
return check(a.p1.x, a.p2.x, p.x);
}
}
private boolean check(long a, long b, long c) {
if (a < b) {
if (c < a || c > b) {
return false;
}
} else {
if (c < b || c > a) {
return false;
}
}
long length = Math.abs(a - b);
long x = Math.abs(c - a);
return x * 5 >= length && (length - x) * 5 >= length;
}
public void run() {
try {
solution();
in.reader.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
private class Scanner {
BufferedReader reader;
StringTokenizer tokenizer;
public Scanner(Reader reader) {
this.reader = new BufferedReader(reader);
this.tokenizer = new StringTokenizer("");
}
public boolean hasNext() throws IOException {
while (!tokenizer.hasMoreTokens()) {
String next = reader.readLine();
if (next == null) {
return false;
}
tokenizer = new StringTokenizer(next);
}
return true;
}
public String next() throws IOException {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public String nextLine() throws IOException {
tokenizer = new StringTokenizer("");
return reader.readLine();
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
public static void main(String[] args) throws IOException {
new Thread(null, new Main(), "", 1 << 28).start();
}
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
Scanner in = new Scanner(new InputStreamReader(System.in));
}
| Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | 9af96f2a2359562c151bd11ac30b4356 | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | import java.util.*;
import java.awt.geom.Line2D;
import java.awt.geom.Point2D;
import java.awt.geom.Point2D.Double;
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
public class Main implements Runnable {
public void solution() throws IOException {
// System.out.println(dist(new Point2D.Double(0, 0), new
// Point2D.Double(1, 200000000), new Point2D.Double(1, 200000000 - 1)));
int ts = in.nextInt();
while (ts-- > 0) {
Line2D a = new Line2D.Double(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt());
Line2D b = new Line2D.Double(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt());
Line2D c = new Line2D.Double(in.nextInt(), in.nextInt(), in.nextInt(), in.nextInt());
boolean ok = false;
ok |= check(a, b, c);
ok |= check(b, c, a);
ok |= check(c, a, b);
out.println(ok ? "YES" : "NO");
}
}
private boolean check(Line2D a, Line2D b, Line2D c) {
boolean ok = false;
ok |= a.getP1().equals(b.getP1()) && checkAngle(a.getP1(), a.getP2(), b.getP2());
ok |= a.getP1().equals(b.getP2()) && checkAngle(a.getP1(), a.getP2(), b.getP1());
ok |= a.getP2().equals(b.getP1()) && checkAngle(a.getP2(), a.getP1(), b.getP2());
ok |= a.getP2().equals(b.getP2()) && checkAngle(a.getP2(), a.getP1(), b.getP1());
if (ok) {
if (dist(a.getP1(), a.getP2(), c.getP1()) < EPS && dist(b.getP1(), b.getP2(), c.getP2()) < EPS) {
ok &= checkQuater(a, c.getP1());
ok &= checkQuater(b, c.getP2());
} else if (dist(a.getP1(), a.getP2(), c.getP2()) < EPS && dist(b.getP1(), b.getP2(), c.getP1()) < EPS) {
ok &= checkQuater(a, c.getP2());
ok &= checkQuater(b, c.getP1());
} else {
ok = false;
}
}
return ok;
}
private double dist(Point2D A, Point2D B, Point2D p) {
Point2D a = new Point2D.Double(p.getX() - A.getX(), p.getY() - A.getY());
Point2D b = new Point2D.Double(B.getX() - A.getX(), B.getY() - A.getY());
return Math.abs(cross(a, b) / length(A, B));
}
private double length(Point2D a, Point2D b) {
double x = a.getX() - b.getX();
double y = a.getY() - b.getY();
return Math.sqrt(x * x + y * y);
}
private double cross(Point2D a, Point2D b) {
return a.getX() * b.getY() - a.getY() * b.getX();
}
final double EPS = 1e-11;
private boolean checkAngle(Point2D a, Point2D b, Point2D c) {
long x1 = (long) (b.getX() - a.getX());
long x2 = (long) (c.getX() - a.getX());
long y1 = (long) (b.getY() - a.getY());
long y2 = (long) (c.getY() - a.getY());
return x1 * x2 + y1 * y2 >= 0 && x1 * y2 != x2 * y1;
}
private boolean checkQuater(Line2D a, Point2D p) {
if (a.getP1().getX() == a.getP2().getX()) {
return check(a.getP1().getY(), a.getP2().getY(), p.getY());
} else {
return check(a.getP1().getX(), a.getP2().getX(), p.getX());
}
}
private boolean check(double a, double b, double c) {
if (a < b) {
if (c < a || c > b) {
return false;
}
} else {
if (c < b || c > a) {
return false;
}
}
double length = Math.abs(a - b);
double x = Math.abs(c - a);
return x * 5 >= length && (length - x) * 5 >= length;
}
public void run() {
try {
solution();
in.reader.close();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
private class Scanner {
private BufferedReader reader;
private StringTokenizer tokenizer;
public Scanner(Reader reader) {
this.reader = new BufferedReader(reader);
this.tokenizer = new StringTokenizer("");
}
public boolean hasNext() throws IOException {
while (!tokenizer.hasMoreTokens()) {
String next = reader.readLine();
if (next == null) {
return false;
}
tokenizer = new StringTokenizer(next);
}
return true;
}
public String next() throws IOException {
hasNext();
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public String nextLine() throws IOException {
tokenizer = new StringTokenizer("");
return reader.readLine();
}
}
public static void main(String[] args) throws IOException {
new Thread(null, new Main(), "", 1 << 28).start();
}
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
Scanner in = new Scanner(new InputStreamReader(System.in));
}
| Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | 49fc7c2babe77143b65459b9488f864d | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes |
import java.io.*;
import java.util.*;
public class LetterA
{
public Scanner in = new Scanner(System.in);
public PrintStream out = System.out;
public Pair[][] P;
public Pair a, b, c;
public void main()
{
int i;
int TCase, cc;
TCase = in.nextInt();
for(cc=1;cc<=TCase;++cc)
{
P = new Pair[3][2];
for(i=0;i<3;++i)
{
P[i][0] = new Pair(in.nextLong(), in.nextLong());
P[i][1] = new Pair(in.nextLong(), in.nextLong());
}
out.println(isA()?"YES":"NO");
}
}//end public void main()
public boolean isA()
{
int i, j, k;
int x, y;
Pair a, b, c;
a = b = c = null;
k = 0;
for(i=0;i<3;++i) if(P[i][0].equals(P[i][1])) return false;
outter:
for(i=0;i<3;++i) for(j=i+1;j<3;++j)
{
for(x=0;x<2;++x) for(y=0;y<2;++y)
{
if(P[i][x].equals(P[j][y]))
{
a = P[i][x];
b = P[i][1-x];
c = P[j][1-y];
k = 3 - i - j;
break outter;
}
}
}
if(a == null) return false;
if(!angleTest(a, b, c)) return false;
return edgeTest(a, b, c, P[k][0], P[k][1]) || edgeTest(a, b, c, P[k][1], P[k][0]);
}
public double tol = 1e-12;
public boolean eq(double x, double y) { return Math.abs(x - y) < tol; }
public boolean angleTest(Pair a, Pair b, Pair c)
{
double T = Math.abs(angle(b.subtract(a), c.subtract(a)));
return (tol < T && T <= Math.PI/2.0 + tol);
}
public double angle(Pair a, Pair b) { return Math.atan2(cross(a, b), dot(a, b)); }
public double cross(Pair a, Pair b) { return a.x*b.y - a.y*b.x; }
public double dot(Pair a, Pair b) { return a.x*b.x + a.y*b.y; }
public boolean edgeTest(Pair a, Pair b, Pair c, Pair d, Pair e)
{
Pair ab = b.subtract(a);
Pair ac = c.subtract(a);
return split(d, a, b) && split(e, a, c);
}
//Is p on the segment/line formed by a --> b?
public boolean split(Pair p, Pair a, Pair b)
{
if(eq(a.x, b.x))
{
//WARNING: does endpoint count as on edge?
if(eq(p.x, a.x) && Math.min(a.y, b.y) < p.y && p.y < Math.max(a.y, b.y))
{
double t = (p.y - Math.min(a.y, b.y))/(Math.max(a.y, b.y) - Math.min(a.y, b.y));
t = Math.min(t, 1-t);
return t/(1-t)+tol >= 0.25;
}
return false;
}
double t = (p.x-a.x)/(b.x-a.x);
//adjust range of t based on problem requirement
if(0.0 < t && t < 1.0 && eq(p.y, a.y+t*(b.y-a.y)))
{
t = Math.min(t, 1-t);
return t/(1-t)+tol >= 0.25;
}
return false;
}
//double point
//Use eq(double, double)
private class Pair implements Comparable<Pair>
{
public double x, y;
public Pair(double xx, double yy) { x = xx; y = yy; }
public int compareTo(Pair u)
{
if(!eq(x, u.x)) return (x < u.x ? -1 : 1);
if(!eq(y, u.y)) return (y < u.y ? -1 : 1);
return 0;
}
public boolean equals(Pair u) { return compareTo(u) == 0; }
public Pair midpoint(Pair u) { return new Pair((x + u.x)/2, (y + u.y)/2); }
public Pair add(Pair u) { return new Pair(x + u.x, y + u.y); }
public Pair subtract(Pair u) { return new Pair(x - u.x, y - u.y); }
public Pair multiply(double s) { return new Pair(x*s, y*s); }
public double length() { return Math.sqrt(x*x + y*y); }
public double dist(Pair u) { return subtract(u).length(); }
public Pair rotate(double t)
{
double xx, yy;
xx = x*Math.cos(t) - y*Math.sin(t);
yy = x*Math.sin(t) + y*Math.cos(t);
return new Pair(xx, yy);
}
public double[] ret() { return new double[]{x, y}; }
public String toString() { return "("+x+","+y+")"; }
}
public static void main(String[] args)
{
(new LetterA()).main();
}
} | Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | cf70498cf9f142f19a2f5865113377cd | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | import java.util.*;
import java.io.*;
public class Solution {
static class Segment {
long x1, y1, x2, y2;
public Segment(BufferedReader br) throws IOException {
StringTokenizer st = new StringTokenizer(br.readLine());
x1 = Long.parseLong(st.nextToken());
y1 = Long.parseLong(st.nextToken());
x2 = Long.parseLong(st.nextToken());
y2 = Long.parseLong(st.nextToken());
}
void invert() {
long tmp = x1;
x1 = x2;
x2 = tmp;
tmp = y1;
y1 = y2;
y2 = tmp;
}
}
static boolean belongs(long x, long y, Segment a) {
long A = a.y2 - a.y1;
long B = a.x1 - a.x2;
long C = -A * a.x1 - B * a.y1;
if (A * x + B * y + C != 0)
return false;
if (x < Math.min(a.x1, a.x2) || x > Math.max(a.x1, a.x2))
return false;
if (y < Math.min(a.y1, a.y2) || y > Math.max(a.y1, a.y2))
return false;
return true;
}
static long scalar(Segment a, Segment b) {
long dx1 = a.x2 - a.x1;
long dy1 = a.y2 - a.y1;
long dx2 = b.x2 - b.x1;
long dy2 = b.y2 - b.y1;
return dx1 * dx2 + dy1 * dy2;
}
static boolean goodDivision(long x, long y, Segment a) {
long dist1 = (x - a.x1) * (x - a.x1) + (y - a.y1) * (y - a.y1);
long dist2 = (x - a.x2) * (x - a.x2) + (y - a.y2) * (y - a.y2);
if (dist1 > dist2) {
long tmp = dist1;
dist1 = dist2;
dist2 = tmp;
}
return 4 * 4 * dist1 >= dist2;
}
static boolean ok(Segment a, Segment b, Segment c) {
if (a.x1 != b.x1 || a.y1 != b.y1)
return false;
if (!belongs(c.x1, c.y1, a) || !belongs(c.x2, c.y2, b))
return false;
if (scalar(a, b) < 0)
return false;
if (!goodDivision(c.x1, c.y1, a) || !goodDivision(c.x2, c.y2, b))
return false;
return true;
}
static boolean isA(Segment a, Segment b, Segment c) {
boolean res = false;
for (int mask = 0; mask < 8; mask++) {
if ((mask & 1) != 0)
a.invert();
if ((mask & 2) != 0)
b.invert();
if ((mask & 4) != 0)
c.invert();
res |= ok(a, b, c);
if ((mask & 1) != 0)
a.invert();
if ((mask & 2) != 0)
b.invert();
if ((mask & 4) != 0)
c.invert();
}
return res;
}
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine());
for (int i = 0; i < t; i++) {
Segment a = new Segment(in);
Segment b = new Segment(in);
Segment c = new Segment(in);
out.println(isA(a, b, c) || isA(a, c, b) || isA(b, c, a) ? "YES" : "NO");
}
out.close();
}
} | Java | ["3\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | b80c2c219d52b6953849090ce3f780c9 | train_003.jsonl | 1273154400 | Little Petya learns how to write. The teacher gave pupils the task to write the letter A on the sheet of paper. It is required to check whether Petya really had written the letter A.You are given three segments on the plane. They form the letter A if the following conditions hold: Two segments have common endpoint (lets call these segments first and second), while the third segment connects two points on the different segments. The angle between the first and the second segments is greater than 0 and do not exceed 90 degrees. The third segment divides each of the first two segments in proportion not less than 1β/β4 (i.e. the ratio of the length of the shortest part to the length of the longest part is not less than 1β/β4). | 64 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.fill;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.sort;
public class Main {
public static void main(String[] args) throws IOException {
try {
if (new File("input.txt").exists()) {
System.setIn(new FileInputStream("input.txt"));
}
} catch (SecurityException e) {
}
new Main().run();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
Segment s1, s2, s3;
void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
s1 = new Segment();
s2 = new Segment();
s3 = new Segment();
for (int T = nextInt(), t = 0; t < T; t++) {
s1.p1.x = nextInt();
s1.p1.y = nextInt();
s1.p2.x = nextInt();
s1.p2.y = nextInt();
s1.getV();
s2.p1.x = nextInt();
s2.p1.y = nextInt();
s2.p2.x = nextInt();
s2.p2.y = nextInt();
s2.getV();
s3.p1.x = nextInt();
s3.p1.y = nextInt();
s3.p2.x = nextInt();
s3.p2.y = nextInt();
s3.getV();
boolean ok = true;
if (s1.nul() || s2.nul() || s3.nul() || same(s1, s2) || same(s1, s3) || same(s2, s3) || nulAngle(s1, s2) || nulAngle(s1, s3) || nulAngle(s2, s3))
ok = false;
if (ok) {
ok = false;
if (havePoint(s1, s2)) {
if (validAngle(s1, s2)) {
if (s1.contains(s3.p1) && s2.contains(s3.p2)) {
if (validDists(s1, s3.p1) && validDists(s2, s3.p2))
ok = true;
} else if (s1.contains(s3.p2) && s2.contains(s3.p1)) {
if (validDists(s1, s3.p2) && validDists(s2, s3.p1))
ok = true;
}
}
} else if (havePoint(s1, s3)) {
if (validAngle(s1, s3)) {
if (s1.contains(s2.p1) && s3.contains(s2.p2)) {
if (validDists(s1, s2.p1) && validDists(s3, s2.p2))
ok = true;
} else if (s1.contains(s2.p2) && s3.contains(s2.p1)) {
if (validDists(s1, s2.p2) && validDists(s3, s2.p1))
ok = true;
}
}
} else if (havePoint(s2, s3)) {
if (validAngle(s2, s3)) {
if (s2.contains(s1.p1) && s3.contains(s1.p2)) {
if (validDists(s2, s1.p1) && validDists(s3, s1.p2))
ok = true;
} else if (s2.contains(s1.p2) && s3.contains(s1.p1)) {
if (validDists(s2, s1.p2) && validDists(s3, s1.p1))
ok = true;
}
}
}
}
out.println(ok ? "YES" : "NO");
}
out.close();
}
boolean validDists(Segment s, Point p) {
long d1 = sqr(s.p1.x - p.x) + sqr(s.p1.y - p.y);
long d2 = sqr(s.p2.x - p.x) + sqr(s.p2.y - p.y);
long minD = min(d1, d2);
long maxD = max(d1, d2);
return 16 * minD >= maxD;
}
long sqr(long x) {
return x * x;
}
boolean nulAngle(Segment s1, Segment s2) {
return vec(s1.v, s2.v) == 0;
}
long vec(Point p1, Point p2) {
return p1.x * p2.y - p2.x * p1.y;
}
boolean havePoint(Segment s1, Segment s2) {
if (s1.p1.equals(s2.p1))
return true;
if (s1.p1.equals(s2.p2)) {
s2.invert();
return true;
}
if (s1.p2.equals(s2.p2)) {
s1.invert();
s2.invert();
return true;
}
if (s1.p2.equals(s2.p1)) {
s1.invert();
return true;
}
return false;
}
boolean same(Segment s1, Segment s2) {
return s1.p1.equals(s2.p1) && s1.p2.equals(s2.p2) || s1.p1.equals(s2.p2) && s1.p2.equals(s2.p1);
}
boolean validAngle(Segment s1, Segment s2) {
return mul(s1.v, s2.v) >= 0;
}
long mul(Point p1, Point p2) {
return p1.x * p2.x + p1.y * p2.y;
}
class Point {
long x, y;
@Override
public boolean equals(Object obj) {
Point p = (Point) obj;
return x == p.x && y == p.y;
}
}
class Segment {
Point p1, p2;
Point v;
long a, b, c;
Segment() {
p1 = new Point();
p2 = new Point();
v = new Point();
}
boolean nul() {
return p1.equals(p2);
}
void getV() {
v.x = p2.x - p1.x;
v.y = p2.y - p1.y;
a = p2.y - p1.y;
b = p1.x - p2.x;
c = -(a * p1.x + b * p1.y);
}
boolean contains(Point p) {
if (a * p.x + b * p.y + c == 0) {
long minX = min(p1.x, p2.x);
long maxX = max(p1.x, p2.x);
long minY = min(p1.y, p2.y);
long maxY = max(p1.y, p2.y);
if (minX <= p.x && p.x <= maxX && minY <= p.y && p.y <= maxY)
return true;
}
return false;
}
void invert() {
v.x *= -1;
v.y *= -1;
}
}
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\n4 4 6 0\n4 1 5 2\n4 0 4 4\n0 0 0 6\n0 6 2 -4\n1 1 0 1\n0 0 0 5\n0 5 2 -1\n1 2 0 1"] | 1 second | ["YES\nNO\nYES"] | null | Java 6 | standard input | [
"implementation",
"geometry"
] | 254a61b06acd3f25e7287ab90be614f1 | The first line contains one integer t (1ββ€βtββ€β10000) β the number of test cases to solve. Each case consists of three lines. Each of these three lines contains four space-separated integers β coordinates of the endpoints of one of the segments. All coordinates do not exceed 108 by absolute value. All segments have positive length. | 2,000 | Output one line for each test case. Print Β«YESΒ» (without quotes), if the segments form the letter A and Β«NOΒ» otherwise. | standard output | |
PASSED | 0c513180f3a96fca630bf6dfc0d1c128 | train_003.jsonl | 1429286400 | Polycarp invented a new way to encode strings. Let's assume that we have string T, consisting of lowercase English letters. Let's choose several pairs of letters of the English alphabet in such a way that each letter occurs in at most one pair. Then let's replace each letter in T with its pair letter if there is a pair letter for it. For example, if you chose pairs (l, r), (p, q) and (a, o), then word "parallelogram" according to the given encoding principle transforms to word "qolorreraglom".Polycarpus already has two strings, S and T. He suspects that string T was obtained after applying the given encoding method from some substring of string S. Find all positions mi in S (1ββ€βmiββ€β|S|β-β|T|β+β1), such that T can be obtained fro substring SmiSmiβ+β1... Smiβ+β|T|β-β1 by applying the described encoding operation by using some set of pairs of English alphabet letters | 256 megabytes | import java.util.Comparator;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Random;
import java.util.Collection;
import java.util.List;
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.math.BigInteger;
import java.util.Collections;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author AlexFetisov
*/
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);
TaskF solver = new TaskF();
solver.solve(1, in, out);
out.close();
}
}
class TaskF {
public static final long P = 3731;
public static final long G = 1000000007L;
static Random r = new Random(P);
public static final long MOD1 = BigInteger.probablePrime(30, r).longValue();
public static final long MOD2 = BigInteger.probablePrime(30, r).longValue();
public static final long Pr = BigInteger.valueOf(P).modInverse(BigInteger.valueOf(MOD1)).longValue();
public static final long Gr = BigInteger.valueOf(G).modInverse(BigInteger.valueOf(MOD2)).longValue();
static int MAXN = 3 * 100000;
static long[] pPowRev = new long[MAXN];
static long[] gPow = new long[MAXN];
static long[] gPowRev = new long[MAXN];
static long[] pPow = new long[MAXN];
static {
pPow[0] = 1;
pPowRev[0] = 1;
gPow[0] = 1;
gPowRev[0] = 1;
for (int i = 1; i < MAXN; ++i) {
pPow[i] = (pPow[i-1] * P) % MOD1;
pPowRev[i] = (pPowRev[i-1] * Pr) % MOD1;
gPow[i] = (gPow[i-1] * G) % MOD2;
gPowRev[i] = (gPowRev[i-1] * Gr) % MOD2;
}
}
char[] c;
char[] p;
int n, m;
long[][] sumPowP;
long[][] sumPowG;
void prepareSums() {
sumPowP = new long[26][n];
sumPowG = new long[26][n];
for (int i = 0; i < n; ++i) {
for (int j = 0; j < 26; ++j) {
if (c[i] - 'a' == j) {
sumPowP[j][i] = pPow[i];
sumPowG[j][i] = gPow[i];
}
if (i != 0) {
sumPowP[j][i] += sumPowP[j][i-1];
sumPowG[j][i] += sumPowG[j][i-1];
sumPowP[j][i] %= MOD1;
sumPowG[j][i] %= MOD2;
}
}
}
}
int[][] first;
long hashPP, hashPG;
void prepareFirst() {
first = new int[26][n];
ArrayUtils.fill(first, -1);
for (int i = n-1; i >= 0; --i) {
for (int j = 0; j < 26; ++j) {
if (i < n-1) {
first[j][i] = first[j][i + 1];
}
if (c[i] - 'a' == j) {
first[j][i] = i;
}
}
}
for (int i = n-1; i >= 0; --i) {
for (int j = 0; j < 26; ++j) {
if (first[j][i] == -1) {
first[j][i] = 100000000;
}
}
}
}
int[] firstMet;
void prepareP() {
firstMet = new int[26];
Arrays.fill(firstMet, -1);
for (int i = 0; i < m; ++i) {
int x = p[i] - 'a';
if (firstMet[x] == -1) {
firstMet[x] = i;
}
}
// Integer[] letters = new Integer[26];
for (int i = 0; i < 26; ++i) {
// letters[i] = i;
if (firstMet[i] == -1) {
firstMet[i] = 100000000;
}
}
// Arrays.sort(letters, new Comparator<Integer>() {
// @Override
// public int compare(Integer a, Integer b) {
// return firstMet[a] - firstMet[b];
// }
// });
// toLetterP = new int[26];
// boolean[] mark = new boolean[26];
// int[] toLetter = new int[26];
// Arrays.fill(toLetter, -1);
// for (int i = 0; i < 26; ++i) {
// if (toLetter[letters[i]] == -1) {
// toLetter[letters[i]] = i;
// toLetter[i] = letters[i];
// }
// }
hashPG = hashPP = 0;
for (int i = 0; i < m; ++i) {
hashPP += (pPow[i] * (p[i] - 'a' + 1)) % MOD1;
hashPP %= MOD1;
hashPG += (gPow[i] * (p[i] - 'a' + 1)) % MOD2;
hashPG %= MOD2;
}
}
public void solve(int testNumber, InputReader in, PrintWriter out) {
n = in.nextInt();
m = in.nextInt();
c = in.nextString().toCharArray();
p = in.nextString().toCharArray();
prepareP();
prepareSums();
prepareFirst();
Integer[] letters = new Integer[26];
for (int i = 0; i < 26; ++i) {
letters[i] = i;
}
int[] toLetter = new int[26];
List<Integer> res = new ArrayList<Integer>();
loop: for (int begin = 0, end = m-1; end < n; ++begin, ++end) {
final int finalBegin = begin;
for (int i = 0; i < 26; ++i) {
letters[i] = i;
}
// Arrays.sort(letters, new Comparator<Integer>() {
// @Override
// public int compare(Integer a, Integer b) {
// return first[a][finalBegin] - first[b][finalBegin];
// }
// });
long curHashP = 0;
long curHashG = 0;
Arrays.fill(toLetter, -1);
for (int i = 0; i < 26; ++i) {
if (firstMet[i] < m) {
if (toLetter[c[begin + firstMet[i]] - 'a'] != -1 || toLetter[i] != -1) {
if (toLetter[c[begin + firstMet[i]] - 'a'] != i || toLetter[i] != c[begin + firstMet[i]] - 'a') continue loop;
continue;
}
toLetter[c[begin + firstMet[i]] - 'a'] = i;
toLetter[i] = c[begin + firstMet[i]] - 'a';
}
}
for (int i = 0; i < 26; ++i){
if (firstMet[i] >= m && toLetter[i] == -1) {
if (first[i][begin] < begin + m) {
continue loop;
}
}
}
// for (int j = 0; j < 26; ++j) {
// toLetter[letters[j]] = j;
// }
// Arrays.fill(toLetter, -1);
// for (int i = 0; i < 26; ++i) {
// if (toLetter[letters[i]] == -1) {
// toLetter[letters[i]] = i;
// toLetter[i] = letters[i];
// }
// }
for (int j = 0; j < 26; ++j) {
long hp = getSum(sumPowP, begin, end, j, MOD1);
hp *= pPowRev[begin];
hp %= MOD1;
hp *= (toLetter[j] + 1);
hp %= MOD1;
curHashP += hp;
curHashP %= MOD1;
long hg = getSum(sumPowG, begin, end, j, MOD2);
hg *= gPowRev[begin];
hg %= MOD2;
hg *= (toLetter[j] + 1);
hg %= MOD2;
curHashG += hg;
curHashG %= MOD2;
}
if (curHashP == hashPP && curHashG == hashPG) {
res.add(begin);
}
}
out.println(res.size());
for (int x : res) {
out.print((x + 1) + " ");
}
}
private long getSum(long[][] sum, int from, int to, int id, long M) {
if (from == 0) return sum[id][to];
return (sum[id][to] - sum[id][from-1] + M) % M;
}
}
class ArrayUtils {
public static void fill(int[][] f, int value) {
for (int i = 0; i < f.length; ++i) {
Arrays.fill(f[i], value);
}
}
}
class InputReader {
private BufferedReader reader;
private StringTokenizer stt;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
return null;
}
}
public String nextString() {
while (stt == null || !stt.hasMoreTokens()) {
stt = new StringTokenizer(nextLine());
}
return stt.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextString());
}
}
| Java | ["11 5\nabacabadaba\nacaba", "21 13\nparaparallelogramgram\nqolorreraglom"] | 3 seconds | ["3\n1 3 7", "1\n5"] | null | Java 8 | standard input | [
"hashing",
"string suffix structures",
"strings"
] | faa26846744b7b7f90ba1b521fee0bc6 | The first line of the input contains two integers, |S| and |T| (1ββ€β|T|ββ€β|S|ββ€β2Β·105) β the lengths of string S and string T, respectively. The second and third line of the input contain strings S and T, respectively. Both strings consist only of lowercase English letters. | 2,400 | Print number k β the number of suitable positions in string S. In the next line print k integers m1,βm2,β...,βmk β the numbers of the suitable positions in the increasing order. | standard output | |
PASSED | c0cf870af499efef1711592009b76212 | train_003.jsonl | 1429286400 | Polycarp invented a new way to encode strings. Let's assume that we have string T, consisting of lowercase English letters. Let's choose several pairs of letters of the English alphabet in such a way that each letter occurs in at most one pair. Then let's replace each letter in T with its pair letter if there is a pair letter for it. For example, if you chose pairs (l, r), (p, q) and (a, o), then word "parallelogram" according to the given encoding principle transforms to word "qolorreraglom".Polycarpus already has two strings, S and T. He suspects that string T was obtained after applying the given encoding method from some substring of string S. Find all positions mi in S (1ββ€βmiββ€β|S|β-β|T|β+β1), such that T can be obtained fro substring SmiSmiβ+β1... Smiβ+β|T|β-β1 by applying the described encoding operation by using some set of pairs of English alphabet letters | 256 megabytes | import java.util.*;
import java.io.*;
public class f
{
public static void main(String[] arg) throws IOException
{
new f();
}
public f() throws IOException
{
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int m = in.nextInt();
if(m == 1)
{
out.println(n);
for(int i = 1; i <= n; i++) out.print(i + " ");
}
else
{
char[] s = in.next().toCharArray();
char[] p = in.next().toCharArray();
int[] ts = new int[n];
int[] ts2 = new int[n];
int[] tp = new int[m];
int[] tp2 = new int[m];
int[] ret = new int[n+m+1];
int[] ret2 = new int[n+m+1];
boolean[][] possible = new boolean[26][n];
int[] z = new int[n+m+1];
int[] z1 = new int[n+m+1];
for(int i = 0; i < 26; i++)
{
for(int j = i; j < 26; j++)
{
for(int k = 0; k < m; k++)
{
if(p[k]-'a' == i) tp[k] = 1;
else tp[k] = 0;
if(p[k]-'a' == j) tp2[k] = 1;
else tp2[k] = 0;
}
for(int k = 0; k < n; k++)
{
if(s[k]-'a' == j) ts[k] = 1;
else ts[k] = 0;
if(s[k]-'a' == i) ts2[k] = 1;
else ts2[k] = 0;
}
fuse(tp, ts, ret);
fuse(tp2, ts2, ret2);
getZV(ret, z);
getZV(ret2, z1);
for(int k = 0; k < n; k++)
{
if(z[k+1+p.length] >= p.length && z1[k+1+p.length] >= p.length)
{
possible[i][k] = true;
possible[j][k] = true;
}
}
}
}
ArrayList<Integer> ans =new ArrayList<Integer>();
for(int i = 0; i < n; i++)
{
boolean f = true;
for(int j = 0; j < 26; j++)
{
if(!possible[j][i]) f = false;
}
if(f) ans.add(i);
}
out.println(ans.size());
for(int v : ans) out.print((v+1)+" ");
}
in.close(); out.close();
}
public void fuse(int[] p, int[] s, int[] ret)
{
for(int i = 0; i < p.length; i++) ret[i] = p[i];
ret[p.length] = oo;
for(int i = 0; i < s.length; i++) ret[i+p.length+1] = s[i];
}
int oo = -1_000_000;
boolean equal(int a, int b)
{
if(a == oo || b == oo) return false;
if(a == b) return true;
if(a < 0 && b < 0) return true;
if(a < 0 && b >= -a) return true;
return false;
}
public void getZV(int[] s, int[] z)
{
int n = s.length;
Arrays.fill(z, 0);
int L = 0, R = 0;
for (int i = 1; i < n; i++) {
if (i > R) {
L = R = i;
while (R < n && s[R-L] == s[R]) R++;
z[i] = R-L; R--;
} else {
int k = i-L;
if (z[k] < R-i+1) z[i] = z[k];
else {
L = i;
while (R < n && s[R-L] == s[R]) R++;
z[i] = R-L; R--;
}
}
}
}
class FastScanner
{
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in)
{
br = new BufferedReader(new InputStreamReader(in));
st = new StringTokenizer("");
}
public String next() throws IOException
{
while(!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 void close() throws IOException
{
br.close();
}
}
} | Java | ["11 5\nabacabadaba\nacaba", "21 13\nparaparallelogramgram\nqolorreraglom"] | 3 seconds | ["3\n1 3 7", "1\n5"] | null | Java 8 | standard input | [
"hashing",
"string suffix structures",
"strings"
] | faa26846744b7b7f90ba1b521fee0bc6 | The first line of the input contains two integers, |S| and |T| (1ββ€β|T|ββ€β|S|ββ€β2Β·105) β the lengths of string S and string T, respectively. The second and third line of the input contain strings S and T, respectively. Both strings consist only of lowercase English letters. | 2,400 | Print number k β the number of suitable positions in string S. In the next line print k integers m1,βm2,β...,βmk β the numbers of the suitable positions in the increasing order. | standard output | |
PASSED | 0820bdb0d2a7b512254c40d4d1a89fc7 | train_003.jsonl | 1429286400 | Polycarp invented a new way to encode strings. Let's assume that we have string T, consisting of lowercase English letters. Let's choose several pairs of letters of the English alphabet in such a way that each letter occurs in at most one pair. Then let's replace each letter in T with its pair letter if there is a pair letter for it. For example, if you chose pairs (l, r), (p, q) and (a, o), then word "parallelogram" according to the given encoding principle transforms to word "qolorreraglom".Polycarpus already has two strings, S and T. He suspects that string T was obtained after applying the given encoding method from some substring of string S. Find all positions mi in S (1ββ€βmiββ€β|S|β-β|T|β+β1), such that T can be obtained fro substring SmiSmiβ+β1... Smiβ+β|T|β-β1 by applying the described encoding operation by using some set of pairs of English alphabet letters | 256 megabytes | import java.math.*;
import java.util.*;
public class Main {
public static int mod=1000000007;
public static int NN=200010;
public static int n, m, f=1;
public static int[][] ha=new int[NN][27];
public static int[] hb=new int[27];
public static int calc(int st, int ed, int i) {
return (ha[ed][i]-(int)((long)f*ha[st][i]%mod)+mod)%mod;
}
public static boolean can(int u) {
int[] x=new int[27], flag=new int[27];
for(int i=0; i<26; i++) x[i]=calc(u, u+m, i);
for(int i=0; i<26; i++) if(flag[i]==0 && x[i]!=hb[i]) {
boolean ok=true;
flag[i]=1;
for(int j=i+1; j<26; j++) {
if(flag[j]==0 && x[i]==hb[j] && x[j]==hb[i]) {
flag[j]=1; ok=false; break;
}
}
if(ok) return false;
}
return true;
}
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
n=in.nextInt(); m=in.nextInt();
String a=in.next(), b=in.next();
for(int i=0; i<m; i++) f=(2*f)%mod;
for(int i=0; i<n; i++) {
for(int j=0; j<26; j++) ha[i+1][j]=2*ha[i][j]%mod;
int u=a.charAt(i)-'a';
ha[i+1][u]=(ha[i+1][u]+1)%mod;
}
for(int i=0; i<m; i++) {
for(int j=0; j<26; j++) hb[j]=2*hb[j]%mod;
int u=b.charAt(i)-'a';
hb[u]=(hb[u]+1)%mod;
}
Vector<Integer> ans=new Vector<Integer>();
for(int i=0; i+m<=n; i++) {
if(can(i)) ans.add(i+1);
}
System.out.println(ans.size());
for(int i=0; i<ans.size(); i++) System.out.print(ans.elementAt(i)+" ");
}
}
| Java | ["11 5\nabacabadaba\nacaba", "21 13\nparaparallelogramgram\nqolorreraglom"] | 3 seconds | ["3\n1 3 7", "1\n5"] | null | Java 8 | standard input | [
"hashing",
"string suffix structures",
"strings"
] | faa26846744b7b7f90ba1b521fee0bc6 | The first line of the input contains two integers, |S| and |T| (1ββ€β|T|ββ€β|S|ββ€β2Β·105) β the lengths of string S and string T, respectively. The second and third line of the input contain strings S and T, respectively. Both strings consist only of lowercase English letters. | 2,400 | Print number k β the number of suitable positions in string S. In the next line print k integers m1,βm2,β...,βmk β the numbers of the suitable positions in the increasing order. | standard output | |
PASSED | 16b42c9d0bc6737cc105b5b70fda0532 | train_003.jsonl | 1429286400 | Polycarp invented a new way to encode strings. Let's assume that we have string T, consisting of lowercase English letters. Let's choose several pairs of letters of the English alphabet in such a way that each letter occurs in at most one pair. Then let's replace each letter in T with its pair letter if there is a pair letter for it. For example, if you chose pairs (l, r), (p, q) and (a, o), then word "parallelogram" according to the given encoding principle transforms to word "qolorreraglom".Polycarpus already has two strings, S and T. He suspects that string T was obtained after applying the given encoding method from some substring of string S. Find all positions mi in S (1ββ€βmiββ€β|S|β-β|T|β+β1), such that T can be obtained fro substring SmiSmiβ+β1... Smiβ+β|T|β-β1 by applying the described encoding operation by using some set of pairs of English alphabet letters | 256 megabytes | import java.io.BufferedWriter;
import java.util.InputMismatchException;
import java.util.ArrayList;
import java.util.NoSuchElementException;
import java.util.List;
import java.math.BigInteger;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.io.IOException;
import java.util.Arrays;
import java.io.InputStream;
import java.util.Random;
import java.io.OutputStreamWriter;
import java.util.Comparator;
/**
* 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);
OutputWriter out = new OutputWriter(outputStream);
TaskF solver = new TaskF();
solver.solve(1, in, out);
out.close();
}
}
class TaskF {
char[] S, T, f;
int[] prevS, prevT, first;
long[] power, revPower, hashS, hashT;
long MOD, MULTIPLIER, REVERSE_MULTIPLIER;
IntList list, firstS, firstT;
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n=in.readInt(), m=in.readInt();
S= IOUtils.readCharArray(in, n);
T= IOUtils.readCharArray(in, m);
firstS=new IntArrayList();
firstT=new IntArrayList();
prevS=new int[n];
prevT=new int[m];
hashS=new long[n];
hashT=new long[m];
calcFirst(S, prevS, firstS);
calcFirst(T, prevT, firstT);
first=firstT.toArray();
power=new long[n+1];
revPower=new long[n+1];
Random random = new Random(System.currentTimeMillis());
MOD = IntegerUtils.nextPrime((long) (1e9 + random.nextInt((int) 1e9)));
MULTIPLIER = random.nextInt((int) 1e9 - 200001) + 200001;
REVERSE_MULTIPLIER = IntegerUtils.reverse(MULTIPLIER, MOD);
power[0]=revPower[0]=1;
for (int i=1; i<=n; i++) {
power[i]=power[i-1]*MULTIPLIER%MOD;
revPower[i]=revPower[i-1]*REVERSE_MULTIPLIER%MOD;
}
calcHash(prevS, hashS);
calcHash(prevT, hashT);
f=new char[26];
list=new IntArrayList();
for (int i=0; i+m<=n; i++) if (f(i)) list.add(i+1);
out.printLine(list.size());
out.printLine(list.toArray());
}
void calcFirst(char[] str, int[] prev, IntList first) {
int[] aux= ArrayUtils.createArray(26, -1);
for (int i=0; i<str.length; i++) {
int c=str[i]-'a';
if (aux[c]==-1) {
first.add(i);
prev[i]=S.length;
} else prev[i]=i-aux[c];
aux[c]=i;
}
}
void calcHash(int[] str, long[] hash) {
hash[0]=str[0];
for (int i=1; i<str.length; i++) hash[i]=(hash[i-1]+power[i]*str[i]%MOD)%MOD;
}
long getHashS(int from, int to) {
//System.out.println(from+" "+to+" "+hashS.length+" "+revPower.length);
return from>to?0:(hashS[to]-(from>0?hashS[from-1]:0)+MOD)%MOD*revPower[from]%MOD;
}
long getHashT(int from, int to) {
return from>to?0:(hashT[to]-(from>0?hashT[from-1]:0)+MOD)%MOD*revPower[from]%MOD;
}
boolean f(int from) {
long hs=getHashS(0+from, first[0]-1+from), ht=getHashT(0, first[0]-1);
for (int i=1; i<first.length; i++) {
hs=(hs+getHashS(first[i-1]+1+from, first[i]-1+from)*power[first[i-1]+1]%MOD)%MOD;
ht=(ht+getHashT(first[i-1]+1, first[i]-1)*power[first[i-1]+1]%MOD)%MOD;
}
hs=(hs+getHashS(first[first.length-1]+1+from, T.length-1+from)*power[first[first.length-1]+1]%MOD)%MOD;
ht=(ht+getHashT(first[first.length-1]+1, T.length-1)*power[first[first.length-1]+1]%MOD)%MOD;
if (hs!=ht) return false;
Arrays.fill(f, ' ');
for (int i: first) {
char cs=S[from+i], ct=T[i];
if ((f[cs-'a']!=' ' && f[cs-'a']!=ct) || (f[ct-'a']!=' ' && f[ct-'a']!=cs)) return false;
f[cs-'a']=ct;
f[ct-'a']=cs;
}
return true;
}
}
abstract class IntList extends IntCollection implements Comparable<IntList> {
public abstract int get(int index);
public IntIterator iterator() {
return new IntIterator() {
private int size = size();
private int index = 0;
public int value() throws NoSuchElementException {
if (!isValid())
throw new NoSuchElementException();
return get(index);
}
public void advance() throws NoSuchElementException {
if (!isValid())
throw new NoSuchElementException();
index++;
}
public boolean isValid() {
return index < size;
}
};
}
public int hashCode() {
int hashCode = 1;
for (IntIterator i = iterator(); i.isValid(); i.advance())
hashCode = 31 * hashCode + i.value();
return hashCode;
}
public boolean equals(Object obj) {
if (!(obj instanceof IntList))
return false;
IntList list = (IntList)obj;
if (list.size() != size())
return false;
IntIterator i = iterator();
IntIterator j = list.iterator();
while (i.isValid()) {
if (i.value() != j.value())
return false;
i.advance();
j.advance();
}
return true;
}
public int compareTo(IntList o) {
IntIterator i = iterator();
IntIterator j = o.iterator();
while (true) {
if (i.isValid()) {
if (j.isValid()) {
if (i.value() != j.value()) {
if (i.value() < j.value())
return -1;
else
return 1;
}
} else
return 1;
} else {
if (j.isValid())
return -1;
else
return 0;
}
i.advance();
j.advance();
}
}
}
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 char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public void print(int[] array) {
for (int i = 0; i < array.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(array[i]);
}
}
public void printLine(int[] array) {
print(array);
writer.println();
}
public void close() {
writer.close();
}
public void printLine(int i) {
writer.println(i);
}
}
class IOUtils {
public static char[] readCharArray(InputReader in, int size) {
char[] array = new char[size];
for (int i = 0; i < size; i++)
array[i] = in.readCharacter();
return array;
}
}
class IntArrayList extends IntList {
private int[] array;
private int size;
public IntArrayList() {
this(10);
}
public IntArrayList(int capacity) {
array = new int[capacity];
}
public int get(int index) {
if (index >= size)
throw new IndexOutOfBoundsException();
return array[index];
}
public int size() {
return size;
}
public void add(int value) {
ensureCapacity(size + 1);
array[size++] = value;
}
public void ensureCapacity(int newCapacity) {
if (newCapacity > array.length) {
int[] newArray = new int[Math.max(newCapacity, array.length << 1)];
System.arraycopy(array, 0, newArray, 0, size);
array = newArray;
}
}
public int[] toArray() {
int[] array = new int[size];
System.arraycopy(this.array, 0, array, 0, size);
return array;
}
}
abstract class IntCollection {
public abstract IntIterator iterator();
public abstract int size();
public abstract void add(int value);
public int[] toArray() {
int size = size();
int[] array = new int[size];
int i = 0;
for (IntIterator iterator = iterator(); iterator.isValid(); iterator.advance())
array[i++] = iterator.value();
return array;
}
}
class IntegerUtils {
public static long power(long base, long exponent, long mod) {
if (base >= mod)
base %= mod;
if (exponent == 0)
return 1 % mod;
long result = power(base, exponent >> 1, mod);
result = result * result % mod;
if ((exponent & 1) != 0)
result = result * base % mod;
return result;
}
public static long reverse(long number, long module) {
return power(number, module - 2, module);
}
public static boolean isPrime(long number) {
if (number < 2)
return false;
for (long i = 2; i * i <= number; i++) {
if (number % i == 0)
return false;
}
return true;
}
public static long nextPrime(long from) {
if (from <= 2)
return 2;
from += 1 - (from & 1);
while (!isPrime(from))
from += 2;
return from;
}
}
class ArrayUtils {
public static int[] createArray(int count, int value) {
int[] array = new int[count];
Arrays.fill(array, value);
return array;
}
}
interface IntIterator {
public int value() throws NoSuchElementException;
/*
* @throws NoSuchElementException only if iterator already invalid
*/
public void advance() throws NoSuchElementException;
public boolean isValid();
}
| Java | ["11 5\nabacabadaba\nacaba", "21 13\nparaparallelogramgram\nqolorreraglom"] | 3 seconds | ["3\n1 3 7", "1\n5"] | null | Java 8 | standard input | [
"hashing",
"string suffix structures",
"strings"
] | faa26846744b7b7f90ba1b521fee0bc6 | The first line of the input contains two integers, |S| and |T| (1ββ€β|T|ββ€β|S|ββ€β2Β·105) β the lengths of string S and string T, respectively. The second and third line of the input contain strings S and T, respectively. Both strings consist only of lowercase English letters. | 2,400 | Print number k β the number of suitable positions in string S. In the next line print k integers m1,βm2,β...,βmk β the numbers of the suitable positions in the increasing order. | standard output | |
PASSED | e713e6d8e70e85b3a51019b65be6218f | train_003.jsonl | 1429286400 | Polycarp invented a new way to encode strings. Let's assume that we have string T, consisting of lowercase English letters. Let's choose several pairs of letters of the English alphabet in such a way that each letter occurs in at most one pair. Then let's replace each letter in T with its pair letter if there is a pair letter for it. For example, if you chose pairs (l, r), (p, q) and (a, o), then word "parallelogram" according to the given encoding principle transforms to word "qolorreraglom".Polycarpus already has two strings, S and T. He suspects that string T was obtained after applying the given encoding method from some substring of string S. Find all positions mi in S (1ββ€βmiββ€β|S|β-β|T|β+β1), such that T can be obtained fro substring SmiSmiβ+β1... Smiβ+β|T|β-β1 by applying the described encoding operation by using some set of pairs of English alphabet letters | 256 megabytes | import java.io.*;
import java.util.*;
import java.math.*;
public class F implements Runnable {
private static BufferedReader in;
private static PrintWriter out;
private static StringTokenizer st;
private static Random rnd;
final int totalLetters = 26;
private void solve() throws IOException {
nextInt();
nextInt();
String firstWord = nextToken();
String secondWord = nextToken();
int[][] firstDifferences = getDifferences(firstWord);
int[][] secondDifferences = getDifferences(secondWord);
int[][] firstPoses = getPoses(firstWord);
int[][] secondPoses = getPoses(secondWord);
int[] z = new int[firstWord.length() + 1 + secondWord.length()];
int[][] matches = new int[totalLetters][firstWord.length()];
for (int i = 0; i < totalLetters; i++) {
Arrays.fill(matches[i], -1);
for (int j = 0; j < totalLetters; j++) {
if (secondDifferences[i] != null
&& firstDifferences[j] != null
&& secondDifferences[i].length <= firstDifferences[j].length
&& secondDifferences[i].length >= 1) {
calcZFunction(secondDifferences[i], firstDifferences[j], z);
int textLength = firstDifferences[j].length;
int wordLength = secondDifferences[i].length;
for (int k = 0; k < textLength; k++) {
int pos = k + 1 + wordLength;
if (z[pos] > wordLength)
throw new AssertionError();
if (z[pos] == wordLength) {
int startPos = firstPoses[j][k] - secondPoses[i][0];
// out.println(i + " " + j + " " + k + " " +
// startPos);
if (startPos >= 0) {
// out.println("Letter " + i
// + " can start with letter " + j
// + " + from pos " + startPos);
if (matches[i][startPos] != -1)
throw new AssertionError();
matches[i][startPos] = j;
}
}
}
}
}
}
int[] currentMatches = new int[totalLetters];
List<Integer> result = new ArrayList<>();
for (int pos = 0; pos <= firstWord.length() - secondWord.length(); pos++) {
boolean can = true;
Arrays.fill(currentMatches, -1);
for (int i = 0; i < totalLetters; i++) {
if (secondPoses[i].length >= 2) {
int match = matches[i][pos];
if (match == -1) {
can = false;
} else {
if (currentMatches[match] != -1
&& currentMatches[match] != i) {
can = false;
} else if (currentMatches[i] != -1
&& currentMatches[i] != match) {
can = false;
}
currentMatches[match] = i;
currentMatches[i] = match;
}
} else if (secondPoses[i].length >= 1) {
int secondPos = secondPoses[i][0];
int match = firstWord.charAt(pos + secondPos) - 'a';
if (currentMatches[match] != -1
&& currentMatches[match] != i) {
can = false;
} else if (currentMatches[i] != -1
&& currentMatches[i] != match) {
can = false;
}
currentMatches[match] = i;
currentMatches[i] = match;
}
}
if (can) {
result.add(pos + 1);
}
}
out.println(result.size());
for (int i : result) {
out.print(i);
out.print(' ');
}
out.print('\n');
}
private int[][] getPoses(String word) {
int[][] result = new int[totalLetters][];
for (int i = 0; i < totalLetters; i++) {
char letter = (char) ('a' + i);
int cnt = 0;
for (int j = 0; j < word.length(); j++) {
char c = word.charAt(j);
if (letter == c)
++cnt;
}
result[i] = new int[cnt];
cnt = 0;
for (int j = 0; j < word.length(); j++) {
char c = word.charAt(j);
if (c == letter) {
result[i][cnt++] = j;
}
}
}
return result;
}
private void calcZFunction(int[] word, int[] text, int[] z) {
int wordLength = word.length;
int textLength = text.length;
Arrays.fill(z, 0, wordLength + textLength + 1, 0);
int l = 0, r = -1;
for (int i = 1; i <= wordLength + textLength; i++) {
if (l <= i && i < r) {
z[i] = Math.min(r - i, z[i - l]);
}
while (i + z[i] <= wordLength + textLength
&& getChar(word, text, i + z[i]) == getChar(word, text,
z[i])) {
++z[i];
}
if (i + z[i] > r) {
l = i;
r = i + z[i];
}
}
}
private int getChar(int[] word, int[] text, int pos) {
if (pos < word.length)
return word[pos];
else if (pos == word.length)
return -1;
else
return text[pos - word.length - 1];
}
private int[][] getDifferences(String word) {
int[][] result = new int[totalLetters][];
for (int i = 0; i < totalLetters; i++) {
char letter = (char) ('a' + i);
int cnt = 0;
for (int j = 0; j < word.length(); j++) {
char c = word.charAt(j);
if (letter == c)
++cnt;
}
if (cnt == 0)
continue;
result[i] = new int[cnt - 1];
int lastPos = -1;
cnt = 0;
for (int j = 0; j < word.length(); j++) {
char c = word.charAt(j);
if (c == letter) {
if (lastPos != -1)
result[i][cnt++] = j - lastPos;
lastPos = j;
}
}
}
return result;
}
public static void main(String[] args) {
new F().run();
}
public void run() {
try {
final String className = this.getClass().getName().toLowerCase();
try {
in = new BufferedReader(new FileReader(className + ".in"));
out = new PrintWriter(new FileWriter(className + ".out"));
// in = new BufferedReader(new FileReader("input.txt"));
// out = new PrintWriter(new FileWriter("output.txt"));
} catch (FileNotFoundException e) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
rnd = new Random();
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(42);
}
}
private String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String line = in.readLine();
if (line == null)
return null;
st = new StringTokenizer(line);
}
return st.nextToken();
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | Java | ["11 5\nabacabadaba\nacaba", "21 13\nparaparallelogramgram\nqolorreraglom"] | 3 seconds | ["3\n1 3 7", "1\n5"] | null | Java 8 | standard input | [
"hashing",
"string suffix structures",
"strings"
] | faa26846744b7b7f90ba1b521fee0bc6 | The first line of the input contains two integers, |S| and |T| (1ββ€β|T|ββ€β|S|ββ€β2Β·105) β the lengths of string S and string T, respectively. The second and third line of the input contain strings S and T, respectively. Both strings consist only of lowercase English letters. | 2,400 | Print number k β the number of suitable positions in string S. In the next line print k integers m1,βm2,β...,βmk β the numbers of the suitable positions in the increasing order. | standard output | |
PASSED | 6558fc31f5199f138eb50a3f409d14c8 | train_003.jsonl | 1429286400 | Polycarp invented a new way to encode strings. Let's assume that we have string T, consisting of lowercase English letters. Let's choose several pairs of letters of the English alphabet in such a way that each letter occurs in at most one pair. Then let's replace each letter in T with its pair letter if there is a pair letter for it. For example, if you chose pairs (l, r), (p, q) and (a, o), then word "parallelogram" according to the given encoding principle transforms to word "qolorreraglom".Polycarpus already has two strings, S and T. He suspects that string T was obtained after applying the given encoding method from some substring of string S. Find all positions mi in S (1ββ€βmiββ€β|S|β-β|T|β+β1), such that T can be obtained fro substring SmiSmiβ+β1... Smiβ+β|T|β-β1 by applying the described encoding operation by using some set of pairs of English alphabet letters | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.StringTokenizer;
public class F {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
int[][][] z;
void solve() throws IOException {
int sn = readInt();
int tn = readInt();
char[] s = readString().toCharArray();
char[] t = readString().toCharArray();
List<Integer>[] inFirst = new List[26];
List<Integer>[] inSecond = new List[26];
for (int i = 0; i < 26; i++) {
inFirst[i] = new ArrayList<Integer>();
inSecond[i] = new ArrayList<Integer>();
}
int[] position = new int[sn];
for (int i = 0; i < sn; i++) {
int index = s[i] - 'a';
position[i] = inFirst[index].size();
inFirst[index].add(i);
}
for (int i = 0; i < tn; i++) {
inSecond[t[i] - 'a'].add(i);
}
int[][] first = new int[26][];
int[][] second = new int[26][];
for (int i = 0; i < 26; i++) {
if (inFirst[i].size() != 0) {
first[i] = new int[inFirst[i].size() - 1];
for (int j = 1; j < inFirst[i].size(); j++) {
first[i][j - 1] = inFirst[i].get(j) - inFirst[i].get(j - 1);
}
} else {
first[i] = new int[0];
}
if (inSecond[i].size() != 0) {
second[i] = new int[inSecond[i].size() - 1];
for (int j = 1; j < inSecond[i].size(); j++) {
second[i][j - 1] = inSecond[i].get(j) - inSecond[i].get(j - 1);
}
} else {
second[i] = new int[0];
}
}
int[][] tZ = new int[26][];
for (int j = 0; j < 26; ++j) {
tZ[j] = z(second[j]);
}
z = new int[26][][];
for (int i = 0; i < 26; i++) {
z[i] = new int[26][];
for (int j = 0; j < 26; j++) {
z[i][j] = zFunction(first[i], second[j], tZ[j]);
}
}
List<Integer> result = new ArrayList<Integer>();
int[] match = new int[26];
for (int i = 0; i < sn - tn + 1; i++) {
boolean res = true;
Arrays.fill(match, -1);
for (int c = 0; c < 26; c++) {
if (inSecond[c].size() == 0) {
continue;
}
int to = s[i + inSecond[c].get(0)] - 'a';
if (match[to] != -1 && match[to] != c) {
res = false;
break;
}
if(match[c] != -1 && match[c] != to) {
res = false;
break;
}
match[to] = c;
match[c] = to;
int curToPos = inSecond[c].get(0) + i;
int curPos = position[curToPos];
if (z[to][c].length == 0) {
if (second[c].length != 0) {
res = false;
break;
}
} else if(second[c].length == 0) {
} else if (curPos >= z[to][c].length || z[to][c][curPos] != second[c].length) {
res = false;
break;
}
}
if (res) {
result.add(i + 1);
}
}
out.println(result.size());
for (int i : result) {
out.print(i + " ");
}
}
int[] z(int[] s) {
int n = s.length;
int[] z = new int[n];
for (int i = 1, l = 0, r = 0; i < n; ++i) {
if (i <= r)
z[i] = Math.min(r - i + 1, z[i - l]);
while (i + z[i] < n && s[z[i]] == s[i + z[i]])
++z[i];
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
if (z.length > 0) z[0] = n;
return z;
}
int[] zFunction(int[] s, int[] p, int[] pZ) {
int n = s.length;
int[] z = new int[n];
for (int i = 0, l = 0, r = 0; i < n; ++i) {
if (i > 0 && i <= r)
z[i] = Math.min(r - i + 1, pZ[i - l]);
while (i + z[i] < n && z[i] < p.length && p[z[i]] == s[i + z[i]])
++z[i];
if (i + z[i] - 1 > r) {
l = i;
r = i + z[i] - 1;
}
}
return z;
}
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());
}
int[] readArr(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = readInt();
}
return res;
}
long[] readArrL(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = readLong();
}
return res;
}
public static void main(String[] args) {
new F().run();
}
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);
}
}
} | Java | ["11 5\nabacabadaba\nacaba", "21 13\nparaparallelogramgram\nqolorreraglom"] | 3 seconds | ["3\n1 3 7", "1\n5"] | null | Java 8 | standard input | [
"hashing",
"string suffix structures",
"strings"
] | faa26846744b7b7f90ba1b521fee0bc6 | The first line of the input contains two integers, |S| and |T| (1ββ€β|T|ββ€β|S|ββ€β2Β·105) β the lengths of string S and string T, respectively. The second and third line of the input contain strings S and T, respectively. Both strings consist only of lowercase English letters. | 2,400 | Print number k β the number of suitable positions in string S. In the next line print k integers m1,βm2,β...,βmk β the numbers of the suitable positions in the increasing order. | standard output | |
PASSED | c91018de09e44138ef7aef0783fa9015 | train_003.jsonl | 1462633500 | Bear Limak has n colored balls, arranged in one long row. Balls are numbered 1 through n, from left to right. There are n possible colors, also numbered 1 through n. The i-th ball has color ti.For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant.There are non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class ProblemC {
public static void main(String[] args) {
InputReader in = new InputReader();
PrintWriter out = new PrintWriter(System.out);
new ProblemC().solve(in, out);
out.close();
}
public void solve(InputReader in, PrintWriter out) {
int n = in.nextInt();
int[] t = new int[n];
for (int i = 0; i < n; i++) {
t[i] = in.nextInt() - 1;
}
int[] res = new int[n];
for (int i = 0; i < n; i++) {
int[] countColor = new int[n];
int maxCount = 0;
int maxColor = t[i];
for (int j = i; j < n; j++) {
countColor[t[j]]++;
if (countColor[t[j]] > maxCount) {
res[t[j]]++;
maxColor = t[j];
maxCount = countColor[t[j]];
} else if (countColor[t[j]] == maxCount) {
res[Math.min(t[j], maxColor)]++;
maxColor = Math.min(t[j], maxColor);
} else {
res[maxColor]++;
}
}
}
for (int i = 0; i < n; i++) {
out.print(res[i] + " ");
}
}
static class InputReader {
public BufferedReader br;
public StringTokenizer st;
public InputReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
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());
}
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\n1 2 1 2", "3\n1 1 1"] | 2 seconds | ["7 3 0 0", "6 0 0"] | NoteIn the first sample, color 2 is dominant in three intervals: An interval [2,β2] contains one ball. This ball's color is 2 so it's clearly a dominant color. An interval [4,β4] contains one ball, with color 2 again. An interval [2,β4] contains two balls of color 2 and one ball of color 1. There are 7 more intervals and color 1 is dominant in all of them. | Java 7 | standard input | [] | f292c099e4eac9259a5bc9e42ff6d1b5 | The first line of the input contains a single integer n (1ββ€βnββ€β5000)Β β the number of balls. The second line contains n integers t1,βt2,β...,βtn (1ββ€βtiββ€βn) where ti is the color of the i-th ball. | 1,500 | Print n integers. The i-th of them should be equal to the number of intervals where i is a dominant color. | standard output | |
PASSED | e555f0e39985c305dae5180579e615cd | train_003.jsonl | 1462633500 | Bear Limak has n colored balls, arranged in one long row. Balls are numbered 1 through n, from left to right. There are n possible colors, also numbered 1 through n. The i-th ball has color ti.For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant.There are non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant. | 256 megabytes | import java.util.*;
import java.math.*;
import java.io.*;
import java.text.DecimalFormat;
import java.math.BigInteger;
public class Main{
static int d=20,n;
static int mod=1000000007 ;
static ArrayList<ArrayList<Integer>> arr;
static int[] a,b,c;
public static void main(String[] args) throws IOException {
s.init(System.in);
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n;
n=s.ni();
int[] a=new int[n];
for(int i=0;i<n;i++){
a[i]=s.ni();
}
int[] ans=new int[n+1];
int[] dp;
int col=0;
for(int i=0;i<n;i++){
int max=1;
col=a[i];
dp=new int[n+1];
dp[a[i]]++;
ans[a[i]]++;
for(int j=i+1;j<n;j++){
dp[a[j]]++;
if(dp[a[j]]>max){
max=dp[a[j]];
col=a[j];
}
else if(dp[a[j]]==max && a[j]<col){
col=a[j];
}
ans[col]++;
}
}
for(int i=1;i<=n;i++)
out.print(ans[i]+" ");
out.println("");
out.close();
}
public static int gcd(int x,int y){
if(x%y==0)
return y;
else
return gcd(y,x%y);
}
public static class s {
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("");
}
/** get next word */
static String ns() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int ni() throws IOException {
return Integer.parseInt( ns() );
}
static double nd() throws IOException {
return Double.parseDouble( ns() );
}
static long nl() throws IOException {
return Long.parseLong( ns() );
}
}
} | Java | ["4\n1 2 1 2", "3\n1 1 1"] | 2 seconds | ["7 3 0 0", "6 0 0"] | NoteIn the first sample, color 2 is dominant in three intervals: An interval [2,β2] contains one ball. This ball's color is 2 so it's clearly a dominant color. An interval [4,β4] contains one ball, with color 2 again. An interval [2,β4] contains two balls of color 2 and one ball of color 1. There are 7 more intervals and color 1 is dominant in all of them. | Java 7 | standard input | [] | f292c099e4eac9259a5bc9e42ff6d1b5 | The first line of the input contains a single integer n (1ββ€βnββ€β5000)Β β the number of balls. The second line contains n integers t1,βt2,β...,βtn (1ββ€βtiββ€βn) where ti is the color of the i-th ball. | 1,500 | Print n integers. The i-th of them should be equal to the number of intervals where i is a dominant color. | standard output | |
PASSED | a7efb6caae6df695096f20ceea62ce08 | train_003.jsonl | 1462633500 | Bear Limak has n colored balls, arranged in one long row. Balls are numbered 1 through n, from left to right. There are n possible colors, also numbered 1 through n. The i-th ball has color ti.For a fixed interval (set of consecutive elements) of balls we can define a dominant color. It's a color occurring the biggest number of times in the interval. In case of a tie between some colors, the one with the smallest number (index) is chosen as dominant.There are non-empty intervals in total. For each color, your task is to count the number of intervals in which this color is dominant. | 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;
import java.util.*;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class x
{
public static void main(String[] args)
{
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
int n=in.nextInt();
int ar[]=new int[n];
int i,j,idx;
int h[]=new int[n+1];
for(i=0;i<n;i++)
{
ar[i]=in.nextInt();
}
int max=-99999999;
int times[]=new int[n+1];
for(i=0;i<n;i++)
{
max=-99999999;
Arrays.fill(times,0);
idx=i;
int min=99999999;
for(j=i;j<n;j++)
{
times[ar[j]]++;
if(times[ar[j]]>=max)
{
//out.println("i = "+i);
if(max<times[ar[j]])
{
min=99999999;
}
max=times[ar[j]];
if(ar[j]<=min)
{
min=ar[j];
idx=j;
}
}
h[ar[idx]]++;
}
//out.println(Arrays.toString(times));
//out.println(Arrays.toString(h));
}
for(i=1;i<=n;i++)
{
out.print(h[i]+" ");
}
out.println();
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 double nextDouble()
{
return Double.parseDouble(next());
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
}
} | Java | ["4\n1 2 1 2", "3\n1 1 1"] | 2 seconds | ["7 3 0 0", "6 0 0"] | NoteIn the first sample, color 2 is dominant in three intervals: An interval [2,β2] contains one ball. This ball's color is 2 so it's clearly a dominant color. An interval [4,β4] contains one ball, with color 2 again. An interval [2,β4] contains two balls of color 2 and one ball of color 1. There are 7 more intervals and color 1 is dominant in all of them. | Java 7 | standard input | [] | f292c099e4eac9259a5bc9e42ff6d1b5 | The first line of the input contains a single integer n (1ββ€βnββ€β5000)Β β the number of balls. The second line contains n integers t1,βt2,β...,βtn (1ββ€βtiββ€βn) where ti is the color of the i-th ball. | 1,500 | Print n integers. The i-th of them should be equal to the number of intervals where i is a dominant color. | standard output | |
PASSED | 9d4e8bdd1011219731784c71382fcca5 | train_003.jsonl | 1455894000 | Consider the decimal presentation of an integer. Let's call a number d-magic if digit d appears in decimal presentation of the number on even positions and nowhere else.For example, the numbers 1727374, 17, 1 are 7-magic but 77, 7, 123, 34, 71 are not 7-magic. On the other hand the number 7 is 0-magic, 123 is 2-magic, 34 is 4-magic and 71 is 1-magic.Find the number of d-magic numbers in the segment [a,βb] that are multiple of m. Because the answer can be very huge you should only find its value modulo 109β+β7 (so you should find the remainder after dividing by 109β+β7). | 256 megabytes | import java.io.*;
import java.util.*;
//import javafx.util.*;
import java.math.*;
//import java.lang.*;
public class Main
{
// static int n;
static HashSet<Integer> adj[];
static boolean vis[];
// static long ans[];
// static int arr[];
static long mod=1000000007;
static final long oo=(long)1e18;
public static void main(String[] args) throws IOException {
// Scanner sc=new Scanner(System.in);
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
br = new BufferedReader(new InputStreamReader(System.in));
// int test=nextInt();
int test=1;
outer: while(test--!=0){
int m=nextInt();
int d=nextInt();
String a=next();
String b=next();
// BigInteger aa = new BigInteger(a);
// aa = aa.subtract(BigInteger.ONE);
// a = aa.toString();
// System.out.println(a);
int n=b.length();
Long dp1[][][]=new Long[n+1][2][m];
Long dp2[][][]=new Long[n+1][2][m];
long ans=find(b,dp1,m,d,n,0,0,0)-find(a,dp2,m,d,n,0,0,0);
boolean check=true;
long rem=0;
for(int i=0;i<n;i++){
int val=(int)(a.charAt(i)-'0');
if(i%2!=0){
if(val!=d){
check=false;
break;
}
}
else{
if(val==d){
check=false;
break;
}
}
rem=(10*rem+val)%m;
}
//pw.println(ans);
if(rem==0&&check)
ans=(ans+1)%mod;
ans=(ans+mod)%mod;
{
// pw.println(find(b,dp1,m,d,n,0,0,0)+" "+find(a,dp2,m,d,n,0,0,0));
}
pw.println(ans);
}
pw.close();
}
static long find(String s,Long dp[][][],int m,int d,int n,int idx,int check,int rem){
if(idx==n){
if(rem==0)
return 1;
return 0;
}
if(dp[idx][check][rem]!=null)
return dp[idx][check][rem];
int lim=(check==0)?(int)(s.charAt(idx)-'0'):9;
dp[idx][check][rem]=0l;
if(idx%2!=0){
if(d<=lim){
int newr=(rem*10+d)%m;
if(check==0&&d==lim){
dp[idx][check][rem]=(dp[idx][check][rem]+find(s,dp,m,d,n,idx+1,0,newr))%mod;
// dp[idx][check][rem]%=mod;
}
else{
dp[idx][check][rem]=(dp[idx][check][rem]+find(s,dp,m,d,n,idx+1,1,newr))%mod;
// dp[idx][check][rem]%=mod;
}
}
}
else{
// System.out.println(idx+" "+lim);
for(int i=0;i<=lim;i++){
if(i!=d){
// System.out.println(i);
int newr=(rem*10+i)%m;
if(check==0&&i==lim){
dp[idx][check][rem]=(dp[idx][check][rem]+find(s,dp,m,d,n,idx+1,0,newr))%mod;
// dp[idx][check][rem]%=mod;
}
else{
dp[idx][check][rem]=(dp[idx][check][rem]+find(s,dp,m,d,n,idx+1,1,newr))%mod;
// dp[idx][check][rem]%=mod;
}
}
}
}
return dp[idx][check][rem];
}
public static BufferedReader br;
public static StringTokenizer st;
public static String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public static Integer nextInt() {
return Integer.parseInt(next());
}
public static Long nextLong() {
return Long.parseLong(next());
}
public static Double nextDouble() {
return Double.parseDouble(next());
}
// static class Pair{
// int x;int y;
// Pair(int x,int y,int z){
// this.x=x;
// this.y=y;
// // this.z=z;
// // this.z=z;
// // this.i=i;
// }
// }
// static class sorting implements Comparator<Pair>{
// public int compare(Pair a,Pair b){
// //return (a.y)-(b.y);
// if(a.y==b.y){
// return -1*(a.z-b.z);
// }
// return (a.y-b.y);
// }
// }
static long ncr(long n,long r){
if(r==0)
return 1;
long val=ncr(n-1,r-1);
val=(n*val)%mod;
val=(val*modInverse(r,mod))%mod;
return val;
}
public static int[] na(int n)throws IOException{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = nextInt();
return a;
}
static class query implements Comparable<query>{
int l,r,idx,block;
static int len;
query(int l,int r,int i){
this.l=l;
this.r=r;
this.idx=i;
this.block=l/len;
}
public int compareTo(query a){
return block==a.block?r-a.r:block-a.block;
}
}
static class Pair{ //implements Comparable<Pair>{
Pair prev;int val;
Pair(Pair prev,int val){
// this.index=index;
this.prev=prev;
this.val=val;
//this.z=z;
}
// public int compareTo(Pair p){
// //return (x-p.x);
// if(x>p.x)
// return 1;
// if(x<p.x)
// return -1;
// return y-p.y;
// //return (x-a.x)>0?1:-1;
// }
}
// static class sorting implements Comparator<Pair>{
// public int compare(Pair a1,Pair a2){
// if(o1.a==o2.a)
// return (o1.b>o2.b)?1:-1;
// else if(o1.a>o2.a)
// return 1;
// else
// return -1;
// }
// }
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 long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
// To compute x^y under modulo m
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;
if (y % 2 == 0)
return p;
else
return (x * p) % m;
}
static long fast_pow(long base,long n,long M){
if(n==0)
return 1;
if(n==1)
return base;
long halfn=fast_pow(base,n/2,M);
if(n%2==0)
return ( halfn * halfn ) % M;
else
return ( ( ( halfn * halfn ) % M ) * base ) % M;
}
static long modInverse(long n,long M){
return fast_pow(n,M-2,M);
}
// (1,1)
// (3,2) (3,5)
} | Java | ["2 6\n10\n99", "2 0\n1\n9", "19 7\n1000\n9999"] | 2 seconds | ["8", "4", "6"] | NoteThe numbers from the answer of the first example are 16, 26, 36, 46, 56, 76, 86 and 96.The numbers from the answer of the second example are 2, 4, 6 and 8.The numbers from the answer of the third example are 1767, 2717, 5757, 6707, 8797 and 9747. | Java 11 | standard input | [
"dp"
] | 19564d66e0de78780f4a61c69f2c8e27 | The first line contains two integers m,βd (1ββ€βmββ€β2000, 0ββ€βdββ€β9) β the parameters from the problem statement. The second line contains positive integer a in decimal presentation (without leading zeroes). The third line contains positive integer b in decimal presentation (without leading zeroes). It is guaranteed that aββ€βb, the number of digits in a and b are the same and don't exceed 2000. | 2,200 | Print the only integer a β the remainder after dividing by 109β+β7 of the number of d-magic numbers in segment [a,βb] that are multiple of m. | standard output | |
PASSED | bd75e0dcc93c041227e959478270b66a | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.*;
import java.io.ObjectInputStream.GetField;
import java.math.*;
import java.text.*;
import java.util.*;
//Codeforces
public class MainCodeforces1 {
private static MyScanner in;
private static PrintStream out;
private static boolean LOCAL_TEST = false;
private static void solve() throws IOException
{
int n = in.nextInt();
ArrayList<Integer> zero = new ArrayList<>();
ArrayList<Integer> pos = new ArrayList<>();
ArrayList<Integer> neg = new ArrayList<>();
for (int i = 0; i < n; i++) {
int a = in.nextInt();
if (a == 0)
zero.add(0);
else if (a < 0)
neg.add(a);
else
pos.add(a);
}
out.println("1 " + neg.get(0));
if (pos.size() > 0) {
out.print(pos.size());
for (int i = 0; i < pos.size(); i++) {
out.print(" " + pos.get(i));
}
out.println();
out.print(zero.size() + neg.size() - 1);
for (int i = 0; i < zero.size(); i++) {
out.print(" 0");
}
for (int i = 1; i < neg.size(); i++) {
out.print(" " + neg.get(i));
}
out.println();
}
else {
out.print("2");
out.print(" " + neg.get(1));
out.print(" " + neg.get(2));
out.println();
out.print(zero.size() + neg.size() - 3);
for (int i = 0; i < zero.size(); i++) {
out.print(" 0");
}
for (int i = 3; i < neg.size(); i++) {
out.print(" " + neg.get(i));
}
out.println();
}
}
public static void main(String[] args) throws IOException {
// helpers for input/output
out = System.out;
try {
String cname = System.getenv("COMPUTERNAME");
LOCAL_TEST = (cname.equals("ALPHA530"));
} catch (Exception e) {
}
if (LOCAL_TEST) {
in = new MyScanner("E:\\zin.txt");
}
else {
boolean usingFileForIO = false;
if (usingFileForIO) {
// using input.txt and output.txt as I/O
in = new MyScanner("input.txt");
out = new PrintStream("output.txt");
}
else {
in = new MyScanner();
out = System.out;
}
}
solve();
}
// =====================================
static class MyScanner {
Scanner inp = null;
public MyScanner() throws IOException
{
inp = new Scanner(System.in);
}
public MyScanner(String inputFile) throws IOException {
inp = new Scanner(new FileInputStream(inputFile));
}
public int nextInt() throws IOException {
return inp.nextInt();
}
public long nextLong() throws IOException {
return inp.nextLong();
}
public double nextDouble() throws IOException {
return inp.nextDouble();
}
public String nextString() throws IOException {
return inp.next();
}
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 320ec98c7078a201d25e4a2f3445abca | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
private static void solve(InputReader in, OutputWriter out) {
int n = in.nextInt();
List<Integer> positive, negative;
positive = new ArrayList<Integer>();
negative = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int m = in.nextInt();
if (m > 0)
positive.add(m);
else if (m < 0)
negative.add(m);
}
out.println("1 " + negative.get(0));
if (negative.size() % 2 == 0) {
out.print(negative.size() - 2 + positive.size());
for (int i = 2; i < negative.size(); i++)
out.print(" " + negative.get(i));
for (Integer i: positive)
out.print(" " + i);
out.print("\n2 0 " + negative.get(1));
} else {
out.print(negative.size() - 1 + positive.size());
for (int i = 1; i < negative.size(); i++)
out.print(" " + negative.get(i));
for (Integer i: positive)
out.print(" " + i);
out.print("\n1 0");
}
}
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
solve(in, out);
in.close();
out.close();
}
private static class InputReader {
private BufferedReader br;
private StringTokenizer st;
InputReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
st = null;
}
String nextLine() {
String line = null;
try {
line = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return line;
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String line = nextLine();
if (line == null) return null;
st = new StringTokenizer(line);
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static class OutputWriter {
BufferedWriter bw;
OutputWriter(OutputStream os) {
bw = new BufferedWriter(new OutputStreamWriter(os));
}
void print(int i) {
print(Integer.toString(i));
}
void println(int i) {
println(Integer.toString(i));
}
void print(long l) {
print(Long.toString(l));
}
void println(long l) {
println(Long.toString(l));
}
void print(double d) {
print(Double.toString(d));
}
void println(double d) {
println(Double.toString(d));
}
void print(boolean b) {
print(Boolean.toString(b));
}
void println(boolean b) {
println(Boolean.toString(b));
}
void print(char c) {
try {
bw.write(c);
} catch (IOException e) {
e.printStackTrace();
}
}
void println(char c) {
println(Character.toString(c));
}
void print(String s) {
try {
bw.write(s);
} catch (IOException e) {
e.printStackTrace();
}
}
void println(String s) {
print(s);
print('\n');
}
void close() {
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 7ae05ed8d2ee36700e1be46a1e24533a | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.*;
/**
*
* @author greggy
*/
public class Array {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int group[] = new int[n];
for (int i = 0; i < n; i++)
group[i] = in.nextInt();
Arrays.sort(group);
if(group[n-1] > 0) {
System.out.println("1 " + group[0]);
System.out.println("1 " + group[n-1]);
System.out.print(n-2 + " ");
for(int j=1; j < n-1; j++)
System.out.print(group[j]+ " ");
}
else {
System.out.println("1 " + group[0]);
System.out.println("2 " + group[1] + " " + group[2]);
System.out.print(n-3 + " ");
for(int j=3; j < n; j++)
System.out.print(group[j]+ " ");
}
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | d8cf1e040d7e0874b9e42211310c93da | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes |
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args){
FastScanner sc = new FastScanner();
int n = sc.nextInt();
int[] arr= new int[n];
List<Integer> pos = new ArrayList<Integer>();
List<Integer> negs = new ArrayList<Integer>();
List<Integer> zeros = new ArrayList<Integer>();
for(int i=0;i<n;i++){
arr[i] = sc.nextInt();
if(arr[i] == 0){
zeros.add(arr[i]);
}else if(arr[i] < 0){
negs.add(arr[i]);
}else{
pos.add(arr[i]);
}
}
if(pos.size() == 0){
pos.add(negs.get(0));
negs.remove(0);
pos.add(negs.get(0));
negs.remove(0);
}
if(negs.size() %2 == 0){
zeros.add(negs.get(0));
negs.remove(0);
}
System.out.print(negs.size() + " ");
for(int i: negs){
System.out.print(i + " ");
}
System.out.println();
System.out.print(pos.size() + " ");
for(int i: pos){
System.out.print(i + " ");
}
System.out.println();
System.out.print(zeros.size() + " ");
for(int i: zeros){
System.out.print(i + " ");
}
System.out.println();
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 1395e11eb92ea9a2b60cf0cde970886d | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.*;
public class Main {
public static void main (String [] args){
int k=0; int m=0;
int cnt1=0; int cnt2=0;
int cnt3=0;
Scanner in = new Scanner (System.in);
int n= in.nextInt();
int array1 []=new int [n];
int array2 []=new int [n];
for(int i=1;i<=n;i++){
int a = in.nextInt();
if(a<0){
cnt1++;
array1[k]=a;
k++;
}
if(a>0){
cnt2++;
array2[m]=a;
m++;
}
if(a==0){
cnt3++;
}
}
if(cnt1==1){
System.out.print("1" + " ");
System.out.print(array1[0]);
System.out.println();
System.out.print(cnt2 + " ");
for(int i=0;i<cnt2;i++){
System.out.print(array2[i] + " ");
}
System.out.println();
System.out.print(cnt3 + " ");
for (int i=0;i<cnt3;i++){
System.out.print("0" + " ");
}
}
else if (cnt1>1 && cnt1%2!=0){
System.out.print((cnt1-2) + " ");
for (int i=2;i<cnt1;i++){
System.out.print(array1[i] + " ");
}
System.out.println();
System.out.print((cnt2+2) + " ");
System.out.print(array1[0] + " " + array1[1] + " ");
for(int i=0;i<cnt2;i++){
System.out.print(array2[i] + " ");
}
System.out.println();
System.out.print(cnt3 + " ");
for (int i=0;i<cnt3;i++){
System.out.print("0" + " ");
}
}
else if(cnt1>3 && cnt1%2==0){
System.out.print((cnt1-3) + " ");
for (int i=3;i<cnt1;i++){
System.out.print(array1[i] + " ");
}
System.out.println();
System.out.print((cnt2+2) + " ");
System.out.print(array1[0] + " " + array1[1] + " ");
for(int i=0;i<cnt2;i++){
System.out.print(array2[i] + " ");
}
System.out.println();
System.out.print((cnt3+1) + " ");
System.out.print(array1[2] + " ");
for (int i=0;i<cnt3;i++){
System.out.print("0" + " ");
}
}
else if(cnt1==2){
System.out.print((cnt1-1) + " ");
for (int i=1;i<cnt1;i++){
System.out.print(array1[i] + " ");
}
System.out.println();
System.out.print((cnt2) + " ");
for(int i=0;i<cnt2;i++){
System.out.print(array2[i] + " ");
}
System.out.println();
System.out.print((cnt3+1) + " ");
System.out.print(array1[0] + " ");
for (int i=0;i<cnt3;i++){
System.out.print("0" + " ");
}
}
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | fc2edc1d89839007ebab1840c226fc91 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.*;
import java.util.*;
public class a
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int x = 0,y = 0,z = 0;
int a[] = new int[n+1],b[] = new int[n+1];
boolean flag = false,find = false;
for (int i=1;i<=n;i++)
{
a[i] = sc.nextInt();
if (a[i] > 0 && !flag)
{
b[i] = 1;
y = i;
flag = true;
}
if (a[i] < 0 && !find)
{
find = true;
x = i;
b[i] = 1;
}
}
if (flag)
{
System.out.println(1+" "+a[x]);
System.out.println(1+" "+a[y]);
System.out.print(n-2+" ");
for (int i=1;i<=n;i++)
if (b[i] == 0) System.out.print(a[i]+" ");
System.out.println();
}
else
{
for (int i=1;i<=n;i++)
if (b[i] == 0 && a[i] < 0)
{
y = i;
b[i] = 1;
break;
}
for (int i=1;i<=n;i++)
if (b[i] == 0 && a[i] < 0)
{
z = i;
b[i] = 1;
break;
}
System.out.println(1+" "+a[x]);
System.out.println(2+" "+a[y]+" "+a[z]);
System.out.print(n-3+" ");
for (int i=1;i<=n;i++)
if (b[i] == 0) System.out.print(a[i]+" ");
System.out.println();
}
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | dc336d38b9780dc8275bb1b74b08e8a3 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Smile {
static Scanner in;
static PrintWriter out;
static void print(List<Integer> x) {
out.print(x.size());
for (Integer y : x) {
out.print(" " + y);
}
out.println();
}
public static void main(String[] args) {
in = new Scanner(System.in);
out = new PrintWriter(System.out);
int n = in.nextInt();
List<Integer> a, b, c;
a = new ArrayList<Integer>();
b = new ArrayList<Integer>();
c = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int x = in.nextInt();
if (x < 0) {
a.add(x);
} else if (x == 0) {
b.add(x);
} else {
c.add(x);
}
}
if (c.size() == 0) {
c.add(a.get(0));
a.remove(0);
c.add(a.get(0));
a.remove(0);
}
out.println("1 " + a.get(0));
a.remove(0);
b.addAll(a);
print(c);
print(b);
out.close();
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | fbbd4e0fd8094257590e29045819f357 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class Smile {
static Scanner in;
static PrintWriter out;
public static void main(String[] args) {
in = new Scanner(System.in);
out = new PrintWriter(System.out);
int n = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
int k = 0;
for (int i = 0; i < n; i++) {
if (a[i] == 0) {
k++;
}
}
out.println(1 + " " + (a[0]));
int count = 1;
while (a[count] < 0) {
count++;
}
if (count == 1) {
out.print(n - k - 1 + " ");
for (int i = k + 1; i < n; i++) {
out.print(a[i] + " ");
}
out.println();
out.print(k + " ");
for (int i = 1; i <= k; i++) {
out.print(a[i] + " ");
}
} else if (count == 2) {
out.print(n - 3 + " ");
for (int i = 3; i < n; i++) {
out.print(a[i] + " ");
}
out.println();
out.print(2 + " " + a[1] + " " + a[2]);
} else if (count > 2) {
out.print(2 + " " + a[1] + " " + a[2]);
out.println();
out.print(n - 3 + " ");
for (int i = 3; i < n; i++) {
out.print(a[i] + " ");
}
}
/*
{
out.print(count - 1 + " ");
for (int i = 1; i < count; i++) {
out.print(a[i] + " ");
}
out.println();
out.print(n - count + " ");
for (int i = count; i < n; i++) {
out.print(a[i] + " ");
}
}
out.println();
*/ out.close();
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 7951040302d9c27ff1365d364811f66d | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader t = new BufferedReader(new InputStreamReader(System.in));
int no = Integer.parseInt(t.readLine());
String h[] = t.readLine().split(" ");
int x [] =new int [h.length];
for(int i=0;i<no;i++){
x[i]=Integer.parseInt(h[i]);
}
int num[]=new int[3];
for(int i=0;i<x.length;i++){
if(x[i]<0){
num[0]++;
}else if(x[i]>0){
num[1]++;
}else{
num[2]++;
}
}
int [] neg;
int [] pos;
int [] zero;
if(num[1]>0){
pos=new int[num[1]];
int k=0;
for(int i=0;i<pos.length;k++){
if(x[k]>0){
pos[i]=x[k];
i++;
}
}
neg=new int[1];
k=0;
int j = 0;
while(neg[0]==0){
if(x[k]<0){
neg[0]=x[k];
j=k;
}
k++;
}
zero=new int [num[2]+(num[0]-1)];
k=0;
for(int i=0;i<zero.length;k++){
if(x[k]<=0 && k!=j){
zero[i]=x[k];
i++;
}
}
}else{
pos=new int[2];
int k=0;
int i=0;
while(pos[1]==0){
if(x[k]<0){
pos[i]=x[k];
i++;
}
k++;
}
neg=new int[1];
while(neg[0]==0){
if(x[k]<0){
neg[0]=x[k];
}
k++;
}
zero=new int [num[2]+(num[0]-3)];
int j=0;
for(i=0;i<zero.length;j++){
if(x[j]==0 || (x[j]<0 && j>=k)){
zero[i]=x[j];
i++;
}
}
}
System.out.print(neg.length+" ");
for(int i=0;i<neg.length-1;i++){
System.out.print(neg[i]+" ");
}
System.out.println(neg[neg.length-1]);
System.out.print(pos.length+" ");
for(int i=0;i<pos.length-1;i++){
System.out.print(pos[i]+" ");
}
System.out.println(pos[pos.length-1]);
System.out.print(zero.length+" ");
for(int i=0;i<zero.length-1;i++){
System.out.print(zero[i]+" ");
}
System.out.println(zero[zero.length-1]);
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 8ffcc0b37e530ee938c5a70b2610cae6 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Array {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] a = br.readLine().split(" ");
int n =Integer.parseInt(a[0]);
a = br.readLine().split(" ");
int indPos = -1;
int fNeg = -1;
int sNeg = -1;
int tNeg = -1;
boolean mshFlag = false;
for (int i=0;!mshFlag&&i<n;i++) {
int curr = Integer.parseInt(a[i]);
if (curr>0) {
indPos = i;
}
else if (curr<0) {
if (fNeg==-1) {
fNeg = i;
}
else if (sNeg==-1){
sNeg = i;
}
else {
tNeg = i;
}
}
mshFlag = ((indPos!=-1)&&(fNeg!=-1))||(tNeg!=-1);
}
if (indPos>-1) {
System.out.println(1+" "+a[fNeg]);
System.out.println(1+" "+a[indPos]);
System.out.print((n-2)+" ");
for (int i=0;i<n;i++) {
if (i!=indPos&&i!=fNeg) {
System.out.print(a[i]+" ");
}
}
}
else {
System.out.println(1+" "+a[fNeg]);
System.out.println(2+" "+a[sNeg]+" "+a[tNeg]);
System.out.print((n-3)+" ");
for (int i=0;i<n;i++) {
if (i!=sNeg&&i!=fNeg&&i!=tNeg) {
System.out.print(a[i]+" ");
}
}
}
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | e92e9ceda26c7cc515a028238ce2a0ea | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Array {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] a = br.readLine().split(" ");
int n =Integer.parseInt(a[0]);
a = br.readLine().split(" ");
int indPos = -1;
int fNeg = -1;
int sNeg = -1;
int tNeg = -1;
boolean flag = false;
for (int i=0;!flag&&i<n;i++) {
int curr = Integer.parseInt(a[i]);
if (curr>0) {
indPos = i;
}
else if (curr<0) {
if (fNeg==-1) {
fNeg = i;
}
else if (sNeg==-1){
sNeg = i;
}
else {
tNeg = i;
}
}
flag = ((indPos!=-1)&&(fNeg!=-1))||(tNeg!=-1);
}
if (indPos>-1) {
System.out.println(1+" "+a[fNeg]);
System.out.println(1+" "+a[indPos]);
System.out.print((n-2)+" ");
for (int i=0;i<n;i++) {
if (i!=indPos&&i!=fNeg) {
System.out.print(a[i]+" ");
}
}
}
else {
System.out.println(1+" "+a[fNeg]);
System.out.println(2+" "+a[sNeg]+" "+a[tNeg]);
System.out.print((n-3)+" ");
for (int i=0;i<n;i++) {
if (i!=sNeg&&i!=fNeg&&i!=tNeg) {
System.out.print(a[i]+" ");
}
}
}
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 06c5c18863578ef7fafdd2664b1aa070 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
/**
*
* @author V1029
*/
public class A {
public static void main(String args[])throws IOException {
int n,mcount=0,pcount=0,pflag,mflag=1,zflag = 0;
ArrayList alm=new ArrayList();
ArrayList alp=new ArrayList();
ArrayList alz=new ArrayList();
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
n=P(br.readLine());
String[]inputarr = br.readLine().split(" ");
for(int i=0;i<inputarr.length;i++){
if(P(inputarr[i])<0)mcount++;
if(P(inputarr[i])>0)pcount++;
}
if((mcount%2==0)) zflag=1;
else if((mcount%2==1)) zflag=0;
for(int j=0;j<inputarr.length;j++){
if((P(inputarr[j])<0)&&(mflag>0)){
alm.add(P(inputarr[j]));
mflag--;
}
else if(((P(inputarr[j])<0)&&(zflag>0))||(P(inputarr[j])==0) ){
alz.add(P(inputarr[j]));
zflag--;
}
else alp.add(P(inputarr[j]));
}
Object[] marr = alm.toArray();
Object[] zarr = alz.toArray();
Object[] parr = alp.toArray();
System.out.print(marr.length+" ");
for(int i=0;i<marr.length;i++){
System.out.print(marr[i]+" ");
}
System.out.println();
System.out.print(parr.length+" ");
for(int i=0;i<parr.length;i++){
System.out.print(parr[i]+" ");
}
System.out.println();
System.out.print(zarr.length+" ");
for(int i=0;i<zarr.length;i++){
System.out.print(zarr[i]+" ");
}
}
public static int P(String s){
return Integer.parseInt(s);
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 76b4e61f936e8d61755df8f9e3396964 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.Arrays;
import java.util.LinkedList;
import java.util.Scanner;
public class Array {
public static void main(String[]args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
LinkedList<Integer> negative= new LinkedList<Integer>();
LinkedList<Integer> positive= new LinkedList<Integer>();
LinkedList<Integer> zeroSet= new LinkedList<Integer>();
zeroSet.add(0);
for(int i = 1;i <= n ;i++){
int x = sc.nextInt();
if(x < 0) negative.add(x);
else if(x > 0)positive.add(x);
}if(negative.size()%2 == 0){
zeroSet.add(negative.removeFirst());
}
if(positive.size() == 0){
positive.add(negative.removeFirst());
positive.add(negative.removeFirst());
}
String s1=""+negative.size();
while(!negative.isEmpty()){
s1+=" "+negative.removeFirst();
}
System.out.println(s1);
String s2=""+positive.size();
while(!positive.isEmpty()){
s2+=" "+positive.removeFirst();
}
System.out.println(s2);
String s3=""+zeroSet.size();
while(!zeroSet.isEmpty()){
s3+=" "+zeroSet.removeFirst();
}
System.out.println(s3);
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 91aaf9aa76b82cf2e40558710147a509 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int x = in.nextInt();
StringBuilder out = new StringBuilder();
List<Integer> first = new ArrayList<>();
List<Integer> second = new ArrayList<>();
List<Integer> third = new ArrayList<>();
int temp;
for (int i = 1; i <= x; i++) {
temp = in.nextInt();
if(temp < 0){
first.add(temp);
}
else if(temp>0){
second.add(temp);
}
else{
third.add(temp);
}
}
if(first.size()%2 == 0 && !second.isEmpty()){
third.add(first.get(0));
first.remove(0);
}
else if(first.size()%2 == 0 && second.isEmpty()){
second.add(first.get(0));
first.remove(0);
second.add(first.get(0));
first.remove(0);
third.add(first.get(0));
first.remove(0);
}
else if(first.size()%2 != 0 && second.isEmpty()){
second.add(first.get(0));
first.remove(0);
second.add(first.get(0));
first.remove(0);
}
out.append(first.size()+" ");
for (int i = 0; i < first.size(); i++) {
out.append(first.get(i)+" ");
}
out.trimToSize();
out.append("\n");
out.append(second.size()+" ");
for (int i = 0; i < second.size(); i++) {
out.append(second.get(i)+" ");
}
out.trimToSize();
out.append("\n");
out.append(third.size()+" ");
for (int i = 0; i < third.size(); i++) {
out.append(third.get(i)+" ");
}
out.trimToSize();
out.append("\n");
System.out.print(out);
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 58a68ad2801dc8f217d7fd9b2df79095 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
StringBuilder out = new StringBuilder();
List<Integer> first = new ArrayList<>();
List<Integer> second = new ArrayList<>();
List<Integer> third = new ArrayList<>();
int temp;
for (int i = 1; i <= a; i++) {
temp = in.nextInt();
if(temp < 0){
first.add(temp);
}
else if(temp>0){
second.add(temp);
}
else{
third.add(temp);
}
}
if(first.size()%2 == 0 && !second.isEmpty()){
third.add(first.get(0));
first.remove(0);
}
else if(first.size()%2 == 0 && second.isEmpty()){
second.add(first.get(0));
first.remove(0);
second.add(first.get(0));
first.remove(0);
third.add(first.get(0));
first.remove(0);
}
else if(first.size()%2 != 0 && second.isEmpty()){
second.add(first.get(0));
first.remove(0);
second.add(first.get(0));
first.remove(0);
}
out.append(first.size()+" ");
for (int i = 0; i < first.size(); i++) {
out.append(first.get(i)+" ");
}
out.trimToSize();
out.append("\n");
out.append(second.size()+" ");
for (int i = 0; i < second.size(); i++) {
out.append(second.get(i)+" ");
}
out.trimToSize();
out.append("\n");
out.append(third.size()+" ");
for (int i = 0; i < third.size(); i++) {
out.append(third.get(i)+" ");
}
out.trimToSize();
out.append("\n");
System.out.print(out);
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 89fdbfda420ee6a6041037ff1f50ff6a | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.awt.Point;
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.Map.Entry;
import static java.lang.Math.*;
public class Solve implements Runnable{
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
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()){
try{
tok = new StringTokenizer(in.readLine());
}catch (Exception e){
return null;
}
}
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 Thread(null, new Solve(), "", 256 * (1L << 20)).start();
}
long timeBegin, timeEnd;
void time(){
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
void debug(Object... objects){
if (!ONLINE_JUDGE){
for (Object o: objects){
System.err.println(o.toString());
}
}
}
int sm(int x){
if (x==1) return 0;else return 1;
}
int gcd(int a,int b){
if (b==0) return a;
else{
return gcd(b,a%b);
}
}
void solve() throws IOException{
int n=readInt();
int k=0;
int km=0;
int[] a=new int[n];
for (int i=0;i<n;i++){
int x=readInt();
a[i]=x;
if (x==0) k++;
if (x<0) km++;
}
Arrays.sort(a);
out.println("1 "+a[0]);
km--;
int ans=n-1-k;
if (km%2==1) ans--;
out.print((ans)+" ");
int j=1;
if (km%2==1) j++;
for (int i=j;i<n;i++){
if (a[i]!=0){
out.print(a[i]+" ");
}
}
out.println();
if (km%2==1) {
out.print((k+1)+" ");
}else{
out.print(k+" ");
}
for (int i=0;i<k;i++){
out.print("0 ");
}
if (km%2==1) out.println(a[1]);else out.println();
}
public void run(){
try{
timeBegin = System.currentTimeMillis();
init();
solve();
out.close();
time();
}catch (Exception e){
e.printStackTrace(System.err);
System.exit(-1);
}
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | efad22b866f9f0a5f96a4f90a46cfb58 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
public class TaskA {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int c1=0;
int[] a = new int[n];
for(int i=0; i<n; i++){
a[i] = in.nextInt();
if(a[i]<0) c1++;
}
Arrays.sort(a);
System.out.println(1 + " " +a[0]);
if(a[n-1]>0){
System.out.println(1 + " " + a[n-1]);
System.out.print(n-2 + " ");
for(int i=1; i<n-1; i++)
System.out.print(a[i] + " ");
}
else{
System.out.println(2 + " "+ a[1] + " " + a[2]);
System.out.print(n-3 + " ");
for(int i=3; i<n; i++)
System.out.print(a[i]+" ");
}
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 2b75f80dead9aa2a33344e9fefaeb83e | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 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 A_Div2_181 {
public static void main(String[]arg) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
int[]a = new int[n];
StringTokenizer st = new StringTokenizer(in.readLine());
for(int i = 0; i < n; i++)
{
a[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(a);
String s1 = "", s2 = "", s3 = "";
ArrayList<Integer> x = new ArrayList<Integer>();
ArrayList<Integer> y = new ArrayList<Integer>();
ArrayList<Integer> z = new ArrayList<Integer>();
for(int i = 0; i < n; i++)
{
if(a[i] > 0)
{
x.add(a[i]);
}
else if(a[i] < 0)
{
y.add(a[i]);
}
else
{
z.add(a[i]);
}
}
if(x.size() == 0)
{
x.add(y.remove(0));
x.add(y.remove(0));
}
if(fi)
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class A_Div2_181 {
public static void main(String[]arg) throws IOException
{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(in.readLine());
int[]a = new int[n];
StringTokenizer st = new StringTokenizer(in.readLine());
for(int i = 0; i < n; i++)
{
a[i] = Integer.parseInt(st.nextToken());
}
Arrays.sort(a);
String s1 = "", s2 = "", s3 = "";
int i = 0;
boolean end = false;
s1 += "1 "+a[i];
i++;
int neg = 0;
int a1 = 1, a2 = 0, a3 = 0;
for(;!end;)
{
if(a[i] < 0)
{
s2 += a[i] + " ";
a2++;
neg++;
i++;
}
else
{
end = true;
}
if(neg == 2)
{
end = true;
}
}
if(neg < 2)
{
//System.out.println("in");
end = false;
s3 = s2;
a3 = a2;
for(;!end;)
{
if(a[i] <= 0)
{
a3++;
s3 += a[i] + " ";
i++;
}
else
{
end = true;
}
}
s2 = "";
a2 = 0;
for(; i < n; i++)
{
a2++;
//System.out.println(a[i]);
s2 += a[i] + " ";
}
}
else
{
for(; i < n; i++)
{
s3 += a[i] + " ";
a3++;
}
}
s2 = s2.trim();
s3 = s3.trim();
s3 = a3 + " " + s3;
s2 = a2 + " " + s2;
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 40e4de00f1c416d8cbd97e24bc04e1e9 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.*; //Scanner;
import java.io.PrintWriter; //PrintWriter
public class R181_Div2_A //Name: Array
{
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
solve(in, out);
out.close();
in.close();
}
public static void solve(Scanner in, PrintWriter out)
{
int n = in.nextInt();
ArrayList<Integer> n1 = new ArrayList<Integer>();
ArrayList<Integer> n2 = new ArrayList<Integer>();
ArrayList<Integer> n3 = new ArrayList<Integer>();
int a;
for (int i = 0; i < n; i++)
{
a = in.nextInt();
if (a == 0)
n3.add(a);
else if (a < 0)
n1.add(a);
else // a > 0
n2.add(a);
}
if (n1.size() % 2 == 0)
{
//Place one negative to 0 area
n3.add(n1.get(n1.size()-1));
n1.remove(n1.size()-1);
}
if (n2.size() == 0) //need 2 negatives
{
n2.add(n1.get(n1.size()-1));
n2.add(n1.get(n1.size()-2));
n1.remove(n1.size()-1);
n1.remove(n1.size()-1);
}
printNums(n1,out);
printNums(n2,out);
printNums(n3,out);
}
private static void printNums(ArrayList<Integer> nums, PrintWriter out)
{
out.print(nums.size());
for (int i : nums)
out.print(" " + i);
out.println();
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 6f55d26e1ec4157d9e6e982be6ea7bb3 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.LinkedList;
import java.io.*;
public class sets {
public static void grouping(String [] arr) {
LinkedList<Integer> neg = new LinkedList<Integer>();
int first = 0;
LinkedList<Integer> pos = new LinkedList<Integer>();
int second = 0;
LinkedList<Integer> zero = new LinkedList<Integer>();
int third = 0;
boolean acc = true;
int negatives = getNegatives(arr);
if(negatives == 2) {
acc = false;
}
boolean odd = false;
boolean even = false;
int ev = 0;
for(int i = 0 ; i < arr.length ; i++) {
int elem = Integer.parseInt(arr[i]);
if(elem < 0 && odd == false) {
odd = true;
first++;
neg.add(elem);
} else if(elem < 0 && even == false && acc == true) {
ev++;
second++;
pos.add(elem);
if(ev%2 == 0)
even = true;
}else if (elem <= 0) {
third++;
zero.add(elem);
}else if (elem > 0) {
if(second <= first && second <= third) {
second++;
pos.add(elem);
}
else if(first <= second && first <= third){
first ++;
neg.add(elem);
}
else {zero.add(elem);
third ++;
}
}
}
System.out.print(first + " " );
for(int i =0; i < neg.size();i++){
System.out.print(neg.get(i)+ " ");
}
System.out.print("\n" + second + " ");
for(int i =0; i < pos.size();i++){
System.out.print(pos.get(i)+ " ");
}
System.out.print("\n" + third + " ");
for(int i =0; i < zero.size();i++){
System.out.print(zero.get(i)+ " ");
}
}
public static int getNegatives(String[] arr ) {
int count = 0;
for(int i =0;i<arr.length;i++) {
int a = Integer.parseInt(arr[i]);
if(a<0)
count++;
}
return count;
}
public static void main(String[]args) throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader (System.in));
br.readLine();
String str = br.readLine();
String [] arr = str.split(" ");
grouping (arr);
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | c3fe48f4f0c7946560505ed7733a2df0 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class CF_181_C {
public static void main(String[] args) {
FastScanner sc = new FastScanner(System.in);
int n = sc.nextInt();
int[] nums = new int[n];
int totneg = 0;
for(int i=0; i<n; i++){
nums[i]=sc.nextInt();
if(nums[i]<0) totneg++;
}
ArrayList<Integer> first = new ArrayList<Integer>();
ArrayList<Integer> sec = new ArrayList<Integer>();
ArrayList<Integer> t = new ArrayList<Integer>();
for(int i=0; i<n; i++){
if (nums[i]<0 && first.size()==0){
first.add(nums[i]);
} else if ((sec.size()==0 && totneg>2 && nums[i]<0)|| (sec.size()==1 && totneg>2 && nums[i]<0) ||(sec.size()==0 && totneg<=2 && nums[i]>0)){
sec.add(nums[i]);
} else {
t.add(nums[i]);
}
}
System.out.print((first.size())+" ");
for(int i =0; i<first.size(); i++){
System.out.print(first.get(i));
if(i!=first.size()-1){
System.out.print(" ");
}
}
System.out.println();
System.out.print((sec.size())+" ");
for(int i =0; i<sec.size(); i++){
System.out.print(sec.get(i));
if(i!=sec.size()-1){
System.out.print(" ");
}
}
System.out.println();
System.out.print((t.size())+" ");
for(int i =0; i<t.size(); i++){
System.out.print(t.get(i));
if(i!=t.size()){
System.out.print(" ");
}
}
System.out.println();
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String next() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return null;
st = new StringTokenizer(s);
}
return st.nextToken();
}
boolean hasMoreTokens() {
while (st == null || !st.hasMoreTokens()) {
String s = null;
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (s == null)
return false;
st = new StringTokenizer(s);
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble(){
return Double.parseDouble(next());
}
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 0d6e242847697c53282115af2880c99d | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.TreeSet;
import java.util.Arrays;
public class Array {
public static void main(String[] args) throws Exception {
BufferedReader br =new BufferedReader( new InputStreamReader(System.in));
int begin,end,size;
int n=Integer.parseInt(br.readLine());
TreeSet<Integer> lessThanZero= new TreeSet<Integer>();
TreeSet<Integer> greaterThanZero= new TreeSet<Integer>();
TreeSet<Integer> equalToZero= new TreeSet<Integer>();
int[] a =new int[n];
String[] parts=br.readLine().split(" ");
for(int i=0;i<n;i++){
a[i]=Integer.parseInt(parts[i]);
}
Arrays.sort(a);
begin=1;
end=n;
end--;
System.out.println("1 "+a[0]);
if(a[end]>0)
{System.out.println("1 "+a[end]);
end--;}
else
{System.out.println("2 "+a[begin]+" "+a[begin+1]);
begin=begin+2;}
size=1+end-begin;
System.out.print( size+ " ");
for(int i=begin; i<=end;i++)
System.out.print(" "+a[i]);
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 403100a9b61bf9d98824719b57ef9a57 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.Arrays;
import java.util.HashSet;
import java.util.Scanner;
public class DoubleCola {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int inputs = scan.nextInt();
int[] values = new int[inputs];
boolean flag = false;
for (int i = 0; i < inputs; i++) {
values[i] = scan.nextInt();
}
Arrays.sort(values);
System.out.println(1 + " " + values[0]);
if(values[1] > 0 || (values[1] * values[2]) > 0){
System.out.println(2 + " " + values[1] + " " + values[2]);
}else{
System.out.println("1 " + values[inputs-1]);
flag = true;
}
if(flag){
System.out.print(values.length-2 + " ");
for(int i = 1; i < inputs-1; i++){
System.out.print(values[i] + " ");
}
}else{
System.out.print(values.length-3 + " ");
for(int i = 3; i < inputs; i++){
System.out.print(values[i] + " ");
}
}
}
}
/*int firstIndex = 0;
for (int i = 0; true; i++) {
if (values[i] < 0) {
s1.add(values[i]);
firstIndex = i;
break;
}else{
break;
}
}
int secondIndex = 0;
for(int i = firstIndex+1; i < firstIndex + 3 ; i ++){
if(((values[firstIndex] * values[firstIndex+1]) > 0)){
System.out.println("Working");
s2.add(values[i]);
secondIndex = i;
}
}
for(int i = secondIndex+1; i < inputs; i++){
s3.add(values[i]);
}
java.util.Iterator<Integer> i1 = s1.iterator();
java.util.Iterator<Integer> i2 = s2.iterator();
java.util.Iterator<Integer> i3 = s3.iterator();
System.out.print("less " + s1.size() + ":");
while (i1.hasNext()) {
System.out.print(" " + i1.next());
}
System.out.println();
System.out.print("more " + s2.size() + ":");
while (i2.hasNext()) {
System.out.print(" " + i2.next());
}
System.out.println();
System.out.print("zero " + s3.size() + ":");
while (i3.hasNext()) {
System.out.print(" " + i3.next());
}*/ | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 54981041ad957e7b8a83a60bf7e90c24 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
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);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
}
class TaskA {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n = in.nextInt();
ArrayList<Integer> neg = new ArrayList<Integer>();
ArrayList<Integer> pos = new ArrayList<Integer>();
ArrayList<Integer> zero = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int temp = in.nextInt();
if (temp < 0) neg.add(temp);
else if (temp > 0) pos.add(temp);
else zero.add(temp);
}
for (int i = 0; i < 1000; i++) {
if (pos.size()==0){
pos.add(neg.remove(0));
pos.add(neg.remove(0));
}
if (neg.size() % 2 == 0) {
zero.add(neg.remove(0));
}
}
out.print(neg.size()+" ");
for(Integer val : neg) out.print(val + " ");
out.println();
out.print(pos.size()+" ");
for(Integer val : pos) out.print(val + " ");
out.println();
out.print(zero.size()+" ");
for(Integer val : zero) out.print(val + " ");
out.println();
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | c053cf0cea1cdcf1e9732754758513bd | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | 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 = in.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++)
a[i] = in.nextInt();
Arrays.sort(a);
int id1 = 0;
for(int i = 0; i < n; i++)
if(a[i] == 0) {
id1 = i;
break;
}
int id2 = 0;
for(int j = n - 1; j >= 0; j--)
if(a[j] == 0) {
id2 = j;
}
if(id1 % 2 == 1) {
System.out.print(1);
System.out.print(" " + a[0]);
System.out.printf("\n%d", n - id2 - 2 + id1 );
for(int i = 1; i < id1; i++)
System.out.print(" " + a[i]);
for(int i = id2 + 1; i < n; i++)
System.out.print(" " + a[i]);
System.out.printf("\n%d", id2 - id1 + 1);
for(int i = id1; i <= id2; i++)
System.out.print(" " + a[i]);
}
else {
System.out.print(1);
System.out.print(" " + a[0]);
System.out.printf("\n%d", n - id2 - 3 + id1 );
for(int i = 2; i < id1; i++)
System.out.print(" " + a[i]);
for(int i = id2 + 1; i < n; i++)
System.out.print(" " + a[i]);
System.out.printf("\n%d", id2 - id1 + 2);
System.out.print(" " + a[1]);
for(int i = id1; i <= id2; i++)
System.out.print(" " + a[i]);
}
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 1c9a4de4260f7ad9b576584ffb6de476 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | 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 = in.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++)
a[i] = in.nextInt();
Arrays.sort(a);
int id1 = 0;
for(int i = 0; i < n; i++)
if(a[i] == 0) {
id1 = i;
break;
}
int id2 = 0;
for(int j = n - 1; j >= 0; j--)
if(a[j] == 0) {
id2 = j;
break;
}
if(id1 % 2 == 1) {
System.out.print(1);
System.out.print(" " + a[0]);
System.out.printf("\n%d", n - id2 - 2 + id1 );
for(int i = 1; i < id1; i++)
System.out.print(" " + a[i]);
for(int i = id2 + 1; i < n; i++)
System.out.print(" " + a[i]);
System.out.printf("\n%d", id2 - id1 + 1);
for(int i = id1; i <= id2; i++)
System.out.print(" " + a[i]);
}
else {
System.out.print(1);
System.out.print(" " + a[0]);
System.out.printf("\n%d", n - id2 - 3 + id1 );
for(int i = 2; i < id1; i++)
System.out.print(" " + a[i]);
for(int i = id2 + 1; i < n; i++)
System.out.print(" " + a[i]);
System.out.printf("\n%d", id2 - id1 + 2);
System.out.print(" " + a[1]);
for(int i = id1; i <= id2; i++)
System.out.print(" " + a[i]);
}
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | e911ab32028a6268b63d62a91d37839c | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;
public class A {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++)
a[i] = in.nextInt();
in.close();
Arrays.sort(a);
int id1 = 0;
for(int i = 0; i < n; i++)
if(a[i] == 0) {
id1 = i;
break;
}
int id2 = 0;
for(int j = n - 1; j >= 0; j--)
if(a[j] == 0) {
id2 = j;
break;
}
if(id1 % 2 == 1) {
out.print(1);
out.print(" " + a[0]);
out.printf("\n%d", n - id2 - 2 + id1 );
for(int i = 1; i < id1; i++)
out.print(" " + a[i]);
for(int i = id2 + 1; i < n; i++)
out.print(" " + a[i]);
out.print("\n");
out.print(id2 - id1 + 1);
for(int i = id1; i <= id2; i++)
out.print(" " + a[i]);
out.close();
}
else {
out.print(1);
out.print(" " + a[0]);
out.printf("\n%d", n - id2 - 3 + id1);
for(int i = 2; i < id1; i++)
out.print(" " + a[i]);
for(int i = id2 + 1; i < n; i++)
out.print(" " + a[i]);
out.printf("\n%d", id2 - id1 + 2);
out.print(" " + a[1]);
for(int i = id1; i <= id2; i++)
out.print(" " + a[i]);
out.close();
}
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | f9295a828ad36014ed5648f72d888933 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.*;
import java.util.*;
public class Cf300a {
public static void main(String[] args) throws IOException {
InputStreamReader fin = new InputStreamReader(System.in);
Scanner scr = new Scanner(fin);
int n = scr.nextInt();
int [] k = new int [n];
for (int i = 0; i < n; i++) {
k[i] = scr.nextInt();
}
ArrayList <Integer> neg = new ArrayList<>();
ArrayList <Integer> pos = new ArrayList<>();
ArrayList <Integer> nul = new ArrayList<>();
for (int i = 0; i < n; i++) {
if (k[i] == 0) {
nul.add(k[i]);
} else if (k[i] > 0) {
pos.add(k[i]);
} else {
neg.add(k[i]);
}
}
if (pos.isEmpty()) {
pos.add(neg.get(0));
neg.remove(0);
pos.add(neg.get(0));
neg.remove(0);
}
if (neg.size()%2==0) {
nul.add(neg.get(0));
neg.remove(0);
}
PrintWriter fout = new PrintWriter(System.out);
fout.print(neg.size());
for (int i = 0; i < neg.size(); i++) {
fout.print(" "+neg.get(i));
}
fout.println();
fout.print(pos.size());
for (int i = 0; i < pos.size(); i++) {
fout.print(" "+pos.get(i));
}
fout.println();
fout.print(nul.size());
for (int i = 0; i < nul.size(); i++) {
fout.print(" "+nul.get(i));
}
fout.flush();
fout.close();
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | b8068ff1b7844cd3ea2121becb9f4c59 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.*;
import java.util.*;
public class A181{
private BufferedReader in;
private StringTokenizer st;
private PrintWriter out;
int getSign(int []x,int i,int j){
if(i>j) return 2;
int sign = 1;
for (int k = i; k <= j; k++) {
if(x[k]<0){
sign *= -1;
}
if(x[k] == 0){
sign = 0;
break;
}
}
return sign;
}
void solve() throws IOException{
int n = nextInt();
int n1 = 0,n2 = 0,n3 = 0;
int []x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = nextInt();
}
boolean []del = new boolean[n];
for (int i = 0; i < del.length; i++) {
if(x[i] < 0){
del[i] = true;
out.println("1 "+x[i]);
break;
}
}
boolean found = false;
for (int i = 0; i < del.length; i++) {
if(x[i] > 0){
del[i] = true;
out.println("1 "+x[i]);
found = true;
break;
}
}
if(found){
out.print(n-2);
}
if(!found){
l:for (int i = 0; i < del.length; i++) {
for (int j = i+1; j < del.length; j++) {
if(!del[i] && !del[j] && x[i]<0 && x[j]<0){
del[i] = del[j] = true;
out.println("2 "+x[i]+" "+x[j]);
break l;
}
}
}
out.print(n-3);
}
for (int i = 0; i < del.length; i++) {
if(!del[i]){
out.print(" "+x[i]);
}
}
out.println();
}
A181() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
eat("");
solve();
out.close();
}
private void eat(String str) {
st = new StringTokenizer(str);
}
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
eat(line);
}
return st.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());
}
public static void main(String[] args) throws IOException {
new A181();
}
int gcd(int a,int b){
if(b>a) return gcd(b,a);
if(b==0) return a;
return gcd(b,a%b);
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 9fb4cf1fe28c3f58412624760efd0da8 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class P300A {
public static void prt(ArrayList<Integer> a) {
System.out.print(a.size() + " ");
for (int i = 0; i < a.size() - 1; i++) {
System.out.print(a.get(i) + " ");
}
System.out.print(a.get(a.size() - 1));
System.out.println();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> s1 = new ArrayList<Integer>();
ArrayList<Integer> s2 = new ArrayList<Integer>();
ArrayList<Integer> s3 = new ArrayList<Integer>();
boolean neg = false;
for (int i = 1; i <= n; i++) {
int tmp = sc.nextInt();
if (tmp == 0)
s3.add(tmp);
if (tmp < 0)
if (!neg) {
s1.add(tmp);
neg = true;
} else
s3.add(tmp);
if (tmp > 0)
s2.add(tmp);
}
sc.close();
// Since we are guaranteed a solution exists, if the >0 list is empty, we
// can take two negatives from the zero list.
if (s2.size() == 0)
while (s2.size() != 2)
for (int i = 0; i < s3.size(); i++) {
if (s3.get(i) < 0) {
s2.add(s3.get(i));
s3.remove(i);
break;
}
}
prt(s1);
prt(s2);
prt(s3);
}
} | Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 69559f28b584851fb3645efc554d45a5 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.Scanner;
/**
* Created by root on 28/5/16.
*/
public class a {
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int size=sc.nextInt();
int[] num=new int[size];
// read the array
for (int i = 0; i <size ; i++) {
num[i]=sc.nextInt();
}
// sort array
for (int i = 0; i < size-1; i++) {
for (int j = i+1; j < size; j++) {
// System.out.println(num[i]+" "+num[j]);
if(num[i]> num[j]){
num[i]+=num[j];
num[j]=num[i]-num[j];
num[i]=num[i]-num[j];
}
// System.out.println(num[i]+" "+num[j]);
}
}
// determine the zeroth position
int zero=0;
for (int i=0;i<size;i++){
if(num[i]==0){
zero=i;
}
}
//if no positive numbers and oddd number of negetives
if(zero==size-1 && zero%2!=0){
System.out.print(zero-2);
for(int i=0;i<(zero-2);i++){
System.out.print(" "+num[i]);
}
System.out.println();
System.out.print("2"+" "+num[zero-2]+" "+num[zero-1]);
System.out.println();
System.out.println("1 0");
}
// if no positive nos and even number of negetives
else if (zero== size-1 && zero%2==0){
System.out.print(zero-3);
for(int i=0;i<(zero-3);i++){
System.out.print(" "+num[i]);
}
System.out.println();
System.out.print("2"+" "+num[zero-3]+" "+num[zero-2]);
System.out.println();
System.out.println("2 0"+" "+num[zero-1]);
}
else if(zero!=size-1 && zero%2!=0){
System.out.print(zero);
for (int i = 0; i <zero ; i++) {
System.out.print(" "+num[i]);
}
System.out.println();
System.out.print(size-zero-1);
for (int i = zero+1; i <size ; i++) {
System.out.print(" "+num[i]);
}
System.out.println();
System.out.println("1 0");
}else if(zero!=size-1 && zero%2==0){
System.out.print(zero-1);
for (int i = 0; i <zero-1 ; i++) {
System.out.print(" "+num[i]);
}
System.out.println();
System.out.print(size-zero-1);
for (int i = zero+1; i <size ; i++) {
System.out.print(" "+num[i]);
}
System.out.println();
System.out.println("2 0"+" "+num[zero-1]);
}
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | fd7241a919a366fd27e0fe6a865961d1 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.util.Scanner;
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author yy
*/
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);
A solver = new A();
solver.solve(1, in, out);
out.close();
}
}
class A {
public void solve(int testNumber, Scanner in, PrintWriter out) {
int n;
n = in.nextInt();
int n1 = 0, n2 = 0, n3 = 0;
int a[] = new int[n];
int x[] = new int[n];
int y[] = new int[n];
int z[] = new int[n];
for (int i = 0; i < n; ++i) {
a[i] = in.nextInt();
if (a[i] < 0) {
x[n1++] = a[i];
} else
if (a[i] == 0) {
z[n3++] = a[i];
} else y[n2++] = a[i];
}
if (n2 == 0) {
y[n2++] = x[--n1];
y[n2++] = x[--n1];
}
if (n1 % 2 == 0) {
z[n3++] = x[--n1];
}
out.print(n1 + " ");
for (int i = 0; i < n1; ++i) {
out.print(x[i] + " ");
}
out.print("\n");
out.print(n2 + " ");
for (int i = 0; i < n2; ++i) {
out.print(y[i] + " ");
}
out.print("\n");
out.print(n3 + " ");
for (int i = 0; i < n3; ++i) {
out.print(z[i] + " ");
}
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 50301fd23e2ca69a9b64596fde1f6d1f | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
bf.readLine();
String[] s = bf.readLine().split(" ");
int[] a = new int[s.length];
for(int i=0;i<a.length;i++)
a[i] = Short.parseShort(s[i]);
int n=0;
int p=0;
int z=0;
for(int i=0;i<a.length;i++)
if(a[i]==0)
z++;
else
if(a[i]>0)
p++;
else
if(a[i]<0)
n++;
Arrays.sort(a);
int[] n1,p1,z1;
if(p==0&&n%2==1&&n>2){
p1 = new int[2];
p1[0] = a[0];
p1[1] = a[1];
n1 = new int[n-2];
for(int i=2;i-2<n1.length;i++)
n1[i-2] = a[i];
z1 = new int[z];
System.out.print(n1.length + " ");
for(int i=0;i<n1.length;i++)
if(i==n1.length-1)
System.out.println(n1[i]);
else
System.out.print(n1[i]+" ");
System.out.print(p1.length+" ");
for(int i=0;i<p1.length;i++)
if(i==p1.length-1)
System.out.println(p1[i]);
else
System.out.print(p1[i]+" ");
System.out.print(z1.length+" ");
for(int i=0;i<z1.length;i++)
if(i==z1.length-1)
System.out.println(z1[i]);
else
System.out.print(z1[i]+" ");
}else
if(p==0&&n%2==0&&n>2){
p1 = new int[2];
p1[0] = a[0];
p1[1] = a[1];
n1 = new int[1];
n1[0] = a[2];
z1 = new int[a.length-3];
for(int i=3;i-3<z1.length;i++)
z1[i-3] = a[i];
System.out.print(n1.length + " ");
for(int i=0;i<n1.length;i++)
if(i==n1.length-1)
System.out.println(n1[i]);
else
System.out.print(n1[i]+" ");
System.out.print(p1.length+" ");
for(int i=0;i<p1.length;i++)
if(i==p1.length-1)
System.out.println(p1[i]);
else
System.out.print(p1[i]+" ");
System.out.print(z1.length+" ");
for(int i=0;i<z1.length;i++)
if(i==z1.length-1)
System.out.println(z1[i]);
else
System.out.print(z1[i]+" ");
}else
if(p>0&&n%2==1){
n1 = new int[n];
for(int i=0;i<n;i++)
n1[i] = a[i];
z1 = new int[z];
p1 = new int[p];
for(int i=0;i<p;i++)
p1[i] = a[i+z+n];
System.out.print(n1.length + " ");
for(int i=0;i<n1.length;i++)
if(i==n1.length-1)
System.out.println(n1[i]);
else
System.out.print(n1[i]+" ");
System.out.print(p1.length+" ");
for(int i=0;i<p1.length;i++)
if(i==p1.length-1)
System.out.println(p1[i]);
else
System.out.print(p1[i]+" ");
System.out.print(z1.length+" ");
for(int i=0;i<z1.length;i++)
if(i==z1.length-1)
System.out.println(z1[i]);
else
System.out.print(z1[i]+" ");
}else
if(p>0&&n%2==0){
n1 = new int[1];
n1[0] = a[0];
z1 = new int[z+n-1];
for(int i=0;i<z1.length;i++)
z1[i] = a[i+1];
p1 = new int[p];
for(int i=0;i<p;i++)
p1[i] = a[i+n+z];
System.out.print(n1.length + " ");
for(int i=0;i<n1.length;i++)
if(i==n1.length-1)
System.out.println(n1[i]);
else
System.out.print(n1[i]+" ");
System.out.print(p1.length+" ");
for(int i=0;i<p1.length;i++)
if(i==p1.length-1)
System.out.println(p1[i]);
else
System.out.print(p1[i]+" ");
System.out.print(z1.length+" ");
for(int i=0;i<z1.length;i++)
if(i==z1.length-1)
System.out.println(z1[i]);
else
System.out.print(z1[i]+" ");
}
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 1af5cfe69a1c9763598c72308ed33524 | train_003.jsonl | 1366903800 | Vitaly has an array of n distinct integers. Vitaly wants to divide this array into three non-empty sets so as the following conditions hold: The product of all numbers in the first set is less than zero (β<β0). The product of all numbers in the second set is greater than zero (β>β0). The product of all numbers in the third set is equal to zero. Each number from the initial array must occur in exactly one set. Help Vitaly. Divide the given array. | 256 megabytes | import java.sql.Time;
import java.util.Arrays;
import java.util.Locale;
import java.util.Scanner;
public class MainTest
{
static void print(int a[])
{
for(int i=0;i<a.length;i++) System.out.print(a[i]+" ");
System.out.println();
}
static void print(long a[])
{
for(int i=0;i<a.length;i++) System.out.print(a[i]+" ");
System.out.println();
}
static void println(int a[])
{
for(int i=0;i<a.length;i++) System.out.println(a[i]+" ");
}
static int sum(int []a)
{
int res=0;
for(int i=0;i<a.length;i++) res+=a[i];
return res;
}
/*static int getMax()
{
int max=0;
int k=0;
for(int i=0;i<a.length;i++) if(a[i]>max) {max=a[i]; k=i;}
a[k]=-1;
return max;
}*/
/*static int[] getSimple(int from,int to)
{
int a[]=new int[to-from];
for(int i=0;i<a.length;i++) a[i]=from+i;
//print(a);
int sum=0;
for(int i=0;i<a.length;i++)
{
if(a[i]>0)
{
for(int j=i+1;j<a.length;j++) if(a[j]%a[i]==0) a[j]=0;
sum++;
}
}
int b[]=new int[sum];
sum=0;
for(int i=0;i<a.length;i++) if(a[i]>0) {b[sum]=a[i]; sum++;}
//System.out.println("SIMPLE!");
//print(a);
return b;
}*/
static long[] getSimple(int from,int to)
{
long a[]=new long[to-from];
for(int i=0;i<a.length;i++) a[i]=from+i;
//print(a);
int sum=0;
for(int i=0;i<a.length;i++)
{
if(a[i]>0)
{
for(int j=i+1;j<a.length;j++) if(a[j]%a[i]==0) a[j]=0;
sum++;
}
}
long b[]=new long[sum];
sum=0;
for(int i=0;i<a.length;i++) if(a[i]>0) {b[sum]=a[i]; sum++;}
//System.out.println("SIMPLE!");
print(b);
return b;
}
static int check(int a)
{
if(a==1) return 0;
else return 1;
}
static int[] inverse(int []a, int x,int y)
{
int b[]=new int[a.length];
for(int i=0;i<a.length;i++)
{
if(i>=x&&i<=y)
{
if(a[i]==1) b[i]=0;
else b[i]=1;
continue;
}
b[i]=a[i];
}
return b;
}
//static int a[];
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int n=in.nextInt();
int a[]=new int [n];
int n0=0;
int n1=0;
int n2=0;
for(int i=0;i<n;i++)
{
a[i]=in.nextInt();
if(a[i]>0) {n1++; continue;}
if(a[i]<0) {n2++; continue;}
if(a[i]==0) {n0++; continue;}
}
System.out.print(1);
for(int i=0;i<n;i++)
{
if(a[i]<0) {System.out.print(" "+a[i]); a[i]=0; break;}
}
System.out.println();
if(n1==0)
{
System.out.print(2);
for(int i=0;i<n;i++)
{
if(a[i]<0) {System.out.print(" "+a[i]); a[i]=0; n1++;}
if(n1==2) break;
}
System.out.println();
n2-=2;
}
else if(n1>0)
{
System.out.print(n1);
for(int i=0;i<n;i++)
{
if(a[i]>0) System.out.print(" "+a[i]);
}
System.out.println();
}
System.out.print(n0+n2-1);
for(int i=0;i<n;i++)
{
if(a[i]<0) System.out.print(" "+a[i]);
}
for(int i=0;i<n0;i++) System.out.print(" "+0);
}
}
| Java | ["3\n-1 2 0", "4\n-1 -2 -3 0"] | 2 seconds | ["1 -1\n1 2\n1 0", "1 -1\n2 -3 -2\n1 0"] | null | Java 7 | standard input | [
"constructive algorithms",
"implementation",
"brute force"
] | 03cf2cc26c84aab685ee78a1d6318b30 | The first line of the input contains integer n (3ββ€βnββ€β100). The second line contains n space-separated distinct integers a1,βa2,β...,βan (|ai|ββ€β103) β the array elements. | 1,100 | In the first line print integer n1 (n1β>β0) β the number of elements in the first set. Then print n1 numbers β the elements that got to the first set. In the next line print integer n2 (n2β>β0) β the number of elements in the second set. Then print n2 numbers β the elements that got to the second set. In the next line print integer n3 (n3β>β0) β the number of elements in the third set. Then print n3 numbers β the elements that got to the third set. The printed sets must meet the described conditions. It is guaranteed that the solution exists. If there are several solutions, you are allowed to print any of them. | standard output | |
PASSED | 9685fa6e25e6dd808eadebbdef958920 | train_003.jsonl | 1340983800 | A widely known among some people Belarusian sport programmer Yura possesses lots of information about cars. That is why he has been invited to participate in a game show called "Guess That Car!".The game show takes place on a giant parking lot, which is 4n meters long from north to south and 4m meters wide from west to east. The lot has nβ+β1 dividing lines drawn from west to east and mβ+β1 dividing lines drawn from north to south, which divide the parking lot into nΒ·m 4 by 4 meter squares. There is a car parked strictly inside each square. The dividing lines are numbered from 0 to n from north to south and from 0 to m from west to east. Each square has coordinates (i,βj) so that the square in the north-west corner has coordinates (1,β1) and the square in the south-east corner has coordinates (n,βm). See the picture in the notes for clarifications.Before the game show the organizers offer Yura to occupy any of the (nβ+β1)Β·(mβ+β1) intersection points of the dividing lines. After that he can start guessing the cars. After Yura chooses a point, he will be prohibited to move along the parking lot before the end of the game show. As Yura is a car expert, he will always guess all cars he is offered, it's just a matter of time. Yura knows that to guess each car he needs to spend time equal to the square of the euclidean distance between his point and the center of the square with this car, multiplied by some coefficient characterizing the machine's "rarity" (the rarer the car is, the harder it is to guess it). More formally, guessing a car with "rarity" c placed in a square whose center is at distance d from Yura takes cΒ·d2 seconds. The time Yura spends on turning his head can be neglected.It just so happened that Yura knows the "rarity" of each car on the parking lot in advance. Help him choose his point so that the total time of guessing all cars is the smallest possible. | 256 megabytes | import java.io.*;
import java.util.*;
public class cf201b {
static int n,m;
static int[][] v;
public static void main(String[] args) {
FastIO in = new FastIO(), out = in;
n = in.nextInt();
m = in.nextInt();
v = new int[n][m];
long[] xv = new long[n];
long[] yv = new long[m];
for(int i=0; i<n; i++)
for(int j=0; j<m; j++) {
v[i][j] = in.nextInt();
xv[i] += v[i][j];
yv[j] += v[i][j];
}
long[] fx = find(xv, 0, n);
long[] fy = find(yv, 0, m);
out.println(fx[0]+fy[0]);
out.println(fx[1] + " " + fy[1]);
out.close();
}
static long[] find(long[] v, int lo, int hi) {
while(hi - lo > 6) {
int lt = (lo*2 + hi) / 3;
int rt = (lo + 2*hi) / 3;
if(cost(v,lt) <= cost(v,rt))
hi = rt;
else
lo = lt;
}
long best = lo;
long bestc = cost(v, lo);
while(++lo <= hi) {
long tempc = cost(v, lo);
if(tempc < bestc) {
bestc = tempc;
best = lo;
}
}
return new long[]{bestc, best};
}
static long cost(long[] v, long pos) {
long ans = 0;
for(int i=0; i<v.length; i++)
ans += v[i]*dist2(4*i+2,pos*4);
return ans;
}
static long dist2(long x, long y) {
return (x-y)*(x-y);
}
static class FastIO extends PrintWriter {
BufferedReader br;
StringTokenizer st;
public FastIO() {
this(System.in, System.out);
}
public FastIO(InputStream in, OutputStream out) {
super(new BufferedWriter(new OutputStreamWriter(out)));
br = new BufferedReader(new InputStreamReader(in));
scanLine();
}
public void scanLine() {
try {
st = new StringTokenizer(br.readLine().trim());
} catch (Exception e) {
throw new RuntimeException(e.getMessage());
}
}
public int numTokens() {
if (!st.hasMoreTokens()) {
scanLine();
return numTokens();
}
return st.countTokens();
}
public String next() {
if (!st.hasMoreTokens()) {
scanLine();
return next();
}
return st.nextToken();
}
public double nextDouble() {
return Double.parseDouble(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["2 3\n3 4 5\n3 9 1", "3 4\n1 0 0 0\n0 0 3 0\n0 0 5 5"] | 2 seconds | ["392\n1 1", "240\n2 3"] | NoteIn the first test case the total time of guessing all cars is equal to 3Β·8β+β3Β·8β+β4Β·8β+β9Β·8β+β5Β·40β+β1Β·40β=β392.The coordinate system of the field: | Java 7 | standard input | [
"dp",
"math"
] | 45ac482a6b95f44a26b7363e6756c8d1 | The first line contains two integers n and m (1ββ€βn,βmββ€β1000) β the sizes of the parking lot. Each of the next n lines contains m integers: the j-th number in the i-th line describes the "rarity" cij (0ββ€βcijββ€β100000) of the car that is located in the square with coordinates (i,βj). | 1,800 | In the first line print the minimum total time Yura needs to guess all offered cars. In the second line print two numbers li and lj (0ββ€βliββ€βn,β0ββ€βljββ€βm) β the numbers of dividing lines that form a junction that Yura should choose to stand on at the beginning of the game show. If there are multiple optimal starting points, print the point with smaller li. If there are still multiple such points, print the point with smaller lj. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 0e9ba386cee915307e3b208f4fa359af | train_003.jsonl | 1340983800 | A widely known among some people Belarusian sport programmer Yura possesses lots of information about cars. That is why he has been invited to participate in a game show called "Guess That Car!".The game show takes place on a giant parking lot, which is 4n meters long from north to south and 4m meters wide from west to east. The lot has nβ+β1 dividing lines drawn from west to east and mβ+β1 dividing lines drawn from north to south, which divide the parking lot into nΒ·m 4 by 4 meter squares. There is a car parked strictly inside each square. The dividing lines are numbered from 0 to n from north to south and from 0 to m from west to east. Each square has coordinates (i,βj) so that the square in the north-west corner has coordinates (1,β1) and the square in the south-east corner has coordinates (n,βm). See the picture in the notes for clarifications.Before the game show the organizers offer Yura to occupy any of the (nβ+β1)Β·(mβ+β1) intersection points of the dividing lines. After that he can start guessing the cars. After Yura chooses a point, he will be prohibited to move along the parking lot before the end of the game show. As Yura is a car expert, he will always guess all cars he is offered, it's just a matter of time. Yura knows that to guess each car he needs to spend time equal to the square of the euclidean distance between his point and the center of the square with this car, multiplied by some coefficient characterizing the machine's "rarity" (the rarer the car is, the harder it is to guess it). More formally, guessing a car with "rarity" c placed in a square whose center is at distance d from Yura takes cΒ·d2 seconds. The time Yura spends on turning his head can be neglected.It just so happened that Yura knows the "rarity" of each car on the parking lot in advance. Help him choose his point so that the total time of guessing all cars is the smallest possible. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class B {
public static void main(String[] args) {
MScanner sc = new MScanner();
PrintWriter out = new PrintWriter(System.out);
int H = sc.nextInt();
int W = sc.nextInt();
int[][] grid = sc.nextInt(H,W);
long[] SUMW = new long[W];
long[] SUMH = new long[H];
for(int a=0;a<H;a++){
for(int b=0;b<W;b++){
SUMH[a]+=grid[a][b];
SUMW[b]+=grid[a][b];
}
}
long[] WW = new long[W+1];
long[] HH = new long[H+1];
for(int x = 0; x<= W; x++){
for(int b=0;b<W;b++){
long dx = Math.abs(x-b);
dx<<=2;
if(x<=b)dx+=2;
else dx-=2;
dx*=dx;
WW[x]+=dx*SUMW[b];
}
}
for(int y = 0; y<= H; y++){
for(int a=0;a<H;a++){
long dy = Math.abs(y-a);
dy<<=2;
if(y<=a)dy+=2;
else dy-=2;
dy*=dy;
HH[y]+=dy*SUMH[a];
}
}
long best = Long.MAX_VALUE;
int tx = 0;
int ty =0;
for(int a=0;a<=H;a++)
for(int b=0;b<=W;b++){
if(HH[a]+WW[b]<best){
best = HH[a]+WW[b];
tx = b;
ty = a;
}
// out.println((HH[a]+WW[b])+" "+a+" "+b);
}
out.println(best);
out.println(ty+" "+tx);
out.close();
}
static class MScanner{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public MScanner(){
stream = System.in;
//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[] nextInt(int N){
int[] ret = new int[N];
for(int a=0;a<N;a++)
ret[a] = nextInt();
return ret;
}
int[][] nextInt(int N, int M){
int[][] ret = new int[N][M];
for(int a=0;a<N;a++)
ret[a] = nextInt(M);
return ret;
}
long nextLong(){
return Long.parseLong(next());
}
long[] nextLong(int N){
long[] ret = new long[N];
for(int a=0;a<N;a++)
ret[a] = nextLong();
return ret;
}
double nextDouble(){
return Double.parseDouble(next());
}
double[] nextDouble(int N){
double[] ret = new double[N];
for(int a=0;a<N;a++)
ret[a] = nextDouble();
return ret;
}
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[] next(int N){
String[] ret = new String[N];
for(int a=0;a<N;a++)
ret[a] = next();
return ret;
}
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();
}
String[] nextLine(int N){
String[] ret = new String[N];
for(int a=0;a<N;a++)
ret[a] = nextLine();
return ret;
}
}
}
| Java | ["2 3\n3 4 5\n3 9 1", "3 4\n1 0 0 0\n0 0 3 0\n0 0 5 5"] | 2 seconds | ["392\n1 1", "240\n2 3"] | NoteIn the first test case the total time of guessing all cars is equal to 3Β·8β+β3Β·8β+β4Β·8β+β9Β·8β+β5Β·40β+β1Β·40β=β392.The coordinate system of the field: | Java 7 | standard input | [
"dp",
"math"
] | 45ac482a6b95f44a26b7363e6756c8d1 | The first line contains two integers n and m (1ββ€βn,βmββ€β1000) β the sizes of the parking lot. Each of the next n lines contains m integers: the j-th number in the i-th line describes the "rarity" cij (0ββ€βcijββ€β100000) of the car that is located in the square with coordinates (i,βj). | 1,800 | In the first line print the minimum total time Yura needs to guess all offered cars. In the second line print two numbers li and lj (0ββ€βliββ€βn,β0ββ€βljββ€βm) β the numbers of dividing lines that form a junction that Yura should choose to stand on at the beginning of the game show. If there are multiple optimal starting points, print the point with smaller li. If there are still multiple such points, print the point with smaller lj. Please do not use the %lld specifier to read or write 64-bit integers in Π‘++. It is preferred to use the cin, cout streams or the %I64d specifier. | standard output | |
PASSED | 127edf279bc430013813a33919861c89 | train_003.jsonl | 1340983800 | A widely known among some people Belarusian sport programmer Lesha decided to make some money to buy a one square meter larger flat. To do this, he wants to make and carry out a Super Rated Match (SRM) on the site Torcoder.com. But there's a problem β a severe torcoder coordinator Ivan does not accept any Lesha's problem, calling each of them an offensive word "duped" (that is, duplicated). And one day they nearely quarrelled over yet another problem Ivan wouldn't accept.You are invited to act as a fair judge and determine whether the problem is indeed brand new, or Ivan is right and the problem bears some resemblance to those used in the previous SRMs.You are given the descriptions of Lesha's problem and each of Torcoder.com archive problems. The description of each problem is a sequence of words. Besides, it is guaranteed that Lesha's problem has no repeated words, while the description of an archive problem may contain any number of repeated words.The "similarity" between Lesha's problem and some archive problem can be found as follows. Among all permutations of words in Lesha's problem we choose the one that occurs in the archive problem as a subsequence. If there are multiple such permutations, we choose the one with the smallest number of inversions. Then the "similarity" of a problem can be written as , where n is the number of words in Lesha's problem and x is the number of inversions in the chosen permutation. Note that the "similarity" p is always a positive integer.The problem is called brand new if there is not a single problem in Ivan's archive which contains a permutation of words from Lesha's problem as a subsequence.Help the boys and determine whether the proposed problem is new, or specify the problem from the archive which resembles Lesha's problem the most, otherwise. | 256 megabytes | // practice with kaiboy
import java.io.*;
import java.util.*;
public class CF202B extends PrintWriter {
CF202B() { super(System.out, true); }
Scanner sc = new Scanner(System.in);
public static void main(String[] $) {
CF202B o = new CF202B(); o.main(); o.flush();
}
String[] tt; int[] ii; int n;
String[][] ss; int m;
boolean check(String[] ss, int k, String[] tt, int n) {
for (int i = 0, j = 0; i < n; i++, j++) {
while (j < k && !ss[j].equals(tt[ii[i]]))
j++;
if (j == k)
return false;
}
return true;
}
int find() {
for (int h = 0; h < m; h++)
if (check(ss[h], ss[h].length, tt, n))
return h;
return -1;
}
void main() {
n = sc.nextInt();
tt = new String[n];
ii = new int[n];
for (int i = 0; i < n; i++)
tt[i] = sc.next();
m = sc.nextInt();
ss = new String[m][];
for (int h = 0; h < m; h++) {
int k = sc.nextInt();
ss[h] = new String[k];
for (int j = 0; j < k; j++)
ss[h][j] = sc.next();
}
int p = 1;
for (int i = 0; i < n; i++)
p = p * n;
int x_ = n * n, h_ = -1;
for (int b = 0; b < p; b++) {
for (int a = b, i = 0; i < n; i++) {
ii[i] = a % n;
a /= n;
}
int x = 0;
out:
for (int i = 0; i < n; i++)
for (int j = i + 1; j < n; j++) {
if (ii[i] == ii[j]) {
x = x_ + 1;
break out;
}
if (ii[i] > ii[j])
x++;
}
if (x_ >= x) {
int h = find();
if (h != -1 && (x_ > x || x_ == x && h_ > h)) {
x_ = x; h_ = h;
}
}
}
if (h_ == -1)
println("Brand new problem!");
else {
println(h_ + 1);
print("[:");
p = n * (n - 1) / 2 - x_ + 1;
while (p-- > 0)
print("|");
println(":]");
}
}
}
| Java | ["4\nfind the next palindrome\n1\n10 find the previous palindrome or print better luck next time", "3\nadd two numbers\n3\n1 add\n2 two two\n3 numbers numbers numbers", "4\nthese papers are formulas\n3\n6 what are these formulas and papers\n5 papers are driving me crazy\n4 crazy into the night", "3\nadd two decimals\n5\n4 please two decimals add\n5 decimals want to be added\n4 two add decimals add\n4 add one two three\n7 one plus two plus three equals six"] | 5 seconds | ["1\n[:||||||:]", "Brand new problem!", "1\n[:||||:]", "3\n[:|||:]"] | NoteLet us remind you that the number of inversions is the number of pairs of words that follow in the permutation not in their original order. Thus, for example, if the original problem is "add two numbers", then permutation "numbers add two" contains two inversions β pairs of words "numbers" and "add", "numbers" and "two". Sequence b1,ββb2,ββ...,ββbk is a subsequence of sequence a1,βa2,ββ...,ββan if there exists such a set of indices 1ββ€βi1β<ββi2β<β... ββ<βikββ€βn that aijββ=ββbj (in other words, if sequence b can be obtained from a by deleting some of its elements).In the first test case the first problem contains the "find the palindrome next" permutation as a subsequence, in which the number of inversions equals 1 (words "palindrome" and "next").In the second test case there is no problem that contains a permutation of words from Lesha's problem as a subsequence. | Java 11 | standard input | [
"brute force"
] | 18f44ab990ef841c947e2da831321845 | The first line contains a single integer n (1ββ€βnββ€β15) β the number of words in Lesha's problem. The second line contains n space-separated words β the short description of the problem. The third line contains a single integer m (1ββ€βmββ€β10) β the number of problems in the Torcoder.com archive. Next m lines contain the descriptions of the problems as "k s1 s2 ... sk", where k (1ββ€βkββ€β500000) is the number of words in the problem and si is a word of the problem description. All words from all problem descriptions contain no more than 10 lowercase English letters. It is guaranteed that the total length of words in all problem descriptions does not exceed 500015. | 1,700 | If Lesha's problem is brand new, print string "Brand new problem!" (without quotes). Otherwise, on the first line print the index of the archive problem which resembles Lesha's problem most. If there are multiple such problems, print the one with the smallest index. On the second line print a string consisting of characters [:, character | repeated p times, and characters :], where p is the "similarity" between this problem and Lesha's one. The archive problems are numbered starting from one in the order in which they are given in the input. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.