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 | 38771d4ed6becf5098f0bf9702d59ea9 | train_001.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.*;
public class evenSubSum{
static void count(ArrayList<Integer> a, int n)
{
int flag=0;
int oddcount=0;
int prev=0;
int next=0;
for(int i=0; i<n; i++)
{
if(a.get(i)%2==0)
{
flag=1;
System.out.println("1");
System.out.println(i+1);
break;
}
}
if(flag==0)
{
if(n==1)
{
System.out.println("-1");
}
else
{
System.out.println("2");
System.out.println("1 2");
}
}
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for(int i=0; i<t; i++)
{
ArrayList<Integer> a= new ArrayList<Integer>();
int n=sc.nextInt();
for(int j=0; j<n; j++)
{
int x=sc.nextInt();
a.add(x);
}
count(a,n);
}
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 72e5b39e6b78e65ab468bd13ffe30fa7 | train_001.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
public class GFG {
public static void main (String[] args) {
Scanner s = new Scanner(System.in);
int t;
t=s.nextInt();
for(int k=0;k<t;k++){
int oddcount=0;
int N;
N=s.nextInt();
int arr[]=new int[N];
for(int i=0;i<N;i++){
arr[i]=s.nextInt();
}
int f=0;
ArrayList<Integer> arr1=new ArrayList<Integer>();
for(int i=0;i<N;i++){
if(arr[i]%2==0){
System.out.println(1);
System.out.println(i+1);
f=1;
break;
}
else{
oddcount++;
arr1.add(i+1);
if(oddcount==2){
System.out.println(2);
for(int x=0;x<arr1.size();x++)
System.out.print(arr1.get(x)+" ");
System.out.println();
f=2;
break;
}
}
}
if(f!=1 && f!=2){
System.out.println(-1);
}
}
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 53833d6440afcc9c0f77cac63cff05bc | train_001.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.io.*;
import java.util.*;
import java.lang.*;
public class GFG {
public static void main (String[] args) {
Scanner s = new Scanner(System.in);
int t;
t=s.nextInt();
for(int k=0;k<t;k++){
int oddcount=0;
int N;
N=s.nextInt();
int arr[]=new int[N];
for(int i=0;i<N;i++){
arr[i]=s.nextInt();
}
int f=0;
for(int i=0;i<N;i++){
if(arr[i]%2==0){
System.out.println(1);
System.out.println(i+1);
f=1;
break;
}
else{
oddcount++;
if(oddcount==2){
System.out.println(2);
System.out.println(i+" "+(i+1));
f=2;
break;
}
}
}
if(f!=1 && f!=2){
System.out.println(-1);
}
}
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 2f22d9805f3747ed8f62b0269f69149a | train_001.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.io.*;
import java.util.*;
public class A
{
static int n;
static int[] arr;
static String str;
public static void main(String[] args) throws IOException
{
Flash f = new Flash();
int T = f.ni();
for(int tc = 1; tc <= T; tc++){
n = f.ni(); arr = f.arr(n);
fn();
}
}
static void fn()
{
if(n == 1 && arr[0] % 2 == 1){
System.out.println("-1");
return;
}
for(int i = 0; i < n; i++){
if((arr[i]&1) == 0){
System.out.println("1");
System.out.println(i+1);
return;
}
}
System.out.println("2");
System.out.println("1 2");
}
static void sort(int[] a){
List<Integer> A = new ArrayList<>();
for(int i : a) A.add(i);
Collections.sort(A);
for(int i = 0; i < A.size(); i++) a[i] = A.get(i);
}
static int swap(int itself, int dummy)
{
return itself;
}
//SecondThread
static class Flash
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next(){
while(!st.hasMoreTokens()){
try{
st = new StringTokenizer(br.readLine());
}catch(IOException e){
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine(){
String s = new String();
try{
s = br.readLine().trim();
}catch(IOException e){
e.printStackTrace();
}
return s;
}
//nextInt()
int ni(){
return Integer.parseInt(next());
}
int[] arr(int n){
int[] a = new int[n];
for(int i = 0; i < n; i++) a[i] = ni();
return a;
}
//nextLong()
long nl(){
return Long.parseLong(next());
}
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 290b17a1fa005816f8ba647873b479fb | train_001.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class A {
static int T;
static int N;
static int arr[];
static int Answer;
static int seq[];
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
StringBuilder sb = new StringBuilder();
T = Integer.parseInt(br.readLine());
for (int t = 1; t <= T; t++) {
N = Integer.parseInt(br.readLine());
arr = new int[N + 1];
st = new StringTokenizer(br.readLine());
for (int i = 1; i <= N; i++) {
arr[i] = Integer.parseInt(st.nextToken());
}
Answer = -1;
// 1개
seq = new int[N+1];
DFS(1,0,0);
sb.append(Answer + "\n");
if (Answer != -1) {
for(int i=0;i<Answer;i++) {
sb.append(seq[i]+" ");
}
sb.append("\n");
}
}
System.out.print(sb.toString());
}
public static boolean DFS(int index, int sum ,int cnt) {
if(index>N) {
if(sum%2==0 && sum!=0) {
Answer = cnt;
return true;
}
return false;
}
//비선택
if(DFS(index+1,sum,cnt))
return true;
//선택
seq[cnt] = index;
if(DFS(index+1,sum+arr[index],cnt+1))
return true;
return false;
}
}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | fd946893809853aa70947ccf51a34124 | train_001.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.util.*;
import java.io.*;
import java.lang.Number;
import java.math.BigInteger;
public class sub {
public static void main(String[] args) {
InputReader reader = new InputReader(System.in);
OutputWriter out = new OutputWriter(System.out);
int t = reader.readInt();
for (int x = 0; x < t; x++) {
int n = reader.readInt();
List<Long> lst = new ArrayList<>();
boolean done = false;
for (int y = 1; y <= n; y++) {
long in = reader.readLong();
if (in%2 == 0 && done != true) {
out.printLine("1");
out.printLine("" + y);
done = true;
}
lst.add(in);
}
if (done == false) {
if (lst.size() == 1) {
out.printLine("-1");
} else {
out.printLine("2");
out.printLine("1 2");
}
}
}
out.close();
}
}
/*class cmp implements Comparator<Pair> {
@Override
public int compare (Pair first, Pair second) {
if (first.x - first.y == second.x - second.y) {
return first.x - second.x;
}
return (first.x - first.y) - (second.x - second.y);
}
}
class Pair {
public int x, y;
public Pair(int x, int y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "(" + x + "," + y + ")";
}
}
*/
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 double readDouble() {
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, readInt());
}
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, readInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
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 printLine(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 368ca24c2f75a7a82a54e343f3e94bc4 | train_001.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | // package com.algo.codeforces;
import java.util.Scanner;
public class EvenSubsetSum {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
String[] output = new String[t];
for (int i = 0; i < t; i++) {
int n = scanner.nextInt();
int outputEvenIndex = -1;
int outputOddIndex1 = -1, outputOddIndex2 = -1;
for (int j = 0; j < n; j++) {
int a = scanner.nextInt();
if (outputEvenIndex == -1) {
if (a % 2 == 0) {
outputEvenIndex = j + 1;
} else {
if (outputOddIndex1 == -1) {
outputOddIndex1 = j + 1;
} else if (outputOddIndex2 == -1) {
outputOddIndex2 = j + 1;
}
}
}
}
if (outputEvenIndex != -1) {
output[i] = "1\n"+outputEvenIndex;
} else if(outputOddIndex1 != -1 && outputOddIndex2 != -1) {
output[i] = "2\n"+outputOddIndex1 +" "+ outputOddIndex2;
} else {
output[i] = "-1";
}
}
for (String opt: output) {
System.out.println(opt);
}
}
}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 8da7a01d008398a2e9450c5919af36a2 | train_001.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | // package sec;
import java.util.*;
import java.lang.Math;
public class hut {
public static void main (String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
for (int i = 0; i < t; i++) {
int n=sc.nextInt();
int[] a=new int[n];
for (int j = 0; j < a.length; j++) {
a[j]=sc.nextInt();
}
int flag=0;
int cnt=0;
//index1=0;
for (int j = 0; j < a.length; j++) {
if(a[j]%2==0) {
System.out.println(1);
System.out.println(j+1);
flag=1;
break;
}
else {
cnt++;
if(cnt==2) {
System.out.println(2);
System.out.print(1+" ");
System.out.print(2);
System.out.println();
flag=1;
break;
}
}
}
if(flag==0)System.out.println(-1);
}
}
}
| Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | b44427db0570e1a5347b160a33ed7ca3 | train_001.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.util.Arrays;
import java.util.Scanner;
public class Codeforces {
private static Scanner scanner;
public static void main(String[] args) {
scanner = new Scanner(System.in);
int t = Integer.parseInt(scanner.nextLine());
while (t-- > 0) {
solve();
}
}
static void solve() {
scanner.nextLine();
int[] a = Arrays.stream(scanner.nextLine().split(" "))
.mapToInt(Integer::parseInt)
.toArray();
for (int i = 0; i < a.length; i++) {
int cur = a[i];
if (cur % 2 == 0) {
System.out.println(1);
System.out.println(i + 1);
return;
}
}
if (a.length == 1) {
System.out.println(-1);
} else {
System.out.println(2);
System.out.println("1 2");
}
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 2cfb55283b0444c7658876e4716c1673 | train_001.jsonl | 1583573700 | You are given an array $$$a$$$ consisting of $$$n$$$ positive integers. Find a non-empty subset of its elements such that their sum is even (i.e. divisible by $$$2$$$) or determine that there is no such subset.Both the given array and required subset may contain equal values. | 512 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws Exception {
// FCApplicationManager applicationManager = new FCApplicationManager();
//applicationManager.init();
Scanner sc = new Scanner(System.in);
Integer testcases = sc.nextInt();
sc.nextLine();
for (int i = 0; i < testcases; i++) {
Integer size = sc.nextInt();
int oddCount = 0;
int firstOddIndex = 0;
sc.nextLine();
ArrayList<Integer> array = new ArrayList<>();
for (int j = 1; j <= size; j++) {
Integer num = sc.nextInt();
if (num % 2 == 0) {
System.out.println(1);
System.out.println(j);
break;
}
oddCount++;
if (oddCount != 2) {
firstOddIndex = j;
}
if (oddCount == 2) {
System.out.println(2);
System.out.println(Integer.toString(firstOddIndex) + ' ' + Integer.toString(j));
break;
}
if (j == size) {
System.out.println("-1");
}
}
if(i!=testcases-1)
sc.nextLine();
}
//System.exit(0);
}
} | Java | ["3\n3\n1 4 3\n1\n15\n2\n3 5"] | 1 second | ["1\n2\n-1\n2\n1 2"] | NoteThere are three test cases in the example.In the first test case, you can choose the subset consisting of only the second element. Its sum is $$$4$$$ and it is even.In the second test case, there is only one non-empty subset of elements consisting of the first element, however sum in it is odd, so there is no solution.In the third test case, the subset consisting of all array's elements has even sum. | Java 11 | standard input | [
"dp",
"implementation",
"greedy",
"brute force"
] | 3fe51d644621962fe41c32a2d90c7f94 | The first line contains a single integer $$$t$$$ ($$$1 \leq t \leq 100$$$), number of test cases to solve. Descriptions of $$$t$$$ test cases follow. A description of each test case consists of two lines. The first line contains a single integer $$$n$$$ ($$$1 \leq n \leq 100$$$), length of array $$$a$$$. The second line contains $$$n$$$ integers $$$a_1, a_2, \ldots, a_n$$$ ($$$1 \leq a_i \leq 100$$$), elements of $$$a$$$. The given array $$$a$$$ can contain equal values (duplicates). | 800 | For each test case output $$$-1$$$ if there is no such subset of elements. Otherwise output positive integer $$$k$$$, number of elements in the required subset. Then output $$$k$$$ distinct integers ($$$1 \leq p_i \leq n$$$), indexes of the chosen elements. If there are multiple solutions output any of them. | standard output | |
PASSED | 5b7eca78c885e1f0348cf717f6398019 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.*;
public class Solution {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
int n = sc.nextInt();
String s = sc.next();
String res = "";
while (s.length() != 0) {
if (s.length() % 2 == 1) {
res = res + s.charAt(0);
} else {
res = s.charAt(0) + res;
}
s = s.substring(1);
}
System.out.println(res);
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 99f3cb3072d80317373657a99ba4df59 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.Scanner;
public class L16_B
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String s=sc.next();
char[] a=new char[n];
int l,r;
if (n % 2 != 0)
{
a[n/2]=s.charAt(0);
s=s.substring(1);
l=(n/2)-1;
r=(n/2)+1;
}
else
{
l=(n/2)-1;
r=(n/2);
}
int count=0;
for(int i=0;i<s.length();i++)
{
if(count % 2 == 0)
{
a[l]=s.charAt(i);
l--;
}
else
{
a[r]=s.charAt(i);
r++;
}
count++;
}
for (int i=0;i<n;i++)
System.out.print(a[i]);
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | e88816b18118daa81f774758f8db0d2b | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes |
import java.io.* ;
import java.util.* ;
import java.lang.* ;
public class Main {
public static void main( String[] args ) {
Scanner in = new Scanner(System.in) ;
int n = in.nextInt() ;
String s = in.next() ;
char[] ans = new char[n] ;
int med=(n>>1) + ((1==(n&1))?0:-1), f=(1==(n&1))?-1:1, step=(1==(n&1))?1:-1 ;
for ( int i = 0 ; i < n ; ++i ) {
ans[med] = s.charAt(i) ;
med += f ;
f = f*-1 + step ;
step *= -1 ;
}
System.out.println(String.valueOf(ans)) ;
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 8745717e7d02727f3a08bb68e2ed9179 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.*;
public class Main {
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
int n = scanner.nextInt(), a = 1, l = 1, R = 1;
String s = scanner.next();
char[] c = new char[n];
if (s.length() % 2 == 0) {
c[(s.length() / 2)-1] = s.charAt(0);
a =(s.length() / 2)-1;
s = replace(s, 0);
} else {
c[((s.length() + 1) / 2) - 1] = s.charAt(0);
a =((s.length() + 1) / 2) - 1;
s = replace(s, 0);
}
while (s.length() != 0) {
if (s.length() % 2 == 0) {
c[a - l] = s.charAt(0);
s = replace(s, 0);
l++;
} else {
c[a+ R] = s.charAt(0);
s = replace(s, 0);
R++;
}
}
System.out.println(c);
}
private static String replace(String s, int c) {
String r = "";
for (int i = 0; i < s.length(); i++) {
if (i != c) {
r += s.charAt(i);
}
}
return r;
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 28817a06e4553bcdf8337204dff7ea1e | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.ArrayList;
import java.util.Scanner;
public class Decoding {
public static void main (String args[]){
Scanner s = new Scanner(System.in);
int n=s.nextInt();
String word=s.next();
char[] beforDecode=word.toCharArray();
char[] afterDecode=new char[n];
if(n%2==1){
int index=1;
afterDecode[n/2]=beforDecode[0];
for(int i=1;i+1<n;i+=2){
afterDecode[n/2-index]=beforDecode[i];
afterDecode[n/2+index]=beforDecode[i+1];
index++;
}
}else {
int index = 0;
for (int i = 0; i + 1 < n; i += 2) {
afterDecode[n / 2 - index - 1] = beforDecode[i];
afterDecode[n / 2 + index] = beforDecode[i + 1];
index++;
}
}
System.out.println(afterDecode);
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 6f95ccf4e7ec50930e3e5f91e29923dd | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.Scanner;
public class Decoding
{
public static void main(String [] args)
{
Scanner input = new Scanner(System.in);
int n = input.nextInt();
StringBuffer str = new StringBuffer();
str.append(input.next());
StringBuffer ans = new StringBuffer();
for(int i = str.length() - 1; i >= 0; i--)
ans.insert(ans.length() / 2, str.charAt(i));
System.out.println(ans);
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | b037c0a95d961f0878bfd61ab1ccae6a | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes |
import java.util.Scanner;
public class Orden {
public static void main(String[] args) {
Scanner dato = new Scanner(System.in);
int n = dato.nextInt();
String palabra = dato.next();
String resp= "";
for (int i = 0; i < n; i++) {
if ((n-i)%2==0) {
resp = palabra.charAt(i)+resp;
}else{
resp += palabra.charAt(i);
}
}
System.out.println(resp);
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | a2b9b9c4d8441a671a6ef9e4cf4e4700 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.*;
public class solution {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
String s=sc.nextLine();
String a=""+s.charAt(0);
if(n%2==0) {
for(int i=1;i<n;i++) {
if(i%2!=0) {
a+=s.charAt(i);
}
else {
a=s.charAt(i)+a;
}
}
}
else {
for(int i=1;i<n;i++) {
if(i%2==0) {
a+=s.charAt(i);
}
else {
a=s.charAt(i)+a;
}
}
}
System.out.print(a);
sc.close();
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 716eefb47ac0220293a05fca1c7a4f30 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | // Long Contest 1, Problem P
import java.nio.charset.Charset;
import java.lang.StringBuilder;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
Charset charset = Charset.forName("ascii");
FastIO rd = new FastIO(System.in, System.out, charset);
int n = rd.readInt();
String word = rd.readString();
String res = "";
if(n%2 == 0) {
for(int i=0; i < n; i++) {
char ch = word.charAt(i);
if(i%2 == 0) {
res = ch+res;
}
else {
res = res+ch;
}
}
}
else {
for(int i=0; i < n; i++) {
char ch = word.charAt(i);
if(i%2 == 0) {
res = res+ch;
}
else {
res = ch+res;
}
}
}
System.out.println(res);
}
}
class FastIO {
public final StringBuilder cache = new StringBuilder(1 << 20);
private final InputStream is;
private final OutputStream os;
private final Charset charset;
private StringBuilder defaultStringBuf = new StringBuilder(1 << 8);
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastIO(InputStream is, OutputStream os, Charset charset) {
this.is = is;
this.os = os;
this.charset = charset;
}
public FastIO(InputStream is, OutputStream os) {
this(is, os, Charset.forName("ascii"));
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public long readLong() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
long val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public double readDouble() {
boolean sign = true;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+';
next = read();
}
long val = 0;
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
if (next != '.') {
return sign ? val : -val;
}
next = read();
long radix = 1;
long point = 0;
while (next >= '0' && next <= '9') {
point = point * 10 + next - '0';
radix = radix * 10;
next = read();
}
double result = val + (double) point / radix;
return sign ? result : -result;
}
public String readString(StringBuilder builder) {
skipBlank();
while (next > 32) {
builder.append((char) next);
next = read();
}
return builder.toString();
}
public String readString() {
defaultStringBuf.setLength(0);
return readString(defaultStringBuf);
}
public String readLine() {
skipBlank();
String s="";
int offset = 0;
while (next != -1 && next != '\n') {
s += (char) next;
next = read();
}
return s;
}
public int readString(char[] data, int offset) {
skipBlank();
int originalOffset = offset;
while (next > 32) {
data[offset++] = (char) next;
next = read();
}
return offset - originalOffset;
}
public int readString(byte[] data, int offset) {
skipBlank();
int originalOffset = offset;
while (next > 32) {
data[offset++] = (byte) next;
next = read();
}
return offset - originalOffset;
}
public char readChar() {
skipBlank();
char c = (char) next;
next = read();
return c;
}
public void flush() {
try {
os.write(cache.toString().getBytes(charset));
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public boolean hasMore() {
skipBlank();
return next != -1;
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 2093f3c37e0fa15313d6f94644f2217c | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | //package hiougyf;
import java.io.*;
import java.util.*;
import java.util.Map.Entry;
public class Solution {
public static void main(String[] args){
Scanner sc =new Scanner(System.in);
int n=sc.nextInt();
String s=sc.next();
String ans="";
ans+=s.charAt(0);
if(n%2==0) {
for(int i=1;i<n;i++) {
if(i%2==0) {
ans=s.charAt(i)+ans;
}
else if (i%2!=0) {
ans=ans+s.charAt(i);
}
}
}
else if(n%2!=0) {
for(int i=1;i<n;i++) {
if(i%2!=0) {
ans=s.charAt(i)+ans;
}
else if (i%2==0) {
ans=ans+s.charAt(i);
}
}
}
System.out.println(ans);
}
public static int floorSqrt(int x)
{
// Base Cases
if (x == 0 || x == 1)
return x;
// Do Binary Search for floor(sqrt(x))
int start = 1, end = x, ans=0;
while (start <= end)
{
int mid = (start + end) / 2;
// If x is a perfect square
if (mid*mid == x)
return mid;
// Since we need floor, we update answer when mid*mid is
// smaller than x, and move closer to sqrt(x)
if (mid*mid < x)
{
start = mid + 1;
ans = mid;
}
else // If mid*mid is greater than x
end = mid-1;
}
return ans;
}
static int div(int n,int b) {
int g=-1;
for(int i=2;i<=Math.sqrt(n);i++) {
if(n%i==0&&i!=b) {
g=i;
break;
}
}
return g;
}
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static int lcm(int a, int b)
{
return (a*b)/gcd(a, b);
}
public static boolean isPrime1(int n) {
if (n <= 1) {
return false;
}
if (n == 2) {
return true;
}
for (int i = 2; i <= Math.sqrt(n) + 1; i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
if (n == 2) {
return true;
}
if (n % 2 == 0) {
return false;
}
for (int i = 3; i <= Math.sqrt(n) + 1; i = i + 2) {
if (n % i == 0) {
return false;
}
}
return true;
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 7c47ae57ac83b98e9a0a944b6da3c8b6 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Decoding {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solution sol = new Solution();
sol.solve(in, out);
out.close();
}
private static class Solution {
private void solve(InputReader in, PrintWriter out) {
int n = in.ni();
String s = in.n();
StringBuilder result = new StringBuilder();
if (n % 2 == 0)
for (int i = 0; i < n; ++i)
if (i % 2 == 0)
result.insert(0, s.charAt(i));
else
result.insert(result.length(), s.charAt(i));
else
for (int i = 0; i < n; ++i)
if (i % 2 == 0)
result.insert(result.length(), s.charAt(i));
else
result.insert(0, s.charAt(i));
out.println(result);
}
}
private static class InputReader {
private BufferedReader reader;
private StringTokenizer tokenizer;
private InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
private String n() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
private int ni() {
return Integer.parseInt(n());
}
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | b6c6d521cdac6b7bbf148ea75f4d3cb9 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.*;
import java.math.*;
public class Euler {
public static void main(String[] args) {
Scanner in=new Scanner(System.in);
int n = in.nextInt();
String str = in.next();
String ans = "";
ans += str.charAt(0);
for (int i = 1; i < n; i++) {
if (i % 2 == 0) {
ans = str.charAt(i) + ans;
} else {
ans = ans + str.charAt(i);
}
}
if (n % 2 == 1) {
StringBuilder sb = new StringBuilder(ans);
sb = sb.reverse();
ans = sb.toString();
}
System.out.println(ans);
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | feeca4f11c817a22d773f4ad8a56ee61 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class Main {
private static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
private static Lib l = new Lib();
/**
* Decoding
* https://codeforces.com/problemset/problem/746/B
*/
public static void main(String[] args) {
final int len = l.nextInt();
final String encoded = l.nextLine();
int flip = len % 2 == 0 ? 1 : 0;
StringBuilder left = new StringBuilder();
StringBuilder right = new StringBuilder();
for (int i = 0; i < len; i++) {
if (i % 2 == flip) {
right.append(encoded.charAt(i));
} else {
left.append(encoded.charAt(i));
}
}
out.println(left.reverse().append(right));
out.close();
}
public static class Lib {
BufferedReader br;
StringTokenizer st;
public Lib() {
br = new BufferedReader(new InputStreamReader(System.in));
}
private 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[] nextLongArr(int size) {
long[] a = new long[size];
for (int i = 0; i < size; i++) {
a[i] = nextInt();
}
return a;
}
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;
}
void shuffle(long[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
long tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
}
void sort(long[] arr) {
// mitigate quicksort's worst case
shuffle(arr);
Arrays.sort(arr);
}
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 38bd1baa9e787d136a00017d9e80bf10 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | // package com.company;
import java.io.*;
import java.util.*;
/**
* @ author jigar_nainuji_4_u
*/
public class Main {
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String s = br.readLine();
char ch[]=new char[n];
if(n%2!=0)
{
int k = n/2+1;
int j = n/2-1;
ch[n/2]=s.charAt(0);
boolean flag=false;
for(int i=1;i<n;i++)
{
if(flag==false)
{
ch[j]=s.charAt(i);
j--;
flag=true;
}
else
{
ch[k]=s.charAt(i);
k++;
flag=false;
}
}
}
else
{
ch[n-1]=s.charAt(n-1);
int k=n/2-1;
int j=n/2;
boolean flag = false;
for(int i=0;i<n-1;i++)
{
if(flag==false)
{
ch[k]=s.charAt(i);
k--;
flag=true;
}
else
{
ch[j]=s.charAt(i);
j++;
flag=false;
}
}
}
String ans= String.valueOf(ch);
System.out.println(ans);
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 88f5d1072063ee987a8c193ccac6b33a | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | // package test;
import java.io.*;
import java.util.* ;
public class Testr {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine()) ;
String y = br.readLine();
StringBuilder k = new StringBuilder(y) ;
int l = k.length();
String ans = "" ;
int i ;
if(l%2 == 1) ans+= k.charAt(0);
if(l%2 == 0)
{
for ( i = 0 ; i < l ; i++) {
if(i%2 ==0) ans = k.charAt(i) + ans ;
else ans = ans + k.charAt(i) ;
}
}
else {
for ( i = 1 ; i < l ; i++) {
if(i%2 ==1) ans = k.charAt(i) + ans ;
else ans = ans + k.charAt(i) ;
}
}
System.out.println(ans);
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | d59f6e259b27f0a57035f854b3b1047a | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.Scanner;
public class p9 {
public static void main(String[] args) {
Scanner s=new Scanner(System.in);
int n=s.nextInt();
s.nextLine();
String str=s.nextLine();
int right=n-1;
int left=n-2;
int idx1=0;
int idx2=n-1;
char[] arr=new char[n];
while(right>=0 && left>=0){
arr[idx1]=str.charAt(left);
arr[idx2]=str.charAt(right);
idx2--;
idx1++;
right=right-2;
left=left-2;
}
if(n%2!=0){
arr[n/2]=str.charAt(0);
}
String ans=new String(arr);
System.out.println(ans);
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 064076dfdd8b36e0719ad5e79d3eda2f | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.LinkedList;
import java.util.Scanner;
public class main {
public static void main(String args[]){
Scanner scanner = new Scanner(System.in);
/*
int len =scanner.nextInt();
String s = scanner.next();
int x = s.length();
String t = "";
for(int i = 0 ; i < x; i++){
int length = s.length();
double middle;
if(length % 2 == 0){
middle = (length / 2 ) -1;
t+=s.charAt( (int) middle); // add char to string //
s = s.substring(0,(int) middle) + s.substring( (int) middle+1); // remove char from string //
}
else
{
middle = Math.floor(length / 2);
t+=s.charAt( (int) middle);
s = s.substring(0,(int) middle)+s.substring( (int) middle+1);
}
}
System.out.println(t);
*/
int x = scanner.nextInt();
String incoding = scanner.next();
LinkedList<Character> decoding = new LinkedList<>();
int length = incoding.length();
for(int i = 0 ; i < incoding.length() ; i++){
if(length % 2 == 0){
decoding.push(incoding.charAt(i));
length--;
}
else{
decoding.add(incoding.charAt(i)); // back //
length--;
}
}
for(char c : decoding){
System.out.print(c);
}
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 601745035b3ac6ecafc28e8e70bd837b | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes |
import java.util.Scanner;
public class League {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int size = sc.nextInt();
sc.nextLine();
String s = sc.nextLine();
String result = "";
int curr_len = size;
for(int i =0; i<size; i++){
if((curr_len & 1) == 1){
result = result + s.charAt(i) ;
}else{
result = s.charAt(i) + result ;
}
curr_len--;
}
System.out.print(result);
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | f3c6e25e0514d5dec76b9637e93a140f | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.*;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
String s=sc.next();
int[] x=new int[n];
if(n%2!=0) {
for(int i=n-2;i>=0;i-=2) {
System.out.print(s.charAt(i));
}
System.out.print(s.charAt(0));
for(int i=2;i<n;i+=2) {
System.out.print(s.charAt(i));
}
}else {
for(int i=n-2;i>=0;i-=2) {
System.out.print(s.charAt(i));
}
for(int i=1;i<n;i+=2) {
System.out.print(s.charAt(i));
}
}
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | c631fc869ec26c024a10e6920030972a | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.Scanner;
public class Main {
private static Scanner in = new Scanner(System.in);
public static void main(String[] args) {
// write your code here
int len = in.nextInt();
String needed = "",given = in.next();
int i = 0;
needed += given.charAt(0);
len--;
while(len>0)
{
i++;
if(len % 2 == 0)
{
needed = given.charAt(i) + needed;
len--;
}
else
{
needed = needed + given.charAt(i);
len--;
}
}
System.out.println(needed);
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 5a010aebde16ad2afd5ee8282ce38d89 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.*;
import java.lang.Math.*;
public class CF {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
char c[]= sc.next().toCharArray();
char c1[]=new char[c.length];
int i=0;
if(c.length==1)
{
System.out.println(c[0]);
System.exit(0);
}
if(c.length==2)
{
System.out.print(c[0]);
System.out.print(c[1]);
System.exit(0);
}
if(c.length%2!=0)
{
int mid=(c.length/2);
int a=mid-1;
int b=mid+1;
c1[mid]=c[i++];
while(a>=0 && b<c.length)
{
c1[a]=c[i++];
c1[b]=c[i++];
a--;
b++;
}
for(int j=0;j<c1.length;j++)
{
System.out.print(c1[j]);
}
System.exit(0);
}
if(c.length%2==0)
{
int mid=c.length/2;
int b=mid;
int a=mid-1;
while(a>=0 && b<c.length)
{
c1[a]=c[i++];
c1[b]=c[i++];
a--;
b++;
}
for(int j=0;j<c1.length;j++)
{
System.out.print(c1[j]);
}
System.exit(0);
}
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 868e55fdd4063e41729065e984ba930c | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Decoding {
public static void main(String[]args) {
Scanner s = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
int n=s.nextInt();
s.nextLine();
String str=s.nextLine();
char[]array=new char[n];
if(n%2==1){
int mid=n/2;
array[mid]=str.charAt(0);
int e=mid+1;
int odd=mid-1;
for(int i=1;i<n;i++){
if(i%2==1){
array[odd]=str.charAt(i);
odd--;
}else{
array[e]=str.charAt(i);
e++;
}
}
}else{
int mid=(n/2)-1;
array[mid]=str.charAt(0);
int e=mid-1;
int odd=mid+1;
for(int i=1;i<n;i++){
if(i%2==1){
array[odd]=str.charAt(i);
odd++;
}else{
array[e]=str.charAt(i);
e--;
}
}
}
for(int i=0;i<array.length;i++)
System.out.print(array[i]);
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 6c3e493b77a0387d218ee9e1e4fd4fec | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes |
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
int n=Reader.nextInt();
String x=Reader.next();
StringBuilder out=new StringBuilder();
out.append(x.charAt(0));
for (int i = 1; i < n; i++) {
if(i%2==0)
out.append(x.charAt(i));
else
out.insert(0,x.charAt(i));
}
if(n%2==0)
System.out.println(out.reverse());
else
System.out.println(out);
}
}
class Reader {
static StringTokenizer tokenizer;
static BufferedReader reader;
public static void init(InputStream input) throws UnsupportedEncodingException {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
public static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static String nextLine() throws IOException {
return reader.readLine();
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
}
| Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 53040f755e86df2a6f769467f429df70 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.Scanner;
public class program {
public static void main(String[] args) {
Scanner scan= new Scanner(System.in);
int n=scan.nextInt();
String str = scan.next();
if (n%2!=0) {
for (int i = n-2; i >0; i-=2) {
System.out.print(str.charAt(i));
}
for (int i = 0; i < n; i+=2) {
System.out.print(str.charAt(i));
}
}else {
for (int i = n-2; i >=0; i-=2) {
System.out.print(str.charAt(i));
}
for (int i = 1; i < n; i+=2) {
System.out.print(str.charAt(i));
}
}
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | 0f199c40968deccdeabeab4a2a8742c4 | train_001.jsonl | 1482057300 | Polycarp is mad about coding, that is why he writes Sveta encoded messages. He calls the median letter in a word the letter which is in the middle of the word. If the word's length is even, the median letter is the left of the two middle letters. In the following examples, the median letter is highlighted: contest, info. If the word consists of single letter, then according to above definition this letter is the median letter. Polycarp encodes each word in the following way: he writes down the median letter of the word, then deletes it and repeats the process until there are no letters left. For example, he encodes the word volga as logva.You are given an encoding s of some word, your task is to decode it. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
public class Main {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader scn = new FastReader();
StringBuilder res = new StringBuilder();
int n = scn.nextInt();
String str = scn.nextLine();
char[] arr = new char[n];
int i = 0;
int j = n - 1;
int p = n - 1;
while(p >= 0){
if(p - 1 >= 0 ){
arr[i] = str.charAt(p - 1);
}
arr[j] = str.charAt(p);
i++;
j--;
p -= 2;
}
for (int k = 0; k < arr.length; k++) {
res.append(arr[k]);
}
System.out.println(res);
}
} | Java | ["5\nlogva", "2\nno", "4\nabba"] | 1 second | ["volga", "no", "baba"] | NoteIn the first example Polycarp encoded the word volga. At first, he wrote down the letter l from the position 3, after that his word looked like voga. After that Polycarp wrote down the letter o from the position 2, his word became vga. Then Polycarp wrote down the letter g which was at the second position, the word became va. Then he wrote down the letter v, then the letter a. Thus, the encoding looked like logva.In the second example Polycarp encoded the word no. He wrote down the letter n, the word became o, and he wrote down the letter o. Thus, in this example, the word and its encoding are the same.In the third example Polycarp encoded the word baba. At first, he wrote down the letter a, which was at the position 2, after that the word looked like bba. Then he wrote down the letter b, which was at the position 2, his word looked like ba. After that he wrote down the letter b, which was at the position 1, the word looked like a, and he wrote down that letter a. Thus, the encoding is abba. | Java 11 | standard input | [
"implementation",
"strings"
] | 2a414730d1bc7eef50bdb631ea966366 | The first line contains a positive integer n (1 ≤ n ≤ 2000) — the length of the encoded word. The second line contains the string s of length n consisting of lowercase English letters — the encoding. | 900 | Print the word that Polycarp encoded. | standard output | |
PASSED | fd38daf3a7526747314260de9ffb92bb | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* Created with IntelliJ IDEA.
* User: AUtemuratov
* Date: 07.04.14
* Time: 15:43
* To change this template use File | Settings | File Templates.
*/
public class Main {
static int n,x,sum,max=-111111111;
static char c[][] = new char[1111][1111];
static boolean ok = false;
static String s = "";
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer tk = new StringTokenizer(bf.readLine(), " ");
n = Integer.parseInt(tk.nextToken());
for (int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if (i%2==0 && j%2==0) {
//s += "C";
//asdfasdf
c[i][j] = 'C';
sum++;
} else if (i%2==0 && j%2==1) {
//s += ".";
c[i][j] = '.';
} else if (i%2==1 && j%2==0) {
//s += ".";
c[i][j] = '.';
} else if (i%2==1 && j%2==1) {
//s += "C";
c[i][j] = 'C';
sum++;
}
}
//s += "\n";
}
System.out.println(sum);
//System.out.println(s);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
pw.print(c[i][j]);
}
pw.println();
}
pw.close();
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | d9fe65433addae6fb01b892eaf20a7e5 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Scanner;
import java.util.StringTokenizer;
/**
* Created with IntelliJ IDEA.
* User: AUtemuratov
* Date: 07.04.14
* Time: 15:43
* To change this template use File | Settings | File Templates.
*/
public class Main {
static int n,x,sum,max=-111111111;
static char c[][] = new char[1111][1111];
static boolean ok = false;
static String s = "";
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer tk = new StringTokenizer(bf.readLine(), " ");
n = Integer.parseInt(tk.nextToken());
for (int i=0; i<n; i++) {
for(int j=0; j<n; j++) {
if (i%2==0 && j%2==0) {
//s += "C";
//asdfasdf
c[i][j] = 'C';
sum++;
} else if (i%2==0 && j%2==1) {
//s += ".";
c[i][j] = '.';
} else if (i%2==1 && j%2==0) {
//s += ".";
c[i][j] = '.';
} else if (i%2==1 && j%2==1) {
//s += "C";
c[i][j] = 'C';
sum++;
}
}
//s += "\n";
}
System.out.println(sum);
//System.out.println(s);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
pw.print(c[i][j]);
}
pw.println();
}
pw.close();
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 55ea95fb84df5c87f1f5eade15fde33b | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main
{
static BufferedReader reader;
static StringTokenizer tokenizer;
static PrintWriter writer;
static int nextInt() throws IOException
{
return Integer.parseInt(nextToken());
}
static long nextLong() throws IOException
{
return Long.parseLong(nextToken());
}
static double nextDouble() throws IOException
{
return Double.parseDouble(nextToken());
}
static boolean eof = false;
static String nextToken() throws IOException
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public static void main(String[] args) throws IOException
{
tokenizer = null;
reader = new BufferedReader(new InputStreamReader(System.in));
writer = new PrintWriter(System.out);
banana();
reader.close();
writer.close();
}
static void banana() throws IOException
{
int n = nextInt();
char pp[] =
{ 'C', '.' };
StringBuilder sb = new StringBuilder();
System.out.println((n * n + 1) / 2);
for (int i = 0, index = i % 2; i < n; ++i, index = i % 2)
{
for (int j = 0; j < n; ++j)
{
sb.append(pp[index]);
index = 1 - index;
}
sb.append("\n");
}
System.out.println(sb.toString());
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 3fbd14ab052274c5744826e028759601 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws Exception {
Reader.init(System.in);
int n = Reader.nextInt() ;
StringBuilder s = new StringBuilder () ;
boolean is = true ;
if (n%2==0) {
System.out.println((n/2)*n);
} else {
System.out.println(((n/2)+1)*((n/2)+1)+(n/2)*(n/2));
}
for (int i=0 ; i<n ; i++) {
if (is)
for (int j=0 ; j<n ; j++) {
if (j%2==0)
s.append((j==n-1)?"C\n":"C");
else
s.append((j==n-1)?".\n":".");
} else
for (int j=0 ; j<n ; j++) {
if (j%2!=0)
s.append((j==n-1)?"C\n":"C");
else
s.append((j==n-1)?".\n":".");
}
if (is)
is=false;
else is=true;
}
System.out.println(s.deleteCharAt(s.length()-1));
}
public static void Fill (long [] a)throws Exception {
for (int i=0 ; i<a.length ; i++)
a[i]=Reader.nextLong();
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 2bb22d52e7c27c9e2a2b168cc1a7766d | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.*;
public class A
{
public static void main(String[]args)throws IOException
{
DataInputStream N=new DataInputStream(System.in);
int n=Integer.parseInt(N.readLine());
int h=n/2;;
if(n%2==0)System.out.println(n*n/2);
else
{
System.out.println((h+1)*(h+1)+h*h);
}
int flag=1;
for(int i=0;i<n;i++)
{
flag=(i+1)%2;
for(int j=0;j<n;j++)
{
if(flag==1)System.out.print("C");
else System.out.print(".");
flag=(flag+1)%2;
}
System.out.println();
}
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 78ff9f0b879b0d70b69dd7f106ec5819 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.*;
public class codetest {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int answer = (int)Math.pow(n,2) / 2 + n % 2;
System.out.println(answer);
String outline = "C.C.C.C.C.C.C.C.C.C.";
for (int i = 0; i < 6; i++)
outline += outline;
for (int i = 0; i < n; i++)
System.out.println(i % 2 == 0 ? outline.substring(0,n) : outline.substring(1, n+1));
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 4260a6d23dc5b7ebc36b9b0feaab4d1d | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
System.out.println(n % 2 == 0 ? n * n / 2 : n * n / 2 + 1);
StringBuilder s0 = new StringBuilder();
StringBuilder s1 = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i < 2) {
for (int j = 0; j < n; j++) {
(i % 2 == 0 ? s0 : s1).append((i + j) % 2 == 0 ? 'C' : '.');
}
}
System.out.println(i % 2 == 0 ? s0 : s1);
}
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | b26b86107404b177ade1fb5e07add2be | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.*;
public class Problem384A {
StreamTokenizer in;
PrintWriter out;
public static void main( String[] args) throws IOException {
new Problem384A().run();
}
public int nextInt() throws IOException {
in.nextToken();
return (int)in.nval;
}
void run() throws IOException
{
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
out.flush();
}
void solve() throws IOException
{
int n = nextInt();
long k = n*n;
boolean chet = (n%2==0);
boolean symb = true;
out.println(Math.round(k / 2.0));
if ( chet ){
for ( int i = 0; i<n; i++){
if (symb)
for ( int j = 0; j<n/2; j++)
out.print("C.");
else
for ( int j = 0 ; j<n/2; j++)
out.print(".C");
symb=!symb;
out.println();
}
}
else{
for ( int i = 0; i<n; i++){
if ( symb ) {
out.print("C");
for ( int j = 0 ; j<(n-1)/2;j++)
out.print(".C");
}
else {
out.print(".");
for ( int j = 0 ; j<(n-1)/2;j++)
out.print("C.");
}
out.println();
symb=!symb;
}
}
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 92acd71987af88088214cfbbc28eccb0 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Codeforces {
public static void main(String[] args) {
CustomInputReader in = new CustomInputReader(System.in);
Task solver = new Task();
solver.solve(in);
}
}
class Task {
public void solve(CustomInputReader in) {
int n = in.nextInt();
char[][] a = new char[n][n];
for (int i = 0; i < n; i++) Arrays.fill(a[i], '.');
int coders = 0;
for (int i = 0; i < n; i++) {
for (int j = i % 2; j < n; j += 2) {
a[i][j] = 'C';
coders++;
}
}
System.out.println(coders);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.print(a[i][j]);
}
System.out.println();
}
}
}
class CustomInputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public CustomInputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 0ebca4fb09124e97b5ff4e7db47f7508 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Codeforces {
public static void main(String[] args) {
CustomInputReader in = new CustomInputReader(System.in);
Task solver = new Task();
solver.solve(in);
}
}
class Task {
public void solve(CustomInputReader in) {
int n = in.nextInt();
char[][] a = new char[n][n];
for (int i = 0; i < n; i++) Arrays.fill(a[i], '.');
int coders = n * (n / 2);
if (n % 2 == 1) {
coders += n - n / 2;
}
System.out.println(coders);
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
if ((i + j) % 2 == 0) {
System.out.print('C');
} else {
System.out.print('.');
}
}
System.out.println();
}
}
}
class CustomInputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public CustomInputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 02e9103fd4e61af2c62558df250b4d70 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
if(n==1)
{
System.out.println(1);
System.out.println("C");return;
}
if(n==2)
{
System.out.println(2);
System.out.println("C.\n.C");return;
}
char[][] ch = new char[n][n];
for(int i = 0 ; i <n;++i)for(int j = 0 ; j <n;++j)ch[i][j]='.';
int count =0;
for(int j = 0 ; j <n ;++j)
{
for(int i = 0 ; i < n ;)
{
if(j%2==0)
{
ch[i][j]='C';
count++;
i +=2;
}
if(j%2!=0)
{
if(i==0)i=1;
ch[i][j]='C';
count++;
i +=2;
}
}
}
StringBuilder sb = new StringBuilder("");
sb.append(count+"\n");
for(int i = 0 ; i <n;i++)
{
for(int j = 0 ; j <n;++j)
{
sb.append(ch[i][j]);
}
sb.append("\n");
}
System.out.print(sb);
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | c5917f7fc2f5877bc3b0de42ffef6be1 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
public class CF384A {
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
for(String ln;(ln=in.readLine())!=null;){
int N = Integer.parseInt(ln);
char[][] m = new char[N][N];
StringBuilder sb = new StringBuilder();
int cant = 0;
for(int i=0;i<N;++i){
for(int j=0;j<N;++j){
m[i][j]='.';
if(i%2==0 && j%2==0){
m[i][j]='C';
cant++;
}
else if(i%2!=0 && j%2!=0){
m[i][j]='C';
cant++;
}
}
sb.append(new String(m[i])+"\n");
}
System.out.println(cant);
System.out.print(new String(sb));
}
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 05b2018c25c24c08354f2e97da5bddca | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | //package javaapplication1;
import java.io.*;
import java.util.*;
public class JavaApplication1 implements Runnable {
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
final boolean OJ = System.getProperty("ONLINE_JUDGE") != null;
void init() throws FileNotFoundException {
if (OJ) {
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");
}
}
@Override
public void run() {
try {
init();
solve();
out.close();
} catch(Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
public static void main(String[] args) {
new Thread(null, new JavaApplication1(), "", (1L << 20) * 256).start();
}
String readString() {
while(!tok.hasMoreTokens()) {
try {
tok = new StringTokenizer(in.readLine());
} catch(Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
return tok.nextToken();
}
int readInt() {
return Integer.parseInt(readString());
}
////////////////////////////////////////////////////////////////////////////////
class Pair implements Comparable {
public int a, b;
public Pair(int a, int b) {
this.a = a;
this.b = b;
}
public int compareTo(Object x) {
int cx = ((Pair)x).a;
int cy = ((Pair)x).b;
if (a > cx) {
return 1;
} else if (a < cx) {
return -1;
} else {
if (b > cy) {
return 1;
} else if (b < cy) {
return -1;
} else {
return 0;
}
}
}
}
void solve() {
int n = readInt();
out.println(n * n / 2 + (n & 1));
for(int i = 0; i < n; i++) {
for(int j = 0; j < n; j++) {
if (((i + j) & 1) == 0) {
out.print("C");
} else {
out.print(".");
}
}
out.println();
}
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 91b1dd94b46c66b18a80624e0e46d795 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class A {
static Scanner sc=new Scanner(System.in);
public static void main(String[] args) {
int n=sc.nextInt();
if(n%2==0)
System.out.println(n*n/2);
else
System.out.println((n*n+1)/2);
if (n>=2) {
String a="",b="";
for (int i = 0; i < n; i++)
if(i%2==0) {a=a+"C";b=b+".";}
else {a=a+".";b=b+"C";}
for (int i = 0; i < n; i++)
System.out.println((i%2==0)?a:b);
} else
System.out.println("C");
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | d67f683964119bbd79899f6b39ec569f | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n =Integer.parseInt(br.readLine());
System.out.println((int) Math.ceil(Math.pow((n), 2) / 2));
String tab = "";
for (int i = 0; i < n; ++i) {
for (int j = 0; j < n; ++j) {
if (i % 2 != 0) {
if (j % 2 != 0) {
tab += "C";
} else {
tab += ".";
}
} else {
if (j % 2 != 0) {
tab += ".";
} else {
tab += "C";
}
}
}
System.out.println(tab);
tab="";
}
System.exit(0);
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 66e173e7473f42fd96eaeccaa3ffb3ed | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner t =new Scanner(System.in);
int n=t.nextInt();
StringBuilder b1=new StringBuilder();
StringBuilder b2=new StringBuilder();
StringBuilder b3=new StringBuilder();
int c1=0,c2=0,c3=0;
for(int i=0; i<n;i++){
if(i%2==0){
c1++;
b1.append("C");
b2.append(".");
}
else{
c2++;
b1.append(".");
b2.append("C");
}
}
b1.append("\n");
b2.append("\n");
for(int i=0; i<n;i++){
if(i%2==0){
b3.append(b1);
c3+=c1;
}
else{
b3.append(b2);
c3+=c2;
}
}
System.out.print(c3+"\n"+b3);
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 7e4da779031167cf63cb5598337408d7 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.*;
public class Code {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int con = 0;
ArrayList<String> r = new ArrayList();
for (int i = 1; i <= a; i++) {
if (i % 2 != 0) {
for (int j = 1; j <= a; j++) {
if (j % 2 != 0) {
r.add("C");
con++;
} else {
r.add(".");
}
}
} else {
for (int j = 1; j <= a; j++) {
if (j % 2 == 0) {
r.add("C");
con++;
} else {
r.add(".");
}
}
}
}
System.out.println(con);
int u = 0;
String q = "";
int aux = 0;
for (int i = 0; i < a; i++) {
q = "";
for (int j = aux; q.length() < a; j++) {
q += r.get(j);
}
aux += a ;
System.out.println(q);
}
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | bf45fa711f546a0249063f738d4eeb2c | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.*;
/**
* 384A: Coder
*/
public class Main {
public static void main (String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int total;
char test = 'C';
total = Integer.parseInt(br.readLine());
if(total%2==0) {System.out.println((total*total)/2);}
else {System.out.println(((total*total)/2)+1);}
for(int i=0; i<total; i++) {
for(int j=0; j<total; j++) {
if((i%2==0 && j%2==0) || (i%2==1 && j%2==1)) {test = 'C';}
else {test = '.';}
if(j<total-1) {System.out.print(test);}
else {System.out.println(test);}
}
}
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 4354f93509da27795cf9cb7ee225a0ed | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class MainChess {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String line = "";
StringBuilder out = new StringBuilder();
while ((line = in.readLine()) != null && line.length() != 0) {
StringBuilder out2 = new StringBuilder();
int n = Integer.parseInt(line);
long total = 0;
boolean c = true;
for (int i = 0; i < n; i++) {
if(n%2==0)
c = !c;
for (int j = 0; j < n; j++) {
if(c){
out2.append("C");
total++;
}else
out2.append(".");
c = !c;
}
out2.append("\n");
}
out.append(total+"\n"+out2);
}
System.out.print(out);
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 1436d2fc6ba968f75e24964e7112e500 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Arrays;
import java.util.Scanner;
/*
Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).
Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder.
Input
The first line contains an integer n (1 ≤ n ≤ 1000).
Output
On the first line print an integer, the maximum number of Coders that can be placed on the chessboard.
On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'.
If there are multiple correct answers, you can print any.
*/
/**
*
* @author TUNGPT.HO
*/
public class Coder {
static char next(char c) {
if (c == 'C') {
return '.';
} else {
return 'C';
}
}
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
if (n % 2 == 0) {
System.out.println(n * n / 2);
} else {
System.out.println((n * n + 1) / 2);
}
String part1 = "C.";
String part2 = ".C";
for (int i = 0; i <= (n / 2) + 1; i++) {
part1 += "C.";
part2 += ".C";
}
if (part1.length() >= n) {
part1 = part1.substring(0, n);
}
if (part2.length() >= n) {
part2 = part2.substring(0, n);
}
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
System.out.println(part1);
} else {
System.out.println(part2);
}
}
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 086d92d1e66dc9325b473c3feacad86b | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class Task384A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
sc.close();
StringBuilder sb = new StringBuilder();
boolean f = true;
for (int i = 0; i < n; i++) {
if (f) {
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
sb.append('C');
} else {
sb.append('.');
}
}
} else {
for (int j = 0; j < n; j++) {
if (j % 2 == 0) {
sb.append('.');
} else {
sb.append('C');
}
}
}
sb.append('\n');
f = !f;
}
System.out.println((n * n + 1) / 2);
System.out.println(sb);
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 1ac8ebbe70184849e97d403f6df7c2e9 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.lang.*;
import java.io.*;
import java.util.*;
public class Coder {
public static void main(String[] args) throws java.lang.Exception {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(in, out);
out.close();
}
}
class TaskA {
public void solve(InputReader in, PrintWriter out) {
StringBuilder sb0 = new StringBuilder();
StringBuilder sb1 = new StringBuilder();
int n = in.nextInt();
int i;
for (i=0; i<n; ++i) {
if ((i&1) == 1) {
sb1.append('C');
sb0.append('.');
} else {
sb0.append('C');
sb1.append('.');
}
}
String s0 = sb0.toString();
String s1 = sb1.toString();
int ans;
if (n%2 == 0) {
ans = n/2 * n;
} else {
int n_ = (n+1)/2;
ans = n_*n_ + (n_-1)*(n_-1);
}
out.println(ans);
for (i=0; i<n; ++i) {
if ((i&1) == 1) {
out.println(s1);
} else {
out.println(s0);
}
}
}
}
class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer==null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 3e606673c49bbe478c1c756317648e3f | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Library {
static int[] di = { 0, -1, 0, 1 };
static int[] dj = { -1, 0, 1, 0 };
static int[] di_knight = { -1, -1, -2, -2, 1, 1, 2, 2 };
static int[] dj_knight = { 2, -2, -1, 1, 2, -2, -1, 1 };
static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
public static void main(String[] args) throws Exception {
int n = readInt();
char arr[][] = new char[n][n];
int cnt = 0;
for (int i = 0; i < arr.length; i++) {
int j = 0;
if(i % 2 != 0){
j = 1;
}
for (; j < arr.length; j+=2){
arr[i][j] = 'C';
cnt++;
}
}
System.out.println(cnt);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
sb.append(arr[i][j]=='C' ? 'C' : '.');
}sb.append("\n");
}
System.out.println(sb);
}
// Check if String A is smaller lexicographically than String B
public static boolean compareStringLexicographically(String A, String B) {
if (A == null || B == null)
return true;
if (B.indexOf(A) == 0)
return true;
for (int i = 0; i < Math.min(A.length(), B.length()); i++) {
if (A.charAt(i) != B.charAt(i))
return A.charAt(i) < B.charAt(i);
}
return A.length() < B.length();
}
public static int roundUp(int a, int b) {
return (int) Math.ceil(a / (b * 1.0));
}
// Read Integer
public static int readInt() throws Exception {
return Integer.parseInt(br.readLine());
}
// Read Integer
public static int readInt(String str) throws Exception {
return Integer.parseInt(str);
}
// Read Integer Array
public static int[] readIntArray() throws Exception {
String[] s = br.readLine().split(" ");
int arr[] = new int[s.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = readInt(s[i]);
}
return arr;
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 89bfbd882113bbaa7023afa8a120ab91 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Library {
static int[] di = { 0, -1, 0, 1 };
static int[] dj = { -1, 0, 1, 0 };
static int[] di_knight = { -1, -1, -2, -2, 1, 1, 2, 2 };
static int[] dj_knight = { 2, -2, -1, 1, 2, -2, -1, 1 };
static BufferedReader br = new BufferedReader(new InputStreamReader(
System.in));
public static void main(String[] args) throws Exception {
int n = readInt();
char arr[][] = new char[n][n];
int cnt = 0;
for (int i = 0; i < arr.length; i++) {
int j = 0;
if(i % 2 != 0){
j = 1;
}
for (; j < arr.length; j+=2){
arr[i][j] = 'C';
cnt++;
}
}
System.out.println(cnt);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
sb.append(arr[i][j]=='C' ? 'C' : '.');
}sb.append("\n");
}
System.out.println(sb);
}
// Check if String A is smaller lexicographically than String B
public static boolean compareStringLexicographically(String A, String B) {
if (A == null || B == null)
return true;
if (B.indexOf(A) == 0)
return true;
for (int i = 0; i < Math.min(A.length(), B.length()); i++) {
if (A.charAt(i) != B.charAt(i))
return A.charAt(i) < B.charAt(i);
}
return A.length() < B.length();
}
public static int roundUp(int a, int b) {
return (int) Math.ceil(a / (b * 1.0));
}
// Read Integer
public static int readInt() throws Exception {
return Integer.parseInt(br.readLine());
}
// Read Integer
public static int readInt(String str) throws Exception {
return Integer.parseInt(str);
}
// Read Integer Array
public static int[] readIntArray() throws Exception {
String[] s = br.readLine().split(" ");
int arr[] = new int[s.length];
for (int i = 0; i < arr.length; i++) {
arr[i] = readInt(s[i]);
}
return arr;
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | f54e98b9b24909da56f1d65e7f933dab | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
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("");
private void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
private void solve() throws IOException {
int n = nextInt();
out.println(n * n / 2 + (n * n) % 2);
for(int i = 0; i < n; i++){
int k = (i+1) % 2;
for(int j = 0; j < n; j++){
out.print(((k == 0) ? '.' : 'C'));
k ^= 1;
}
out.println();
}
}
void chk(boolean b) {
if (b)
return;
System.out.println(new Error().getStackTrace()[1]);
exit(999);
}
void deb(String fmt, Object... args) {
System.out.printf(Locale.US, fmt + "%n", args);
}
String nextToken() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 07a413c4a388cd4693ee8d4b687c1655 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF225A {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer stringTokenizer;
String next() throws IOException{
while(stringTokenizer == null || !stringTokenizer.hasMoreTokens()){
stringTokenizer = new StringTokenizer(br.readLine());
}
return stringTokenizer.nextToken();
}
CF225A() throws IOException{
}
int nextInt() throws IOException{
return Integer.parseInt(next());
}
long nextLong() throws IOException{
return Long.parseLong(next());
}
void solve() throws IOException{
//EnterCoder here...
int n = nextInt();
int count = ((n*n)+(1))/2;
String[][] s = new String[n][n];
for(int i =0;i<n;i+=2){
for(int j=0;j<n;j+=2){
s[i][j] ="C";
}
for(int j=1;j<n;j+=2){
s[i][j]=".";
}
}
for(int i =1;i<n;i+=2){
for(int j=0;j<n;j+=2){
s[i][j]= ".";
}
for(int j=1;j<n;j+=2){
s[i][j]="C";
}
}
pw.println(count);
for(int i =0;i<n;i++){
for(int j=0;j<n;j++){
pw.print(s[i][j]);
}
pw.println();
}
pw.close();
}
public static void main(String[] args) throws IOException{
new CF225A().solve();
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 5bc11381485b7705763a0f3b7cb13232 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.io.*;
import java.util.*;
public class CF225A {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
StringTokenizer stringTokenizer;
String next() throws IOException{
while(stringTokenizer == null || !stringTokenizer.hasMoreTokens()){
stringTokenizer = new StringTokenizer(br.readLine());
}
return stringTokenizer.nextToken();
}
CF225A() throws IOException{
}
int nextInt() throws IOException{
return Integer.parseInt(next());
}
long nextLong() throws IOException{
return Long.parseLong(next());
}
void solve() throws IOException{
//EnterCoder here...
int n = nextInt();
int count = ((n*n)+(1))/2;
pw.println(count);
for(int i =0;i<n;i++){
for(int j=0;j<n;j++){
pw.print((i+j)%2==0?'C':'.');
}
pw.println();
}
pw.close();
}
public static void main(String[] args) throws IOException{
new CF225A().solve();
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | f2c4fe209861097b9332443f0c7bd0aa | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
StringBuffer ss=new StringBuffer("");
int x=in.nextInt();
int c=x*x;
if(c%2==0)
c=c/2;
else
c=(c/2)+1;
while(ss.length()<=x){
ss.append("C.");
}
System.out.println(c);
int i=0;
boolean b=true;
while(++i<=x)
{
if(b)
System.out.print(ss.substring(0,0+x)+"\n");
else
System.out.print(ss.substring(1,1+x)+"\n");
b=!b;
}
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | fca745203ed2c956f9a789ae1431e4e4 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees{
public static void main(String[] args){
Scanner in =new Scanner(System.in);
String ss="";
int x=in.nextInt();
int c=x*x;
if(c%2==0) c=c/2; else c=(c/2)+1;
while(ss.length()<=x){ ss+="C.";}
System.out.println(c);
int i=0;
boolean b=true;
String s1=ss.substring(0,0+x)+"\n";
String s2=ss.substring(1,1+x)+"\n";
while(++i<=x)
{
if(b)
System.out.print(s1);
else
System.out.print(s2);
b=!b;
}}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | a47e06362fb5c5a6d7af2d7a140be78d | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees{
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String ss="";
int x=in.nextInt();
int c=x*x;
if(c%2==0) c=c/2; else c=(c/2)+1;
while(ss.length()<=x){ ss+="C.";}
System.out.println(c);
int i=0;
boolean b=true;
String s1=ss.substring(0,0+x)+"\n";
String s2=ss.substring(1,1+x)+"\n";
while(++i<=x)
{
if(b)
System.out.print(s1);
else
System.out.print(s2);
b=!b;
}}} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | ad88f33ee05a8f3ddb8b29e678566e09 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
StringBuilder s=new StringBuilder("");
String ss="";
int x=in.nextInt();
int c=x*x;
if(c%2==0)
c=c/2;
else
c=(c/2)+1;
while(ss.length()<=x){
ss+="C.";
}
System.out.println(c);
int i=0;
boolean b=true;
while(++i<=x)
{
if(b)
s.append(ss.substring(0,0+x)+"\n");
else
s.append(ss.substring(1,1+x)+"\n");
b=!b;
}
System.out.println(s);
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 7578bead1ef99d8dc0ece1a795a0f6d4 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String ss="C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.";
int x=in.nextInt();
int c=x*x;
if(c%2==0)
c=c/2;
else
c=(c/2)+1;
System.out.println(c);
int i=0;
boolean b=true;
while(++i<=x)
{
if(b)
{
System.out.println(ss.substring(0,0+x));
b=!b;
}
else
{
System.out.println(ss.substring(1,1+x));
b=!b;
}
}
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 4781b5812b48427b66f4fbfeb7d4b3db | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String ss="";
int x=in.nextInt();
int c=x*x;
if(c%2==0)
c=c/2;
else
c=(c/2)+1;
while(ss.length()<=x){
ss+="C.";
}
System.out.println(c);
int i=0;
boolean b=true;
String s1=ss.substring(0,0+x)+"\n";
String s2=ss.substring(1,1+x)+"\n";
while(++i<=x)
{
if(b)
System.out.print(s1);
else
System.out.print(s2);
b=!b;
}
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | c77eb92788d6f94c618fae0c7ff4b79b | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String ss="";
int x=in.nextInt();
int c=x*x;
if(c%2==0)
c=c/2;
else
c=(c/2)+1;
while(ss.length()<=x){
ss+="C.";
}
System.out.println(c);
int i=0;
boolean b=true;
while(++i<=x)
{
if(b)
System.out.print(ss.substring(0,0+x)+"\n");
else
System.out.print(ss.substring(1,1+x)+"\n");
b=!b;
}
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 869d17f8208b386c46a071a073d98125 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees{
public static void main(String[] args){
Scanner in =new Scanner(System.in);
String ss="";
int x=in.nextInt();
int c=x*x;
if(c%2==0) c=c/2; else c=(c/2)+1;
while(ss.length()<=x){ ss+="C.";}
System.out.println(c);
int i=0;
boolean b=true;
String s1=ss.substring(0,0+x)+"\n";
String s2=ss.substring(1,1+x)+"\n";
while(++i<=x)
{
if(b)
System.out.print(s1);
else
System.out.print(s2);
b=!b;
}}} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | fc07b17ed8673af8dad4717da14c2607 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String ss="";
int x=in.nextInt();
int c=x*x;
if(c%2==0)
c=c/2;
else
c=(c/2)+1;
for(int i=0;ss.length()<=x;i++){
ss+="C.";
}
System.out.println(c);
int i=0;
boolean b=true;
while(++i<=x)
{
if(b)
{
System.out.println(ss.substring(0,0+x));
b=!b;
}
else
{
System.out.println(ss.substring(1,1+x));
b=!b;
}
}
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 1f2df3d89cb6b3b8af04fa4b9312507f | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String ss="";
int x=in.nextInt();
int c=x*x;
if(c%2==0) c=c/2; else c=(c/2)+1;
while(ss.length()<=x){ ss+="C.";}
System.out.println(c);
int i=0;
boolean b=true;
String s1=ss.substring(0,0+x)+"\n";
String s2=ss.substring(1,1+x)+"\n";
while(++i<=x)
{
if(b)
System.out.print(s1);
else
System.out.print(s2);
b=!b;
}}} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 4867c7136b27dd0b6f5352301bd5b7f4 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String ss="C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.C.";
int x=in.nextInt();
int c=x*x;
if(c%2==0)
c=c/2;
else
c=(c/2)+1;
System.out.println(c);
int i=0;
boolean b=true;
while(++i<=x)
{
if(b)
{
System.out.println(ss.substring(0,0+x));
b=!b;
}
else
{
System.out.println(ss.substring(1,1+x));
b=!b;
}
}
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 91a194fd7c5d3c639256ee6f01f58c98 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String ss="";
int x=in.nextInt();
int c=x*x;
/////////
if(c%2==0)
c=c/2;
else
c=(c/2)+1;
////////
while(ss.length()<=x){
ss+="C.";
}
/////
System.out.println(c);
int i=0;
boolean b=true;
String s1=ss.substring(0,0+x)+"\n";
String s2=ss.substring(1,1+x)+"\n";
//////
while(++i<=x)
{
if(b)
System.out.print(s1);
else
System.out.print(s2);
b=!b;
}
//////
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 51ee009352af33362ab842b5d346d5cd | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.Scanner;
public class chees {
public static void main(String[] args) {
Scanner in =new Scanner(System.in);
String ss="";
int x=in.nextInt();
int c=x*x;
if(c%2==0) c=c/2; else c=(c/2)+1;
while(ss.length()<=x){ ss+="C.";}
System.out.println(c);
int i=0;
boolean b=true;
String s1=ss.substring(0,0+x)+"\n";
String s2=ss.substring(1,1+x)+"\n";
while(++i<=x)
{
if(b)
System.out.print(s1);
else
System.out.print(s2);
b=!b;
}
}} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 0f2e657a8fb4cb0b65b4c70da0b73cf6 | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
int N = in.nextInt();
System.out.println((N*N+1)/2);
StringBuilder sb = new StringBuilder();
for (int r=0; r<N; r++) {
for (int c=0; c<N; c++)
sb.append((r+c)%2==0 ? 'C' : '.');
sb.append('\n');
}
System.out.print(sb.toString());
}
} | Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 37d9a72005a81c03bcb88426b43c736d | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes |
import java.util.Scanner;
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author KHALED
*/
public class Coder {
public static void main(String[] args) {
Scanner scan=new Scanner(System.in);
int n=scan.nextInt();
char[][]arr=new char[n][n];
if(n%2==0)
System.out.println((n*n)/2);
else
System.out.println((n*n+1)/2);
char c='C';
String s1="";
String s2="";
for (int j = 0; j < n; j++)
{
if(c=='C')
{
s1+=c;
c='.';
s2+=c;
}
else
{
s1+=c;
c='C';
s2+=c;
}
}
for (int i = 0; i < n; i++)
{
if((i+1)%2==1)
System.out.println(s1);
else
System.out.println(s2);
}
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | 06a414a7013b56a87f9c4472d847b4cf | train_001.jsonl | 1390231800 | Iahub likes chess very much. He even invented a new chess piece named Coder. A Coder can move (and attack) one square horizontally or vertically. More precisely, if the Coder is located at position (x, y), he can move to (or attack) positions (x + 1, y), (x–1, y), (x, y + 1) and (x, y–1).Iahub wants to know how many Coders can be placed on an n × n chessboard, so that no Coder attacks any other Coder. | 256 megabytes |
import java.util.Scanner;
public class A {
public static void main(String[] args) {
int n = (new Scanner(System.in)).nextInt();
int a = (n + 1) / 2;
int b = n / 2;
int r = a * a + b * b;
System.out.println(r);
String s = "";
for (int i = 0; i <= n; i++) {
if (i % 2 == 0) {
s += '.';
} else {
s += "C";
}
}
for (int i = 0; i < n; i++) {
if (i % 2 == 0) {
System.out.println(s.substring(1));
} else {
System.out.println(s.substring(0, s.length() - 1));
}
}
}
}
| Java | ["2"] | 1 second | ["2\nC.\n.C"] | null | Java 7 | standard input | [
"implementation"
] | 1aede54b41d6fad3e74f24a6592198eb | The first line contains an integer n (1 ≤ n ≤ 1000). | 800 | On the first line print an integer, the maximum number of Coders that can be placed on the chessboard. On each of the next n lines print n characters, describing the configuration of the Coders. For an empty cell print an '.', and for a Coder print a 'C'. If there are multiple correct answers, you can print any. | standard output | |
PASSED | f3962dcafae056afad267332b9620aef | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.PrintStream;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
ExpectedSquareBeauty solver = new ExpectedSquareBeauty();
solver.solve(1, in, out);
out.close();
}
static class ExpectedSquareBeauty {
long MOD = (int) (1e9) + 7;
public void solve(int testNumber, InputReader in, PrintWriter out) {
long N = in.nextInt();
long[][] p = new long[(int) N][2];
long tot = 1;
for (int i = 0; i < N; i++) {
p[i][0] = in.nextInt();
}
for (int i = 0; i < N; i++) {
p[i][1] = in.nextInt();
tot *= inter(p[i]);
tot %= MOD;
}
long res = ((tot * N) % MOD * N) % MOD;
long[] cross = new long[(int) N - 1];
for (int i = 0; i < N - 1; i++) {
cross[i] = MathUtils.modFraction(tot, (inter(p[i]) * inter(p[i + 1]) % MOD), MOD) * inter(p[i], p[i + 1]);
cross[i] %= MOD;
}
res += sum(cross) * (1 - 2 * N);
res = MathUtils.mod(res, MOD);
long counter = 0;
for (int i = 1; i < N - 1; i++) {
res += (2 * inter(p[i - 1], p[i], p[i + 1]) * MathUtils.modFraction(tot, ((inter(p[i]) * inter(p[i + 1]) % MOD) * inter(p[i - 1]) % MOD), MOD));
res %= MOD;
res += (((2 * counter * inter(p[i], p[i + 1])) % MOD * (MathUtils.modFraction(tot, (inter(p[i + 1]) * inter(p[i]) % MOD), MOD)))) % MOD;
counter += MathUtils.modFraction(inter(p[i - 1], p[i]), inter(p[i]) * inter(p[i - 1]), MOD);
res %= MOD;
counter %= MOD;
}
out.println(MathUtils.modFraction(res, tot, MOD));
}
long sum(long[] arr) {
long sum = 0;
for (long n : arr) {
sum += n;
sum %= MOD;
}
return sum;
}
long inter(long[]... p) {
long min = 0;
long max = Integer.MAX_VALUE;
for (long[] point : p) {
min = Math.max(point[0], min);
max = Math.min(point[1], max);
}
return Math.max(0, max - min + 1);
}
}
static class MathUtils {
public static long inverse(long a, long mod) {
long[] inv = extended_gcd(a, mod);
if (inv[0] != 1) {
return 0;
} else {
return (inv[1] + 2 * mod) % mod;
}
}
public static long modFraction(long a, long b, long mod) {
//a is numerator, b is denominator
a %= mod;
b %= mod;
long invB = inverse(b, mod);
if (invB == 0) {
int[] arr = new int[3];
System.out.println(arr[4]);
return -1;
} else {
return (invB * a) % mod;
}
}
public static long[] extended_gcd(long a, long b) {
//three numbers, first is gcd, second is x, third is y
if (a == 0) {
return new long[]{b, 0, 1};
}
long[] next = extended_gcd(b % a, a);
long tempX = next[1];
next[1] = next[2] - (b / a) * next[1];
next[2] = tempX;
return next;
}
public static long mod(long a, long mod) {
return (a + (Math.abs(a) + mod - 1) / mod * mod) % mod;
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | b6e2a85635e32cb3dd4d659279b89c98 | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import static java.lang.Math.max;
import static java.lang.Math.min;
public class ProblemF {
static class FastScanner {
private BufferedReader reader;
private StringTokenizer tokenizer;
public FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
}
public String next() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
static class BeautySquare {
private final int MOD = (int) 1e9 + 7;
int norm(int a) {
while (a >= MOD)
a -= MOD;
while (a < 0)
a += MOD;
return a;
}
int mul(int a, int b) {
return (int) ((long) a * b % MOD);
}
int binPow(int a, int k) {
int ans = 1;
for (; k > 0; k >>= 1) {
if ((k & 1) == 1)
ans = mul(ans, a);
a = mul(a, a);
}
return ans;
}
int inv(int a) {
return binPow(a, MOD - 2);
}
int[] l, r;
int[] p; // p = 1 - q
int n;
int getEq(int i) {
int pSame = 0;
if (i > 0) {
int overlap = max(0, min(r[i - 1], min(r[i], r[i + 1])) - max(l[i - 1], max(l[i], l[i + 1])));
int divider = mul(r[i - 1] - l[i - 1], mul(r[i] - l[i], r[i + 1] - l[i + 1]));
pSame = mul(overlap, inv(divider));
}
return norm(norm(p[i] + p[i + 1] - 1) + pSame);
}
void solve() {
p = new int[n];
p[0] = 1;
for (int i = 1; i < n; i++) {
int overlap = max(0, min(r[i - 1], r[i]) - max(l[i - 1], l[i]));
int qi = mul(overlap, inv(mul(r[i - 1] - l[i - 1], r[i] - l[i])));
p[i] = norm(1 - qi);
}
int sum = 0;
for (int i = 0; i < n; i++) {
sum = norm(sum + p[i]);
}
int ans = sum;
for (int i = 0; i < n; i++) {
int cSum = sum;
for (int j = max(0, i - 1); j < min(n, i + 2); j++) {
cSum = norm(cSum - p[j]);
}
ans = norm(ans + mul(p[i], cSum));
if (i > 0) {
ans = norm(ans + getEq(i - 1));
}
if (i + 1 < n) {
ans = norm(ans + getEq(i));
}
}
System.out.println(ans);
}
void run() throws IOException {
FastScanner sc = new FastScanner();
n = sc.nextInt();
l = new int[n];
r = new int[n];
for (int i = 0; i < n; i++) {
l[i] = sc.nextInt();
}
for (int i = 0; i < n; i++) {
r[i] = sc.nextInt() + 1;
}
solve();
}
}
public static void main(String[] args) throws IOException {
BeautySquare bs = new BeautySquare();
bs.run();
}
}
| Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | 88d4395c5d6481e678b39aee58b0c5a1 | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class F {
public static int mod = 1000000007;
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
// Scanner scan = new Scanner(System.in);
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(bf.readLine());
StringTokenizer st = new StringTokenizer(bf.readLine());
int[] l = new int[n]; for(int i=0; i<n; i++) l[i] = Integer.parseInt(st.nextToken());
st = new StringTokenizer(bf.readLine());
int[] r = new int[n]; for(int i=0; i<n; i++) r[i] = Integer.parseInt(st.nextToken());
long ans = 0;
for(int i=1; i<n; i++) {
ans += prob(l[i-1], r[i-1], l[i], r[i]);
ans %= mod;
}
long toAdd = ans;
ans *= 3;
ans += 1;
ans %= mod;
long toAddSq = 0L + exp((int)toAdd, 2);
for(int i=1; i<n; i++) {
toAddSq -= 1L*prob(l[i-1], r[i-1], l[i], r[i])*prob(l[i-1], r[i-1], l[i], r[i])%mod;
toAddSq += mod;
toAddSq %= mod;
}
toAddSq %= mod;
for(int i=2; i<n; i++) {
toAddSq -= mult(prob(l[i-2], r[i-2], l[i-1], r[i-1]), prob(l[i-1], r[i-1], l[i], r[i]))*2 % mod;
toAddSq += prob3(l[i-2], r[i-2], l[i-1], r[i-1], l[i], r[i])*2;
toAddSq += mod;
toAddSq %= mod;
}
ans += toAddSq;
ans %= mod;
ans += mod;
ans %= mod;
out.println(ans);
// int n = Integer.parseInt(st.nextToken());
// int n = scan.nextInt();
out.close(); System.exit(0);
}
public static int prob3(int l1, int r1, int l2, int r2, int l3, int r3) {
long ans = 0;
ans += prob(l1, r1, l2, r2);
ans += prob(l2, r2, l3, r3);
int up = Math.min(Math.min(r1, r2), r3);
int low = Math.max(Math.max(l1, l2), l3);
ans -= (1 - (1L*Math.max(0, up-low+1)*inv(mult3(r1-l1+1, r2-l2+1, r3-l3+1))%mod));
ans %= mod;
ans += mod;
ans %= mod;
return (int)ans;
}
public static int prob(int l1, int r1, int l2, int r2) {
// probability different
int low = Math.max(l1, l2);
int high = Math.min(r1, r2);
return (1 - mult((Math.max(0, high-low+1)), inv(mult(r1-l1+1, r2-l2+1))) + mod) % mod;
}
// Exponentation
public static int mult(int a, int b) {
return (int)(1L*a*b % mod);
}
public static int mult3(int a, int b, int c ) {
return mult(mult(a, b), c);
}
public static int exp(int base, int e) {
if(e == 0) return 1;
if(e == 1) return base;
int val = exp(base, e/2);
int ans = (int)(1L*val*val % mod);
if(e % 2 == 1)
ans = (int)(1L*ans*base % mod);
return ans;
}
// Exponentation
public static int inv(int base) {
return exp(base, mod-2);
}
}
| Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | dea973b32a72d14dfa304563712730ec | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | /**
* @author derrick20
* Soooo hard but finally figured it out.
*/
import java.io.*;
import java.util.*;
/*
Equal: 500000005
P1^2 - P2: 125000002
Indep: 0
Dep: 1
*/
public class ExpectedSquareBeauty {
public static void main(String args[]) throws Exception {
FastScanner sc = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int N = sc.nextInt();
L = new long[N];
R = new long[N];
range = new long[N];
for (int i = 0; i < N; i++) {
L[i] = sc.nextLong();
}
for (int i = 0; i < N; i++) {
R[i] = sc.nextLong();
}
for (int i = 0; i < N; i++) {
range[i] = R[i] - L[i] + 1;
}
borderProb = new long[N];
long P2 = 0; // Sum of second power of each term
long equalComponent = 0;
for (int i = 1; i < N; i++) {
borderProb[i] = probDiff(i);
P2 = addSelf(P2, multiply(borderProb[i], borderProb[i]));
// System.out.println(borderProb[i]);
equalComponent = addSelf(equalComponent, borderProb[i]);
}
// System.out.println("Equal: " + equalComponent);
long P1 = equalComponent; // First power of each term
// I don't know why this line is necessary
// todo COME BACK TO THIS!!!!
equalComponent = (3 * equalComponent + 1) % mod;
long independentComponent = subSelf(multiply(P1, P1), P2);
// System.out.println("P1^2 - P2: " + independentComponent);
for (int i = 1; i < N; i++) {
if (i < N - 1) {
independentComponent = subSelf(independentComponent, multiply(2, multiply(borderProb[i], borderProb[i + 1])));
// System.out.println("Indep: " + independentComponent);
}
}
long dependentComponent = 0;
for (int i = 1; i <= N - 2; i++) {
dependentComponent = addSelf(dependentComponent, multiply(2, probTripleDiff(i)));
}
// System.out.println("Dep: " + dependentComponent);
long ans = equalComponent;
ans = addSelf(ans, independentComponent);
ans = addSelf(ans, dependentComponent);
out.println(ans);
out.close();
}
static long mod = (long) 1e9 + 7;
static long[] L, R;
static long[] range;
static long[] borderProb;
// Find probability of this part having a contribution, meaning
// x_i-1 != x_i.
static long probDiff(int i) {
// If the common range is screwed up,
// like L R L2 R2, then they CAN'T OVERLAP, so 0!
long commonRange = Math.max(0, Math.min(R[i], R[i - 1]) - Math.max(L[i], L[i - 1]) + 1);
long probSame = multiply(commonRange, modInverse(multiply(range[i], range[i - 1])));
long probDiff = (1 - probSame + mod) % mod;
return probDiff;
}
// Let's say the parameter will be the middle i value.
// Find probability of all three of these being different. The reason
// we do this is because the contribution of E[i]*E[j] will only yield
// anything if x_i-1 != x_i AND x_j-1 != x_j
// Normally, those will be independent events, so they may be multiplied,
// but in thise case, if j = i + 1, we must remove overcounting
// Specifically, we are asking P(x_i-1 != x_i != x_i+1)
// This is saying 1 - P(x_i-1 != x_i OR x_i != x_i+1)
// Then, we can use inclusion-exclusion to get that OR venn-diagram
// statement part. Basically, this simplifies to become:
// Let p_i = P(x_i != x_i-1). (We will store this in borderProb[])
// p_i + p_i+1 - (1 - P(the three area all equal))
static long probTripleDiff(int i) {
long res = addSelf(borderProb[i], borderProb[i + 1]);
long commonRange = Math.max(0, Math.min(R[i + 1], Math.min(R[i], R[i - 1])) - Math.max(L[i + 1], Math.max(L[i], L[i - 1])) + 1);
long denominator = modInverse(multiply(range[i - 1], multiply(range[i], range[i + 1])));
long probAllEqual = multiply(commonRange, denominator);
res = subSelf(res, 1 - probAllEqual);
return res;
}
static long multiply(long a, long b) {
return (a * b) % mod;
}
static long subSelf(long val, long sub) {
val -= sub;
if (val < 0) {
val += mod;
}
return val;
}
static long addSelf(long val, long add) {
val += add;
if (val >= mod) {
val -= mod;
}
return val;
}
static long modInverse(long x) {
return fastExpo(x, mod - 2);
}
static long fastExpo(long x, long k) {
if (k == 0) {
return 1;
}
else if (k == 1) {
return x;
}
else {
long root = fastExpo(x, k / 2);
long ans = (root * root) % mod;
if (k % 2 == 1) {
ans = (ans * x) % mod;
}
return ans;
}
}
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 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 | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | b952ddaca956d631b90b54a317c64902 | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.io.*;
import java.util.*;
public class Codeforces1187F {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int[] l = new int[n];
for (int i = 0; i < n; i++) {
l[i] = Integer.parseInt(st.nextToken());
}
st = new StringTokenizer(br.readLine());
int[] r = new int[n];
for (int i = 0; i < n; i++) {
r[i] = Integer.parseInt(st.nextToken());
}
//Ok im going to add some comments cause this is a fun problem
//so for 1 \le i \le n-1, let X_i be the indicator function of
//x_i = x_{i+1}. Note that the number of connected components
//is n - \sum X_i. So, the final answer is:
//n^2 - 2*n*sum E[X_i] + E((sum X_i)^2)
//= (n - sum E[X_i])^2 + sum Cov(X_i, X_j).
//We only need to calculate covariance for i = j or |i-j| = 1 since
//otherwise it is clear that X_i and X_j are independent.
//Now, E[X_i] is just P(x_i = x_{i+1}) and Cov(X_i, X_{i+1}) is just
//P(x_i = x_{i+1} = x_{i+2})-(P(x_i = x_{i+1}) P(x_{i+1} = x_{i+2}))
if (n == 1) {
System.out.println(1);
}
else {
int mod = 1000000007;
int[] length = new int[n];
for (int i = 0; i < n; i++) {
length[i] = r[i] - l[i] + 1;
}
//exp[i] = E[X_i]
int[] exp = new int[n-1];
for (int i = 0; i < n-1; i++) {
exp[i] = Math.max(0, 1 + Math.min(r[i], r[i+1]) - Math.max(l[i], l[i+1]));
exp[i] = multiply(exp[i], inverse(multiply(length[i], length[i+1], mod), mod), mod);
}
//thing[i] = P(x_i = x_{i+1} = x_{i+2})
int[] thing = new int[n-2];
for (int i = 0; i < n-2; i++) {
thing[i] = Math.max(0, 1 + Math.min(Math.min(r[i], r[i+1]), r[i+2]) - Math.max(Math.max(l[i], l[i+1]), l[i+2]));
thing[i] = multiply(thing[i], inverse(multiply(length[i], multiply(length[i+1], length[i+2], mod), mod), mod), mod);
}
//ok now we are basically ready
int sumE = 0;
for (int i = 0; i < n-1; i++) {
sumE = (sumE+exp[i])%mod;
}
int sumCov = sumE;
for (int i = 0; i < n-1; i++) {
sumCov = (sumCov + mod - multiply(exp[i], exp[i], mod))%mod;
}
for (int i = 0; i < n-2; i++) {
sumCov = (sumCov + (2*thing[i])%mod)%mod;
sumCov = (sumCov + 2*(mod - multiply(exp[i], exp[i+1], mod))%mod)%mod;
}
int k = (n + mod - sumE)%mod;
k = multiply(k, k, mod);
k = (k+sumCov)%mod;
System.out.println(k);
}
}
public static int multiply (int a, int b, int n) {
long ab = (long) a * (long) b;
return ((int) (ab%n));
}
public static int inverse (int a, int n) {
int m = n;
int r1 = 1;
int r2 = 0;
int r3 = 0;
int r4 = 1;
while ((a > 0) && (n > 0)) {
if (n >= a) {
r3 -= r1*(n/a);
r4 -= r2*(n/a);
n = n%a;
}
else {
int tmp = a;
a = n;
n = tmp;
tmp = r1;
r1 = r3;
r3 = tmp;
tmp = r2;
r2 = r4;
r4 = tmp;
}
}
if (a == 0) {
if (r3 >= 0)
return (r3%m);
else
return (m+(r3%m));
}
else {
if (r1 >= 0)
return (r1%m);
else
return (m+(r1%m));
}
}
}
| Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | b8683029d7fbb1fed3220e900fc5e2c4 | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.io.Closeable;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) throws Exception {
Thread thread = new Thread(null, new TaskAdapter(), "", 1 << 27);
thread.start();
thread.join();
}
static class TaskAdapter implements Runnable {
@Override
public void run() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastInput in = new FastInput(inputStream);
FastOutput out = new FastOutput(outputStream);
TaskF solver = new TaskF();
solver.solve(1, in, out);
out.close();
}
}
static class TaskF {
public void solve(int testNumber, FastInput in, FastOutput out) {
int n = in.readInt();
int[] ls = new int[n + 1];
int[] rs = new int[n + 1];
for (int i = 1; i <= n; i++) {
ls[i] = in.readInt();
}
for (int i = 1; i <= n; i++) {
rs[i] = in.readInt();
}
Modular mod = new Modular(1e9 + 7);
Power pow = new Power(mod);
int[] range = new int[n + 1];
for (int i = 0; i <= n; i++) {
range[i] = rs[i] - ls[i] + 1;
}
int[] invRange = new int[n + 1];
for (int i = 0; i <= n; i++) {
invRange[i] = pow.inverse(range[i]);
}
int[] probF = new int[n + 1];
for (int i = 1; i <= n; i++) {
int p = mod.mul(invRange[i - 1], invRange[i]);
int intersect = intersect(ls[i - 1], rs[i - 1], ls[i], rs[i]);
p = mod.mul(p, intersect);
probF[i] = mod.subtract(1, p);
}
PreSum ps = new PreSum(probF);
int part1 = 0;
for (int i = 2; i <= n; i++) {
int local1 = mod.valueOf(ps.intervalSum(1, i - 2));
local1 = mod.mul(local1, probF[i]);
int intersect = intersect(ls[i - 2], rs[i - 2], ls[i - 1], rs[i - 1],
ls[i], rs[i]);
int probBoth = intersect;
probBoth = mod.mul(probBoth, invRange[i - 2]);
probBoth = mod.mul(probBoth, invRange[i - 1]);
probBoth = mod.mul(probBoth, invRange[i]);
int local2 = 1;
local2 = mod.subtract(local2, mod.subtract(1, probF[i - 1]));
local2 = mod.subtract(local2, mod.subtract(1, probF[i]));
local2 = mod.plus(local2, probBoth);
int local = mod.plus(local1, local2);
part1 = mod.plus(part1, local);
}
part1 = mod.mul(part1, 2);
int part2 = 0;
for (int i = 1; i <= n; i++) {
part2 = mod.plus(part2, probF[i]);
}
int exp = mod.plus(part1, part2);
out.println(exp);
}
public int intersect(int l1, int r1, int l2, int r2) {
int l = Math.max(l1, l2);
int r = Math.min(r1, r2);
return Math.max(r - l + 1, 0);
}
public int intersect(int l1, int r1, int l2, int r2, int l3, int r3) {
int l = Math.max(l1, Math.max(l2, l3));
int r = Math.min(r1, Math.min(r2, r3));
return Math.max(r - l + 1, 0);
}
}
static class PreSum {
private long[] pre;
public PreSum(long[] a) {
int n = a.length;
pre = new long[n];
pre[0] = a[0];
for (int i = 1; i < n; i++) {
pre[i] = pre[i - 1] + a[i];
}
}
public PreSum(int[] a) {
int n = a.length;
pre = new long[n];
pre[0] = a[0];
for (int i = 1; i < n; i++) {
pre[i] = pre[i - 1] + a[i];
}
}
public long intervalSum(int l, int r) {
if (l > r) {
return 0;
}
if (l == 0) {
return pre[r];
}
return pre[r] - pre[l - 1];
}
}
static class FastInput {
private final InputStream is;
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastInput(InputStream is) {
this.is = is;
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
bufLen = -1;
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
}
static class FastOutput implements AutoCloseable, Closeable {
private StringBuilder cache = new StringBuilder(10 << 20);
private final Writer os;
public FastOutput(Writer os) {
this.os = os;
}
public FastOutput(OutputStream os) {
this(new OutputStreamWriter(os));
}
public FastOutput println(int c) {
cache.append(c).append('\n');
return this;
}
public FastOutput flush() {
try {
os.append(cache);
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
return this;
}
public void close() {
flush();
try {
os.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public String toString() {
return cache.toString();
}
}
static class Power {
final Modular modular;
public Power(Modular modular) {
this.modular = modular;
}
public int pow(int x, long n) {
if (n == 0) {
return modular.valueOf(1);
}
long r = pow(x, n >> 1);
r = modular.valueOf(r * r);
if ((n & 1) == 1) {
r = modular.valueOf(r * x);
}
return (int) r;
}
public int inverse(int x) {
return pow(x, modular.m - 2);
}
}
static class Modular {
int m;
public Modular(int m) {
this.m = m;
}
public Modular(long m) {
this.m = (int) m;
if (this.m != m) {
throw new IllegalArgumentException();
}
}
public Modular(double m) {
this.m = (int) m;
if (this.m != m) {
throw new IllegalArgumentException();
}
}
public int valueOf(int x) {
x %= m;
if (x < 0) {
x += m;
}
return x;
}
public int valueOf(long x) {
x %= m;
if (x < 0) {
x += m;
}
return (int) x;
}
public int mul(int x, int y) {
return valueOf((long) x * y);
}
public int plus(int x, int y) {
return valueOf(x + y);
}
public int subtract(int x, int y) {
return valueOf(x - y);
}
public String toString() {
return "mod " + m;
}
}
}
| Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | 15da8edc92ef5d727f0256e1e2b00d82 | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.util.*;
import java.io.*;
public class EdD {
static long[] mods = {1000000007, 998244353, 1000000009};
static long mod = mods[0];
public static MyScanner sc;
public static PrintWriter out;
public static void main(String[] omkar) throws Exception{
// TODO Auto-generated method stub
sc = new MyScanner();
out = new PrintWriter(System.out);
long n = sc.nextLong();
long[] l = readArrayLong((int)n);
long[] r = readArrayLong((int)n);
long ans = n*n;
long[] indicators = new long[(int)(n-1)];
long sum = 0;
long sumsq = 0;
for(int j = 0;j<n-1;j++){
long diff = Math.min(r[j+1], r[j]) - Math.max(l[j+1], l[j]) + 1;
diff = Math.max(0, diff);
long p1 = r[j]-l[j]+1;
long p2 = r[j+1]-l[j+1]+1;
long denom = p1*p2;
denom%=mod;
indicators[j] = diff*power(denom, mod-2);
indicators[j]%=mod;
sum+=indicators[j];
sumsq+=(indicators[j]*indicators[j])%mod;
sum%=mod;
sumsq%=mod;
}
ans-=2L*n*sum;
ans%=mod;
ans+=mod;
ans%=mod;
ans+=sum;
ans%=mod;
long sum1 = 0;
long sum2 = 0;
for(int j = 0;j<n-2;j++){
long right = Math.min(r[j], Math.min(r[j+1], r[j+2]));
long left = Math.max(l[j], Math.max(l[j+1], l[j+2]));
long diff = Math.max(right-left+1, 0);
long denom = 1;
denom*=(r[j]-l[j]+1L);
denom%=mod;
denom*=(r[j+1]-l[j+1]+1L);
denom%=mod;
denom*=(r[j+2]-l[j+2]+1L);
denom%=mod;
sum1+=(diff*power(denom, mod-2))%mod;
sum1%=mod;
sum2+=(indicators[j]*indicators[j+1])%mod;
sum2%=mod;
}
sum1*=2L;
sum1%=mod;
ans+=sum1;
ans%=mod;
ans+=(sum*sum)%mod;
ans%=mod;
ans-=sumsq;
ans%=mod;
ans+=mod;
ans%=mod;
sum2*=2L;
sum2%=mod;
ans-=sum2;
ans%=mod;
ans+=mod;
ans%=mod;
out.println(ans);
out.println();
out.close();
}
public static void sort(int[] array){
ArrayList<Integer> copy = new ArrayList<Integer>();
for (int i : array)
copy.add(i);
Collections.sort(copy);
for(int i = 0;i<array.length;i++)
array[i] = copy.get(i);
}
static String[] readArrayString(int n){
String[] array = new String[n];
for(int j =0 ;j<n;j++)
array[j] = sc.next();
return array;
}
static int[] readArrayInt(int n){
int[] array = new int[n];
for(int j = 0;j<n;j++)
array[j] = sc.nextInt();
return array;
}
static int[] readArrayInt1(int n){
int[] array = new int[n+1];
for(int j = 1;j<=n;j++){
array[j] = sc.nextInt();
}
return array;
}
static long[] readArrayLong(int n){
long[] array = new long[n];
for(int j =0 ;j<n;j++)
array[j] = sc.nextLong();
return array;
}
static double[] readArrayDouble(int n){
double[] array = new double[n];
for(int j =0 ;j<n;j++)
array[j] = sc.nextDouble();
return array;
}
static int minIndex(int[] array){
int minValue = Integer.MAX_VALUE;
int minIndex = -1;
for(int j = 0;j<array.length;j++){
if (array[j] < minValue){
minValue = array[j];
minIndex = j;
}
}
return minIndex;
}
static int minIndex(long[] array){
long minValue = Long.MAX_VALUE;
int minIndex = -1;
for(int j = 0;j<array.length;j++){
if (array[j] < minValue){
minValue = array[j];
minIndex = j;
}
}
return minIndex;
}
static int minIndex(double[] array){
double minValue = Double.MAX_VALUE;
int minIndex = -1;
for(int j = 0;j<array.length;j++){
if (array[j] < minValue){
minValue = array[j];
minIndex = j;
}
}
return minIndex;
}
static long power(long x, long y){
if (y == 0)
return 1;
if (y%2 == 1)
return (x*power(x, y-1))%mod;
return power((x*x)%mod, y/2)%mod;
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try{
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is | Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | 5c3a8f365f3298e3dc28e288efffc31f | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.math.BigInteger;
import java.util.Scanner;
public class ExpectedSquareBeauty {
static final long MOD = 1000000007;
static final BigInteger BIG_MOD = BigInteger.valueOf(MOD);
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
long[][] ranges = new long[n][2];
long[] lens = new long[n];
long b = 1;
for (int i = 0; i < n; i++) {
ranges[i][0] = in.nextInt();
}
for (int i = 0; i < n; i++) {
ranges[i][1] = in.nextInt();
lens[i] = ranges[i][1] - ranges[i][0] + 1;
}
long[] adjProbs = new long[n];
for (int i = 1; i < n; i++) {
long rangeLen = Math.max(0L, Math.min(ranges[i - 1][1], ranges[i][1]) - Math.max(ranges[i - 1][0], ranges[i][0]) + 1L);
adjProbs[i] = 1L + MOD;
adjProbs[i] -= (rangeLen *
BigInteger
.valueOf(lens[i - 1] * lens[i])
.modInverse(BIG_MOD).longValueExact()
) % MOD;
adjProbs[i] %= MOD;
b += adjProbs[i];
b %= MOD;
}
long answer = (b * b) % MOD;
for (int i = 1; i < n; i++) {
answer += MOD;
answer -= (adjProbs[i] * adjProbs[i]) % MOD;
answer += adjProbs[i];
answer %= MOD;
}
for (int i = 2; i < n; i++) {
answer += MOD;
answer -= (2L * adjProbs[i - 1] * adjProbs[i]) % MOD;
answer %= MOD;
long abcLen = Math.max(0L, Math.min(ranges[i - 2][1], Math.min(ranges[i - 1][1], ranges[i][1])) - Math.max(ranges[i - 2][0], Math.max(ranges[i - 1][0], ranges[i][0])) + 1L);
long abLen = Math.max(0L, Math.min(ranges[i - 2][1], ranges[i - 1][1]) - Math.max(ranges[i - 2][0], ranges[i - 1][0]) + 1L);
long bcLen = Math.max(0L, Math.min(ranges[i - 1][1], ranges[i][1]) - Math.max(ranges[i - 1][0], ranges[i][0]) + 1L);
long denomRecip = BigInteger
.valueOf(lens[i - 2] * ((lens[i - 1] * lens[i]) % MOD))
.modInverse(BIG_MOD)
.longValueExact();
long p = (abcLen * (((lens[i - 2] - 1) * (lens[i] - 1)) % MOD)) % MOD;
p += ((abLen - abcLen) * (((lens[i - 2] - 1) * lens[i]) % MOD)) % MOD;
p += ((bcLen - abcLen) * ((lens[i - 2] * (lens[i] - 1)) % MOD)) % MOD;
p += ((lens[i - 1] - abLen - bcLen + abcLen) * ((lens[i - 2] * lens[i]) % MOD)) % MOD;
p %= MOD;
p *= denomRecip;
p %= MOD;
answer += 2L * p;
answer %= MOD;
}
System.out.println(answer);
}
}
| Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | edd2645717c839d7393446fb87cac421 | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
FExpectedSquareBeauty solver = new FExpectedSquareBeauty();
solver.solve(1, in, out);
out.close();
}
static class FExpectedSquareBeauty {
NumberTheory.Mod107 mod = new NumberTheory.Mod107();
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
long[] l = in.readLongArray(n);
long[] r = in.readLongArray(n);
for (int i = 0; i < n; i++)
r[i]++;
long[] c = new long[n];
for (int i = 0; i < n; i++)
c[i] = r[i] - l[i];
long[] o = new long[n - 1];
for (int i = 0; i < n - 1; i++) {
o[i] = Math.min(r[i], r[i + 1]) - Math.max(l[i], l[i + 1]);
o[i] = Math.max(o[i], 0);
}
LongSegmentTree sum = new LongSegmentTree(n - 1, mod::add, 0);
long answer = 1;
for (int i = 0; i < n - 1; i++) {
long pr = mod.subtract(1, mod.div(o[i], mod.mult(c[i], c[i + 1])));
sum.update_LAZY(i, pr);
answer = mod.add(answer, pr);
}
sum.rebuild();
for (int i = 0; i + 2 < n; i++) {
int j = i + 1, k = i + 2;
long overlap = Util.min(r[i], r[j], r[k]) - Util.max(l[i], l[j], l[k]);
overlap = Math.max(overlap, 0);
long pr_both = mod.div(overlap, mod.mult(c[i], c[j], c[k]));
long pr_1 = mod.div(o[i], mod.mult(c[i], c[j]));
long pr_2 = mod.div(o[j], mod.mult(c[j], c[k]));
long pr = mod.subtract(mod.add(1, pr_both), mod.add(pr_1, pr_2));
pr = mod.mult(2, pr);
answer = mod.add(answer, pr);
}
for (int i = 0; i < n - 1; i++) {
long pr_i = sum.get(i);
int a = Math.max(0, i - 1), b = Math.min(i + 2, n - 1);
long pr_j = mod.subtract(sum.query(0, n - 1), sum.query(a, b));
answer = mod.add(answer, mod.mult(pr_i, pr_j));
}
answer = mod.add(answer, sum.query(0, n - 1), sum.query(0, n - 1));
out.println(answer);
}
}
static class Util {
public static long max(long... x) {
long max = Long.MIN_VALUE;
for (long i : x) {
max = Math.max(i, max);
}
return max;
}
public static long min(long... x) {
long min = Long.MAX_VALUE;
for (long i : x) {
min = Math.min(i, min);
}
return min;
}
private Util() {
}
}
static class NumberTheory {
private static void ASSERT(boolean assertion) {
if (!assertion)
throw new AssertionError();
}
public abstract static class Modulus<M extends NumberTheory.Modulus<M>> {
final ArrayList<Long> factorial = new ArrayList<>();
final ArrayList<Long> invFactorial = new ArrayList<>();
public abstract long modulus();
public Modulus() {
super();
factorial.add(1L);
invFactorial.add(1L);
}
public long normalize(long x) {
x %= modulus();
if (x < 0)
x += modulus();
return x;
}
public long add(long... x) {
long r = 0;
for (long i : x)
r = add(r, i);
return r;
}
public long add(long a, long b) {
long v = a + b;
return v < modulus() ? v : v - modulus();
}
public long subtract(long a, long b) {
long v = a - b;
return v < 0 ? v + modulus() : v;
}
public long mult(long... x) {
long r = 1;
for (long i : x)
r = mult(r, i);
return r;
}
public long mult(long a, long b) {
return (a * b) % modulus();
}
public long div(long a, long b) {
return mult(a, inv(b));
}
public long inv(long value) {
long g = modulus(), x = 0, y = 1;
for (long r = value; r != 0; ) {
long q = g / r;
g %= r;
long temp = g;
g = r;
r = temp;
x -= q * y;
temp = x;
x = y;
y = temp;
}
ASSERT(g == 1);
ASSERT(y == modulus() || y == -modulus());
return normalize(x);
}
}
public static class Mod107 extends NumberTheory.Modulus<NumberTheory.Mod107> {
public long modulus() {
return 1_000_000_007L;
}
}
}
static class InputReader {
public final BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] readLongArray(int n) {
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = nextLong();
}
return x;
}
}
static class LongSegmentTree {
public final int size;
public final long[] value;
protected final LongSegmentTree.Combiner combiner;
protected final long identityElement;
public LongSegmentTree(int size, LongSegmentTree.Combiner combiner, long identityElement) {
this.size = size;
value = new long[2 * size];
Arrays.fill(value, identityElement);
this.combiner = combiner;
this.identityElement = identityElement;
}
protected long combine(long a, long b) {
return combiner.combine(a, b);
}
public void rebuild() {
for (int i = size - 1; i > 0; i--) {
value[i] = combine(value[2 * i], value[2 * i + 1]);
}
}
public long get(int i) {
return value[size + i];
}
public void update_LAZY(int i, long v) {
i += size;
value[i] = v;
}
public long query(int i, int j) {
long res_left = identityElement, res_right = identityElement;
for (i += size, j += size; i < j; i /= 2, j /= 2) {
if ((i & 1) == 1)
res_left = combine(res_left, value[i++]);
if ((j & 1) == 1)
res_right = combine(value[--j], res_right);
}
return combine(res_left, res_right);
}
public interface Combiner {
long combine(long a, long b);
}
}
}
| Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | a9d49e90c9262eab9efe0cb03b8b8cdf | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author anand.oza
*/
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);
FExpectedSquareBeauty solver = new FExpectedSquareBeauty();
solver.solve(1, in, out);
out.close();
}
static class FExpectedSquareBeauty {
static final NumberTheory.Mod107 m = new NumberTheory.Mod107();
public void solve(int testNumber, InputReader in, PrintWriter out) {
solveModular(testNumber, in, out);
}
public void solveModular(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
long[] l = new long[n];
long[] r = new long[n];
for (int i = 0; i < n; i++)
l[i] = in.nextInt();
for (int i = 0; i < n; i++)
r[i] = in.nextInt() + 1;
long[] prDiff = new long[n];
long[] prDiffGivenPrevDiff = new long[n];
long[] prDiffGivenPrevSame = new long[n];
prDiff[0] = 1;
for (int i = 1; i < n; i++) {
long overlapL = Math.max(l[i], l[i - 1]);
long overlapR = Math.min(r[i], r[i - 1]);
long overlap = Math.max(0, overlapR - overlapL);
prDiff[i] = m.subtract(1, m.div(overlap, m.mult(r[i] - l[i], r[i - 1] - l[i - 1])));
prDiff[i] = m.subtract(1, m.div(overlap, m.mult(m.subtract(r[i], l[i]), m.subtract(r[i - 1], l[i - 1]))));
}
for (int i = 2; i < n; i++) {
long prevL = Math.max(l[i - 2], l[i - 1]);
long prevR = Math.min(r[i - 2], r[i - 1]);
long overlapL = Math.max(l[i], prevL);
long overlapR = Math.min(r[i], prevR);
long overlap = Math.max(0, overlapR - overlapL);
prDiffGivenPrevSame[i] = overlap == 0 ? 1 : m.subtract(1, m.div(overlap, m.mult(m.subtract(r[i], l[i]), m.subtract(prevR, prevL))));
}
for (int i = 2; i < n; i++) {
prDiffGivenPrevDiff[i] = prDiff[i - 1] == 0 ? 1 : m.div(m.subtract(prDiff[i], m.mult(prDiffGivenPrevSame[i], m.subtract(1, prDiff[i - 1]))), prDiff[i - 1]);
}
if (n == 1) {
long answer = 1;
out.println(answer);
return;
}
long[] ex = new long[n];
long[] exGivenDiff = new long[n];
long[] exGivenSame = new long[n];
ex[0] = 1;
ex[1] = m.add(ex[0], prDiff[1]);
exGivenDiff[1] = m.add(ex[0], 1);
exGivenSame[1] = ex[0];
for (int i = 2; i < n; i++) {
ex[i] = m.add(ex[i - 1], prDiff[i]);
exGivenDiff[i] = m.add(ex[i - 1], 1);
exGivenSame[i] = ex[i - 1];
}
long[] ex2 = new long[n];
ex2[0] = 1;
ex2[1] = m.add(ex2[0], m.mult(prDiff[1], 3));
for (int i = 2; i < n; i++) {
ex2[i] = ex2[i - 1];
ex2[i] = m.add(ex2[i], m.mult(prDiffGivenPrevDiff[i], m.mult(prDiff[i - 1], m.add(m.mult(2, exGivenDiff[i - 1]), 1))));
ex2[i] = m.add(ex2[i], m.mult(prDiffGivenPrevSame[i], m.mult(m.subtract(1, prDiff[i - 1]), m.add(m.mult(2, exGivenSame[i - 1]), 1))));
}
// System.out.println(Arrays.toString(prDiff));
// System.out.println(Arrays.toString(ex));
// System.out.println(Arrays.toString(ex2));
long answer = ex2[n - 1];
out.println(answer);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
static class NumberTheory {
private static void ASSERT(boolean assertion) {
if (!assertion)
throw new AssertionError();
}
public abstract static class Modulus<M extends NumberTheory.Modulus<M>> {
ArrayList<Long> factorial = new ArrayList<>();
public abstract long modulus();
public Modulus() {
super();
factorial.add(1L);
}
public long normalize(long x) {
x %= modulus();
if (x < 0)
x += modulus();
return x;
}
public long add(long a, long b) {
return normalize(a + b);
}
public long subtract(long a, long b) {
return normalize(a - b);
}
public long mult(long a, long b) {
return normalize(a * b);
}
public long div(long a, long b) {
return mult(a, inv(b));
}
public long inv(long value) {
long g = modulus(), x = 0, y = 1;
for (long r = value; r != 0; ) {
long q = g / r;
g %= r;
long temp = g;
g = r;
r = temp;
x -= q * y;
temp = x;
x = y;
y = temp;
}
ASSERT(g == 1);
ASSERT(y == modulus() || y == -modulus());
return normalize(x);
}
}
public static class Mod107 extends NumberTheory.Modulus<NumberTheory.Mod107> {
public long modulus() {
return 1_000_000_007L;
}
}
}
}
| Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | 42be3c15b6b087680de04e29262ff357 | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
FExpectedSquareBeauty solver = new FExpectedSquareBeauty();
solver.solve(1, in, out);
out.close();
}
static class FExpectedSquareBeauty {
NumberTheory.Mod107 mod = new NumberTheory.Mod107();
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
long[] l = in.readLongArray(n);
long[] r = in.readLongArray(n);
for (int i = 0; i < n; i++)
r[i]++;
long[] c = new long[n];
for (int i = 0; i < n; i++)
c[i] = r[i] - l[i];
long[] o = new long[n - 1];
for (int i = 0; i < n - 1; i++) {
o[i] = Math.min(r[i], r[i + 1]) - Math.max(l[i], l[i + 1]);
o[i] = Math.max(o[i], 0);
}
LongSegmentTree sum = new LongSegmentTree(n - 1, mod::add, 0);
for (int i = 0; i < n - 1; i++) {
long pr = mod.subtract(1, mod.div(o[i], mod.mult(c[i], c[i + 1])));
sum.update_LAZY(i, pr);
}
sum.rebuild();
// We want to find (1 + sum(p_i))^2, which is 1 + 2 * sum(p_i) + sum(p_i)^2
long answer = 0;
// sum(1 * 1)
answer = mod.add(answer, 1);
// 2 * sum(p_i)
answer = mod.add(answer, mod.mult(2, sum.query(0, n - 1)));
// sum(p_i * p_j) for |i-j| == 0
answer = mod.add(answer, sum.query(0, n - 1));
// sum(p_i * p_j) for |i-j| == 1
for (int i = 0; i + 2 < n; i++) {
int j = i + 1, k = i + 2;
long total = mod.mult(c[i], c[j], c[k]);
long both = Util.min(r[i], r[j], r[k]) - Util.max(l[i], l[j], l[k]);
both = Math.max(both, 0);
long first = mod.mult(o[i], c[k]);
long second = mod.mult(c[i], o[j]);
// PIE: total - first - second + both
long pr = mod.subtract(mod.add(total, both), mod.add(first, second));
pr = mod.div(pr, total);
// 2x since we can pick i < j or i > j
answer = mod.add(answer, mod.mult(2, pr));
}
// sum(p_i * p_j) for |i-j| > 1
for (int i = 0; i < n - 1; i++) {
long pr_i = sum.get(i);
int a = Math.max(0, i - 1), b = Math.min(i + 2, n - 1);
long pr_j = mod.subtract(sum.query(0, n - 1), sum.query(a, b));
answer = mod.add(answer, mod.mult(pr_i, pr_j));
}
out.println(answer);
}
}
static class Util {
public static long max(long... x) {
long max = Long.MIN_VALUE;
for (long i : x) {
max = Math.max(i, max);
}
return max;
}
public static long min(long... x) {
long min = Long.MAX_VALUE;
for (long i : x) {
min = Math.min(i, min);
}
return min;
}
private Util() {
}
}
static class NumberTheory {
private static void ASSERT(boolean assertion) {
if (!assertion)
throw new AssertionError();
}
public abstract static class Modulus<M extends NumberTheory.Modulus<M>> {
final ArrayList<Long> factorial = new ArrayList<>();
final ArrayList<Long> invFactorial = new ArrayList<>();
public abstract long modulus();
public Modulus() {
super();
factorial.add(1L);
invFactorial.add(1L);
}
public long normalize(long x) {
x %= modulus();
if (x < 0)
x += modulus();
return x;
}
public long add(long a, long b) {
long v = a + b;
return v < modulus() ? v : v - modulus();
}
public long subtract(long a, long b) {
long v = a - b;
return v < 0 ? v + modulus() : v;
}
public long mult(long... x) {
long r = 1;
for (long i : x)
r = mult(r, i);
return r;
}
public long mult(long a, long b) {
return (a * b) % modulus();
}
public long div(long a, long b) {
return mult(a, inv(b));
}
public long inv(long value) {
long g = modulus(), x = 0, y = 1;
for (long r = value; r != 0; ) {
long q = g / r;
g %= r;
long temp = g;
g = r;
r = temp;
x -= q * y;
temp = x;
x = y;
y = temp;
}
ASSERT(g == 1);
ASSERT(y == modulus() || y == -modulus());
return normalize(x);
}
}
public static class Mod107 extends NumberTheory.Modulus<NumberTheory.Mod107> {
public long modulus() {
return 1_000_000_007L;
}
}
}
static class InputReader {
public final BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] readLongArray(int n) {
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = nextLong();
}
return x;
}
}
static class LongSegmentTree {
public final int size;
public final long[] value;
protected final LongSegmentTree.Combiner combiner;
protected final long identityElement;
public LongSegmentTree(int size, LongSegmentTree.Combiner combiner, long identityElement) {
this.size = size;
value = new long[2 * size];
Arrays.fill(value, identityElement);
this.combiner = combiner;
this.identityElement = identityElement;
}
protected long combine(long a, long b) {
return combiner.combine(a, b);
}
public void rebuild() {
for (int i = size - 1; i > 0; i--) {
value[i] = combine(value[2 * i], value[2 * i + 1]);
}
}
public long get(int i) {
return value[size + i];
}
public void update_LAZY(int i, long v) {
i += size;
value[i] = v;
}
public long query(int i, int j) {
long res_left = identityElement, res_right = identityElement;
for (i += size, j += size; i < j; i /= 2, j /= 2) {
if ((i & 1) == 1)
res_left = combine(res_left, value[i++]);
if ((j & 1) == 1)
res_right = combine(value[--j], res_right);
}
return combine(res_left, res_right);
}
public interface Combiner {
long combine(long a, long b);
}
}
}
| Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | 8ed3071628cba322b4b89b993c3de25a | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.StringTokenizer;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
FExpectedSquareBeauty solver = new FExpectedSquareBeauty();
solver.solve(1, in, out);
out.close();
}
static class FExpectedSquareBeauty {
NumberTheory.Mod107 mod = new NumberTheory.Mod107();
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
long[] l = in.readLongArray(n);
long[] r = in.readLongArray(n);
for (int i = 0; i < n; i++)
r[i]++;
long[] c = new long[n];
for (int i = 0; i < n; i++)
c[i] = r[i] - l[i];
long[] o = new long[n - 1];
for (int i = 0; i < n - 1; i++) {
o[i] = Math.min(r[i], r[i + 1]) - Math.max(l[i], l[i + 1]);
o[i] = Math.max(o[i], 0);
}
long[] p = new long[n - 1];
long sum = 0;
for (int i = 0; i < n - 1; i++) {
p[i] = mod.subtract(1, mod.div(o[i], mod.mult(c[i], c[i + 1])));
sum = mod.add(sum, p[i]);
}
// We want to find (1 + sum(p_i))^2, which is 1 + 2 * sum(p_i) + sum(p_i)^2
long answer = 0;
// sum(1 * 1)
answer = mod.add(answer, 1);
// 2 * sum(p_i)
answer = mod.add(answer, mod.mult(2, sum));
// sum(p_i * p_j) for |i-j| == 0
answer = mod.add(answer, sum);
// sum(p_i * p_j) for |i-j| == 1
for (int i = 0; i + 2 < n; i++) {
int j = i + 1, k = i + 2;
long total = mod.mult(c[i], c[j], c[k]);
long both = Util.min(r[i], r[j], r[k]) - Util.max(l[i], l[j], l[k]);
both = Math.max(both, 0);
long first = mod.mult(o[i], c[k]);
long second = mod.mult(c[i], o[j]);
// PIE: total - first - second + both
long pr = mod.subtract(mod.add(total, both), mod.add(first, second));
pr = mod.div(pr, total);
// 2x since we can pick i < j or i > j
answer = mod.add(answer, mod.mult(2, pr));
}
// sum(p_i * p_j) for |i-j| > 1
for (int i = 0; i < n - 1; i++) {
long pr_i = p[i];
long tooClose = 0;
tooClose = mod.add(tooClose, i - 1 >= 0 ? p[i - 1] : 0);
tooClose = mod.add(tooClose, p[i]);
tooClose = mod.add(tooClose, i + 1 < n - 1 ? p[i + 1] : 0);
long pr_j = mod.subtract(sum, tooClose);
answer = mod.add(answer, mod.mult(pr_i, pr_j));
}
out.println(answer);
}
}
static class Util {
public static long max(long... x) {
long max = Long.MIN_VALUE;
for (long i : x) {
max = Math.max(i, max);
}
return max;
}
public static long min(long... x) {
long min = Long.MAX_VALUE;
for (long i : x) {
min = Math.min(i, min);
}
return min;
}
private Util() {
}
}
static class NumberTheory {
private static void ASSERT(boolean assertion) {
if (!assertion)
throw new AssertionError();
}
public abstract static class Modulus<M extends NumberTheory.Modulus<M>> {
final ArrayList<Long> factorial = new ArrayList<>();
final ArrayList<Long> invFactorial = new ArrayList<>();
public abstract long modulus();
public Modulus() {
super();
factorial.add(1L);
invFactorial.add(1L);
}
public long normalize(long x) {
x %= modulus();
if (x < 0)
x += modulus();
return x;
}
public long add(long a, long b) {
long v = a + b;
return v < modulus() ? v : v - modulus();
}
public long subtract(long a, long b) {
long v = a - b;
return v < 0 ? v + modulus() : v;
}
public long mult(long... x) {
long r = 1;
for (long i : x)
r = mult(r, i);
return r;
}
public long mult(long a, long b) {
return (a * b) % modulus();
}
public long div(long a, long b) {
return mult(a, inv(b));
}
public long inv(long value) {
long g = modulus(), x = 0, y = 1;
for (long r = value; r != 0; ) {
long q = g / r;
g %= r;
long temp = g;
g = r;
r = temp;
x -= q * y;
temp = x;
x = y;
y = temp;
}
ASSERT(g == 1);
ASSERT(y == modulus() || y == -modulus());
return normalize(x);
}
}
public static class Mod107 extends NumberTheory.Modulus<NumberTheory.Mod107> {
public long modulus() {
return 1_000_000_007L;
}
}
}
static class InputReader {
public final BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public long[] readLongArray(int n) {
long[] x = new long[n];
for (int i = 0; i < n; i++) {
x[i] = nextLong();
}
return x;
}
}
}
| Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | 44660597ec2990e568978ed9cc2affc2 | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.IntStream;
public class F {
static long mod = 1000000000L + 7;
class Frac{
long a,b;
Frac(long a, long b){
this.a = a%mod;
this.b = b%mod;
}
Frac(long a){
this.a = a%mod;
this.b = 1;
}
Frac add(Frac x){
long num = (x.a*b + x.b*a)%mod;
long den = (x.b*b)%mod;
return new Frac(num,den);
}
Frac mul(Frac x){
long num = (x.a*a)%mod;
long den = (x.b*b)%mod;
return new Frac(num,den);
}
@Override
public String toString() {
return a + "/" + b;
}
}
void run() {
int n = in.nextInt();
int l[] = new int[n];
int r[] = new int[n];
int i,j;
IntStream.range(0,n).forEach(ii -> l[ii] = in.nextInt());
IntStream.range(0,n).forEach(ii -> r[ii] = in.nextInt());
//g(i) = P(X_i != X_(i-1))
Frac s = new Frac(0);
Frac s2 = new Frac(0);
Frac g1 = new Frac(0);
Frac g2 = new Frac(0);
Frac gsum = new Frac(0);
long u1 = 0;
for(i=1;i<n;i++){
long ui = Math.min(r[i], r[i-1]) - Math.max(l[i],l[i-1]) + 1;
ui = Math.max(ui, 0);
long wi = r[i] - l[i] + 1;
long w1 = r[i-1] - l[i-1] + 1;
Frac gi = new Frac(wi*w1 - ui, wi*w1);
s = s.add(gi);
//s2 = s2 + gi + 2*gsum*gi + 2*h
s2 = s2.add(gi).add(gi.mul(gsum).mul(new Frac(2)));
if(i > 1){
long w2 = r[i-2] - l[i-2] + 1;
long rr = Collections.min(Arrays.asList(r[i-2], r[i-1], r[i]));
long ll = Collections.max(Arrays.asList(l[i-2], l[i-1], l[i]));
long uu = Math.max(rr - ll + 1, 0);
Frac h = new Frac(u1, w2*w1);
h = h.add(new Frac(ui, wi*w1));
long tmp = (((wi*w1)%mod)*w2)%mod;
h = h.add(new Frac(-uu, tmp)); // P(X_(i-2) = X_(i-1) ou X_(i-1) = X_i)
h = h.mul(new Frac(-1)).add(new Frac(1));
s2 = s2.add(h.mul(new Frac(2)));
}
g2 = g1;
g1 = gi;
gsum = gsum.add(g2);
u1 = ui;
}
Frac ansF = s2.add(s.mul(new Frac(2))).add(new Frac(1));
//out.println("s = " + s + " s2 = " + s2 + " ansF = " + ansF);
long ans = BigInteger.valueOf(ansF.b).modInverse(
BigInteger.valueOf(mod)).longValue();
ans = (ans * ansF.a)%mod;
if(ans < 0) ans += mod;
out.println(ans);
}
static MyScanner in;
static PrintWriter out;
public static void main(String[] args) throws IOException {
boolean stdStream = true;
if(stdStream){
in = new MyScanner();
out = new PrintWriter(System.out);
}else{
String fileName = F.class.getSimpleName();
int n_test = 1;
String inputFileName = fileName + String.format(".%02d", n_test) + ".inp";
String outputFileName = fileName + String.format(".%02d", n_test) + ".out";
in = new MyScanner(new BufferedReader(new FileReader(inputFileName)));
out = new PrintWriter(outputFileName);
}
new F().run();
in.close();
out.close();
}
static class MyScanner {
BufferedReader br;
StringTokenizer st;
MyScanner(){
this.br = new BufferedReader(new InputStreamReader(System.in));
}
MyScanner(BufferedReader br) {
this.br = br;
}
void close() throws IOException {
br.close();
}
void findToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
String next() {
findToken();
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | cb20362150fb170446fe37f525e71dfa | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | //package educational.round67;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class F2 {
InputStream is;
PrintWriter out;
String INPUT = "";
public static int[] uniq(int[] a)
{
int n = a.length;
int p = 0;
for(int i = 0;i < n;i++) {
if(i == 0 || a[i] != a[i-1])a[p++] = a[i];
}
return Arrays.copyOf(a, p);
}
int inter(int a, int b, int c, int d)
{
return Math.max(0, Math.min(b, d) - Math.max(a, c) + 1);
}
void solve()
{
int n = ni();
int[] L = na(n);
int[] R = na(n);
int mod = 1000000007;
long E = 1;
long E2 = 1;
long olde = 0;
long oldd = 0;
// E(x') = (sum E(x))/L + 1/L - (1/(KL) if intersects)
// E(x'^2) = (sum E(x^2))/L + 2(sum E(x))/L + 1/L - ((2E(i)+1)/KL if intersects)
//
for(int i = 1;i < n;i++){
long it = inter(L[i-1], R[i-1], L[i], R[i]);
long D = invl((long)(R[i-1]-L[i-1]+1)*(R[i]-L[i]+1)%mod, mod);
long NE = E + 1
- it * D;
NE %= mod;
if(NE < 0)NE += mod;
long len3 = 0;
long minus = 0;
if(i-2 >= 0){
long l3 = Math.max(Math.max(L[i-2], L[i-1]), L[i]);
long r3 = Math.min(Math.min(R[i-2], R[i-1]), R[i]);
len3 = Math.max(r3-l3+1, 0);
}
minus = len3 * (((olde + 1) * invl(R[i-1]-L[i-1]+1, mod) - oldd) % mod) +
(it - len3) * (olde + 1) % mod * invl(R[i-1]-L[i-1]+1, mod) % mod;
long NE2 = E2 + 2 * E - 2 * minus % mod * invl(R[i]-L[i]+1, mod) % mod
+ 1 - it * D;
NE2 %= mod;
if(NE2 < 0)NE2 += mod;
olde = E;
oldd = D;
E = NE;
E2 = NE2;
// tr(E, E2, guessFrac(E, mod), guessFrac(E2, mod));
}
out.println(E2);
}
public static long[] guessFrac(long n, int mod)
{
long min = mod;
long argnum = -1, argden = 0;
for(int den = 1;den <= 200000;den++){
long num = n*den%mod;
if(num*2 >= mod)num -= mod;
if(Math.abs(num) + den < min){
min = Math.abs(num) + den;
argnum = num;
argden = den;
}
}
return argden == 0 ? null : new long[]{argnum, argden};
}
public static long invl(long a, long mod) {
long b = mod;
long p = 1, q = 0;
while (b > 0) {
long c = a / b;
long d;
d = a;
a = b;
b = d % b;
d = p;
p = q;
q = d - c * q;
}
return p < 0 ? p + mod : p;
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new F2().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 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 | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | 7ba376faf0c994f04e77d834ff9d6001 | train_001.jsonl | 1561905900 | Let $$$x$$$ be an array of integers $$$x = [x_1, x_2, \dots, x_n]$$$. Let's define $$$B(x)$$$ as a minimal size of a partition of $$$x$$$ into subsegments such that all elements in each subsegment are equal. For example, $$$B([3, 3, 6, 1, 6, 6, 6]) = 4$$$ using next partition: $$$[3, 3\ |\ 6\ |\ 1\ |\ 6, 6, 6]$$$.Now you don't have any exact values of $$$x$$$, but you know that $$$x_i$$$ can be any integer value from $$$[l_i, r_i]$$$ ($$$l_i \le r_i$$$) uniformly at random. All $$$x_i$$$ are independent.Calculate expected value of $$$(B(x))^2$$$, or $$$E((B(x))^2)$$$. It's guaranteed that the expected value can be represented as rational fraction $$$\frac{P}{Q}$$$ where $$$(P, Q) = 1$$$, so print the value $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | 256 megabytes | // package ContestEd67;
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class mainF {
public static PrintWriter out = new PrintWriter(System.out);
public static FastScanner enter = new FastScanner(System.in);
public static long mod = (long) 1e9 + 7;
public static void main(String[] args) throws IOException {
int n = enter.nextInt();
long l[] = new long[n];
long r[] = new long[n];
for (int i = 0; i < n; i++) {
l[i] = enter.nextInt();
}
for (int i = 0; i < n; i++) {
r[i] = enter.nextInt();
}
if (n == 1) {
System.out.println(1);
return;
}
long I[] = new long[n - 1];
for (int i = 0; i < n - 1; i++) {
long tmp = intersect(l[i], r[i], l[i + 1], r[i + 1]);
//System.out.println(tmp);
I[i] = bin_pow(((r[i + 1] - l[i + 1] + 1) * (r[i] - l[i] + 1))%mod, mod - 2, mod);
I[i] *= tmp;
I[i] %= mod;
I[i] = 1 - I[i];
I[i] = (I[i] % mod + mod) % mod;
}
long II[] = new long[n - 2];
//Inclusion–exclusion principle
for (int i = 0; i < n - 2; i++) {
II[i] = (r[i + 1] - l[i + 1] + 1) * (r[i] - l[i] + 1);
II[i] %= mod;
II[i] *= (r[i + 2] - l[i + 2] + 1);
II[i] %= mod;
II[i] = bin_pow(II[i], mod - 2, mod);
long tmp = (intersect(l[i], r[i], l[i + 1], r[i + 1]) * (r[i + 2] - l[i + 2] + 1));
tmp%=mod;
tmp+= (intersect(l[i + 2], r[i + 2], l[i + 1], r[i + 1]) * (r[i] - l[i] + 1));
tmp %= mod;
tmp -= intersect3(l[i], r[i], l[i + 1], r[i + 1], l[i + 2], r[i + 2]);
tmp = (tmp%mod+mod)%mod;
//System.out.println(tmp);
II[i] *= tmp;
II[i] = 1 - II[i];
II[i] = (II[i] % mod + mod) % mod;
}
/*System.out.println(Arrays.toString(I));
System.out.println(Arrays.toString(II));*/
long a = 0;
for (int i = 0; i < I.length; i++) {
a += I[i];
a %= mod;
}
long b = 0;
for (int i = 0; i < II.length; i++) {
b += II[i];
b %= mod;
}
long ans = (3 * a) % mod + 1 + (2 * b) % mod;
ans %= mod;
for (int i = 0; i < n - 1; i++) {
long tmp = I[i] * (a - ((i + 1 < n - 1) ? I[i + 1] : 0) - I[i] - ((i - 1 >= 0) ? I[i - 1] : 0));
tmp %= mod;
ans += tmp;
ans %= mod;
}
ans = (ans % mod + mod) % mod;
out.println(ans);
out.close();
}
public static long intersect(long x1, long x2, long y1, long y2) {
return Math.max(0, Math.min(y2, x2) - Math.max(x1, y1) + 1);
}
public static long intersect3(long x1, long x2, long y1, long y2, long z1, long z2) {
return Math.max(0, Math.min(Math.min(y2, x2), z2) - Math.max(Math.max(x1, y1), z1) + 1);
}
public static long bin_pow(long a, long b, long mod) {//a^b %mod
long ans = 1;
while (b != 0) {
if ((b & 1) == 1) ans *= a;
a *= a;
ans %= mod;
a %= mod;
b >>= 1;
}
return ans;
}
static class FastScanner {
BufferedReader br;
StringTokenizer stok;
FastScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
String next() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) {
return null;
}
stok = new StringTokenizer(s);
}
return stok.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());
}
char nextChar() throws IOException {
return (char) (br.read());
}
String nextLine() throws IOException {
return br.readLine();
}
}
}
| Java | ["3\n1 1 1\n1 2 3", "3\n3 4 5\n4 5 6"] | 2 seconds | ["166666673", "500000010"] | NoteLet's describe all possible values of $$$x$$$ for the first sample: $$$[1, 1, 1]$$$: $$$B(x) = 1$$$, $$$B^2(x) = 1$$$; $$$[1, 1, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 1, 3]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 1]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[1, 2, 2]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[1, 2, 3]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{6} (1 + 4 + 4 + 9 + 4 + 9) = \frac{31}{6}$$$ or $$$31 \cdot 6^{-1} = 166666673$$$.All possible values of $$$x$$$ for the second sample: $$$[3, 4, 5]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 4, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[3, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[3, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; $$$[4, 4, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 4, 6]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 5]$$$: $$$B(x) = 2$$$, $$$B^2(x) = 4$$$; $$$[4, 5, 6]$$$: $$$B(x) = 3$$$, $$$B^2(x) = 9$$$; So $$$E = \frac{1}{8} (9 + 9 + 4 + 9 + 4 + 4 + 4 + 9) = \frac{52}{8}$$$ or $$$13 \cdot 2^{-1} = 500000010$$$. | Java 8 | standard input | [
"dp",
"probabilities",
"math"
] | 1d01c01bec67572f9c1827ffabcc9793 | The first line contains the single integer $$$n$$$ ($$$1 \le n \le 2 \cdot 10^5$$$) — the size of the array $$$x$$$. The second line contains $$$n$$$ integers $$$l_1, l_2, \dots, l_n$$$ ($$$1 \le l_i \le 10^9$$$). The third line contains $$$n$$$ integers $$$r_1, r_2, \dots, r_n$$$ ($$$l_i \le r_i \le 10^9$$$). | 2,500 | Print the single integer — $$$E((B(x))^2)$$$ as $$$P \cdot Q^{-1} \mod 10^9 + 7$$$. | standard output | |
PASSED | 57bca18ef90dc823a75421b40e471f66 | train_001.jsonl | 1276875000 | You are given an equation: Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of the equation and print all of them in ascending order. | 256 megabytes | import java.io.*;
import java.util.*;
public class p20b
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
double A = sc.nextDouble();
double B = sc.nextDouble();
double C = sc.nextDouble();
if(A==0 && B==0 & C==0) System.out.println("-1");
else if(A==0 && B==0) System.out.println("0");
else if(A==0) System.out.printf("1\n%.9f",-C/B);
else
{
double D = B*B-4*A*C;
if(D<0) System.out.println("0");
else if(D==0) System.out.printf("1\n%.9f", -B/(2*A));
else
{
double N = (-B-Math.sqrt(D))/(2*A);
double P = (-B+Math.sqrt(D))/(2*A);
System.out.printf("2\n%.9f\n%.9f",Math.min(N,P),Math.max(P, N));
}
}
}
} | Java | ["1 -5 6"] | 1 second | ["2\n2.0000000000\n3.0000000000"] | null | Java 7 | standard input | [
"math"
] | 84372885f2263004b74ae753a2f358ac | The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0. | 2,000 | In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point. | standard output | |
PASSED | a175ec251095775262fc5e7c3591c4e5 | train_001.jsonl | 1276875000 | You are given an equation: Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of the equation and print all of them in ascending order. | 256 megabytes | import java.util.Scanner;
public class b {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
double a = sc.nextDouble(), b = sc.nextDouble(), c = sc.nextDouble();
sc.close();
if (a == 0 && b == 0 && c == 0) {
System.out.println("-1");
return;
}
if (a == 0 && b == 0) {
System.out.println("0");
return;
}
if (a == 0) {
System.out.printf("1\n%.10f\n", -c / b);
return;
}
double d = (b * b) - (4 * a * c);
if (d > 0) {
double x1 = (-b - Math.sqrt(d)) / (a * 2), x2 = (-b + Math.sqrt(d)) / (a * 2);
System.out.printf("2\n%.10f\n%.10f\n", Math.min(x1, x2), Math.max(x1, x2));
} else if (d == 0) {
System.out.printf("1\n%.10f\n", -b / (2 * a));
} else {
System.out.println("0");
}
}
} | Java | ["1 -5 6"] | 1 second | ["2\n2.0000000000\n3.0000000000"] | null | Java 7 | standard input | [
"math"
] | 84372885f2263004b74ae753a2f358ac | The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0. | 2,000 | In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point. | standard output | |
PASSED | a78ff54f1dd1caa2496891c8a51cd973 | train_001.jsonl | 1276875000 | You are given an equation: Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of the equation and print all of them in ascending order. | 256 megabytes | import java.util.Scanner;
public class problem20B {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
double A = (double) (scan.nextInt());
double B = (double) (scan.nextInt());
double C = (double) (scan.nextInt());
scan.close();
int K = -2; //otvet
double[] roots = new double[1];
if (A == 0 && B == 0 && C == 0) {
K = -1;
}
if (K == -2 && ((A!=0 && B==0 && C == 0) || (A==0 && B!=0 && C == 0) || (A==0 && B!=0 && C != 0))) {
K = 1;
roots = new double[K];
roots[0] = B == 0 ? 0 : -C / B;
}
if (K == -2 && A == 0 && B == 0 && C != 0) {
K = 0;
}
if (K == -2) {
double D = B*B - 4*A*C;
//System.out.println(D+"");
if (D < 0)
K = 0;
if (D == (double) (0)) {
//System.out.println("D = " + D);
K=1;
roots = new double[K];
roots[0] = (-B) / (2*A);
}
if (D > 0) {
K=2;
roots = new double[K];
roots[0] = ((-B) - Math.sqrt(D)) / (2*A);
roots[1] = ((-B) + Math.sqrt(D)) / (2*A);
}
}
if (K == 2 && roots[0] > roots[1]) {
double tmp = roots[0];
roots[0] = roots[1];
roots[1] = tmp;
}
System.out.println(K);
for (int i = 0; i<K; i++)
System.out.println(roots[i]);
}
} | Java | ["1 -5 6"] | 1 second | ["2\n2.0000000000\n3.0000000000"] | null | Java 7 | standard input | [
"math"
] | 84372885f2263004b74ae753a2f358ac | The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0. | 2,000 | In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point. | standard output | |
PASSED | af57102606a3e16a775f7ec60d559f26 | train_001.jsonl | 1276875000 | You are given an equation: Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of the equation and print all of them in ascending order. | 256 megabytes | import java.util.Scanner;
public class CF_020B {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
long a = read.nextInt();
long b = read.nextInt();
long c = read.nextInt();
long d = b * b - 4 * a * c;
if (a == 0) {
if (b == 0 && c == 0)
System.out.println(-1);
else if (b == 0)
System.out.println(0);
else {
System.out.println(1);
System.out.println((double)-c / b);
}
}
else {
if (d < 0)
System.out.println(0);
else if (d == 0) {
System.out.println(1);
System.out.println(-b / (2 * a));
}
else {
System.out.println(2);
if (a > 0) {
System.out.println((-b - Math.sqrt(d)) / (2 * a));
System.out.println((-b + Math.sqrt(d)) / (2 * a));
}
else {
System.out.println((-b + Math.sqrt(d)) / (2 * a));
System.out.println((-b - Math.sqrt(d)) / (2 * a));
}
}
}
read.close();
}
} | Java | ["1 -5 6"] | 1 second | ["2\n2.0000000000\n3.0000000000"] | null | Java 7 | standard input | [
"math"
] | 84372885f2263004b74ae753a2f358ac | The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0. | 2,000 | In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point. | standard output | |
PASSED | fc4eba203eb058794e41826208a71aa2 | train_001.jsonl | 1276875000 | You are given an equation: Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of the equation and print all of them in ascending order. | 256 megabytes | import java.io.*;
import java.util.*;
public class equation {
static BufferedReader br;
static StringTokenizer st;
static PrintWriter out;
public static boolean isquadratic(int a, int b, int c) {
if (a != 0)
return true;
return false;
}
public static long determinant(int a, int b, int c) {
return (long)(b)*(long)(b)-4*(long)(a)*(long)(c);
}
public static boolean islinear(int a, int b, int c) {
if (a == 0 && b != 0)
return true;
return false;
}
public static void main(String[] args) throws IOException {
InputStream input = System.in;
//InputStream input = new FileInputStream("fileIn.in");
OutputStream output = System.out;
//OutputStream output = new FileOutputStream("fileOut.out");
br = new BufferedReader(new InputStreamReader(input));
out = new PrintWriter(output);
StringTokenizer st = new StringTokenizer(br.readLine());
int a = Integer.parseInt(st.nextToken());
int b = Integer.parseInt(st.nextToken());
int c = Integer.parseInt(st.nextToken());
if (isquadratic(a,b,c))
{
if (determinant(a,b,c) > 0)
{
out.println(2);
if (a > 0)
{
out.println((-b-Math.sqrt(determinant(a,b,c)))/(2*a));
out.println((-b+Math.sqrt(determinant(a,b,c)))/(2*a));
}
if (a < 0)
{
out.println((-b+Math.sqrt(determinant(a,b,c)))/(2*a));
out.println((-b-Math.sqrt(determinant(a,b,c)))/(2*a));
}
}
if (determinant(a,b,c) == 0)
{
out.println(1);
out.println((float)(-b)/(2*a));
}
if (determinant(a,b,c) < 0)
out.println(0);
}
else if (islinear(a,b,c))
{
out.println(1);
out.println((float)(-c)/b);
}
else
{
if (c == 0)
out.println(-1);
else
out.println(0);
}
out.close();
}
}
| Java | ["1 -5 6"] | 1 second | ["2\n2.0000000000\n3.0000000000"] | null | Java 7 | standard input | [
"math"
] | 84372885f2263004b74ae753a2f358ac | The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0. | 2,000 | In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point. | standard output | |
PASSED | 641ed0969f6fa886de36301f4c24f990 | train_001.jsonl | 1276875000 | You are given an equation: Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of the equation and print all of them in ascending order. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* Created by peacefrog on 11/24/15.
* Time : 7:54 PM
*/
public class CF20B {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
PrintWriter out;
long timeBegin, timeEnd;
public void runIO() throws IOException {
timeBegin = System.currentTimeMillis();
InputStream inputStream;
OutputStream outputStream;
if (ONLINE_JUDGE) {
inputStream = System.in;
Reader.init(inputStream);
outputStream = System.out;
out = new PrintWriter(outputStream);
} else {
inputStream = new FileInputStream("/home/peacefrog/Dropbox/IdeaProjects/Problem Solving/input");
Reader.init(inputStream);
out = new PrintWriter(System.out);
}
solve();
out.flush();
out.close();
timeEnd = System.currentTimeMillis();
System.err.println("Time = " + (timeEnd - timeBegin));
}
/*
* Start Solution Here
*/
private void solve() throws IOException {
double a = Reader.nextInt(); //This Variable default in Code Template
double b = Reader.nextInt();
double c = Reader.nextInt();
double inf = b* b - 4 * a* c ;
if(a == 0.0D && b == 0.0D && c == 0 ) {
out.println(-1);
return;
}
if(a == 0.0D && b == 0.0D && c != 0 ) {
out.println(0);
return;
}
if (inf < 0.0D){
out.println(0);
return;
}
if (inf == 0.0D)
{
out.printf("1\n%.10f\n", (-b + Math.sqrt(inf))/(2.0 * a));
return;
}
if (a == 0 )
{
out.printf("1\n%.10f\n", (-c/b) );
return;
}
double ans[] = new double[2];
ans[0] = (-b + Math.sqrt(inf))/(2.0 * a);
ans[1] = (-b - Math.sqrt(inf))/(2.0 * a);
Arrays.sort(ans);
out.printf("2\n%.10f\n%.10f", ans[0] ,ans[1]);
}
public static void main(String[] args) throws IOException {
new CF20B().runIO();
}
static class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/**
* call this method to initialize reader for InputStream
*/
static void init(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
/**
* get next word
*/
static String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
static String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
static long[] nextLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = nextLong();
}
return arr;
}
static int[] nextIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
}
}
| Java | ["1 -5 6"] | 1 second | ["2\n2.0000000000\n3.0000000000"] | null | Java 7 | standard input | [
"math"
] | 84372885f2263004b74ae753a2f358ac | The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0. | 2,000 | In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point. | standard output | |
PASSED | 311428f5104a383c720a3bca2873d5ea | train_001.jsonl | 1276875000 | You are given an equation: Ax2 + Bx + C = 0. Your task is to find the number of distinct roots of the equation and print all of them in ascending order. | 256 megabytes | import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Objects;
import java.util.TreeSet;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Erasyl Abenov
*
*
*/
public class Main {
public static void main(String[] args) throws IOException {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
try (PrintWriter out = new PrintWriter(outputStream)) {
TaskB solver = new TaskB();
solver.solve(1, in, out);
}
}
}
class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) throws IOException{
long a = in.nextLong();
long b = in.nextLong();
long c = in.nextLong();
ArrayList<Double> list = new ArrayList<>();
int count = 0;
if(a == b && b == c && c != 0) list.add(0.0);
else if(a == 0){
if(b == 0){
if(c == 0) count--;
else list.add(0.0);
}
else{
count++;
list.add((-1.0*c)/b);
}
}
else if(b == 0){
if(c <= 0){
count++;
list.add(Math.sqrt((-1.0*c)/a));
}
else list.add(0.0);
}
else if(c == 0){
count += 2;
list.add(-0.0);
list.add((-1.0*b)/a);
}
else{
long D = b*b*1L - 1L*4*a*c;
if(D < 0) count = -1;
else if(D == 0){
count++;
list.add((-b*1.0)/(1L*2*a));
}
else{
count = 2;
list.add((-b + Math.sqrt(D))/(1L*2*a));
list.add((-b - Math.sqrt(D))/(1L*2*a));
}
}
if(count != 0) out.println(count);
Collections.sort(list);
for(double ans : list){
out.printf("%.10f", ans);
out.println();
}
}
}
class InputReader {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(nextLine());
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public BigInteger nextBigInteger(){
return new BigInteger(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
} | Java | ["1 -5 6"] | 1 second | ["2\n2.0000000000\n3.0000000000"] | null | Java 7 | standard input | [
"math"
] | 84372885f2263004b74ae753a2f358ac | The first line contains three integer numbers A, B and C ( - 105 ≤ A, B, C ≤ 105). Any coefficient may be equal to 0. | 2,000 | In case of infinite root count print the only integer -1. In case of no roots print the only integer 0. In other cases print the number of root on the first line and the roots on the following lines in the ascending order. Print roots with at least 5 digits after the decimal point. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.