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 | 6703f97031ac58f15cfb8c43d7c32104 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
for (int i = 0; i < t; i++) {
int n = cin.nextInt();
System.out.println(new Solution().func(n, cin.next()));
}
}
}
class Solution {
public long func(int n, String s) {
char[] chars = s.toCharArray();
long sum = chars.length + 1;
long[] cnt = new long[2];
int dir = 0;
if (chars[0] == 'D') {
dir = 1;
}
int firstD = 0;
for (int i = 0; i < chars.length; i++) {
if (chars[i] == 'R') {
cnt[dir]++;
} else {
cnt[dir ^ 1]++;
}
if (firstD == 0 && chars[i] != chars[0]) {
firstD = i;
}
}
long r = n - 1 - cnt[0];
long d = n - 1 - cnt[1];
if (chars[chars.length - 1] != chars[0]) {
sum += (cnt[0] - firstD + 1) * d;
sum += n * r;
} else {
sum += (cnt[1] + 1) * r;
if (firstD != 0) {
sum += (n - firstD) * d;
}
}
return sum;
}
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 8 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 0470c55b8e9d8695121e10dfd4a3ebd2 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Map.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.System.*;
public class Main
{
public void tq() throws Exception
{
st=new StringTokenizer(bq.readLine());
int tq=i();
sb=new StringBuilder(2000000);
o:
while(tq-->0)
{
int m=i();
String s=s();
int n=s.length();
if(s.indexOf('R')==-1||s.indexOf('D')==-1)
{
sl(m);
continue o;
}
int x=0;
int y=0;
long c=0l;
for(char a:s.toCharArray())
{
if(a=='R')y++;
else x++;
}
int dx=m-x;
int dy=m-y;
int i=0;
x=0;
y=0;
for(char a:s.toCharArray())
{
i++;
if(a!=s.charAt(0))break;
if(a=='R')
{
y++;
}
else
{
x++;
}
}
i--;
c+=1l*dx*dy;
for(int xx=i;xx<n;xx++)
{
if(s.charAt(xx)=='R')
{
c+=dx;
y++;
}
else
{
c+=dy;
x++;
}
//pl(" ",c);
}
// pl(c);
c+=i;
sl(c);
}
p(sb);
}
long mod=998244353l;
int max=Integer.MAX_VALUE,min=Integer.MIN_VALUE;long maxl=Long.MAX_VALUE,minl=Long.MIN_VALUE;
BufferedReader bq = new BufferedReader(new InputStreamReader(in));StringTokenizer st;StringBuilder sb;
public static void main(String[] a)throws Exception{new Main().tq();}
int di[][]={{-1,0},{1,0},{0,-1},{0,1}};
int de[][]={{-1,0},{1,0},{0,-1},{0,1},{-1,-1},{1,1},{-1,1},{1,-1}};
void f(){out.flush();}
int p(int i,int p[]){return p[i]<0?i:(p[i]=p(p[i],p));}
boolean c(int x,int y,int n,int m){return x>=0&&x<n&&y>=0&&y<m;}
int[] so(int ar[])
{
Integer r[] = new Integer[ar.length];
for (int x = 0; x < ar.length; x++) r[x] = ar[x];
sort(r);
for (int x = 0; x < ar.length; x++) ar[x] = r[x];
return ar;
}
long[] so(long ar[])
{
Long r[] = new Long[ar.length];
for (int x = 0; x < ar.length; x++) r[x] = ar[x];
sort(r);
for (int x = 0; x < ar.length; x++) ar[x] = r[x];
return ar;
}
char[] so(char ar[])
{
Character r[] = new Character[ar.length];
for (int x = 0; x < ar.length; x++) r[x] = ar[x];
sort(r);
for (int x = 0; x < ar.length; x++) ar[x] = r[x];
return ar;
}
void p(Object p) {out.print(p);}void pl(Object p) {out.println(p);}void pl() {out.println();}
void s(String s) {sb.append(s);}
void s(int s) {sb.append(s);}
void s(long s) {sb.append(s);}
void s(char s) {sb.append(s);}
void s(double s) {sb.append(s);}
void ss() {sb.append(' ');}
void sl(String s) {s(s);sb.append("\n");}
void sl(int s) {s(s);sb.append("\n");}
void sl(long s) {s(s);sb.append("\n");}
void sl(char s) {s(s);sb.append("\n");}
void sl(double s) {s(s);sb.append("\n");}
void sl() {sb.append("\n");}
int l(int v) {return 31 - Integer.numberOfLeadingZeros(v);}
long l(long v) {return 63 - Long.numberOfLeadingZeros(v);}
int sq(int a) {return (int) sqrt(a);}
long sq(long a) {return (long) sqrt(a);}
int gcd(int a, int b)
{
while (b > 0)
{
int c = a % b;
a = b;
b = c;
}
return a;
}
long gcd(long a, long b)
{
while (b > 0l)
{
long c = a % b;
a = b;
b = c;
}
return a;
}
boolean p(String s, int i, int j)
{
while (i < j) if (s.charAt(i++) != s.charAt(j--)) return false;
return true;
}
boolean[] si(int n)
{
boolean bo[] = new boolean[n + 1];
bo[0] = bo[1] = true;
for (int x = 4; x <= n; x += 2) bo[x] = true;
for (int x = 3; x * x <= n; x += 2)
{
if (!bo[x])
{
int vv = (x << 1);
for (int y = x * x; y <= n; y += vv) bo[y] = true;
}
}
return bo;
}
long mul(long a, long b, long m)
{
long r = 1l;
a %= m;
while (b > 0)
{
if ((b & 1) == 1) r = (r * a) % m;
b >>= 1;
a = (a * a) % m;
}
return r;
}
int i() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return Integer.parseInt(st.nextToken());
}
long l() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return Long.parseLong(st.nextToken());
}
String s() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return st.nextToken();
}
double d() throws IOException
{
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
return Double.parseDouble(st.nextToken());
}
void s(int a[])
{
for (int e : a){sb.append(e);sb.append(' ');}
sb.append("\n");
}
void s(long a[])
{
for (long e : a){sb.append(e);sb.append(' ');}
sb.append("\n");
}
void s(char a[])
{
for (char e : a){sb.append(e);sb.append(' ');}
sb.append("\n");
}
void s(int ar[][]) {for (int a[] : ar) s(a);}
void s(long ar[][]) {for (long a[] : ar) s(a);}
void s(char ar[][]) {for (char a[] : ar) s(a);}
int[] ari(int n) throws IOException
{
int ar[] = new int[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = Integer.parseInt(st.nextToken());
return ar;
}
long[] arl(int n) throws IOException
{
long ar[] = new long[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = Long.parseLong(st.nextToken());
return ar;
}
char[] arc(int n) throws IOException
{
char ar[] = new char[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = st.nextToken().charAt(0);
return ar;
}
double[] ard(int n) throws IOException
{
double ar[] = new double[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = Double.parseDouble(st.nextToken());
return ar;
}
String[] ars(int n) throws IOException
{
String ar[] = new String[n];
if (!st.hasMoreTokens()) st = new StringTokenizer(bq.readLine());
for (int x = 0; x < n; x++) ar[x] = st.nextToken();
return ar;
}
int[][] ari(int n, int m) throws IOException
{
int ar[][] = new int[n][m];
for (int x = 0; x < n; x++)ar[x]=ari(m);
return ar;
}
long[][] arl(int n, int m) throws IOException
{
long ar[][] = new long[n][m];
for (int x = 0; x < n; x++)ar[x]=arl(m);
return ar;
}
char[][] arc(int n, int m) throws IOException
{
char ar[][] = new char[n][m];
for (int x = 0; x < n; x++)ar[x]=arc(m);
return ar;
}
double[][] ard(int n, int m) throws IOException
{
double ar[][] = new double[n][m];
for (int x = 0; x < n; x++)ar[x]=ard(m);
return ar;
}
void p(int ar[])
{
sb = new StringBuilder(11 * ar.length);
for (int a : ar) {sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(long ar[])
{
StringBuilder sb = new StringBuilder(20 * ar.length);
for (long a : ar){sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(double ar[])
{
StringBuilder sb = new StringBuilder(22 * ar.length);
for (double a : ar){sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(char ar[])
{
StringBuilder sb = new StringBuilder(2 * ar.length);
for (char aa : ar){sb.append(aa);sb.append(' ');}
out.println(sb);
}
void p(String ar[])
{
int c = 0;
for (String s : ar) c += s.length() + 1;
StringBuilder sb = new StringBuilder(c);
for (String a : ar){sb.append(a);sb.append(' ');}
out.println(sb);
}
void p(int ar[][])
{
StringBuilder sb = new StringBuilder(11 * ar.length * ar[0].length);
for (int a[] : ar)
{
for (int aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void p(long ar[][])
{
StringBuilder sb = new StringBuilder(20 * ar.length * ar[0].length);
for (long a[] : ar)
{
for (long aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void p(double ar[][])
{
StringBuilder sb = new StringBuilder(22 * ar.length * ar[0].length);
for (double a[] : ar)
{
for (double aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void p(char ar[][])
{
StringBuilder sb = new StringBuilder(2 * ar.length * ar[0].length);
for (char a[] : ar)
{
for (char aa : a){sb.append(aa);sb.append(' ');}
sb.append("\n");
}
p(sb);
}
void pl(Object... ar) {for (Object e : ar) p(e + " ");pl();}
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 8 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 74fd3f02d7ccd43f23e2ccbb6ea116cd | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String args[])
{
FastReader input=new FastReader();
PrintWriter out=new PrintWriter(System.out);
int T=input.nextInt();
while(T-->0)
{
int n=input.nextInt();
String s=input.next();
int r=0,d=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='R') r++;
else d++;
}
if(r==0 && d==0)
{
out.println(1);
}
else if(r==0)
{
out.println(n);
}
else if(d==0)
{
out.println(n);
}
else
{
int c=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='D') break;
c++;
}
int v1=r-c+1;
int v2=n-d-1;
long sum=(long)v1*(long)v2;
c=0;
for(int i=0;i<s.length();i++)
{
if(s.charAt(i)=='R') break;
c++;
}
v1=d-c+1;
v2=n-r-1;
sum+=(long)v1*(long)v2;
v1=n-d-1;
v2=n-r-1;
sum+=(long)v1*(long)v2;
sum+=r+d+1;
out.println(sum);
}
}
out.close();
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str="";
try
{
str=br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 8 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 6e1825529e74f489cb64c1d30104adc3 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class E123E {
static StringBuffer ans1 = new StringBuffer("");
static FastScanner sc = new FastScanner();
static PrintWriter printWriter = new PrintWriter(System.out);
public static void solve(long n, String s) {
if(s.length() == 0) {
printWriter.println(1);
return;
}
long res = n * n;
res -= calc(n, s);
StringBuilder sb = new StringBuilder();
for(char c: s.toCharArray()) sb.append(c == 'R' ? 'D' : 'R');
res -= calc(n, sb.toString());
printWriter.println(res);
}
private static long calc(long n, String s) {
long idx = s.indexOf('R');
if(idx == -1) return n * (n - 1);
long res = idx * (n - 1);
long y = 0;
for(int i = s.length() - 1; i > idx; i--) {
if(s.charAt(i) == 'D') res += y;
else y += 1;
}
// System.out.println("idx " + idx + " res " + res);
return res;
}
public static void main(String[] args) {
int t = sc.nextInt();
for (int tt = 0; tt < t; tt++) {
long n = sc.nextLong();
String s = sc.next();
solve(n, s);
}
printWriter.flush();
}
/*****************************************************************
******************** DO NOT READ AFTER THIS LINE *****************
*****************************************************************/
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
int[][] read2dArray(int n, int m) {
int arr[][] = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = nextInt();
}
}
return arr;
}
ArrayList<Integer> readArrayList(int n) {
ArrayList<Integer> arr = new ArrayList<Integer>();
for (int i = 0; i < n; i++) {
int a = nextInt();
arr.add(a);
}
return arr;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 3266c25a4b94fc70622689f39b07fcf6 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main{
static final int mod=998244353;
static final int maxn=3000+10;
static IO io;
static Integer n,m;
public static void main(String[] args)throws IOException
{
io=new IO();
int t;
t=io.nextInt();
//t=1;
while(t>0) {
t--;
solve();
}
io.close();
}
static char cmp[]={'R','D'};
private static void solve()throws IOException
{
n=io.nextInt();
String s=io.nextLine();
long x=0,y=0;
int i=0;
m=s.length();
long off=0;
if(s.charAt(i)=='D') {
cmp[0]='D';cmp[1]='R';
}else {
cmp[0]='R';cmp[1]='D';
}
while(i<m && s.charAt(i)==cmp[0]) {
i++;off++;
}
//if(i==0) x--;
if(i>=m) {
io.println(n);
}else {
while(i<m) {
if(s.charAt(i)==cmp[0]) x++;
else y++;
i++;
}
long sq=(long) n*(long)n;
//x-=1;
io.println(sq-(long)x*y-(long)(n-1)*off);
}
}
static class IO extends PrintWriter {
BufferedReader br;
StringTokenizer st;
// standard input
public IO() { this(System.in, System.out); }
public IO(InputStream i, OutputStream o) {
super(o);
br = new BufferedReader(new InputStreamReader(i));
}
public IO(String problemName) throws IOException {
super(problemName + ".out");
br = new BufferedReader(new FileReader(problemName + ".in"));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | d33bc54597d2a995a116628957a757e8 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int numCases = Integer.parseInt(br.readLine());
for (int i = 0; i < numCases; i++) {
int length = Integer.parseInt(br.readLine());
char[] in = br.readLine().toCharArray();
int x = 0;
int y = 0;
for (int j = 0; j < in.length; j++) {
if (in[j] == 'D')
y++;
else
x++;
}
long dx = length - x - 1;
long dy = length - y - 1;
long ret = 0;
int count = 0;
int longestSame = 1;
for (int j = 1; j < in.length; j++) {
if (in[j] != in[j - 1]) {
if (count > 0) {
if (in[j] == 'D') {
ret += longestSame * dy;
} else {
ret += longestSame * dx;
}
}
// pw.println(j + " " + ret);
count++;
longestSame = 1;
} else {
longestSame++;
}
}
if (count > 0) {
if (in[in.length - 1] == 'D') {
ret += longestSame * dx;
} else {
ret += longestSame * dy;
}
// pw.println("next 1 " + ret);
ret += (dx + 1) * (dy + 1) - 1;
// pw.println("next 2 " + ret);
pw.println(ret + in.length + 1);
} else {
pw.println(length);
}
}
br.close();
pw.close();
}
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 19ec580f255be8ebfa3269dd00bb8d0e | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.StringTokenizer;
public class App {
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 long count(String s, int maxLength) {
long ans = 0;
int rIndex = s.indexOf("R");
ans = (long) (maxLength - 1) * rIndex;
int missingCells = 0;
for (int j = s.length() - 1; j >= rIndex; j--) {
if (s.charAt(j) == 'D') {
ans += missingCells;
} else {
missingCells++;
}
}
return ans;
}
public static void main(String[] args) {
FastReader reader = new FastReader();
PrintWriter writer = new PrintWriter(System.out, true);
int t = reader.nextInt();
for (int i = 1; i <= t; i++) {
int n = reader.nextInt();
String s = reader.nextLine();
if ((s.startsWith("D") && !s.contains("R")) || (s.startsWith("R") && !s.contains("D"))) {
writer.println(n);
continue;
}
long ans = (long) n * (long) n;
ans -= count(s, n);
StringBuilder q = new StringBuilder();
for (int j = 0; j < s.length(); j++) {
q.append(s.charAt(j) == 'R'
? 'D'
: 'R');
}
ans -= count(q.toString(), n);
writer.println(ans);
}
}
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 9e434cba2fdb2f7424c282ca193e01af | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Scanner scn = new Scanner(System.in);
OutputWriter out = new OutputWriter(System.out);
// Always print a trailing "\n" and close the OutputWriter as shown at the end of your output
// example:
int t = scn.nextInt();
for(int i1=0;i1<t;i1++){
long n=scn.nextInt();long ans=0;
ArrayList<Integer> a1=new ArrayList<Integer>();
ArrayList<Integer> a2=new ArrayList<Integer>();
String s=scn.next();
char c=s.charAt(0);
int a=0;int b=0;
//construct arraylist
for(int i=0;i<s.length();i++){
if(c==s.charAt(i)){
int len=0;
while((i+len)<s.length()&&s.charAt(i)==s.charAt(i+len)){
len++;
}
a1.add(a);a2.add(a+len);
a+=len;
i+=len-1;
}
else{
int len=0;
while((i+len)<s.length()&&s.charAt(i)==s.charAt(i+len)){
len++;
}
a1.add(b);a2.add(b+len);
b+=len;
i+=len-1;
}
}
long b1=n-a;long b2=n-b;
//out.print(a1+" "+a2+" "+b1+" "+b2+"\n");
ans+=a2.get(0)-a1.get(0);
for(int i=1;i<a1.size()-1;i++){
if(i%2==1){
ans+=(a2.get(i)-a1.get(i))*b1;
}
else{
ans+=(a2.get(i)-a1.get(i))*b2;
}
}
if(a1.size()%2==1){
a=a1.get(a1.size()-1);
}
else{
b=a1.get(a1.size()-1);
}
//out.print(ans+" "+a+" "+b+" "+"\n");
if(a1.size()>1){
ans+=(n-a)*(n-b);
}
else{
ans=n;
}
out.print(Long.toString(ans)+"\n");
}
out.close();
}
// fast input
static class Scanner {
public BufferedReader reader;
public StringTokenizer tokenizer;
public Scanner(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
String line = reader.readLine();
if (line == null)
return null;
tokenizer = new StringTokenizer(line);
} catch (Exception e) {
throw(new RuntimeException());
}
}
return tokenizer.nextToken();
}
public int nextInt() { return Integer.parseInt(next()); }
public long nextLong() { return Long.parseLong(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
}
// fast output
static class OutputWriter {
BufferedWriter writer;
public OutputWriter(OutputStream stream) {
writer = new BufferedWriter(new OutputStreamWriter(stream));
}
public void print(int i) throws IOException { writer.write(i); }
public void print(String s) throws IOException { writer.write(s); }
public void print(char[] c) throws IOException { writer.write(c); }
public void close() throws IOException { writer.close(); }
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 047872643f053cdbc91cd5b3bca85ed9 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.*;
import java.util.*;
public class Path {
public static void main(String[] args) throws Exception {
FastIO in = new FastIO();
int t = in.nextInt();
for (int tc=0; tc<t; tc++) {
long n = in.nextLong();
String s = in.next();
int rCount = 0;
int dCount = 0;
for (int i=0; i<s.length(); i++) {
if (s.charAt(i)=='D') dCount++;
else rCount++;
}
if (rCount==0 || dCount==0) {
System.out.println(n);
continue;
}
System.out.println(n*n-topArea(s, n, 'R', 'D')-topArea(s, n, 'D', 'R'));
}
}
public static long topArea(String s, long n, char r, char d) {
int occ = s.indexOf(r);
long ans = 0;
ans+=occ*(n-1);
long size = 0;
for (int i=s.length()-1; i>=occ; i--) {
if (s.charAt(i)==d) ans+=size;
else size++;
}
return ans;
}
static class FastIO {
BufferedReader br;
StringTokenizer st;
PrintWriter pr;
public FastIO() throws IOException
{
br = new BufferedReader(
new InputStreamReader(System.in));
pr = new PrintWriter(System.out);
}
public String next() throws IOException
{
while (st == null || !st.hasMoreElements()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws NumberFormatException, IOException { return Integer.parseInt(next()); }
public long nextLong() throws NumberFormatException, IOException { return Long.parseLong(next()); }
public double nextDouble() throws NumberFormatException, IOException
{
return Double.parseDouble(next());
}
public String nextLine() throws IOException
{
String str = br.readLine();
return str;
}
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 75a5ef4f9409f46ef0bf0546833db598 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.util.*;
import java.io.*;
public class _1644_E {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int t = Integer.parseInt(in.readLine());
while(t-- > 0) {
int n = Integer.parseInt(in.readLine());
String s = in.readLine();
int down = n - 1;
int right = n - 1;
int change = s.length();
for(int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if(c == 'D') {
down--;
}else {
right--;
}
if(change == s.length() && i > 0 && c != s.charAt(i - 1)) {
change = i;
}
}
long res = change + 1;
if(s.charAt(0) == 'D') {
res += down;
}else {
res += right;
}
for(int i = change; i < s.length(); i++) {
if(i == s.length() - 1) {
res += (long)(down + 1) * (right + 1);
}else {
char next = s.charAt(i + 1);
if(next == 'D') {
res += right + 1;
}else {
res += down + 1;
}
}
}
out.println(res);
}
in.close();
out.close();
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 3d2225083c02cbac51df04f3df897849 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class E{
static class pt{
public int x, y;
public pt(int a, int b) {
x=a;y=b;
}
}
public static void main(String[] args) throws IOException {
// br = new BufferedReader(new FileReader(".in"));
// out = new PrintWriter(new FileWriter(".out"));
//new Thread(null, new (), "peepee", 1<<28).start();
int t =readInt();boolean[] vis = new boolean[256];
while(t-->0) {
vis['R']=vis['D'] = false;
long n =readInt();
long tans = 0;
char[] c =read().toCharArray();
int x = 1, y = 1;
long mX = 1, mY = 1;
int bx = -1, by = -1;
int rightPath = 0;
// treat it as a rectangle once you switch directions
for (int i = 0; i < c.length; i++) {
int px = x, py = y;
if (c[i] == 'R') {
x++;
}
else {
y++;
}
vis[c[i]] = true;
if (vis['D'] != vis['R']) {
tans++;
}
else {
if (rightPath==0){
bx=px;
by=py;
}
rightPath++;
}
mX = max(x,mX);
mY = max(y,mY);
}
long ans = 0;
if (bx != -1) {
// System.out.println(mX + " " + bx + " " + mY + " " + by + " ");
// System.out.println((n-bx+1)*(n-by+1));
ans = (n-bx+1)*(n-by+1) - ((mX-bx+1)*(mY-by+1) - rightPath)+1;
}
else {
tans = n;
}
//System.out.println(ans + " " +tans);
out.println(ans+tans);
}
out.close();
}
/* Stupid things to try if stuck:
* n=1, expand bsearch range
* brute force small patterns
* submit stupid intuition
*/
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static StringTokenizer st = new StringTokenizer("");
static String read() throws IOException{return st.hasMoreTokens() ? st.nextToken():(st = new StringTokenizer(br.readLine())).nextToken();}
static int readInt() throws IOException{return Integer.parseInt(read());}
static long readLong() throws IOException{return Long.parseLong(read());}
static double readDouble() throws IOException{return Double.parseDouble(read());}
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | ba38f4bc7e0e5df5a1114c140a4ef6f3 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.RoundingMode;
import java.util.*;
public class Main {
static final long MOD1=1000000007;
static final long MOD=998244353;
static final int NTT_MOD1 = 998244353;
static final int NTT_MOD2 = 1053818881;
static final int NTT_MOD3 = 1004535809;
static long MAX = 1000000000000000000l;
public static void main(String[] args){
PrintWriter out = new PrintWriter(System.out);
InputReader sc=new InputReader(System.in);
int t = sc.nextInt();
for (int i = 0; i < t; i++) {
long n = sc.nextLong();
char[] cs = sc.next().toCharArray();
out.println(solve(n, cs));
}
out.flush();
}
static long solve(long n, char[] cs) {
HashSet<Character> set = new HashSet<>();
for (int i = 0; i < cs.length; i++) set.add(cs[i]);
if (set.size()==1) return n;
long dx = n-1;
long dy = n-1;
for (int i = cs.length-1; i >= 0; i--) {
if (cs[i] == 'R') dy--;
else dx--;
}
long ans = 1 + dy * n;
long now = 1;
boolean f = true;
for (int i = 0; i < cs.length; i++) {
if (cs[i] == 'R') {
ans += now;
}else {
if (f) {
f = false;
now += (dx + 1);
ans += (dx + 1);
}else {
ans += 1;
now += 1;
}
}
}
f = true;
now = 0;
for (int i = 0; i < cs.length; i++) {
if (cs[i] == 'R') {
if (f) {
f = false;
ans -= (dy + 1) * now;
}else {
ans -= now;
}
}else {
now++;
}
}
return ans;
}
static class InputReader {
private InputStream in;
private byte[] buffer = new byte[1024];
private int curbuf;
private int lenbuf;
public InputReader(InputStream in) {
this.in = in;
this.curbuf = this.lenbuf = 0;
}
public boolean hasNextByte() {
if (curbuf >= lenbuf) {
curbuf = 0;
try {
lenbuf = in.read(buffer);
} catch (IOException e) {
throw new InputMismatchException();
}
if (lenbuf <= 0)
return false;
}
return true;
}
private int readByte() {
if (hasNextByte())
return buffer[curbuf++];
else
return -1;
}
private boolean isSpaceChar(int c) {
return !(c >= 33 && c <= 126);
}
private void skip() {
while (hasNextByte() && isSpaceChar(buffer[curbuf]))
curbuf++;
}
public boolean hasNext() {
skip();
return hasNextByte();
}
public String next() {
if (!hasNext())
throw new NoSuchElementException();
StringBuilder sb = new StringBuilder();
int b = readByte();
while (!isSpaceChar(b)) {
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
public int nextInt() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public long nextLong() {
if (!hasNext())
throw new NoSuchElementException();
int c = readByte();
while (isSpaceChar(c))
c = readByte();
boolean minus = false;
if (c == '-') {
minus = true;
c = readByte();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res = res * 10 + c - '0';
c = readByte();
} while (!isSpaceChar(c));
return (minus) ? -res : res;
}
public double nextDouble() {
return Double.parseDouble(next());
}
public int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public double[] nextDoubleArray(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++)
a[i] = nextDouble();
return a;
}
public long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public char[][] nextCharMap(int n, int m) {
char[][] map = new char[n][m];
for (int i = 0; i < n; i++)
map[i] = next().toCharArray();
return map;
}
}
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 896e1eabe81a1e8c0e42732dfd248b45 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.util.*;
import java.io.*;
import java.util.HashMap;
import java.util.function.Consumer;
import java.util.Comparator;
public class FirstProject{
public static void main(String[] args) throws IOException{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
PrintWriter out=new PrintWriter(System.out);
int t=Integer.parseInt(in.readLine());
while(t-- > 0) {
//5
//DRDRDRDR
int n=Integer.parseInt(in.readLine());
String s=in.readLine();//8
int down=n-1;//4
int right=n-1;//4
int change=s.length();//8
for(int i=0;i<s.length();i++) {
char c=s.charAt(i);
if(c == 'D') {
down--;
}else {
right--;
}
if(change == s.length() && i > 0 && c != s.charAt(i-1)) {
change=i;
}
}
long res=change + 1;
if(s.charAt(0) == 'D') {
res+=down;
}else {
res+=right;
}
for(int i = change; i < s.length(); i++) {
if(i == s.length() - 1) {
res += (long)(down + 1) * (right + 1);
}else {
char next = s.charAt(i + 1);
if(next == 'D') {
res += right + 1;
}else {
res += down + 1;
}
}
}
System.out.println(res);
}
}
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | e66f9b26d8dcfd279d69570c098bc226 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | //some updates in import stuff
import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
static int mod = (int) (Math.pow(10, 9)+7);
static final int dx[] = { -1, 0, 1, 0 }, dy[] = { 0, -1, 0, 1 };
static final int[] dx8 = { -1, -1, -1, 0, 0, 1, 1, 1 }, dy8 = { -1, 0, 1, -1, 1, -1, 0, 1 };
static final int[] dx9 = { -1, -1, -1, 0, 0, 0, 1, 1, 1 }, dy9 = { -1, 0, 1, -1, 0, 1, -1, 0, 1 };
static final double eps = 1e-10;
static List<Integer> primeNumbers = new ArrayList<>();
public static void main(String[] args) {
MyScanner sc = new MyScanner();
out = new PrintWriter(new BufferedOutputStream(System.out));
//we haven't yet handled edges cases for your knowledge bois
//so do remember about that bad boi!!!
//this code might work, but i am not very sure about it!!! (so think about it)
//code below
int test = sc.nextInt();
while(test --> 0){
long n = sc.nextInt(); //some basic logic for sure
char[] inp = sc.nextLine().toCharArray(); //this pretty basic
//now count on left, count on right!!! (for sure)
long right = 0;
long down = 0;
long total = n * n;
//this is basic char loop for sure
for(char ch : inp){
if(ch == 'R') right++; else down++;
}
//edge case
if(right == 0 || down == 0){
//simple answer output
out.println(n);
continue;
}
//now figuring out something else (no bad feelings, i just don't code in that language for sure)
long nonReachable = n-1;
long lastA = n-1;
long cR = 0;
long cD = 0;
for(int i = 0; i < inp.length; i++){
if(inp[i] == 'R') cR++; else cD++; //simple basic logic
if(i != (inp.length - 1) && inp[i] == inp[i+1]){
nonReachable += lastA;
continue;
}
//now for the current one add to the answer pls
if(inp[i] == 'R'){
lastA = right - cR;
}else{
lastA = down - cD;
}
nonReachable += lastA;
}
out.println(total - nonReachable);
}
out.close();
}
//Updation Required
//Fenwick Tree (customisable)
//Segment Tree (customisable)
//-----CURRENTLY PRESENT-------//
//Graph
//DSU
//powerMODe
//power
//Segment Tree (work on this one)
//Prime Sieve
//Count Divisors
//Next Permutation
//Get NCR
//isVowel
//Sort (int)
//Sort (long)
//Binomial Coefficient
//Pair
//Triplet
//lcm (int & long)
//gcd (int & long)
//gcd (for binomial coefficient)
//swap (int & char)
//reverse
//primeExponentCounts
//Fast input and output
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//-------------------------------------------------------------------
//GRAPH (basic structure)
public static class Graph{
public int V;
public ArrayList<ArrayList<Integer>> edges;
//2 -> [0,1,2] (current)
Graph(int V){
this.V = V;
edges = new ArrayList<>(V+1);
for(int i= 0; i <= V; i++){
edges.add(new ArrayList<>());
}
}
public void addEdge(int from , int to){
edges.get(from).add(to);
edges.get(to).add(from);
}
}
//DSU (path and rank optimised)
public static class DisjointUnionSets {
int[] rank, parent;
int n;
public DisjointUnionSets(int n)
{
rank = new int[n];
parent = new int[n];
Arrays.fill(rank, 1);
Arrays.fill(parent,-1);
this.n = n;
}
public int find(int curr){
if(parent[curr] == -1)
return curr;
//path compression optimisation
return parent[curr] = find(parent[curr]);
}
public void union(int a, int b){
int s1 = find(a);
int s2 = find(b);
if(s1 != s2){
if(rank[s1] < rank[s2]){
parent[s1] = s2;
rank[s2] += rank[s1];
}else{
parent[s2] = s1;
rank[s1] += rank[s2];
}
}
}
}
//with mod
public static long powerMOD(long x, long y)
{
long res = 1L;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0){
x %= mod;
res %= mod;
res = (res * x)%mod;
}
// y must be even now
y = y >> 1; // y = y/2
x%= mod;
x = (x * x)%mod; // Change x to x^2
}
return res%mod;
}
//without mod
public static long power(long x, long y)
{
long res = 1L;
while (y > 0)
{
// If y is odd, multiply x with result
if ((y & 1) != 0){
res = (res * x);
}
// y must be even now
y = y >> 1; // y = y/
x = (x * x);
}
return res;
}
public static class segmentTree{
public long[] arr;
public long[] tree;
public long[] lazy;
segmentTree(long[] array){
int n = array.length;
arr = new long[n];
for(int i= 0; i < n; i++) arr[i] = array[i];
tree = new long[4*n + 1];
lazy = new long[4*n + 1];
}
public void build(int[]arr, int s, int e, int[] tree, int index){
if(s == e){
tree[index] = arr[s];
return;
}
//otherwise divide in two parts and fill both sides simply
int mid = (s+e)/2;
build(arr, s, mid, tree, 2*index);
build(arr, mid+1, e, tree, 2*index+1);
//who will build the current position dude
tree[index] = Math.min(tree[2*index], tree[2*index+1]);
}
public int query(int sr, int er, int sc, int ec, int index, int[] tree){
if(lazy[index] != 0){
tree[index] += lazy[index];
if(sc != ec){
lazy[2*index+1] += lazy[index];
lazy[2*index] += lazy[index];
}
lazy[index] = 0;
}
//no overlap
if(sr > ec || sc > er) return Integer.MAX_VALUE;
//found the index baby
if(sr <= sc && ec <= er) return tree[index];
//finding the index on both sides hehehehhe
int mid = (sc + ec)/2;
int left = query(sr, er, sc, mid, 2*index, tree);
int right = query(sr, er, mid+1, ec, 2*index + 1, tree);
return Integer.min(left, right);
}
//now we will do point update implementation
//it should be simple then we expected for sure
public void update(int index, int indexr, int increment, int[] tree, int s, int e){
if(lazy[index] != 0){
tree[index] += lazy[index];
if(s != e){
lazy[2*index+1] = lazy[index];
lazy[2*index] = lazy[index];
}
lazy[index] = 0;
}
//no overlap
if(indexr < s || indexr > e) return;
//found the required index
if(s == e){
tree[index] += increment;
return;
}
//search for the index on both sides
int mid = (s+e)/2;
update(2*index, indexr, increment, tree, s, mid);
update(2*index+1, indexr, increment, tree, mid+1, e);
//now update the current range simply
tree[index] = Math.min(tree[2*index+1], tree[2*index]);
}
public void rangeUpdate(int[] tree , int index, int s, int e, int sr, int er, int increment){
//if not at all in the same range
if(e < sr || er < s) return;
//complete then also move forward
if(s == e){
tree[index] += increment;
return;
}
//otherwise move in both subparts
int mid = (s+e)/2;
rangeUpdate(tree, 2*index, s, mid, sr, er, increment);
rangeUpdate(tree, 2*index + 1, mid+1, e, sr, er, increment);
//update current range too na
//i always forget this step for some reasons hehehe, idiot
tree[index] = Math.min(tree[2*index], tree[2*index + 1]);
}
public void rangeUpdateLazy(int[] tree, int index, int s, int e, int sr, int er, int increment){
//update lazy values
//resolve lazy value before going down
if(lazy[index] != 0){
tree[index] += lazy[index];
if(s != e){
lazy[2*index+1] += lazy[index];
lazy[2*index] += lazy[index];
}
lazy[index] = 0;
}
//no overlap case
if(sr > e || s > er) return;
//complete overlap
if(sr <= s && er >= e){
tree[index] += increment;
if(s != e){
lazy[2*index+1] += increment;
lazy[2*index] += increment;
}
return;
}
//otherwise go on both left and right side and do your shit
int mid = (s + e)/2;
rangeUpdateLazy(tree, 2*index, s, mid, sr, er, increment);
rangeUpdateLazy(tree, 2*index + 1, mid+1, e, sr, er, increment);
tree[index] = Math.min(tree[2*index+1], tree[2*index]);
return;
}
}
//prime sieve
public static void primeSieve(int n){
BitSet bitset = new BitSet(n+1);
for(long i = 0; i < n ; i++){
if (i == 0 || i == 1) {
bitset.set((int) i);
continue;
}
if(bitset.get((int) i)) continue;
primeNumbers.add((int)i);
for(long j = i; j <= n ; j+= i)
bitset.set((int)j);
}
}
//number of divisors
public static int countDivisors(long number){
if(number == 1) return 1;
List<Integer> primeFactors = new ArrayList<>();
int index = 0;
long curr = primeNumbers.get(index);
while(curr * curr <= number){
while(number % curr == 0){
number = number/curr;
primeFactors.add((int) curr);
}
index++;
curr = primeNumbers.get(index);
}
if(number != 1) primeFactors.add((int) number);
int current = primeFactors.get(0);
int totalDivisors = 1;
int currentCount = 2;
for (int i = 1; i < primeFactors.size(); i++) {
if (primeFactors.get(i) == current) {
currentCount++;
} else {
totalDivisors *= currentCount;
currentCount = 2;
current = primeFactors.get(i);
}
}
totalDivisors *= currentCount;
return totalDivisors;
}
//primeExponentCounts
public static int primeExponentsCount(int n) {
if (n <= 1)
return 0;
int sqrt = (int) Math.sqrt(n);
int remainingNumber = n;
int result = 0;
for (int i = 2; i <= sqrt; i++) {
while (remainingNumber % i == 0) {
result++;
remainingNumber /= i;
}
}
//in case of prime numbers this would happen
if (remainingNumber > 1) {
result++;
}
return result;
}
//now adding next permutation function to java hehe
public static boolean next_permutation(int[] p) {
for (int a = p.length - 2; a >= 0; --a)
if (p[a] < p[a + 1])
for (int b = p.length - 1;; --b)
if (p[b] > p[a]) {
int t = p[a];
p[a] = p[b];
p[b] = t;
for (++a, b = p.length - 1; a < b; ++a, --b) {
t = p[a];
p[a] = p[b];
p[b] = t;
}
return true;
}
return false;
}
//finding the value of NCR in O(RlogN) time and O(1) space
public static long getNcR(int n, int r)
{
long p = 1, k = 1;
if (n - r < r) r = n - r;
if (r != 0) {
while (r > 0) {
p *= n;
k *= r;
long m = __gcd(p, k);
p /= m;
k /= m;
n--;
r--;
}
}
else {
p = 1;
}
return p;
}
//is vowel function
public static boolean isVowel(char c)
{
return (c=='a' || c=='A' || c=='e' || c=='E' || c=='i' || c=='I' || c=='o' || c=='O' || c=='u' || c=='U');
}
//to sort the array with better method
public static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//sort long
public static void sort(long[] a) {
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
//for calculating binomialCoeff
public static int binomialCoeff(int n, int k)
{
int C[] = new int[k + 1];
// nC0 is 1
C[0] = 1;
for (int i = 1; i <= n; i++) {
// Compute next row of pascal
// triangle using the previous row
for (int j = Math.min(i, k); j > 0; j--)
C[j] = C[j] + C[j - 1];
}
return C[k];
}
//Pair with int int
public static class Pair{
public int a;
public int b;
Pair(int a , int b){
this.a = a;
this.b = b;
}
@Override
public String toString(){
return a + " -> " + b;
}
}
//Triplet with int int int
public static class Triplet{
public int a;
public int b;
public int c;
Triplet(int a , int b, int c){
this.a = a;
this.b = b;
this.c = c;
}
@Override
public String toString(){
return a + " -> " + b;
}
}
//Shortcut function
public static long lcm(long a , long b){
return a * (b/gcd(a,b));
}
//let's make one for calculating lcm basically
public static int lcm(int a , int b){
return (a * b)/gcd(a,b);
}
//int version for gcd
public static int gcd(int a, int b){
if(b == 0)
return a;
return gcd(b , a%b);
}
//long version for gcd
public static long gcd(long a, long b){
if(b == 0)
return a;
return gcd(b , a%b);
}
//for ncr calculator(ignore this code)
public static long __gcd(long n1, long n2)
{
long gcd = 1;
for (int i = 1; i <= n1 && i <= n2; ++i) {
// Checks if i is factor of both integers
if (n1 % i == 0 && n2 % i == 0) {
gcd = i;
}
}
return gcd;
}
//swapping two elements in an array
public static void swap(int[] arr, int left , int right){
int temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
//for char array
public static void swap(char[] arr, int left , int right){
char temp = arr[left];
arr[left] = arr[right];
arr[right] = temp;
}
//reversing an array
public static void reverse(int[] arr){
int left = 0;
int right = arr.length-1;
while(left <= right){
swap(arr, left,right);
left++;
right--;
}
}
public static long expo(long a, long b, long mod) {
long res = 1;
while (b > 0) {
if ((b & 1) == 1L) res = (res * a) % mod; //think about this one for a second
a = (a * a) % mod;
b = b >> 1;
}
return res;
}
//SOME EXTRA DOPE FUNCTIONS
public static long mminvprime(long a, long b) {
return expo(a, b - 2, b);
}
public static long mod_add(long a, long b, long m) {
a = a % m;
b = b % m;
return (((a + b) % m) + m) % m;
}
public static long mod_sub(long a, long b, long m) {
a = a % m;
b = b % m;
return (((a - b) % m) + m) % m;
}
public static long mod_mul(long a, long b, long m) {
a = a % m;
b = b % m;
return (((a * b) % m) + m) % m;
}
public static long mod_div(long a, long b, long m) {
a = a % m;
b = b % m;
return (mod_mul(a, mminvprime(b, m), m) + m) % m;
}
//O(n) every single time remember that
public static long nCr(long N, long K , long mod){
long upper = 1L;
long lower = 1L;
long lowerr = 1L;
for(long i = 1; i <= N; i++){
upper = mod_mul(upper, i, mod);
}
for(long i = 1; i <= K; i++){
lower = mod_mul(lower, i, mod);
}
for(long i = 1; i <= (N - K); i++){
lowerr = mod_mul(lowerr, i, mod);
}
// out.println(upper + " " + lower + " " + lowerr);
long answer = mod_mul(lower, lowerr, mod);
answer = mod_div(upper, answer, mod);
return answer;
}
// long[] fact = new long[2 * n + 1];
// long[] ifact = new long[2 * n + 1];
// fact[0] = 1;
// ifact[0] = 1;
// for (long i = 1; i <= 2 * n; i++)
// {
// fact[(int)i] = mod_mul(fact[(int)i - 1], i, mod);
// ifact[(int)i] = mminvprime(fact[(int)i], mod);
// }
//ifact is basically inverse factorial in here!!!!!(imp)
public static long combination(long n, long r, long m, long[] fact, long[] ifact) {
long val1 = fact[(int)n];
long val2 = ifact[(int)(n - r)];
long val3 = ifact[(int)r];
return (((val1 * val2) % m) * val3) % m;
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
//--------------------------------------------------------
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | f7877677780b0e5c489b890632902cf5 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.*;
import java.util.Arrays;
import java.util.Random;
import java.util.StringTokenizer;
public class codeforces_Edu123_E {
private static void solve(FastIOAdapter in, PrintWriter out) {
int n = in.nextInt();
char[] s = in.next().toCharArray();
int x = 1, y = 1;
for (int i = 0; i < s.length; i++) {
if (s[i] == 'R') y++;
else x++;
}
int len1 = 1, len2 = 0;
boolean gl1 = true;
for (int i = 1; i < s.length; i++) {
if (gl1) {
if (s[i] == s[i - 1]) len1++;
else {
gl1 = false;
len2 = 1;
}
} else {
if (s[i] == s[i - 1]) len2++;
else break;
}
}
long ans = s.length + 1;
if (s[0] == 'R') {
ans += (long) x * (n - y);
} else {
ans += (long) y * (n - x);
}
if (len2 != 0) {
if (s[0] == 'R') {
ans += (long) (y - len1) * (n - x);
} else ans += (long) (x - len1) * (n - y);
ans += (long) (n - x) * (n - y);
}
out.println(ans);
}
public static void main(String[] args) throws Exception {
try (FastIOAdapter ioAdapter = new FastIOAdapter()) {
int count = 1;
count = ioAdapter.nextInt();
while (count-- > 0) {
solve(ioAdapter, ioAdapter.out);
}
}
}
static void ruffleSort(int[] arr) {
int n = arr.length;
Random rnd = new Random();
for (int i = 0; i < n; ++i) {
int tmp = arr[i];
int randomPos = i + rnd.nextInt(n - i);
arr[i] = arr[randomPos];
arr[randomPos] = tmp;
}
Arrays.sort(arr);
}
static class FastIOAdapter implements AutoCloseable {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter((System.out))));
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() {
try {
return br.readLine();
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
@Override
public void close() throws Exception {
out.flush();
out.close();
br.close();
}
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | ed1b7c00088f732031545fc9bf6113cc | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | //package expandthepath;
import java.util.*;
import java.io.*;
public class expandthepath {
public static void main(String[] args) throws IOException {
BufferedReader fin = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(fin.readLine());
StringBuilder fout = new StringBuilder();
while(t-- > 0) {
int n = Integer.parseInt(fin.readLine());
char[] s = fin.readLine().toCharArray();
boolean noBrush = true;
long hSize = 0;
char first = s[0];
for(int i = 0; i < s.length; i++) {
if(s[i] != first) {
noBrush = false;
break;
}
hSize ++;
}
if(noBrush) {
fout.append(n).append("\n");
continue;
}
long bWidth = 1;
long bHeight = 1;
for(int i = (int) hSize; i < s.length; i++) {
if(s[i] == 'D') {
bHeight ++;
}
else {
bWidth ++;
}
}
long mArea = n * (n - hSize);
long ans = mArea - (bWidth * bHeight) + (bWidth + bHeight - 1) + hSize;
fout.append(ans).append("\n");
}
System.out.print(fout);
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 8075625e4149364db8fcc5929fcd1e3f | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | // JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.*;
import java.lang.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.io.*;
public class Eshan {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter out = new PrintWriter(System.out);
static DecimalFormat df = new DecimalFormat("0.0000000");
final static int mod = (int) (1e9 + 7);
final static int MAX = Integer.MAX_VALUE;
final static int MIN = Integer.MIN_VALUE;
static Random rand = new Random();
// ======================= MAIN ==================================
public static void main(String[] args) throws IOException {
long time = System.currentTimeMillis();
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
// ==== start ====
int t = readInt();
// int t = 1;
preprocess();
while (t-- > 0) {
solve();
}
out.flush();
// ==== end ====
if (!oj)
System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
}
private static void solve() throws IOException {
int n = readInt();
String str = readLine();
int len = str.length(), d = n, r = n, diff = 0;
long ans = 0L;
for (int i = 1; i < len; i++) {
if (str.charAt(i) != str.charAt(i - 1)) {
diff = i;
break;
}
}
if (diff == 0) {
out.println(n);
return;
}
for (int i = 0; i < diff; i++) {
ans++;
if (str.charAt(i) == 'R')
d--;
else
r--;
}
for (int i = diff + 1; i < len; i++) {
if (str.charAt(i) == 'R')
d--;
else
r--;
}
ans += 1L * r * d;
if (str.charAt(diff) == 'R')
d--;
else
r--;
for (int i = diff + 1; i < len; i++) {
if (str.charAt(i) == 'R')
ans += r;
else
ans += d;
}
out.println(ans);
}
private static void preprocess() {
}
// ==================== CUSTOM CLASSES ================================
static class Pair implements Comparable<Pair> {
int first, second;
Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair o) {
if (this.first != o.first)
return this.second - o.second;
return this.first - o.first;
}
}
static class DequeNode {
DequeNode prev, next;
int val;
DequeNode(int val) {
this.val = val;
}
DequeNode(int val, DequeNode prev, DequeNode next) {
this.val = val;
this.prev = prev;
this.next = next;
}
}
// ======================= FOR INPUT ==================================
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(readLine());
return st.nextToken();
}
static long readLong() throws IOException {
return Long.parseLong(next());
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
static double readDouble() throws IOException {
return Double.parseDouble(next());
}
static char readCharacter() throws IOException {
return next().charAt(0);
}
static String readString() throws IOException {
return next();
}
static String readLine() throws IOException {
return br.readLine().trim();
}
static int[] readIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = readInt();
return arr;
}
static int[][] read2DIntArray(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
arr[i] = readIntArray(m);
return arr;
}
static List<Integer> readIntList(int n) throws IOException {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(readInt());
return list;
}
static long[] readLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = readLong();
return arr;
}
static long[][] read2DLongArray(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++)
arr[i] = readLongArray(m);
return arr;
}
static List<Long> readLongList(int n) throws IOException {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(readLong());
return list;
}
static char[] readCharArray(int n) throws IOException {
return readString().toCharArray();
}
static char[][] readMatrix(int n, int m) throws IOException {
char[][] mat = new char[n][m];
for (int i = 0; i < n; i++)
mat[i] = readCharArray(m);
return mat;
}
// ========================= FOR OUTPUT ==================================
private static void printIList(List<Integer> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printLList(List<Long> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printIArray(int[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DIArray(int[][] arr) {
for (int i = 0; i < arr.length; i++)
printIArray(arr[i]);
}
private static void printLArray(long[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DLArray(long[][] arr) {
for (int i = 0; i < arr.length; i++)
printLArray(arr[i]);
}
// ====================== TO CHECK IF STRING IS NUMBER ========================
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static boolean isLong(String s) {
try {
Long.parseLong(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
// ==================== FASTER SORT ================================
private static void sort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void sort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
// ==================== MATHEMATICAL FUNCTIONS ===========================
private static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static long power(long a, long b) {
if (b == 0)
return 1L;
long ans = power(a, b >> 1);
ans *= ans;
if ((b & 1) == 1)
ans *= a;
return ans;
}
private static int mod_power(int a, int b) {
if (b == 0)
return 1;
int temp = mod_power(a, b >> 1);
temp %= mod;
temp = (int) ((1L * temp * temp) % mod);
if ((b & 1) == 1)
temp = (int) ((1L * temp * a) % mod);
return temp;
}
private static int multiply(int a, int b) {
return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod);
}
private static boolean isPrime(long n) {
for (long i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
private static long nCr(long n, long r) {
if (n - r > r)
r = n - r;
long ans = 1L;
for (long i = r + 1; i <= n; i++)
ans *= i;
for (long i = 2; i <= n - r; i++)
ans /= i;
return ans;
}
// ==================== Primes using Seive =====================
private static List<Integer> getPrimes(int n) {
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i)
prime[j] = false;
}
}
// return prime;
List<Integer> list = new ArrayList<>();
for (int i = 2; i <= n; i++)
if (prime[i])
list.add(i);
return list;
}
private static int[] SeivePrime(int n) {
int[] primes = new int[n];
for (int i = 0; i < n; i++)
primes[i] = i;
for (int i = 2; i * i < n; i++) {
if (primes[i] != i)
continue;
for (int j = i * i; j < n; j += i) {
if (primes[j] == j)
primes[j] = i;
}
}
return primes;
}
// ==================== STRING FUNCTIONS ================================
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
private static String sortString(String str) {
int[] arr = new int[256];
for (char ch : str.toCharArray())
arr[ch]++;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 256; i++)
while (arr[i]-- > 0)
sb.append((char) i);
return sb.toString();
}
// ==================== LIS & LNDS ================================
private static int LIS(int arr[], int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find1(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find1(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) >= val) {
ret = mid;
j = mid - 1;
} else {
i = mid + 1;
}
}
return ret;
}
private static int LNDS(int[] arr, int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find2(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find2(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) <= val) {
i = mid + 1;
} else {
ret = mid;
j = mid - 1;
}
}
return ret;
}
// =============== Lower Bound & Upper Bound ===========
// less than or equal
private static int lower_bound(List<Integer> list, int val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(List<Long> list, long val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(int[] arr, int val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(long[] arr, long val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
// greater than or equal
private static int upper_bound(List<Integer> list, int val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(List<Long> list, long val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(int[] arr, int val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(long[] arr, long val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
// ==================== UNION FIND =====================
private static int find(int x, int[] parent) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x], parent);
}
private static boolean union(int x, int y, int[] parent, int[] rank) {
int lx = find(x, parent), ly = find(y, parent);
if (lx == ly)
return false;
if (rank[lx] > rank[ly])
parent[ly] = lx;
else if (rank[lx] < rank[ly])
parent[lx] = ly;
else {
parent[lx] = ly;
rank[ly]++;
}
return true;
}
// ==================== SEGMENT TREE (RANGE SUM) =====================
public static class SegmentTree {
int n;
int[] arr, tree, lazy;
SegmentTree(int arr[]) {
this.arr = arr;
this.n = arr.length;
this.tree = new int[(n << 2)];
this.lazy = new int[(n << 2)];
build(1, 0, n - 1);
}
void build(int id, int start, int end) {
if (start == end)
tree[id] = arr[start];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
build(left, start, mid);
build(right, mid + 1, end);
tree[id] = tree[left] + tree[right];
}
}
void update(int l, int r, int val) {
update(1, 0, n - 1, l, r, val);
}
void update(int id, int start, int end, int l, int r, int val) {
distribute(id, start, end);
if (end < l || r < start)
return;
if (start == end)
tree[id] += val;
else if (l <= start && end <= r) {
lazy[id] += val;
distribute(id, start, end);
} else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
update(left, start, mid, l, r, val);
update(right, mid + 1, end, l, r, val);
tree[id] = tree[left] + tree[right];
}
}
int query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
int query(int id, int start, int end, int l, int r) {
if (end < l || r < start)
return 0;
distribute(id, start, end);
if (start == end)
return tree[id];
else if (l <= start && end <= r)
return tree[id];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r);
}
}
void distribute(int id, int start, int end) {
if (start == end)
tree[id] += lazy[id];
else {
tree[id] += lazy[id] * (end - start + 1);
lazy[(id << 1)] += lazy[id];
lazy[(id << 1) + 1] += lazy[id];
}
lazy[id] = 0;
}
}
// ==================== TRIE ================================
static class Trie {
class Node {
Node[] children;
boolean isEnd;
Node() {
children = new Node[26];
}
}
Node root;
Trie() {
root = new Node();
}
public void insert(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
curr.children[ch - 'a'] = new Node();
curr = curr.children[ch - 'a'];
}
curr.isEnd = true;
}
public boolean find(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
return false;
curr = curr.children[ch - 'a'];
}
return curr.isEnd;
}
}
// ==================== FENWICK TREE ================================
static class FT {
long[] tree;
int n;
FT(int[] arr, int n) {
this.n = n;
this.tree = new long[n + 1];
for (int i = 1; i <= n; i++) {
update(i, arr[i - 1]);
}
}
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += idx & -idx;
}
}
long query(int l, int r) {
return getSum(r) - getSum(l - 1);
}
long getSum(int idx) {
long ans = 0L;
while (idx > 0) {
ans += tree[idx];
idx -= idx & -idx;
}
return ans;
}
}
// ==================== BINARY INDEX TREE ================================
static class BIT {
long[][] tree;
int n, m;
BIT(int[][] mat, int n, int m) {
this.n = n;
this.m = m;
tree = new long[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
update(i, j, mat[i - 1][j - 1]);
}
}
}
void update(int x, int y, int val) {
while (x <= n) {
int t = y;
while (t <= m) {
tree[x][t] += val;
t += t & -t;
}
x += x & -x;
}
}
long query(int x1, int y1, int x2, int y2) {
return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1);
}
long getSum(int x, int y) {
long ans = 0L;
while (x > 0) {
int t = y;
while (t > 0) {
ans += tree[x][t];
t -= t & -t;
}
x -= x & -x;
}
return ans;
}
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 5a3d002a10d16fa01515e8eda18507f0 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | // JAI SHREE RAM, HAR HAR MAHADEV, HARE KRISHNA
import java.util.*;
import java.util.Map.Entry;
import java.util.stream.*;
import java.lang.*;
import java.math.BigInteger;
import java.text.DecimalFormat;
import java.io.*;
public class Eshan {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static PrintWriter out = new PrintWriter(System.out);
static DecimalFormat df = new DecimalFormat("0.0000000");
final static int mod = (int) (1e9 + 7);
final static int MAX = Integer.MAX_VALUE;
final static int MIN = Integer.MIN_VALUE;
static Random rand = new Random();
// ======================= MAIN ==================================
public static void main(String[] args) throws IOException {
long time = System.currentTimeMillis();
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
// ==== start ====
int t = readInt();
// int t = 1;
preprocess();
while (t-- > 0) {
solve();
}
out.flush();
// ==== end ====
if (!oj)
System.out.println(Arrays.deepToString(new Object[] { System.currentTimeMillis() - time + " ms" }));
}
private static void solve() throws IOException {
int n = readInt();
String str = readLine();
int len = str.length(), d = n, r = n, diff = 0;
long ans = 0L;
for (int i = 1; i < len; i++) {
if (str.charAt(i) != str.charAt(i - 1)) {
diff = i;
break;
}
}
if (diff == 0) {
out.println(n);
return;
}
for (int i = 0; i < diff; i++) {
ans++;
if (str.charAt(i) == 'R')
d--;
else
r--;
}
for (int i = diff + 1; i < len; i++) {
if (str.charAt(i) == 'R')
d--;
else
r--;
}
ans += 1L * r * d;
if (str.charAt(diff) == 'R')
d--;
else
r--;
for (int i = diff + 1; i < len; i++) {
if (str.charAt(i) == 'R')
ans += r;
else
ans += d;
}
out.println(ans);
}
private static void preprocess() {
}
// ==================== CUSTOM CLASSES ================================
static class Pair implements Comparable<Pair> {
int first, second;
Pair(int first, int second) {
this.first = first;
this.second = second;
}
public int compareTo(Pair o) {
if (this.first != o.first)
return this.second - o.second;
return this.first - o.first;
}
}
static class DequeNode {
DequeNode prev, next;
int val;
DequeNode(int val) {
this.val = val;
}
DequeNode(int val, DequeNode prev, DequeNode next) {
this.val = val;
this.prev = prev;
this.next = next;
}
}
// ======================= FOR INPUT ==================================
static String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(readLine());
return st.nextToken();
}
static long readLong() throws IOException {
return Long.parseLong(next());
}
static int readInt() throws IOException {
return Integer.parseInt(next());
}
static double readDouble() throws IOException {
return Double.parseDouble(next());
}
static char readCharacter() throws IOException {
return next().charAt(0);
}
static String readString() throws IOException {
return next();
}
static String readLine() throws IOException {
return br.readLine().trim();
}
static int[] readIntArray(int n) throws IOException {
int[] arr = new int[n];
for (int i = 0; i < n; i++)
arr[i] = readInt();
return arr;
}
static int[][] read2DIntArray(int n, int m) throws IOException {
int[][] arr = new int[n][m];
for (int i = 0; i < n; i++)
arr[i] = readIntArray(m);
return arr;
}
static List<Integer> readIntList(int n) throws IOException {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(readInt());
return list;
}
static long[] readLongArray(int n) throws IOException {
long[] arr = new long[n];
for (int i = 0; i < n; i++)
arr[i] = readLong();
return arr;
}
static long[][] read2DLongArray(int n, int m) throws IOException {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++)
arr[i] = readLongArray(m);
return arr;
}
static List<Long> readLongList(int n) throws IOException {
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(readLong());
return list;
}
static char[] readCharArray(int n) throws IOException {
return readString().toCharArray();
}
static char[][] readMatrix(int n, int m) throws IOException {
char[][] mat = new char[n][m];
for (int i = 0; i < n; i++)
mat[i] = readCharArray(m);
return mat;
}
// ========================= FOR OUTPUT ==================================
private static void printIList(List<Integer> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printLList(List<Long> list) {
for (int i = 0; i < list.size(); i++)
out.print(list.get(i) + " ");
out.println(" ");
}
private static void printIArray(int[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DIArray(int[][] arr) {
for (int i = 0; i < arr.length; i++)
printIArray(arr[i]);
}
private static void printLArray(long[] arr) {
for (int i = 0; i < arr.length; i++)
out.print(arr[i] + " ");
out.println(" ");
}
private static void print2DLArray(long[][] arr) {
for (int i = 0; i < arr.length; i++)
printLArray(arr[i]);
}
// ====================== TO CHECK IF STRING IS NUMBER ========================
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static boolean isLong(String s) {
try {
Long.parseLong(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
// ==================== FASTER SORT ================================
private static void sort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(int[] arr) {
int n = arr.length;
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void sort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list);
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
private static void reverseSort(long[] arr) {
int n = arr.length;
List<Long> list = new ArrayList<>();
for (int i = 0; i < n; i++)
list.add(arr[i]);
Collections.sort(list, Collections.reverseOrder());
for (int i = 0; i < n; i++)
arr[i] = list.get(i);
}
// ==================== MATHEMATICAL FUNCTIONS ===========================
private static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static int lcm(int a, int b) {
return (a / gcd(a, b)) * b;
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static long power(long a, long b) {
if (b == 0)
return 1L;
long ans = power(a, b >> 1);
ans *= ans;
if ((b & 1) == 1)
ans *= a;
return ans;
}
private static int mod_power(int a, int b) {
if (b == 0)
return 1;
int temp = mod_power(a, b >> 1);
temp %= mod;
temp = (int) ((1L * temp * temp) % mod);
if ((b & 1) == 1)
temp = (int) ((1L * temp * a) % mod);
return temp;
}
private static int multiply(int a, int b) {
return (int) ((((1L * a) % mod) * ((1L * b) % mod)) % mod);
}
private static boolean isPrime(long n) {
for (long i = 2; i * i <= n; i++) {
if (n % i == 0)
return false;
}
return true;
}
private static long nCr(long n, long r) {
if (n - r > r)
r = n - r;
long ans = 1L;
for (long i = r + 1; i <= n; i++)
ans *= i;
for (long i = 2; i <= n - r; i++)
ans /= i;
return ans;
}
// ==================== Primes using Seive =====================
private static List<Integer> getPrimes(int n) {
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
for (int i = 2; i * i <= n; i++) {
if (prime[i]) {
for (int j = i * i; j <= n; j += i)
prime[j] = false;
}
}
// return prime;
List<Integer> list = new ArrayList<>();
for (int i = 2; i <= n; i++)
if (prime[i])
list.add(i);
return list;
}
private static int[] SeivePrime(int n) {
int[] primes = new int[n];
for (int i = 0; i < n; i++)
primes[i] = i;
for (int i = 2; i * i < n; i++) {
if (primes[i] != i)
continue;
for (int j = i * i; j < n; j += i) {
if (primes[j] == j)
primes[j] = i;
}
}
return primes;
}
// ==================== STRING FUNCTIONS ================================
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
private static String sortString(String str) {
int[] arr = new int[256];
for (char ch : str.toCharArray())
arr[ch]++;
StringBuilder sb = new StringBuilder();
for (int i = 0; i < 256; i++)
while (arr[i]-- > 0)
sb.append((char) i);
return sb.toString();
}
// ==================== LIS & LNDS ================================
private static int LIS(int arr[], int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find1(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find1(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) >= val) {
ret = mid;
j = mid - 1;
} else {
i = mid + 1;
}
}
return ret;
}
private static int LNDS(int[] arr, int n) {
List<Integer> list = new ArrayList<>();
for (int i = 0; i < n; i++) {
int idx = find2(list, arr[i]);
if (idx < list.size())
list.set(idx, arr[i]);
else
list.add(arr[i]);
}
return list.size();
}
private static int find2(List<Integer> list, int val) {
int ret = list.size(), i = 0, j = list.size() - 1;
while (i <= j) {
int mid = (i + j) / 2;
if (list.get(mid) <= val) {
i = mid + 1;
} else {
ret = mid;
j = mid - 1;
}
}
return ret;
}
// =============== Lower Bound & Upper Bound ===========
// less than or equal
private static int lower_bound(List<Integer> list, int val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(List<Long> list, long val) {
int ans = -1, lo = 0, hi = list.size() - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(int[] arr, int val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
private static int lower_bound(long[] arr, long val) {
int ans = -1, lo = 0, hi = arr.length - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] <= val) {
ans = mid;
lo = mid + 1;
} else {
hi = mid - 1;
}
}
return ans;
}
// greater than or equal
private static int upper_bound(List<Integer> list, int val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(List<Long> list, long val) {
int ans = list.size(), lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (list.get(mid) >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(int[] arr, int val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
private static int upper_bound(long[] arr, long val) {
int ans = arr.length, lo = 0, hi = ans - 1;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (arr[mid] >= val) {
ans = mid;
hi = mid - 1;
} else {
lo = mid + 1;
}
}
return ans;
}
// ==================== UNION FIND =====================
private static int find(int x, int[] parent) {
if (parent[x] == x)
return x;
return parent[x] = find(parent[x], parent);
}
private static boolean union(int x, int y, int[] parent, int[] rank) {
int lx = find(x, parent), ly = find(y, parent);
if (lx == ly)
return false;
if (rank[lx] > rank[ly])
parent[ly] = lx;
else if (rank[lx] < rank[ly])
parent[lx] = ly;
else {
parent[lx] = ly;
rank[ly]++;
}
return true;
}
// ==================== SEGMENT TREE (RANGE SUM) =====================
public static class SegmentTree {
int n;
int[] arr, tree, lazy;
SegmentTree(int arr[]) {
this.arr = arr;
this.n = arr.length;
this.tree = new int[(n << 2)];
this.lazy = new int[(n << 2)];
build(1, 0, n - 1);
}
void build(int id, int start, int end) {
if (start == end)
tree[id] = arr[start];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
build(left, start, mid);
build(right, mid + 1, end);
tree[id] = tree[left] + tree[right];
}
}
void update(int l, int r, int val) {
update(1, 0, n - 1, l, r, val);
}
void update(int id, int start, int end, int l, int r, int val) {
distribute(id, start, end);
if (end < l || r < start)
return;
if (start == end)
tree[id] += val;
else if (l <= start && end <= r) {
lazy[id] += val;
distribute(id, start, end);
} else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
update(left, start, mid, l, r, val);
update(right, mid + 1, end, l, r, val);
tree[id] = tree[left] + tree[right];
}
}
int query(int l, int r) {
return query(1, 0, n - 1, l, r);
}
int query(int id, int start, int end, int l, int r) {
if (end < l || r < start)
return 0;
distribute(id, start, end);
if (start == end)
return tree[id];
else if (l <= start && end <= r)
return tree[id];
else {
int mid = (start + end) / 2, left = (id << 1), right = left + 1;
return query(left, start, mid, l, r) + query(right, mid + 1, end, l, r);
}
}
void distribute(int id, int start, int end) {
if (start == end)
tree[id] += lazy[id];
else {
tree[id] += lazy[id] * (end - start + 1);
lazy[(id << 1)] += lazy[id];
lazy[(id << 1) + 1] += lazy[id];
}
lazy[id] = 0;
}
}
// ==================== TRIE ================================
static class Trie {
class Node {
Node[] children;
boolean isEnd;
Node() {
children = new Node[26];
}
}
Node root;
Trie() {
root = new Node();
}
public void insert(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
curr.children[ch - 'a'] = new Node();
curr = curr.children[ch - 'a'];
}
curr.isEnd = true;
}
public boolean find(String word) {
Node curr = root;
for (char ch : word.toCharArray()) {
if (curr.children[ch - 'a'] == null)
return false;
curr = curr.children[ch - 'a'];
}
return curr.isEnd;
}
}
// ==================== FENWICK TREE ================================
static class FT {
long[] tree;
int n;
FT(int[] arr, int n) {
this.n = n;
this.tree = new long[n + 1];
for (int i = 1; i <= n; i++) {
update(i, arr[i - 1]);
}
}
void update(int idx, int val) {
while (idx <= n) {
tree[idx] += val;
idx += idx & -idx;
}
}
long query(int l, int r) {
return getSum(r) - getSum(l - 1);
}
long getSum(int idx) {
long ans = 0L;
while (idx > 0) {
ans += tree[idx];
idx -= idx & -idx;
}
return ans;
}
}
// ==================== BINARY INDEX TREE ================================
static class BIT {
long[][] tree;
int n, m;
BIT(int[][] mat, int n, int m) {
this.n = n;
this.m = m;
tree = new long[n + 1][m + 1];
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
update(i, j, mat[i - 1][j - 1]);
}
}
}
void update(int x, int y, int val) {
while (x <= n) {
int t = y;
while (t <= m) {
tree[x][t] += val;
t += t & -t;
}
x += x & -x;
}
}
long query(int x1, int y1, int x2, int y2) {
return getSum(x2, y2) - getSum(x1 - 1, y2) - getSum(x2, y1 - 1) + getSum(x1 - 1, y1 - 1);
}
long getSum(int x, int y) {
long ans = 0L;
while (x > 0) {
int t = y;
while (t > 0) {
ans += tree[x][t];
t -= t & -t;
}
x -= x & -x;
}
return ans;
}
}
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 8f7f73ab2a597ab5d8ce9a28fe35dce3 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | /*
"Everything in the universe is balanced. Every disappointment
you face in life will be balanced by something good for you!
Keep going, never give up."
Just have Patience + 1...
*/
import java.util.*;
import java.lang.*;
import java.io.*;
public class Solution {
public static void main(String[] args) throws java.lang.Exception {
out = new PrintWriter(new BufferedOutputStream(System.out));
sc = new FastReader();
int test = sc.nextInt();
for (int t = 1; t <= test; t++) {
solve();
}
out.close();
}
private static void solve() {
int n = sc.nextInt();
char[] arr = sc.next().toCharArray();
Map<Integer, Integer> mapX = new HashMap<>();
Map<Integer, Integer> mapY = new HashMap<>();
int currRight = 1, currDown = 1;
mapX.put(1, 1);
mapY.put(1, 1);
for (char c : arr) {
if (c == 'D') {
currDown++;
}else {
currRight++;
}
mapX.put(currRight, currDown);
mapY.put(currDown, currRight);
}
int remainingRight = n - currRight;
int remainingDown = n - currDown;
long numberOfCellsRobotVisits = 1 + arr.length;
for (int x : mapX.keySet()) {
if (mapX.get(x) > 1) {
numberOfCellsRobotVisits += Math.min(remainingDown, n - mapX.get(x));
}
}
for (int y : mapY.keySet()) {
if (mapY.get(y) > 1) {
numberOfCellsRobotVisits += Math.min(remainingRight, n - mapY.get(y));
}
}
if (currRight > 1 && currDown > 1) {
numberOfCellsRobotVisits += (long) Math.min(remainingRight, n - currRight) * Math.min(remainingDown, n - currDown);
}
out.println(numberOfCellsRobotVisits);
}
public static FastReader sc;
public static PrintWriter out;
static class FastReader
{
BufferedReader br;
StringTokenizer str;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (str == null || !str.hasMoreElements())
{
try
{
str = new StringTokenizer(br.readLine());
}
catch (IOException lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
}
return str.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 lastMonthOfVacation)
{
lastMonthOfVacation.printStackTrace();
}
return str;
}
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 0b8eb6861483dfe48222491dc02002e2 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.*;
import java.util.*;
public class new1{
static long mod = 1000000007;
public static long gcd(long a, long b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
public static void main(String[] args) throws IOException{
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
FastReader s = new FastReader();
int t = s.nextInt();
for(int z = 0; z < t; z++) {
int n = s.nextInt();
String str = s.next();
int r = 0; int c = 0;
int n1 = str.length();
for(int i = 0; i < n1; i++) {
if(str.charAt(i) == 'D') r++;
else c++;
}
//System.out.println(r + " " + c);
int rem_r = n - r - 1;
int rem_c = n - c - 1;
long ans = 0; boolean rpos = false; boolean cpos = false;
for(int i = 0; i < n1 - 1; i++) {
char pr = str.charAt(i);
char nx = str.charAt(i + 1);
if(pr == 'D') {
rpos = true;
if(pr == nx) {
if(cpos) ans = ans + rem_c;
}
else {
if(rpos) ans = ans + rem_r;
}
}
else {
cpos = true;
if(pr == nx) {
if(rpos) ans = ans + rem_r;
}
else if(cpos) ans = ans + rem_c;
}
ans = ans + 1;
//System.out.println(ans + " " + pr);
}
//System.out.println(r + " " + c + " " + rem_r + " " + rem_c + " " + ans);
if(str.charAt(n1 - 1) == 'D') rpos = true;
else cpos = true;
if(rpos && cpos) ans = ans + ((rem_r + 1L) * (rem_c + 1L));
else if(rpos) ans = ans + rem_r + 1;
else if(cpos) ans = ans + rem_c + 1;
System.out.println(ans + 1 );
}
}
}
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();
}
public int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 0197c975161a56d787c0a0f8c41e4033 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static class Scanner {
Scanner(InputStream in) { this.in = in; } InputStream in;
byte[] bb = new byte[1 << 15]; int i, n;
byte getc() {
if (i == n) {
i = n = 0;
try { n = in.read(bb); } catch (IOException e) {}
}
return i < n ? bb[i++] : 0;
}
int nextInt() {
byte c = 0; while (c <= ' ') c = getc();
boolean negative = false;
if (c == '-') {
negative = true;
c = getc();
}
int a = 0; while (c > ' ') { a = a * 10 + c - '0'; c = getc(); }
if (negative) {
a = -a;
}
return a;
}
long nextLong() {
byte c = 0; while (c <= ' ') c = getc();
long a = 0; while (c > ' ') { a = a * 10 + c - '0'; c = getc(); }
return a;
}
}
public static void main(String[] args) throws Exception {
//Scanner in = new Scanner(System.in);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter out = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.parseInt(in.readLine());
for (int ts=1; ts<=T; ts++) {
int N = Integer.parseInt(in.readLine());
String S = in.readLine();
int rights = 0;
int downs = 0;
for (int i=0; i<S.length(); i++) {
if (S.charAt(i) == 'R')
rights++;
if (S.charAt(i) == 'D')
downs++;
}
if (rights == 0 || downs == 0) {
out.write(N + "\n");
continue;
}
long miss = 0;
boolean right_seen = false;
boolean down_seen = false;
for (int i=0; i<S.length(); i++) {
if (S.charAt(i) == 'D') {
miss += right_seen ? rights : (N-1);
downs--;
down_seen = true;
} else if (S.charAt(i) == 'R') {
miss += down_seen ? downs : (N-1);
rights--;
right_seen = true;
}
}
//out.write(miss + "\n");
out.write((1L * N * N - miss) + "\n");
}
in.close();
out.close();
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | e0bb1d992ef3558853c83672cb7edcfc | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
public class E {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
private static StringTokenizer st;
public static void main(String[] args) throws IOException {
st = new StringTokenizer(reader.readLine());
int T = Integer.parseInt(st.nextToken());
for (int t = 0; t < T; t++) {
st = new StringTokenizer(reader.readLine());
int n = Integer.parseInt(st.nextToken());
st = new StringTokenizer(reader.readLine());
String s = st.nextToken();
int ds = 0;
int rs = 0;
for (char c : s.toCharArray()) {
if (c == 'D') {
ds++;
} else {
rs++;
}
}
long notVisited = (long)(ds + 1) * (rs + 1) - (s.length() + 1);
long result = (long)n * n - notVisited;
if (s.charAt(0) == 'D') {
int i = 1;
while (i < s.length() && s.charAt(i) == 'D') {
i++;
}
if (i == s.length()) {
i = n;
}
result -= (long)i * (n - rs - 1);
} else {
int i = 1;
while (i < s.length() && s.charAt(i) == 'R') {
i++;
}
if (i == s.length()) {
i = n;
}
result -= (long)i * (n - ds - 1);
}
writer.write(Long.toString(result));
writer.newLine();
}
writer.flush();
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | e27137f9175ace016e3c78a835e4e4d0 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.lang.reflect.Array;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;
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);
solve(in, out);
out.close();
}
static String reverse(String s) {
return (new StringBuilder(s)).reverse().toString();
}
static void sieveOfEratosthenes(int n, int factors[], ArrayList<Integer> ar)
{
factors[1]=1;
int p;
for(p = 2; p*p <=n; p++)
{
if(factors[p] == 0)
{
ar.add(p);
factors[p]=p;
for(int i = p*p; i <= n; i += p)
if(factors[i]==0)
factors[i] = p;
}
}
for(;p<=n;p++){
if(factors[p] == 0)
{
factors[p] = p;
ar.add(p);
}
}
}
static void sort(int ar[]) {
int n = ar.length;
ArrayList<Integer> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static void sort1(long ar[]) {
int n = ar.length;
ArrayList<Long> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static void sort2(char ar[]) {
int n = ar.length;
ArrayList<Character> a = new ArrayList<>();
for (int i = 0; i < n; i++)
a.add(ar[i]);
Collections.sort(a);
for (int i = 0; i < n; i++)
ar[i] = a.get(i);
}
static long ncr(long n, long r, long mod) {
if (r == 0)
return 1;
long val = ncr(n - 1, r - 1, mod);
val = (n * val) % mod;
val = (val * modInverse(r, mod)) % mod;
return val;
}
static class SegTree {
long tree[];
long lz[];
long r;
long combine(long a, long b){
return Math.min(a,b);
}
void build(long a[], int v, int tl, int tr) {
if (tl == tr) {
tree[v] = a[tl];
}
else {
int tm = (tl + tr) / 2;
build(a, v*2, tl, tm);
build(a, v*2+1, tm+1, tr);
tree[v] = combine(tree[2*v], tree[2*v+1]);
}
}
void pointUpdate(int v, int tl, int tr, int pos, long val) {
if(tl>pos||tr<pos)
return;
if(tl==pos&&tr==pos){
tree[v] = val;
return;
}
int tm = ((tl + tr) >> 1);
pointUpdate(v*2, tl, tm, pos, val);
pointUpdate(v*2+1, tm+1, tr, pos, val);
tree[v] = combine(tree[2*v],tree[2*v+1]);
}
// void push(int v, int tl, int tr){
// if(tl==tr){
// lz[v] = 0;
// return;
// }
// tree[2*v] += lz[v];
// tree[2*v+1] += lz[v];
// lz[2*v] += lz[v];
// lz[2*v+1] += lz[v];
// lz[v] = 0;
// }
// void rangeUpdate(int v, int tl, int tr, int l, int r, long val) {
// if(tl>r||tr<l)
// return;
// push(v, tl, tr);
// if(tl>=l&&tr<=r){
// tree[v] += val;
// lz[v] += val;
// return;
// }
// int tm = ((tl + tr) >> 1);
// rangeUpdate(v*2, tl, tm, l, r, val);
// rangeUpdate(v*2+1, tm+1, tr, l, r, val);
// tree[v] = combine(tree[2*v],tree[2*v+1]);
// }
long get(int v, int tl, int tr, int l, int r, long val) {
if(l>tr||r<tl||tree[v]>val){
return 0;
}
if (tl == tr) {
tree[v] = Integer.MAX_VALUE;
return 1;
}
int tm = ((tl + tr) >> 1);
long al = get(2*v, tl, tm, l, r, val);
long ar = get(2*v+1, tm+1, tr, l, r, val);
tree[v] = combine(tree[2*v],tree[2*v+1]);
return al+ar;
}
}
static class BIT{
int n;
long tree[];
long operate(long el, long val){
return el+val;
}
void update(int x, long val){
for(;x<n;x+=(x&(-x))){
tree[x] = operate(tree[x], val);
}
}
long get(int x){
long sum = 0;
for(;x>0;x-=(x&(-x))){
sum = operate(sum, tree[x]);
}
return sum;
}
}
static int parent[];
static int rank[];
static long m = 0;
static int find_set(int v) {
if (v == parent[v])
return v;
return find_set(parent[v]);
}
static void make_set(int v) {
parent[v] = v;
rank[v] = 1;
}
static void union_sets(int a, int b) {
a = find_set(a);
b = find_set(b);
if (a != b) {
if (rank[a] < rank[b]){
int tmp = a;
a = b;
b = tmp;
}
parent[b] = a;
// if (rank[a] == rank[b])
// rank[a]++;
rank[a] += rank[b];
max1 = Math.max(max1,rank[a]);
}
}
static int parent1[];
static int rank1[];
static int find_set1(int v) {
if (v == parent1[v])
return v;
return find_set1(parent1[v]);
}
static void make_set1(int v) {
parent1[v] = v;
rank1[v] = 1;
}
static void union_sets1(int a, int b) {
a = find_set1(a);
b = find_set1(b);
if (a != b) {
if (rank1[a] < rank1[b]){
int tmp = a;
a = b;
b = tmp;
}
parent1[b] = a;
// if (rank1[a] == rank1[b])
// rank1[a]++;
rank1[a] += rank1[b];
}
}
static long max1 = 0;
static int count = 0;
static int count1 = 0;
static boolean possible;
public static void solve(InputReader sc, PrintWriter pw){
int i, j = 0;
long mod = 998244353;
// int factors[] = new int[1000001];
// ArrayList<Integer> ar = new ArrayList<>();
// sieveOfEratosthenes(1000000, factors, ar);
// HashSet<Integer> set = new HashSet<>();
// for(int x:ar){
// set.add(x);
// }
// int t = 1;
int t = sc.nextInt();
u: while (t-- > 0) {
int n = sc.nextInt();
char ch[] = sc.next().toCharArray();
int m = ch.length;
int r = 0, d = 0;
for(i=0;i<m;i++){
if(ch[i]=='R')
r++;
else
d++;
}
if(r==0||d==0){
pw.println(n);
continue u;
}
long sum = m+1;
long x = 1, y = 1;
long f = 0;
int r1 = 0, d1 = 0;
for(i=0;i<m;i++){
if(ch[i]=='R'){
x++;
if(f==0||f==1){
r1++;
f = 1;
continue;
}
r1++;
f = 3;
sum += n-(d-d1)-y;
}
else{
y++;
if(f==0||f==2){
d1++;
f = 2;
continue;
}
d1++;
f = 3;
sum += n-(r-r1)-x;
}
}
sum += (n-x+1)*(n-y+1)-1;
pw.println(sum);
}
}
static void KMPSearch(char pat[], char txt[], long pres[]){
int M = pat.length;
int N = txt.length;
int lps[] = new int[M];
int j = 0;
computeLPSArray(pat, M, lps);
int i = 0;
while (i < N) {
if (pat[j] == txt[i]) {
j++;
i++;
}
if (j == M) {
pres[i-1] = 1;
j = lps[j - 1];
}
else if (i < N && pat[j] != txt[i]) {
if (j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
}
static void computeLPSArray(char pat[], int M, int lps[])
{
int len = 0;
int i = 1;
lps[0] = 0;
while (i < M) {
if (pat[i] == pat[len]) {
len++;
lps[i] = len;
i++;
}
else
{
if (len != 0) {
len = lps[len - 1];
}
else
{
lps[i] = len;
i++;
}
}
}
}
static long[][] matrixMult(long a[][], long b[][], long mod){
int n = a.length;
int m = a[0].length;
int p = b[0].length;
long c[][] = new long[n][p];
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
for(int k=0;k<p;k++){
c[i][k] += a[i][j]*b[j][k];
c[i][k] %= mod;
}
}
}
return c;
}
static long[][] exp(long mat[][], long b, long mod){
if(b==0){
int n = mat.length;
long res[][] = new long[n][n];
for(int i=0;i<n;i++){
res[i][i] = 1;
}
return res;
}
long half[][] = exp(mat, b/2, mod);
long res[][] = matrixMult(half, half, mod);
if(b%2==1){
res = matrixMult(res, mat, mod);
}
return res;
}
static void countPrimeFactors(int num, int a[], HashMap<Integer,Integer> pos){
for(int i=2;i*i<num;i++){
if(num%i==0){
int y = pos.get(i);
while(num%i==0){
a[y]++;
num/=i;
}
}
}
if(num>1){
int y = pos.get(num);
a[y]++;
}
}
static void assignAnc(ArrayList<Integer> ar[], int depth[], int sz[], int par[][] ,int curr, int parent, int d){
depth[curr] = d;
sz[curr] = 1;
par[curr][0] = parent;
for(int v:ar[curr]){
if(parent==v)
continue;
assignAnc(ar, depth, sz, par, v, curr, d+1);
sz[curr] += sz[v];
}
}
static int findLCA(int a, int b, int par[][], int depth[]){
if(depth[a]>depth[b]){
a = a^b;
b = a^b;
a = a^b;
}
int diff = depth[b] - depth[a];
for(int i=19;i>=0;i--){
if((diff&(1<<i))>0){
b = par[b][i];
}
}
if(a==b)
return a;
for(int i=19;i>=0;i--){
if(par[b][i]!=par[a][i]){
b = par[b][i];
a = par[a][i];
}
}
return par[a][0];
}
static void formArrayForBinaryLifting(int n, int par[][]){
for(int j=1;j<20;j++){
for(int i=0;i<n;i++){
if(par[i][j-1]==-1)
continue;
par[i][j] = par[par[i][j-1]][j-1];
}
}
}
static long lcm(int a, int b){
return a*b/gcd(a,b);
}
static class CustomPair {
long a[];
CustomPair(long a[]) {
this.a = a;
}
}
static class Pair1 implements Comparable<Pair1> {
long a;
long b;
Pair1(long a, long b) {
this.a = a;
this.b = b;
}
public int compareTo(Pair1 p) {
if(a!=p.a)
return (a<p.a?-1:1);
return (b<p.b?-1:1);
}
}
static class Pair implements Comparable<Pair> {
int a;
int b;
int c;
Pair(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public int compareTo(Pair p) {
if(b!=p.b)
return (b-p.b);
return (a-p.a);
}
}
static class Pair2 implements Comparable<Pair2> {
int a;
int b;
int c;
Pair2(int a, int b, int c) {
this.a = a;
this.b = b;
this.c = c;
}
public int compareTo(Pair2 p) {
return a-p.a;
}
}
static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static long fast_pow(long base, long n, long M) {
if (n == 0)
return 1;
if (n == 1)
return base % M;
long halfn = fast_pow(base, n / 2, M);
if (n % 2 == 0)
return (halfn * halfn) % M;
else
return (((halfn * halfn) % M) * base) % M;
}
static long modInverse(long n, long M) {
return fast_pow(n, M - 2, M);
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 9992768);
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 | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 8cb7ff973bad69e1d2216f4f1d305482 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.util.Arrays;
import java.util.Random;
import java.io.FileWriter;
import java.io.PrintWriter;
/*
Solution Created: 18:14:45 22/02/2022
Custom Competitive programming helper.
*/
public class Main {
public static void solve() {
long n = in.nextLong();
char[] a = in.nca();
long totR = 0, totD = 0;
for(char c : a) {
if(c == 'R') totR++;
else totD++;
}
long extraR = n-totR-1, extraD = n-totD-1; // the r and d max moves we can make
long ans = 1;
if(totD>0) ans*= (extraD+1);
if(totR>0) ans*= (extraR+1);
for(int i = a.length-1; i>=0; i--) {
if(a[i]=='R') totR--;
else totD--;
ans++;
boolean f = a[i] == 'D' && totR>0;
boolean s = a[i] == 'R' && totD>0;
if(f) ans += extraR;
if(s) ans += extraD;
}
out.println(ans);
}
public static void main(String[] args) {
in = new Reader();
out = new Writer();
int t = in.nextInt();
while(t-->0) solve();
out.exit();
}
static Reader in; static Writer out;
static class Reader {
static BufferedReader br;
static StringTokenizer st;
public Reader() {
this.br = new BufferedReader(new InputStreamReader(System.in));
}
public Reader(String f){
try {
this.br = new BufferedReader(new FileReader(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
public double[] nd(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextDouble();
return a;
}
public long[] nl(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
public char[] nca() {
return next().toCharArray();
}
public String[] ns(int n) {
String[] a = new String[n];
for (int i = 0; i < n; i++) a[i] = next();
return a;
}
public int nextInt() {
ensureNext();
return Integer.parseInt(st.nextToken());
}
public double nextDouble() {
ensureNext();
return Double.parseDouble(st.nextToken());
}
public Long nextLong() {
ensureNext();
return Long.parseLong(st.nextToken());
}
public String next() {
ensureNext();
return st.nextToken();
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
private void ensureNext() {
if (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
static class Util{
private static Random random = new Random();
static long[] fact;
public static void initFactorial(int n, long mod) {
fact = new long[n+1];
fact[0] = 1;
for (int i = 1; i < n+1; i++) fact[i] = (fact[i - 1] * i) % mod;
}
public static long modInverse(long a, long MOD) {
long[] gcdE = gcdExtended(a, MOD);
if (gcdE[0] != 1) return -1; // Inverted doesn't exist
long x = gcdE[1];
return (x % MOD + MOD) % MOD;
}
public static long[] gcdExtended(long p, long q) {
if (q == 0) return new long[] { p, 1, 0 };
long[] vals = gcdExtended(q, p % q);
long tmp = vals[2];
vals[2] = vals[1] - (p / q) * vals[2];
vals[1] = tmp;
return vals;
}
public static long nCr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[r], MOD) % MOD * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nCr(int n, int r) {
return (fact[n]/fact[r])/fact[n-r];
}
public static long nPr(int n, int r, long MOD) {
if (r == 0) return 1;
return (fact[n] * modInverse(fact[n - r], MOD) % MOD) % MOD;
}
public static long nPr(int n, int r) {
return fact[n]/fact[n-r];
}
public static boolean isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean[] getSieve(int n) {
boolean[] isPrime = new boolean[n+1];
for (int i = 2; i <= n; i++) isPrime[i] = true;
for (int i = 2; i*i <= n; i++) if (isPrime[i])
for (int j = i; i*j <= n; j++) isPrime[i*j] = false;
return isPrime;
}
static long pow(long x, long pow, long mod){
long res = 1;
x = x % mod;
if (x == 0) return 0;
while (pow > 0){
if ((pow & 1) != 0) res = (res * x) % mod;
pow >>= 1;
x = (x * x) % mod;
}
return res;
}
public static int gcd(int a, int b) {
int tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static long gcd(long a, long b) {
long tmp = 0;
while(b != 0) {
tmp = b;
b = a%b;
a = tmp;
}
return a;
}
public static int random(int min, int max) {
return random.nextInt(max-min+1)+min;
}
public static void dbg(Object... o) {
System.out.println(Arrays.deepToString(o));
}
public static void reverse(int[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
int tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(int[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(long[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
long tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(long[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(float[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
float tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(float[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(double[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
double tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(double[] s) {
reverse(s, 0, s.length-1);
}
public static void reverse(char[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
char tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static void reverse(char[] s) {
reverse(s, 0, s.length-1);
}
public static <T> void reverse(T[] s, int l , int r) {
for(int i = l; i<=(l+r)/2; i++) {
T tmp = s[i];
s[i] = s[r+l-i];
s[r+l-i] = tmp;
}
}
public static <T> void reverse(T[] s) {
reverse(s, 0, s.length-1);
}
public static void shuffle(int[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
int t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(long[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
long t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(float[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
float t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(double[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
double t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void shuffle(char[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
char t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static <T> void shuffle(T[] s) {
for (int i = 0; i < s.length; ++i) {
int j = random.nextInt(i + 1);
T t = s[i];
s[i] = s[j];
s[j] = t;
}
}
public static void sortArray(int[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(long[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(float[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(double[] a) {
shuffle(a);
Arrays.sort(a);
}
public static void sortArray(char[] a) {
shuffle(a);
Arrays.sort(a);
}
public static <T extends Comparable<T>> void sortArray(T[] a) {
Arrays.sort(a);
}
public static int[][] rotate90(int[][] a){
int n = a.length, m = a[0].length;
int[][] ans = new int[m][n];
for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) ans[m-j-1][i] = a[i][j];
return ans;
}
public static char[][] rotate90(char[][] a){
int n = a.length, m = a[0].length;
char[][] ans = new char[m][n];
for(int i = 0; i<n; i++) for(int j = 0; j<m; j++) ans[m-j-1][i] = a[i][j];
return ans;
}
}
static class Writer {
private PrintWriter pw;
public Writer(){
pw = new PrintWriter(System.out);
}
public Writer(String f){
try {
pw = new PrintWriter(new FileWriter(f));
} catch (IOException e) {
e.printStackTrace();
}
}
public void yesNo(boolean condition) {
println(condition?"YES":"NO");
}
public void printArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(int[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void printArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
}
public void printlnArray(long[] a) {
for(int i = 0; i<a.length; i++) print(a[i]+" ");
pw.println();
}
public void print(Object o) {
pw.print(o.toString());
}
public void println(Object o) {
pw.println(o.toString());
}
public void println() {
pw.println();
}
public void flush() {
pw.flush();
}
public void exit() {
pw.close();
}
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | ab900168c914f8df4d363ba24fb8d391 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.util.*;import java.io.*;import java.math.*;
public class Main{
public static void process(int t)throws IOException
{
int n = ni();
String s = nln();
int x = 1, y = 1;
for(int i=0;i<s.length();i++){
if(s.charAt(i) == 'R') x++;
else y++;
}
if(x == 1 || y == 1){
pn(n);
return;
}
long ans = s.length() + 1;
ans += 1l * (n - x) * (n - y);
//pn(ans);
for(int i=0;i<s.length();i++){
if(s.charAt(i) == 'R'){
ans += 1l * (y - i) * (n - x);
break;
}
}
for(int i=0;i<s.length();i++){
if(s.charAt(i) == 'D'){
ans += 1l * (x - i) * (n - y);
break;
}
}
pn(ans);
}
static long mod = 998244353l;
static AnotherReader sc;
static PrintWriter out;
public static void main(String[]args)throws IOException
{
boolean oj = System.getProperty("ONLINE_JUDGE") != null;
if(oj){sc=new AnotherReader();out=new PrintWriter(System.out);}
else{sc=new AnotherReader(100);out=new PrintWriter("output.txt");}
int t=1;
t=ni();
while(t-- > 0) {process(t);}
out.flush();out.close();
}
static void pn(Object o){out.println(o);}
static void p(Object o){out.print(o);}
static void pni(Object o){out.println(o);out.flush();}
static int ni()throws IOException{return sc.nextInt();}
static long nl()throws IOException{return sc.nextLong();}
static double nd()throws IOException{return sc.nextDouble();}
static String nln()throws IOException{return sc.nextLine();}
static int[] nai(int N)throws IOException{int[]A=new int[N];for(int i=0;i!=N;i++){A[i]=ni();}return A;}
static long[] nal(int N)throws IOException{long[]A=new long[N];for(int i=0;i!=N;i++){A[i]=nl();}return A;}
static long gcd(long a, long b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int gcd(int a, int b)throws IOException{return (b==0)?a:gcd(b,a%b);}
static int bit(long n)throws IOException{return (n==0)?0:(1+bit(n&(n-1)));}
/////////////////////////////////////////////////////////////////////////////////////////////////////////
static class AnotherReader{BufferedReader br; StringTokenizer st;
AnotherReader()throws FileNotFoundException{
br=new BufferedReader(new InputStreamReader(System.in));}
AnotherReader(int a)throws FileNotFoundException{
br = new BufferedReader(new FileReader("input.txt"));}
String next()throws IOException{
while (st == null || !st.hasMoreElements()) {try{
st = new StringTokenizer(br.readLine());}
catch (IOException e){ e.printStackTrace(); }}
return st.nextToken(); } int nextInt() throws IOException{
return Integer.parseInt(next());}
long nextLong() throws IOException
{return Long.parseLong(next());}
double nextDouble()throws IOException { return Double.parseDouble(next()); }
String nextLine() throws IOException{ String str = ""; try{
str = br.readLine();} catch (IOException e){
e.printStackTrace();} return str;}}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | d43dbb0d549910e7637ea678eae63df2 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class E {
public static FastScanner s = new FastScanner();
public static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) {
int lines = s.nextInt();
for (int i = 0; i < lines; i += 1) {
out.println(solve(s.nextInt(), s.next()));
}
out.close();
}
public static long solve(long n, String s) {
long r = 0;
long d = 0;
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'R') r++; else d++;
}
if (r == 0 || d == 0) return n;
long ind;
long hei;
long sum = n * n;
ind = s.indexOf("D");
hei = n - 1;
for (int i = 0; i < s.length(); i++) {
if (i == ind) {
hei -= (n - 1 - d);
}
if (s.charAt(i) == 'R') sum -= hei; else hei--;
}
ind = s.indexOf("R");
hei = n - 1;
for (int i = 0; i < s.length(); i++) {
if (i == ind) {
hei -= (n - 1 - r);
}
if (s.charAt(i) == 'D') sum -= hei; else hei--;
}
return sum;
}
static class FastScanner {//copied from secondthread
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();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 11 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 6aeb715ac4a5a23da42c36efe673705a | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.io.*;
import java.text.MessageFormat;
import java.util.Arrays;
import java.util.InputMismatchException;
/**
* Provide prove of correctness before implementation. Implementation can cost a lot of time.
* Anti test that prove that it's wrong.
* <p>
* Do not confuse i j k g indexes, upTo and length. Do extra methods!!! Write more informative names to simulation
* <p>
* Will program ever exceed limit?
* Try all approaches with prove of correctness if task is not obvious.
* If you are given formula/rule: Try to play with it.
* Analytic solution/Hardcoded solution/Constructive/Greedy/DP/Math/Brute force/Symmetric data
* Number theory
* Game theory (optimal play) that consider local and global strategy.
* Start writing the hardest code first
*/
public class CF1644E {
final boolean ONLINE_JUDGE = java.lang.System.getProperty("ONLINE_JUDGE") != null;
private void solveOne(int testCase) {
long n = nextInt();
char[] s = nextString().toCharArray();
long groupW = 1;
long groupH = 1;
for (char c : s) {
if (c == 'R') groupW++;
else groupH++;
}
if(groupH == 1 || groupW == 1) {
System.out.println(n);
return;
}
long notReachable = 0;
boolean wasTurnToRight = false;
boolean wasTurnToDown = false;
long curX = 1;
long curY = 1;
for (char c : s) {
if(c == 'R' && !wasTurnToDown) {
notReachable += n - curY;
}
if(c == 'R' && wasTurnToDown) {
notReachable += groupH - curY;
}
if(c == 'D' && !wasTurnToRight) {
notReachable += n - curX;
}
if(c == 'D' && wasTurnToRight) {
notReachable += groupW - curX;
}
if (c == 'R') curX++;
else curY++;
if(c == 'D') wasTurnToDown = true;
if(c == 'R') wasTurnToRight = true;
}
System.out.println(n * n - notReachable);
}
private void solve() {
int t = nextInt();
for (int testCase = 0; testCase < t; testCase++) {
solveOne(testCase);
}
}
class AssertionRuntimeException extends RuntimeException {
AssertionRuntimeException(String testName,
int testCase,
Object expected,
Object actual, Object... input) {
super("Testcase: " + testCase + "\n expected = " + expected + ",\n actual = " + actual + ",\n " + Arrays.deepToString(input));
}
}
private void assertThat(boolean b) {
if (!b) throw new RuntimeException();
}
private void assertThat(boolean b, String s) {
if (!b) throw new RuntimeException(s);
}
private int assertThatInt(long a) {
assertThat(Integer.MIN_VALUE <= a && a <= Integer.MAX_VALUE);
return (int) a;
}
void _______debug(String str, Object... os) {
if (!ONLINE_JUDGE) {
System.out.println(MessageFormat.format(str, os));
}
}
void _______debug(Object o) {
if (!ONLINE_JUDGE) {
_______debug("{0}", String.valueOf(o));
}
}
private int nextInt() {
return System.in.readInt();
}
private long nextLong() {
return System.in.readLong();
}
private String nextString() {
return System.in.readString();
}
private int[] nextIntArr(int n) {
return System.in.readIntArray(n);
}
private long[] nextLongArr(int n) {
return System.in.readLongArray(n);
}
public static void main(String[] args) {
new CF1644E().run();
}
static class System {
private static FastInputStream in;
private static FastPrintStream out;
}
private void run() {
final boolean USE_IO = ONLINE_JUDGE;
if (USE_IO) {
System.in = new FastInputStream(java.lang.System.in);
System.out = new FastPrintStream(java.lang.System.out);
solve();
System.out.flush();
} else {
final String nameIn = "input.txt";
final String nameOut = "output.txt";
try {
System.in = new FastInputStream(new FileInputStream(nameIn));
System.out = new FastPrintStream(new PrintStream(nameOut));
solve();
System.out.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
private static class FastPrintStream {
private static final int BUF_SIZE = 8192;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastPrintStream() {
this(java.lang.System.out);
}
public FastPrintStream(OutputStream os) {
this.out = os;
}
public FastPrintStream(String path) {
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastPrintStream print(byte b) {
buf[ptr++] = b;
if (ptr == BUF_SIZE) innerFlush();
return this;
}
public FastPrintStream print(char c) {
return print((byte) c);
}
public FastPrintStream print(char[] s) {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerFlush();
}
return this;
}
public FastPrintStream print(String s) {
s.chars().forEach(c -> {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerFlush();
});
return this;
}
//can be optimized
public FastPrintStream print0(char[] s) {
if (ptr + s.length < BUF_SIZE) {
for (char c : s) {
buf[ptr++] = (byte) c;
}
} else {
for (char c : s) {
buf[ptr++] = (byte) c;
if (ptr == BUF_SIZE) innerFlush();
}
}
return this;
}
//can be optimized
public FastPrintStream print0(String s) {
if (ptr + s.length() < BUF_SIZE) {
for (int i = 0; i < s.length(); i++) {
buf[ptr++] = (byte) s.charAt(i);
}
} else {
for (int i = 0; i < s.length(); i++) {
buf[ptr++] = (byte) s.charAt(i);
if (ptr == BUF_SIZE) innerFlush();
}
}
return this;
}
private static int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
public FastPrintStream print(int x) {
if (x == Integer.MIN_VALUE) {
return print((long) x);
}
if (ptr + 12 >= BUF_SIZE) innerFlush();
if (x < 0) {
print((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
private static int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public FastPrintStream print(long x) {
if (x == Long.MIN_VALUE) {
return print("" + x);
}
if (ptr + 21 >= BUF_SIZE) innerFlush();
if (x < 0) {
print((byte) '-');
x = -x;
}
int d = countDigits(x);
for (int i = ptr + d - 1; i >= ptr; i--) {
buf[i] = (byte) ('0' + x % 10);
x /= 10;
}
ptr += d;
return this;
}
public FastPrintStream print(double x, int precision) {
if (x < 0) {
print('-');
x = -x;
}
x += Math.pow(10, -precision) / 2;
// if(x < 0){ x = 0; }
print((long) x).print(".");
x -= (long) x;
for (int i = 0; i < precision; i++) {
x *= 10;
print((char) ('0' + (int) x));
x -= (int) x;
}
return this;
}
public FastPrintStream println(int[] a, int from, int upTo, char separator) {
for (int i = from; i < upTo; i++) {
print(a[i]);
print(separator);
}
print('\n');
return this;
}
public FastPrintStream println(int[] a) {
return println(a, 0, a.length, ' ');
}
public FastPrintStream println(long[] a, int from, int upTo, char separator) {
for (int i = from; i < upTo; i++) {
print(a[i]);
print(separator);
}
print('\n');
return this;
}
public FastPrintStream println(long[] a) {
return println(a, 0, a.length, ' ');
}
public FastPrintStream println(char c) {
return print(c).println();
}
public FastPrintStream println(int x) {
return print(x).println();
}
public FastPrintStream println(long x) {
return print(x).println();
}
public FastPrintStream println(String x) {
return print(x).println();
}
public FastPrintStream println(Object x) {
return print(x.toString()).println();
}
public FastPrintStream println(double x, int precision) {
return print(x, precision).println();
}
public FastPrintStream println() {
return print((byte) '\n');
}
public FastPrintStream printf(String format, Object... args) {
return print(String.format(format, args));
}
private void innerFlush() {
try {
out.write(buf, 0, ptr);
ptr = 0;
} catch (IOException e) {
throw new RuntimeException("innerFlush");
}
}
public void flush() {
innerFlush();
try {
out.flush();
} catch (IOException e) {
throw new RuntimeException("flush");
}
}
}
private static class FastInputStream {
private boolean finished = false;
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public FastInputStream(InputStream stream) {
this.stream = stream;
}
public double[] readDoubleArray(int size) {
double[] array = new double[size];
for (int i = 0; i < size; i++) {
array[i] = readDouble();
}
return array;
}
public String[] readStringArray(int size) {
String[] array = new String[size];
for (int i = 0; i < size; i++) {
array[i] = readString();
}
return array;
}
public char[] readCharArray(int size) {
char[] array = new char[size];
for (int i = 0; i < size; i++) {
array[i] = readCharacter();
}
return array;
}
public char[][] readTable(int rowCount, int columnCount) {
char[][] table = new char[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readCharArray(columnCount);
}
return table;
}
public int[][] readIntTable(int rowCount, int columnCount) {
int[][] table = new int[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = readIntArray(columnCount);
}
return table;
}
public double[][] readDoubleTable(int rowCount, int columnCount) {
double[][] table = new double[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readDoubleArray(columnCount);
}
return table;
}
public long[][] readLongTable(int rowCount, int columnCount) {
long[][] table = new long[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = readLongArray(columnCount);
}
return table;
}
public String[][] readStringTable(int rowCount, int columnCount) {
String[][] table = new String[rowCount][];
for (int i = 0; i < rowCount; i++) {
table[i] = this.readStringArray(columnCount);
}
return table;
}
public String readText() {
StringBuilder result = new StringBuilder();
while (true) {
int character = read();
if (character == '\r') {
continue;
}
if (character == -1) {
break;
}
result.append((char) character);
}
return result.toString();
}
public void readStringArrays(String[]... arrays) {
for (int i = 0; i < arrays[0].length; i++) {
for (int j = 0; j < arrays.length; j++) {
arrays[j][i] = readString();
}
}
}
public long[] readLongArray(int size) {
long[] array = new long[size];
for (int i = 0; i < size; i++) {
array[i] = readLong();
}
return array;
}
public int[] readIntArray(int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = readInt();
}
return array;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int peekNonWhitespace() {
while (isWhitespace(peek())) {
read();
}
return peek();
}
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 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 String readString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
if (Character.isValidCodePoint(c)) {
res.appendCodePoint(c);
}
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuilder buf = new StringBuilder();
int c = read();
while (c != '\n' && c != -1) {
if (c != '\r') {
buf.appendCodePoint(c);
}
c = read();
}
return buf.toString();
}
public String readLine() {
String s = readLine0();
while (s.trim().length() == 0) {
s = readLine0();
}
return s;
}
public String readLine(boolean ignoreEmptyLines) {
if (ignoreEmptyLines) {
return readLine();
} else {
return readLine0();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
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 boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return readString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
} | Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 17 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 837a3c25100fb0b75ad40d65e7c0b1d9 | train_108.jsonl | 1645540500 | Consider a grid of size $$$n \times n$$$. The rows are numbered top to bottom from $$$1$$$ to $$$n$$$, the columns are numbered left to right from $$$1$$$ to $$$n$$$.The robot is positioned in a cell $$$(1, 1)$$$. It can perform two types of moves: D — move one cell down; R — move one cell right. The robot is not allowed to move outside the grid.You are given a sequence of moves $$$s$$$ — the initial path of the robot. This path doesn't lead the robot outside the grid.You are allowed to perform an arbitrary number of modifications to it (possibly, zero). With one modification, you can duplicate one move in the sequence. That is, replace a single occurrence of D with DD or a single occurrence of R with RR.Count the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | 256 megabytes | import java.util.*;
import java.util.function.*;
import java.io.*;
// you can compare with output.txt and expected out
public class RoundEdu123E {
MyPrintWriter out;
MyScanner in;
// final static long FIXED_RANDOM;
// static {
// FIXED_RANDOM = System.currentTimeMillis();
// }
final static String IMPOSSIBLE = "IMPOSSIBLE";
final static String POSSIBLE = "POSSIBLE";
final static String YES = "YES";
final static String NO = "NO";
private void initIO(boolean isFileIO) {
if (System.getProperty("ONLINE_JUDGE") == null && isFileIO) {
try{
in = new MyScanner(new FileInputStream("input.txt"));
out = new MyPrintWriter(new FileOutputStream("output.txt"));
}
catch(FileNotFoundException e){
e.printStackTrace();
}
}
else{
in = new MyScanner(System.in);
out = new MyPrintWriter(new BufferedOutputStream(System.out));
}
}
public static void main(String[] args){
// Scanner in = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
RoundEdu123E sol = new RoundEdu123E();
sol.run();
}
private void run() {
boolean isDebug = false;
boolean isFileIO = true;
boolean hasMultipleTests = true;
initIO(isFileIO);
int t = hasMultipleTests? in.nextInt() : 1;
for (int i = 1; i <= t; ++i) {
int n = in.nextInt();
String s = in.next();
if(isDebug){
out.printf("Test %d\n", i);
}
long ans = solve(n, s);
out.printlnAns(ans);
if(isDebug)
out.flush();
}
in.close();
out.close();
}
private long solve(int n, String s) {
// look at the destination
// if we can use k more R's and l more D's
// pad the right side of our path by k more R's (after the first R appears)
// and pad the lower side by l more D's
int m = s.length();
int[] pos = {0, 0};
for(int i=0; i<m; i++) {
if(s.charAt(i) == 'D')
pos[1]++;
else
pos[0]++;
}
int k = n-1-pos[0];
int l = n-1-pos[1];
boolean hasRappeared = false;
boolean hasDappeared = false;
long ans = 0;
for(int i=0; i<m; i++) {
if(s.charAt(i) == 'D') {
if(hasRappeared) {
ans += k+1;
}
else {
ans ++;
}
hasDappeared = true;
}
else {
if(hasDappeared) {
ans += l+1;
}
else {
ans++;
}
hasRappeared = true;
}
}
if(hasRappeared && hasDappeared)
ans += (long)(k+1)*(l+1);
else if(hasRappeared)
ans += k+1;
else if(hasDappeared)
ans += l+1;
return ans;
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
// 32768?
public MyScanner(InputStream is, int bufferSize) {
br = new BufferedReader(new InputStreamReader(is), bufferSize);
}
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
// br = new BufferedReader(new InputStreamReader(System.in));
// br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));
}
public void close() {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
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;
}
int[][] nextTreeEdges(int n, int offset){
int[][] e = new int[n-1][2];
for(int i=0; i<n-1; i++){
e[i][0] = nextInt()+offset;
e[i][1] = nextInt()+offset;
}
return e;
}
int[][] nextMatrix(int n, int m) {
return nextMatrix(n, m, 0);
}
int[][] nextMatrix(int n, int m, int offset) {
int[][] mat = new int[n][m];
for(int i=0; i<n; i++) {
for(int j=0; j<m; j++) {
mat[i][j] = nextInt()+offset;
}
}
return mat;
}
int[][] nextPairs(int n){
return nextPairs(n, 0);
}
int[][] nextPairs(int n, int offset) {
int[][] xy = new int[2][n];
for(int i=0; i<n; i++) {
xy[0][i] = nextInt() + offset;
xy[1][i] = nextInt() + offset;
}
return xy;
}
int[][] nextGraphEdges(){
return nextGraphEdges(0);
}
int[][] nextGraphEdges(int offset) {
int m = nextInt();
int[][] e = new int[m][2];
for(int i=0; i<m; i++){
e[i][0] = nextInt()+offset;
e[i][1] = nextInt()+offset;
}
return e;
}
int[] nextIntArray(int len) {
return nextIntArray(len, 0);
}
int[] nextIntArray(int len, int offset){
int[] a = new int[len];
for(int j=0; j<len; j++)
a[j] = nextInt()+offset;
return a;
}
long[] nextLongArray(int len) {
return nextLongArray(len, 0);
}
long[] nextLongArray(int len, int offset){
long[] a = new long[len];
for(int j=0; j<len; j++)
a[j] = nextLong()+offset;
return a;
}
}
public static class MyPrintWriter extends PrintWriter{
public MyPrintWriter(OutputStream os) {
super(os);
}
public void printlnAns(long ans) {
println(ans);
}
public void printlnAns(int ans) {
println(ans);
}
public void printlnAns(boolean ans) {
if(ans)
println(YES);
else
println(NO);
}
public void printAns(long[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(long[] arr){
printAns(arr);
println();
}
public void printAns(int[] arr){
if(arr != null && arr.length > 0){
print(arr[0]);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]);
}
}
}
public void printlnAns(int[] arr){
printAns(arr);
println();
}
public <T> void printAns(ArrayList<T> arr){
if(arr != null && arr.size() > 0){
print(arr.get(0));
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i));
}
}
}
public <T> void printlnAns(ArrayList<T> arr){
printAns(arr);
println();
}
public void printAns(int[] arr, int add){
if(arr != null && arr.length > 0){
print(arr[0]+add);
for(int i=1; i<arr.length; i++){
print(" ");
print(arr[i]+add);
}
}
}
public void printlnAns(int[] arr, int add){
printAns(arr, add);
println();
}
public void printAns(ArrayList<Integer> arr, int add) {
if(arr != null && arr.size() > 0){
print(arr.get(0)+add);
for(int i=1; i<arr.size(); i++){
print(" ");
print(arr.get(i)+add);
}
}
}
public void printlnAns(ArrayList<Integer> arr, int add){
printAns(arr, add);
println();
}
public void printlnAnsSplit(long[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public void printlnAnsSplit(int[] arr, int split){
if(arr != null){
for(int i=0; i<arr.length; i+=split){
print(arr[i]);
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr[j]);
}
println();
}
}
}
public <T> void printlnAnsSplit(ArrayList<T> arr, int split){
if(arr != null && !arr.isEmpty()){
for(int i=0; i<arr.size(); i+=split){
print(arr.get(i));
for(int j=i+1; j<i+split; j++){
print(" ");
print(arr.get(j));
}
println();
}
}
}
}
static private void permutateAndSort(long[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
long temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private void permutateAndSort(int[] a) {
int n = a.length;
Random R = new Random(System.currentTimeMillis());
for(int i=0; i<n; i++) {
int t = R.nextInt(n-i);
int temp = a[n-1-i];
a[n-1-i] = a[t];
a[t] = temp;
}
Arrays.sort(a);
}
static private int[][] constructChildren(int n, int[] parent, int parentRoot){
int[][] childrens = new int[n][];
int[] numChildren = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
numChildren[parent[i]]++;
}
for(int i=0; i<n; i++) {
childrens[i] = new int[numChildren[i]];
}
int[] idx = new int[n];
for(int i=0; i<parent.length; i++) {
if(parent[i] != parentRoot)
childrens[parent[i]][idx[parent[i]]++] = i;
}
return childrens;
}
static private int[][][] constructDirectedNeighborhood(int n, int[][] e){
int[] inDegree = new int[n];
int[] outDegree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outDegree[u]++;
inDegree[v]++;
}
int[][] inNeighbors = new int[n][];
int[][] outNeighbors = new int[n][];
for(int i=0; i<n; i++) {
inNeighbors[i] = new int[inDegree[i]];
outNeighbors[i] = new int[outDegree[i]];
}
int[] inIdx = new int[n];
int[] outIdx = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
outNeighbors[u][outIdx[u]++] = v;
inNeighbors[v][inIdx[v]++] = u;
}
return new int[][][] {inNeighbors, outNeighbors};
}
static private int[][] constructNeighborhood(int n, int[][] e) {
int[] degree = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
degree[u]++;
degree[v]++;
}
int[][] neighbors = new int[n][];
for(int i=0; i<n; i++)
neighbors[i] = new int[degree[i]];
int[] idx = new int[n];
for(int i=0; i<e.length; i++) {
int u = e[i][0];
int v = e[i][1];
neighbors[u][idx[u]++] = v;
neighbors[v][idx[v]++] = u;
}
return neighbors;
}
static private void drawGraph(int[][] e) {
makeDotUndirected(e);
try {
final Process process = new ProcessBuilder("dot", "-Tpng", "graph.dot")
.redirectOutput(new File("graph.png"))
.start();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
static private void makeDotUndirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict graph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "--" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
static private void makeDotDirected(int[][] e) {
MyPrintWriter out2 = null;
try {
out2 = new MyPrintWriter(new FileOutputStream("graph.dot"));
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
out2.println("strict digraph {");
for(int i=0; i<e.length; i++){
out2.println(e[i][0] + "->" + e[i][1] + ";");
}
out2.println("}");
out2.close();
}
}
| Java | ["3\n\n4\n\nRD\n\n5\n\nDRDRDRDR\n\n3\n\nD"] | 2 seconds | ["13\n9\n3"] | NoteIn the first testcase, it's enough to consider the following modified paths: RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRRD $$$\rightarrow$$$ RRRDD $$$\rightarrow$$$ RRRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 4)$$$, $$$(3, 4)$$$ and $$$(4, 4)$$$; RD $$$\rightarrow$$$ RRD $$$\rightarrow$$$ RRDD $$$\rightarrow$$$ RRDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(2, 3)$$$, $$$(3, 3)$$$ and $$$(4, 3)$$$; RD $$$\rightarrow$$$ RDD $$$\rightarrow$$$ RDDD — this path visits cells $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(2, 2)$$$, $$$(3, 2)$$$ and $$$(4, 2)$$$. Thus, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(1, 2)$$$, $$$(1, 3)$$$, $$$(1, 4)$$$, $$$(2, 2)$$$, $$$(2, 3)$$$, $$$(2, 4)$$$, $$$(3, 2)$$$, $$$(3, 3)$$$, $$$(3, 4)$$$, $$$(4, 2)$$$, $$$(4, 3)$$$ and $$$(4, 4)$$$.In the second testcase, there is no way to modify the sequence without moving the robot outside the grid. So the only visited cells are the ones that are visited on the path DRDRDRDR.In the third testcase, the cells that are visited on at least one modified path are: $$$(1, 1)$$$, $$$(2, 1)$$$ and $$$(3, 1)$$$.Here are the cells for all testcases: | Java 17 | standard input | [
"brute force",
"combinatorics",
"data structures",
"implementation",
"math"
] | 113a43af2f1c5303f83eb842ef532040 | The first line contains a single integer $$$t$$$ ($$$1 \le t \le 10^4$$$) — the number of testcases. The first line of each testcase contains the single integer $$$n$$$ ($$$2 \le n \le 10^8$$$) — the number of rows and columns in the grid. The second line of each testcase contains a non-empty string $$$s$$$, consisting only of characters D and R, — the initial path of the robot. This path doesn't lead the robot outside the grid. The total length of strings $$$s$$$ over all testcases doesn't exceed $$$2 \cdot 10^5$$$. | 1,900 | For each testcase, print a single integer — the number of cells such that there exists at least one sequence of modifications that the robot visits this cell on the modified path and doesn't move outside the grid. | standard output | |
PASSED | 2ec8aae573fe98b4ac625a8fe58fce26 | train_108.jsonl | 1658673300 | You are given a positive integer $$$n$$$. Since $$$n$$$ may be very large, you are given its binary representation.You should compute the number of triples $$$(a,b,c)$$$ with $$$0 \leq a,b,c \leq n$$$ such that $$$a \oplus b$$$, $$$b \oplus c$$$, and $$$a \oplus c$$$ are the sides of a non-degenerate triangle. Here, $$$\oplus$$$ denotes the bitwise XOR operation.You should output the answer modulo $$$998\,244\,353$$$.Three positive values $$$x$$$, $$$y$$$, and $$$z$$$ are the sides of a non-degenerate triangle if and only if $$$x+y>z$$$, $$$x+z>y$$$, and $$$y+z>x$$$. | 512 megabytes | import java.io.*;
import java.util.*;
public class a{
public static FastScanner fs;
public static long mod=998244353l;
public static void main(String args[])
{
fs=new FastScanner();
char s[]=fs.next().toCharArray();
int condition[]=new int[8];
Arrays.fill(condition,-1);
for(int i=0;i<8;i++)//precomputation to find which condition will get satisfied
{
int bits[]=new int[3];
for(int j=0;j<3;j++)
bits[j]=(i>>j)&1;
List<Integer>temp=new ArrayList<>(Arrays.asList(bits[1]^bits[2],bits[0]^bits[2],bits[0]^bits[1]));
for(Integer it:temp)
{
if(it>0)
{
for(int j=0;j<3;j++)
if(temp.get(j)==0)
condition[i]=j;
break;
}
}
}//implementation of the tutorial
long dp[][]=new long[8][8];
for(int i=0;i<8;i++)
Arrays.fill(dp[i],0l);
dp[0][0]=1l;//havent started anything so with nothing satisfying the less than n and no condition satisfied is 1 way
for(char c:s)
{
int curbit=c-'0';
long ndp[][]=new long[8][8];
for(int i=0;i<8;i++)
Arrays.fill(ndp[i],0l);
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
for(int curbitset=0;curbitset<8;curbitset++)
{
int ni=0,nj=j;
boolean isbad=false;
for(int k=0;k<3;k++)
{
int issmall=(i>>k)&1;
int curbitx=(curbitset>>k)&1;
if(issmall==0 && curbitx>curbit)
{
isbad=true;
break;
}
int nissmall=(issmall>0 ||(curbitx<curbit))?1:0;
ni |= nissmall<<k;
}
if(isbad)
continue;
if(condition[curbitset]>=0)
nj |= 1<<condition[curbitset];
ndp[ni][nj] = (ndp[ni][nj]+dp[i][j])%mod;
}
}
}
dp=ndp;
}
long ans=0l;
for(int i=0;i<8;i++)
ans=(ans+dp[i][7])%mod;
System.out.println(ans);
}
static class FastScanner
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next()
{
while(!st.hasMoreTokens())
{
try{
st=new StringTokenizer(br.readLine());
}
catch(Exception e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
}
} | Java | ["101", "1110", "11011111101010010"] | 4 seconds | ["12", "780", "141427753"] | NoteIn the first test case, $$$101_2=5$$$. The triple $$$(a, b, c) = (0, 3, 5)$$$ is valid because $$$(a\oplus b, b\oplus c, c\oplus a) = (3, 6, 5)$$$ are the sides of a non-degenerate triangle. The triple $$$(a, b, c) = (1, 2, 4)$$$ is valid because $$$(a\oplus b, b\oplus c, c\oplus a) = (3, 6, 5)$$$ are the sides of a non-degenerate triangle. The $$$6$$$ permutations of each of these two triples are all the valid triples, thus the answer is $$$12$$$.In the third test case, $$$11\,011\,111\,101\,010\,010_2=114\,514$$$. The full answer (before taking the modulo) is $$$1\,466\,408\,118\,808\,164$$$. | Java 11 | standard input | [
"bitmasks",
"brute force",
"constructive algorithms",
"dp",
"greedy",
"math"
] | 94bc4b263821713fb5b1de4de331a515 | The first and only line contains the binary representation of an integer $$$n$$$ ($$$0 < n < 2^{200\,000}$$$) without leading zeros. For example, the string 10 is the binary representation of the number $$$2$$$, while the string 1010 represents the number $$$10$$$. | 2,500 | Print one integer — the number of triples $$$(a,b,c)$$$ satisfying the conditions described in the statement modulo $$$998\,244\,353$$$. | standard output | |
PASSED | a2d8662b4345a4c8d254f444c16157eb | train_108.jsonl | 1658673300 | You are given a positive integer $$$n$$$. Since $$$n$$$ may be very large, you are given its binary representation.You should compute the number of triples $$$(a,b,c)$$$ with $$$0 \leq a,b,c \leq n$$$ such that $$$a \oplus b$$$, $$$b \oplus c$$$, and $$$a \oplus c$$$ are the sides of a non-degenerate triangle. Here, $$$\oplus$$$ denotes the bitwise XOR operation.You should output the answer modulo $$$998\,244\,353$$$.Three positive values $$$x$$$, $$$y$$$, and $$$z$$$ are the sides of a non-degenerate triangle if and only if $$$x+y>z$$$, $$$x+z>y$$$, and $$$y+z>x$$$. | 512 megabytes | import java.io.*;
import java.util.*;
public class Div1COPY {
static long MOD = 998244353;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
char[] line = br.readLine().toCharArray();
long[][][] dp = new long[line.length][4][4];
dp[0][1][1] = 3;
dp[0][2][1] = 3;
dp[0][3][0] = 1;
dp[0][0][0] = 1;
for(int i = 1; i < line.length; i++) {
if(line[i] == '1') {
dp[i][0][0] = 2 * dp[i-1][0][0] + dp[i-1][3][0];
dp[i][0][1] = 4 * dp[i-1][0][1] + 6 * dp[i-1][0][0] + 2 * dp[i-1][1][1] + 2 * dp[i-1][2][1];
dp[i][0][2] = 6 * dp[i-1][0][2] + 4 * dp[i-1][0][1] + 2 * dp[i-1][1][1] + 3 * dp[i-1][1][2];
dp[i][0][3] = 8 * dp[i-1][0][3] + 2 * dp[i-1][0][2] + 4 * dp[i-1][1][3] + dp[i-1][1][2];
dp[i][1][1] = 2 * dp[i-1][1][1] + 3 * dp[i-1][3][0];
dp[i][1][2] = 3 * dp[i-1][1][2] + 4 * dp[i-1][2][1] + 2 * dp[i-1][1][1];
dp[i][1][3] = 4 * dp[i-1][1][3] + dp[i-1][1][2];
dp[i][2][1] = 2 * dp[i-1][2][1] + 3 * dp[i-1][3][0];
dp[i][3][0] = dp[i-1][3][0];
}else {
dp[i][0][0] = 2 * dp[i-1][0][0];
dp[i][0][1] = 4 * dp[i-1][0][1] + 6 * dp[i-1][0][0];
dp[i][0][2] = 6 * dp[i-1][0][2] + 4 * dp[i-1][0][1];
dp[i][0][3] = 8 * dp[i-1][0][3] + 2 * dp[i-1][0][2];
dp[i][1][1] = 2 * dp[i-1][1][1];
dp[i][1][2] = 3 * dp[i-1][1][2] + 2 * dp[i-1][1][1];
dp[i][1][3] = 4 * dp[i-1][1][3] + dp[i-1][1][2];
dp[i][2][1] = 2 * dp[i-1][2][1];
dp[i][3][0] = dp[i-1][3][0];
}
for(int j = 0; j < 4; j++) {
dp[i][0][j] %= MOD;
if(dp[i][0][j] < 0)
dp[i][0][j] += MOD;
}
for(int j = 1; j < 4; j++) {
dp[i][1][j] %= MOD;
if(dp[i][1][j] < 0)
dp[i][1][j] += MOD;
}
dp[i][2][1] %= MOD;
if(dp[i][2][1] < 0)
dp[i][2][1] += MOD;
dp[i][3][0] %= MOD;
if(dp[i][3][0] < 0)
dp[i][3][0] += MOD;
//System.out.println(dp[i][0][0] + " " + dp[i][0][1] + " " + dp[i][0][2] + " " + dp[i][0][3]);
//System.out.println(dp[i][1][1] + " " + dp[i][1][2] + " " + dp[i][1][3]);
//System.out.println(dp[i][2][1] + " " + dp[i][3][0]);
}
pw.println((dp[line.length-1][0][3] + dp[line.length-1][1][3]) % MOD);
pw.close();
}
}
| Java | ["101", "1110", "11011111101010010"] | 4 seconds | ["12", "780", "141427753"] | NoteIn the first test case, $$$101_2=5$$$. The triple $$$(a, b, c) = (0, 3, 5)$$$ is valid because $$$(a\oplus b, b\oplus c, c\oplus a) = (3, 6, 5)$$$ are the sides of a non-degenerate triangle. The triple $$$(a, b, c) = (1, 2, 4)$$$ is valid because $$$(a\oplus b, b\oplus c, c\oplus a) = (3, 6, 5)$$$ are the sides of a non-degenerate triangle. The $$$6$$$ permutations of each of these two triples are all the valid triples, thus the answer is $$$12$$$.In the third test case, $$$11\,011\,111\,101\,010\,010_2=114\,514$$$. The full answer (before taking the modulo) is $$$1\,466\,408\,118\,808\,164$$$. | Java 11 | standard input | [
"bitmasks",
"brute force",
"constructive algorithms",
"dp",
"greedy",
"math"
] | 94bc4b263821713fb5b1de4de331a515 | The first and only line contains the binary representation of an integer $$$n$$$ ($$$0 < n < 2^{200\,000}$$$) without leading zeros. For example, the string 10 is the binary representation of the number $$$2$$$, while the string 1010 represents the number $$$10$$$. | 2,500 | Print one integer — the number of triples $$$(a,b,c)$$$ satisfying the conditions described in the statement modulo $$$998\,244\,353$$$. | standard output | |
PASSED | 30093d45c12daaf29406c6264807ac1f | train_108.jsonl | 1658673300 | You are given a positive integer $$$n$$$. Since $$$n$$$ may be very large, you are given its binary representation.You should compute the number of triples $$$(a,b,c)$$$ with $$$0 \leq a,b,c \leq n$$$ such that $$$a \oplus b$$$, $$$b \oplus c$$$, and $$$a \oplus c$$$ are the sides of a non-degenerate triangle. Here, $$$\oplus$$$ denotes the bitwise XOR operation.You should output the answer modulo $$$998\,244\,353$$$.Three positive values $$$x$$$, $$$y$$$, and $$$z$$$ are the sides of a non-degenerate triangle if and only if $$$x+y>z$$$, $$$x+z>y$$$, and $$$y+z>x$$$. | 512 megabytes | import java.io.*;
import java.util.*;
public class a{
public static FastScanner fs;
public static long mod=998244353l;
public static void main(String args[])
{
fs=new FastScanner();
char s[]=fs.next().toCharArray();
int condition[]=new int[8];
Arrays.fill(condition,-1);
for(int i=0;i<8;i++)//precomputation to find which condition will get satisfied
{
int bits[]=new int[3];
for(int j=0;j<3;j++)
bits[j]=(i>>j)&1;
List<Integer>temp=new ArrayList<>(Arrays.asList(bits[1]^bits[2],bits[0]^bits[2],bits[0]^bits[1]));
for(Integer it:temp)
{
if(it>0)
{
for(int j=0;j<3;j++)
if(temp.get(j)==0)
condition[i]=j;
break;
}
}
}//implementation of the tutorial
long dp[][]=new long[8][8];
for(int i=0;i<8;i++)
Arrays.fill(dp[i],0l);
dp[0][0]=1l;//havent started anything so with nothing satisfying the less than n and no condition satisfied is 1 way
for(char c:s)
{
int curbit=c-'0';
long ndp[][]=new long[8][8];
for(int i=0;i<8;i++)
Arrays.fill(ndp[i],0l);
for(int i=0;i<8;i++)
{
for(int j=0;j<8;j++)
{
for(int curbitset=0;curbitset<8;curbitset++)
{
int ni=0,nj=j;
boolean isbad=false;
for(int k=0;k<3;k++)
{
int issmall=(i>>k)&1;
int curbitx=(curbitset>>k)&1;
if(issmall==0 && curbitx>curbit)
{
isbad=true;
break;
}
int nissmall=(issmall>0 ||(curbitx<curbit))?1:0;
ni |= nissmall<<k;
}
if(isbad)
continue;
if(condition[curbitset]>=0)
nj |= 1<<condition[curbitset];
ndp[ni][nj] = (ndp[ni][nj]+dp[i][j])%mod;
}
}
}
dp=ndp;
}
long ans=0l;
for(int i=0;i<8;i++)
ans=(ans+dp[i][7])%mod;
System.out.println(ans);
}
static class FastScanner
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next()
{
while(!st.hasMoreTokens())
{
try{
st=new StringTokenizer(br.readLine());
}
catch(Exception e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
}
} | Java | ["101", "1110", "11011111101010010"] | 4 seconds | ["12", "780", "141427753"] | NoteIn the first test case, $$$101_2=5$$$. The triple $$$(a, b, c) = (0, 3, 5)$$$ is valid because $$$(a\oplus b, b\oplus c, c\oplus a) = (3, 6, 5)$$$ are the sides of a non-degenerate triangle. The triple $$$(a, b, c) = (1, 2, 4)$$$ is valid because $$$(a\oplus b, b\oplus c, c\oplus a) = (3, 6, 5)$$$ are the sides of a non-degenerate triangle. The $$$6$$$ permutations of each of these two triples are all the valid triples, thus the answer is $$$12$$$.In the third test case, $$$11\,011\,111\,101\,010\,010_2=114\,514$$$. The full answer (before taking the modulo) is $$$1\,466\,408\,118\,808\,164$$$. | Java 11 | standard input | [
"bitmasks",
"brute force",
"constructive algorithms",
"dp",
"greedy",
"math"
] | 94bc4b263821713fb5b1de4de331a515 | The first and only line contains the binary representation of an integer $$$n$$$ ($$$0 < n < 2^{200\,000}$$$) without leading zeros. For example, the string 10 is the binary representation of the number $$$2$$$, while the string 1010 represents the number $$$10$$$. | 2,500 | Print one integer — the number of triples $$$(a,b,c)$$$ satisfying the conditions described in the statement modulo $$$998\,244\,353$$$. | standard output | |
PASSED | ef732fe8a0c3ac2b602f001bf0f1276b | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.*;
public class Q1710A {
static int mod = (int) (1e9 + 7);
public static long[] sortLong(long[] a2) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a2)
l.add(i);
Collections.sort(l);
for (int i = 0; i < l.size(); i++)
a2[i] = l.get(i);
return a2;
}
static void solve() {
long n = l();
long m = l();
long k = l();
long[] arr = new long[(int) k];
for (int i = 0; i < arr.length; i++) {
arr[i] = l();
}
// n's case
// arr = sortLong(arr);
if(helper(arr, n, m)){
System.out.println("YES");
return;
}
System.out.println("NO");
}
public static boolean helper(long[] arr, long n, long m) {
boolean chk1r = false;
boolean chk2r = false;
boolean chk1c = false;
boolean chk2c = false;
long row = 0;
long col = 0;
for (int i = 0; i < arr.length; i++) {
long valc = arr[i] / n;
long valr = arr[i] / m;
if (valc > 1) {
col += valc;
if (valc % 2 != 0) {
chk1c = true;
}
if (valc >= 3) {
chk2c = true;
}
}
if (valr > 1) {
row += valr;
if (valr % 2 != 0) {
chk1r = true;
}
if (valr >= 3) {
chk2r = true;
}
}
}
System.err.println(row+" "+col);
if (col >= m) {
if(m%2==0){
return true;
}
if(chk1c){
return true;
}else{
if(chk2c){
return true;
}
}
}
if(row>=n){
if(n%2==0){
return true;
}
if(chk1r){
return true;
}else{
if(chk2r){
return true;
}
}
}
return false;
}
public static void main(String[] args) {
int test = i();
while (test-- > 0) {
solve();
}
}
// ----->segmentTree--> segTree as class
// ----->lazy_Seg_tree --> lazy_Seg_tree as class
// -----> Trie --->Trie as class
// ----->fenwick_Tree ---> fenwick_Tree
// -----> POWER ---> long power(long x, long y) <----
// -----> LCM ---> long lcm(long x, long y) <----
// -----> GCD ---> long gcd(long x, long y) <----
// -----> SIEVE --> ArrayList<Integer> sieve(int N) <-----
// -----> NCR ---> long ncr(int n, int r) <----
// -----> BINARY_SEARCH ---> int binary_Search(long[]arr,long x) <----
// -----> (SORTING OF LONG, CHAR,INT) -->long[] sortLong(long[] a2)<--
// ----> DFS ---> void dfs(ArrayList<ArrayList<Integer>>edges,int child,int
// parent)<---
// ---> NODETOROOT --> ArrayList<Integer>
// node2Root(ArrayList<ArrayList<Integer>>edges,int child,int parent,int tofind)
// <--
// ---> LEVELS_TREE -->levels_Trees(ArrayList<HashSet<Integer>> edges, int
// child, int parent,int[]level,int currLevel) <--
// ---> K_THPARENT --> int k_thparent(int node,int[][]parent,int k) <---
// ---> TWOPOWERITHPARENTFILL --> void twoPowerIthParentFill(int[][]parent) <---
// -----> (INPUT OF INT,LONG ,STRING) -> int i() long l() String s()<-
// tempstart
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
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 String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static InputReader in = new InputReader(System.in);
public static int i() {
return in.Int();
}
public static long l() {
String s = in.String();
return Long.parseLong(s);
}
public static String s() {
return in.String();
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 85fddcf51cbb36445559cbf2f78c886a | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
import java.io.*;
public class A1710{
static FastScanner fs = null;
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
while(t-->0){
int n = fs.nextInt();
int m = fs.nextInt();
int k = fs.nextInt();
int a[] = new int[k];
int mx = 0;
for(int i=0;i<k;i++){
a[i] = fs.nextInt();
mx = Math.max(mx,a[i]);
}
boolean res = false;
long pr = (long)n*(long)m;
if((long)mx>=pr){
res = true;
}
else{
if(n>=0){
long x = (long)0;
int u = 0;
int p =0;
for(int i=0;i<k;i++){
if(a[i]>=(n+n)){
int r = a[i]%(n);
if((a[i]/n)>=3){
if(((a[i])/n)%2==1){
u+=1;
}
p+=1;
}
x+=(long)(a[i]-r);
}
}
if(m%2==1 && u==0){
if(p>=1){
x-=n;
if(x>=pr){
res = true;
}
}
}
else if(x>=pr){
res = true;
}
}
if(m>=0){
// out.println(res);
long x = (long)0;
int u = 0;
int p = 0;
for(int i=0;i<k;i++){
if(a[i]>=(m+m)){
int r = a[i]%(m);
x+=(long)(a[i]-r);
}
if((a[i]/m)>=3){
if(((a[i])/m)%2==1){
u+=1;
}
p+=1;
}
}
// out.println("----"+res+" "+u+" "+p);
if(n%2==1 && u==0){
if(p>=1){
x-=m;
if(x>=pr){
res = true;
}
}
}
else if(x>=pr){
res = true;
}
}
}
// out.println("----");
if(res){
out.println("Yes");
}
else{
out.println("No");
}
}
out.close();
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 99af16da60e20f29b97da136c06a0477 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes |
/**
* @author 86152
* Color the Picture
*/
import java.io.*;
import java.util.*;
import java.util.function.IntUnaryOperator;
import java.util.function.LongUnaryOperator;
import java.util.stream.Collectors;
public class CF1710 {
static In in = new FastIn();
static Out out = new Out(false);
static final long inf = 0x1fffffffffffffffL;
static final int iinf = 0x3fffffff;
static final double eps = 1e-9;
static long mod = 998244353;
void solve() {
int t = in.nextInt();
while (t-- > 0) {
int row = in.nextInt();
int col = in.nextInt();
int k = in.nextInt();
int[] a = in.nextIntArray(k);
out.println(check(row, col, a) || check(col, row, a) ? "Yes" : "No");
}
}
private boolean check(int row, int col, int[] a) {
boolean f = false; long sum = 0;
for (int i = 0; i < a.length; i++) {
int c = a[i] / row;
if (c >= 3) {
f = true;
}
if (c >= 2) {
sum += c;
}
}
return sum >= col && (col % 2 == 0 || f);
}
public static void main(String... args) {
new CF1710().solve();
out.flush();
}
}
class FastIn extends In {
private final BufferedInputStream reader = new BufferedInputStream(System.in);
private final byte[] buffer = new byte[0x10000];
private int i = 0;
private int length = 0;
public int read() {
if (i == length) {
i = 0;
try {
length = reader.read(buffer);
} catch (IOException ignored) {
}
if (length == -1) {
return 0;
}
}
if (length <= i) {
throw new RuntimeException();
}
return buffer[i++];
}
String next() {
StringBuilder builder = new StringBuilder();
int b = read();
while (b < '!' || '~' < b) {
b = read();
}
while ('!' <= b && b <= '~') {
builder.appendCodePoint(b);
b = read();
}
return builder.toString();
}
String nextLine() {
StringBuilder builder = new StringBuilder();
int b = read();
while (b != 0 && b != '\r' && b != '\n') {
builder.appendCodePoint(b);
b = read();
}
if (b == '\r') {
read();
}
return builder.toString();
}
int nextInt() {
long val = nextLong();
if ((int) val != val) {
throw new NumberFormatException();
}
return (int) val;
}
long nextLong() {
int b = read();
while (b < '!' || '~' < b) {
b = read();
}
boolean neg = false;
if (b == '-') {
neg = true;
b = read();
}
long n = 0;
int c = 0;
while ('0' <= b && b <= '9') {
n = n * 10 + b - '0';
b = read();
c++;
}
if (c == 0 || c >= 2 && n == 0) {
throw new NumberFormatException();
}
return neg ? -n : n;
}
}
class In {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 0x10000);
private StringTokenizer tokenizer;
String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException ignored) {
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char[] nextCharArray() {
return next().toCharArray();
}
String[] nextStringArray(int n) {
String[] s = new String[n];
for (int i = 0; i < n; i++) {
s[i] = next();
}
return s;
}
char[][] nextCharGrid(int n, int m) {
char[][] a = new char[n][m];
for (int i = 0; i < n; i++) {
a[i] = next().toCharArray();
}
return a;
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
int[] nextIntArray(int n, IntUnaryOperator op) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = op.applyAsInt(nextInt());
}
return a;
}
int[][] nextIntMatrix(int h, int w) {
int[][] a = new int[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextIntArray(w);
}
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
long[] nextLongArray(int n, LongUnaryOperator op) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = op.applyAsLong(nextLong());
}
return a;
}
long[][] nextLongMatrix(int h, int w) {
long[][] a = new long[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextLongArray(w);
}
return a;
}
List<List<Integer>> nextEdges(int n, int m, boolean directed) {
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
int u = nextInt() - 1;
int v = nextInt() - 1;
res.get(u).add(v);
if (!directed) {
res.get(v).add(u);
}
}
return res;
}
}
class Out {
private final PrintWriter out = new PrintWriter(System.out);
private final PrintWriter err = new PrintWriter(System.err);
boolean autoFlush = false;
boolean enableDebug = false;
Out(boolean enableDebug) {
this.enableDebug = enableDebug;
this.autoFlush = enableDebug;
}
void println(Object... args) {
if (args == null || args.getClass() != Object[].class) {
args = new Object[]{args};
}
out.println(Arrays.stream(args).map(obj -> {
Class<?> clazz = obj == null ? null : obj.getClass();
return clazz == Double.class ? String.format("%.10f", obj) :
clazz == int[].class ? Arrays.toString((int[]) obj) :
clazz == long[].class ? Arrays.toString((long[]) obj) :
clazz == char[].class ? String.valueOf((char[]) obj) :
clazz == double[].class ? Arrays.toString((double[]) obj) :
clazz == boolean[].class ? Arrays.toString((boolean[]) obj) :
obj instanceof Object[] ? Arrays.deepToString((Object[]) obj) :
String.valueOf(obj);
}).collect(Collectors.joining(" ")));
if (autoFlush) {
out.flush();
}
}
void debug(Object... args) {
if (!enableDebug) {
return;
}
if (args == null || args.getClass() != Object[].class) {
args = new Object[]{args};
}
err.println(Arrays.stream(args).map(obj -> {
Class<?> clazz = obj == null ? null : obj.getClass();
return clazz == Double.class ? String.format("%.10f", obj) :
clazz == int[].class ? Arrays.toString((int[]) obj) :
clazz == long[].class ? Arrays.toString((long[]) obj) :
clazz == char[].class ? String.valueOf((char[]) obj) :
clazz == double[].class ? Arrays.toString((double[]) obj) :
clazz == boolean[].class ? Arrays.toString((boolean[]) obj) :
obj instanceof Object[] ? Arrays.deepToString((Object[]) obj) :
String.valueOf(obj);
}).collect(Collectors.joining(" ")));
err.flush();
}
void println(char[] s) {
out.println(String.valueOf(s));
if (autoFlush) {
out.flush();
}
}
void println(double[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (double d : a) {
joiner.add(String.format("%.10f", d));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void println(int[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (int i : a) {
joiner.add(Integer.toString(i));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void println(long[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (long i : a) {
joiner.add(Long.toString(i));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void flush() {
err.flush();
out.flush();
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 00a9d3b06f454f8963f1a1f22679f34a | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes |
/**
* @author 86152
* Color the Picture
*/
import java.io.*;
import java.util.*;
import java.util.function.IntUnaryOperator;
import java.util.function.LongUnaryOperator;
import java.util.stream.Collectors;
public class CF1710 {
static In in = new FastIn();
static Out out = new Out(false);
static final long inf = 0x1fffffffffffffffL;
static final int iinf = 0x3fffffff;
static final double eps = 1e-9;
static long mod = 998244353;
void solve() {
int t = in.nextInt();
while (t-- > 0) {
int h = in.nextInt();
int w = in.nextInt();
int k = in.nextInt();
int[] a = in.nextIntArray(k);
out.println(f(h, w, a) || f(w, h, a) ? "Yes" : "No");
}
}
boolean f(int h, int w, int[] a) {
long sum = 0;
boolean has3 = false;
for (int i = 0; i < a.length; i++) {
int v = a[i] / h;
if (v >= 3) {
has3 = true;
}
if (v >= 2) {
sum += v;
}
}
return sum >= w && (has3 || w % 2 == 0);
}
public static void main(String... args) {
new CF1710().solve();
out.flush();
}
}
class FastIn extends In {
private final BufferedInputStream reader = new BufferedInputStream(System.in);
private final byte[] buffer = new byte[0x10000];
private int i = 0;
private int length = 0;
public int read() {
if (i == length) {
i = 0;
try {
length = reader.read(buffer);
} catch (IOException ignored) {
}
if (length == -1) {
return 0;
}
}
if (length <= i) {
throw new RuntimeException();
}
return buffer[i++];
}
String next() {
StringBuilder builder = new StringBuilder();
int b = read();
while (b < '!' || '~' < b) {
b = read();
}
while ('!' <= b && b <= '~') {
builder.appendCodePoint(b);
b = read();
}
return builder.toString();
}
String nextLine() {
StringBuilder builder = new StringBuilder();
int b = read();
while (b != 0 && b != '\r' && b != '\n') {
builder.appendCodePoint(b);
b = read();
}
if (b == '\r') {
read();
}
return builder.toString();
}
int nextInt() {
long val = nextLong();
if ((int) val != val) {
throw new NumberFormatException();
}
return (int) val;
}
long nextLong() {
int b = read();
while (b < '!' || '~' < b) {
b = read();
}
boolean neg = false;
if (b == '-') {
neg = true;
b = read();
}
long n = 0;
int c = 0;
while ('0' <= b && b <= '9') {
n = n * 10 + b - '0';
b = read();
c++;
}
if (c == 0 || c >= 2 && n == 0) {
throw new NumberFormatException();
}
return neg ? -n : n;
}
}
class In {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 0x10000);
private StringTokenizer tokenizer;
String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException ignored) {
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char[] nextCharArray() {
return next().toCharArray();
}
String[] nextStringArray(int n) {
String[] s = new String[n];
for (int i = 0; i < n; i++) {
s[i] = next();
}
return s;
}
char[][] nextCharGrid(int n, int m) {
char[][] a = new char[n][m];
for (int i = 0; i < n; i++) {
a[i] = next().toCharArray();
}
return a;
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
int[] nextIntArray(int n, IntUnaryOperator op) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = op.applyAsInt(nextInt());
}
return a;
}
int[][] nextIntMatrix(int h, int w) {
int[][] a = new int[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextIntArray(w);
}
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
long[] nextLongArray(int n, LongUnaryOperator op) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = op.applyAsLong(nextLong());
}
return a;
}
long[][] nextLongMatrix(int h, int w) {
long[][] a = new long[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextLongArray(w);
}
return a;
}
List<List<Integer>> nextEdges(int n, int m, boolean directed) {
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
int u = nextInt() - 1;
int v = nextInt() - 1;
res.get(u).add(v);
if (!directed) {
res.get(v).add(u);
}
}
return res;
}
}
class Out {
private final PrintWriter out = new PrintWriter(System.out);
private final PrintWriter err = new PrintWriter(System.err);
boolean autoFlush = false;
boolean enableDebug = false;
Out(boolean enableDebug) {
this.enableDebug = enableDebug;
this.autoFlush = enableDebug;
}
void println(Object... args) {
if (args == null || args.getClass() != Object[].class) {
args = new Object[]{args};
}
out.println(Arrays.stream(args).map(obj -> {
Class<?> clazz = obj == null ? null : obj.getClass();
return clazz == Double.class ? String.format("%.10f", obj) :
clazz == int[].class ? Arrays.toString((int[]) obj) :
clazz == long[].class ? Arrays.toString((long[]) obj) :
clazz == char[].class ? String.valueOf((char[]) obj) :
clazz == double[].class ? Arrays.toString((double[]) obj) :
clazz == boolean[].class ? Arrays.toString((boolean[]) obj) :
obj instanceof Object[] ? Arrays.deepToString((Object[]) obj) :
String.valueOf(obj);
}).collect(Collectors.joining(" ")));
if (autoFlush) {
out.flush();
}
}
void debug(Object... args) {
if (!enableDebug) {
return;
}
if (args == null || args.getClass() != Object[].class) {
args = new Object[]{args};
}
err.println(Arrays.stream(args).map(obj -> {
Class<?> clazz = obj == null ? null : obj.getClass();
return clazz == Double.class ? String.format("%.10f", obj) :
clazz == int[].class ? Arrays.toString((int[]) obj) :
clazz == long[].class ? Arrays.toString((long[]) obj) :
clazz == char[].class ? String.valueOf((char[]) obj) :
clazz == double[].class ? Arrays.toString((double[]) obj) :
clazz == boolean[].class ? Arrays.toString((boolean[]) obj) :
obj instanceof Object[] ? Arrays.deepToString((Object[]) obj) :
String.valueOf(obj);
}).collect(Collectors.joining(" ")));
err.flush();
}
void println(char[] s) {
out.println(String.valueOf(s));
if (autoFlush) {
out.flush();
}
}
void println(double[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (double d : a) {
joiner.add(String.format("%.10f", d));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void println(int[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (int i : a) {
joiner.add(Integer.toString(i));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void println(long[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (long i : a) {
joiner.add(Long.toString(i));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void flush() {
err.flush();
out.flush();
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 8aa6e01216e6dbcdeb78d2e834f5993f | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes |
/**
* @author 86152
* Color the Picture
*/
import java.io.*;
import java.util.*;
import java.util.function.IntUnaryOperator;
import java.util.function.LongUnaryOperator;
import java.util.stream.Collectors;
public class CF1710 {
static In in = new FastIn();
static Out out = new Out(false);
static final long inf = 0x1fffffffffffffffL;
static final int iinf = 0x3fffffff;
static final double eps = 1e-9;
static long mod = 998244353;
void solve() {
int t = in.nextInt();
while (t-- > 0) {
int h = in.nextInt();
int w = in.nextInt();
int k = in.nextInt();
int[] a = in.nextIntArray(k);
out.println(f(h, w, a) || f(w, h, a) ? "Yes" : "No");
}
}
boolean f(int h, int w, int[] a) {
long sum = 0;
boolean has3 = false;
for (int i = 0; i < a.length; i++) {
int v = a[i] / h;
has3 |= v >= 3;
if (v >= 2) {
sum += v;
}
}
return sum >= w && (has3 || w % 2 == 0);
}
public static void main(String... args) {
new CF1710().solve();
out.flush();
}
}
class FastIn extends In {
private final BufferedInputStream reader = new BufferedInputStream(System.in);
private final byte[] buffer = new byte[0x10000];
private int i = 0;
private int length = 0;
public int read() {
if (i == length) {
i = 0;
try {
length = reader.read(buffer);
} catch (IOException ignored) {
}
if (length == -1) {
return 0;
}
}
if (length <= i) {
throw new RuntimeException();
}
return buffer[i++];
}
String next() {
StringBuilder builder = new StringBuilder();
int b = read();
while (b < '!' || '~' < b) {
b = read();
}
while ('!' <= b && b <= '~') {
builder.appendCodePoint(b);
b = read();
}
return builder.toString();
}
String nextLine() {
StringBuilder builder = new StringBuilder();
int b = read();
while (b != 0 && b != '\r' && b != '\n') {
builder.appendCodePoint(b);
b = read();
}
if (b == '\r') {
read();
}
return builder.toString();
}
int nextInt() {
long val = nextLong();
if ((int) val != val) {
throw new NumberFormatException();
}
return (int) val;
}
long nextLong() {
int b = read();
while (b < '!' || '~' < b) {
b = read();
}
boolean neg = false;
if (b == '-') {
neg = true;
b = read();
}
long n = 0;
int c = 0;
while ('0' <= b && b <= '9') {
n = n * 10 + b - '0';
b = read();
c++;
}
if (c == 0 || c >= 2 && n == 0) {
throw new NumberFormatException();
}
return neg ? -n : n;
}
}
class In {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 0x10000);
private StringTokenizer tokenizer;
String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException ignored) {
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char[] nextCharArray() {
return next().toCharArray();
}
String[] nextStringArray(int n) {
String[] s = new String[n];
for (int i = 0; i < n; i++) {
s[i] = next();
}
return s;
}
char[][] nextCharGrid(int n, int m) {
char[][] a = new char[n][m];
for (int i = 0; i < n; i++) {
a[i] = next().toCharArray();
}
return a;
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
int[] nextIntArray(int n, IntUnaryOperator op) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = op.applyAsInt(nextInt());
}
return a;
}
int[][] nextIntMatrix(int h, int w) {
int[][] a = new int[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextIntArray(w);
}
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
long[] nextLongArray(int n, LongUnaryOperator op) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = op.applyAsLong(nextLong());
}
return a;
}
long[][] nextLongMatrix(int h, int w) {
long[][] a = new long[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextLongArray(w);
}
return a;
}
List<List<Integer>> nextEdges(int n, int m, boolean directed) {
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
int u = nextInt() - 1;
int v = nextInt() - 1;
res.get(u).add(v);
if (!directed) {
res.get(v).add(u);
}
}
return res;
}
}
class Out {
private final PrintWriter out = new PrintWriter(System.out);
private final PrintWriter err = new PrintWriter(System.err);
boolean autoFlush = false;
boolean enableDebug = false;
Out(boolean enableDebug) {
this.enableDebug = enableDebug;
this.autoFlush = enableDebug;
}
void println(Object... args) {
if (args == null || args.getClass() != Object[].class) {
args = new Object[]{args};
}
out.println(Arrays.stream(args).map(obj -> {
Class<?> clazz = obj == null ? null : obj.getClass();
return clazz == Double.class ? String.format("%.10f", obj) :
clazz == int[].class ? Arrays.toString((int[]) obj) :
clazz == long[].class ? Arrays.toString((long[]) obj) :
clazz == char[].class ? String.valueOf((char[]) obj) :
clazz == double[].class ? Arrays.toString((double[]) obj) :
clazz == boolean[].class ? Arrays.toString((boolean[]) obj) :
obj instanceof Object[] ? Arrays.deepToString((Object[]) obj) :
String.valueOf(obj);
}).collect(Collectors.joining(" ")));
if (autoFlush) {
out.flush();
}
}
void debug(Object... args) {
if (!enableDebug) {
return;
}
if (args == null || args.getClass() != Object[].class) {
args = new Object[]{args};
}
err.println(Arrays.stream(args).map(obj -> {
Class<?> clazz = obj == null ? null : obj.getClass();
return clazz == Double.class ? String.format("%.10f", obj) :
clazz == int[].class ? Arrays.toString((int[]) obj) :
clazz == long[].class ? Arrays.toString((long[]) obj) :
clazz == char[].class ? String.valueOf((char[]) obj) :
clazz == double[].class ? Arrays.toString((double[]) obj) :
clazz == boolean[].class ? Arrays.toString((boolean[]) obj) :
obj instanceof Object[] ? Arrays.deepToString((Object[]) obj) :
String.valueOf(obj);
}).collect(Collectors.joining(" ")));
err.flush();
}
void println(char[] s) {
out.println(String.valueOf(s));
if (autoFlush) {
out.flush();
}
}
void println(double[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (double d : a) {
joiner.add(String.format("%.10f", d));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void println(int[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (int i : a) {
joiner.add(Integer.toString(i));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void println(long[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (long i : a) {
joiner.add(Long.toString(i));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void flush() {
err.flush();
out.flush();
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 4b9da9efb1ba67dfd376ba70d0008c3f | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes |
/**
* @author 86152
* Color the Picture
*/
import java.io.*;
import java.util.*;
import java.util.function.IntUnaryOperator;
import java.util.function.LongUnaryOperator;
import java.util.stream.Collectors;
public class Main {
static In in = new FastIn();
static Out out = new Out(false);
static final long inf = 0x1fffffffffffffffL;
static final int iinf = 0x3fffffff;
static final double eps = 1e-9;
static long mod = 998244353;
void solve() {
int t = in.nextInt();
while (t-- > 0) {
int h = in.nextInt();
int w = in.nextInt();
int k = in.nextInt();
int[] a = in.nextIntArray(k);
out.println(f(h, w, a) || f(w, h, a) ? "Yes" : "No");
}
}
boolean f(int h, int w, int[] a) {
long sum = 0;
boolean has3 = false;
for (int i = 0; i < a.length; i++) {
int v = a[i] / h;
has3 |= v >= 3;
if (v >= 2) {
sum += v;
}
}
return sum >= w && (has3 || w % 2 == 0);
}
public static void main(String... args) {
new Main().solve();
out.flush();
}
}
class FastIn extends In {
private final BufferedInputStream reader = new BufferedInputStream(System.in);
private final byte[] buffer = new byte[0x10000];
private int i = 0;
private int length = 0;
public int read() {
if (i == length) {
i = 0;
try {
length = reader.read(buffer);
} catch (IOException ignored) {
}
if (length == -1) {
return 0;
}
}
if (length <= i) {
throw new RuntimeException();
}
return buffer[i++];
}
String next() {
StringBuilder builder = new StringBuilder();
int b = read();
while (b < '!' || '~' < b) {
b = read();
}
while ('!' <= b && b <= '~') {
builder.appendCodePoint(b);
b = read();
}
return builder.toString();
}
String nextLine() {
StringBuilder builder = new StringBuilder();
int b = read();
while (b != 0 && b != '\r' && b != '\n') {
builder.appendCodePoint(b);
b = read();
}
if (b == '\r') {
read();
}
return builder.toString();
}
int nextInt() {
long val = nextLong();
if ((int) val != val) {
throw new NumberFormatException();
}
return (int) val;
}
long nextLong() {
int b = read();
while (b < '!' || '~' < b) {
b = read();
}
boolean neg = false;
if (b == '-') {
neg = true;
b = read();
}
long n = 0;
int c = 0;
while ('0' <= b && b <= '9') {
n = n * 10 + b - '0';
b = read();
c++;
}
if (c == 0 || c >= 2 && n == 0) {
throw new NumberFormatException();
}
return neg ? -n : n;
}
}
class In {
private final BufferedReader reader = new BufferedReader(new InputStreamReader(System.in), 0x10000);
private StringTokenizer tokenizer;
String next() {
try {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
} catch (IOException ignored) {
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
char[] nextCharArray() {
return next().toCharArray();
}
String[] nextStringArray(int n) {
String[] s = new String[n];
for (int i = 0; i < n; i++) {
s[i] = next();
}
return s;
}
char[][] nextCharGrid(int n, int m) {
char[][] a = new char[n][m];
for (int i = 0; i < n; i++) {
a[i] = next().toCharArray();
}
return a;
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
int[] nextIntArray(int n, IntUnaryOperator op) {
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = op.applyAsInt(nextInt());
}
return a;
}
int[][] nextIntMatrix(int h, int w) {
int[][] a = new int[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextIntArray(w);
}
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = nextLong();
}
return a;
}
long[] nextLongArray(int n, LongUnaryOperator op) {
long[] a = new long[n];
for (int i = 0; i < n; i++) {
a[i] = op.applyAsLong(nextLong());
}
return a;
}
long[][] nextLongMatrix(int h, int w) {
long[][] a = new long[h][w];
for (int i = 0; i < h; i++) {
a[i] = nextLongArray(w);
}
return a;
}
List<List<Integer>> nextEdges(int n, int m, boolean directed) {
List<List<Integer>> res = new ArrayList<>();
for (int i = 0; i < n; i++) {
res.add(new ArrayList<>());
}
for (int i = 0; i < m; i++) {
int u = nextInt() - 1;
int v = nextInt() - 1;
res.get(u).add(v);
if (!directed) {
res.get(v).add(u);
}
}
return res;
}
}
class Out {
private final PrintWriter out = new PrintWriter(System.out);
private final PrintWriter err = new PrintWriter(System.err);
boolean autoFlush = false;
boolean enableDebug = false;
Out(boolean enableDebug) {
this.enableDebug = enableDebug;
this.autoFlush = enableDebug;
}
void println(Object... args) {
if (args == null || args.getClass() != Object[].class) {
args = new Object[]{args};
}
out.println(Arrays.stream(args).map(obj -> {
Class<?> clazz = obj == null ? null : obj.getClass();
return clazz == Double.class ? String.format("%.10f", obj) :
clazz == int[].class ? Arrays.toString((int[]) obj) :
clazz == long[].class ? Arrays.toString((long[]) obj) :
clazz == char[].class ? String.valueOf((char[]) obj) :
clazz == double[].class ? Arrays.toString((double[]) obj) :
clazz == boolean[].class ? Arrays.toString((boolean[]) obj) :
obj instanceof Object[] ? Arrays.deepToString((Object[]) obj) :
String.valueOf(obj);
}).collect(Collectors.joining(" ")));
if (autoFlush) {
out.flush();
}
}
void debug(Object... args) {
if (!enableDebug) {
return;
}
if (args == null || args.getClass() != Object[].class) {
args = new Object[]{args};
}
err.println(Arrays.stream(args).map(obj -> {
Class<?> clazz = obj == null ? null : obj.getClass();
return clazz == Double.class ? String.format("%.10f", obj) :
clazz == int[].class ? Arrays.toString((int[]) obj) :
clazz == long[].class ? Arrays.toString((long[]) obj) :
clazz == char[].class ? String.valueOf((char[]) obj) :
clazz == double[].class ? Arrays.toString((double[]) obj) :
clazz == boolean[].class ? Arrays.toString((boolean[]) obj) :
obj instanceof Object[] ? Arrays.deepToString((Object[]) obj) :
String.valueOf(obj);
}).collect(Collectors.joining(" ")));
err.flush();
}
void println(char[] s) {
out.println(String.valueOf(s));
if (autoFlush) {
out.flush();
}
}
void println(double[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (double d : a) {
joiner.add(String.format("%.10f", d));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void println(int[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (int i : a) {
joiner.add(Integer.toString(i));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void println(long[] a) {
StringJoiner joiner = new StringJoiner(" ");
for (long i : a) {
joiner.add(Long.toString(i));
}
out.println(joiner);
if (autoFlush) {
out.flush();
}
}
void flush() {
err.flush();
out.flush();
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 4890650cc20be16798878db9c899b4ab | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
static PrintWriter out;
static Kioken sc;
static boolean checkOnlineJudge = System.getProperty("ONLINE_JUDGE") == null;
public static void main(String[] args) throws FileNotFoundException {
boolean t = true;
boolean f = false;
if (checkOnlineJudge) {
out = new PrintWriter("E:/CODEFORCES/output.txt");
sc = new Kioken(new File("E:/CODEFORCES/input.txt"));
} else {
out = new PrintWriter((System.out));
sc = new Kioken();
}
int tt = 1;
tt = sc.nextInt();
while (tt-- > 0) {
solve();
}
out.flush();
out.close();
}
static boolean check(int n, int m, int[] arr, int k){
long totalCols = 0;
boolean hasOdd = false;
for(int i = 0; i < k; i++){
if(arr[i]/n > 1){
totalCols += arr[i]/n;
}
if(arr[i]/n > 2){
hasOdd = true;
}
}
if(m%2 == 0 && totalCols >= m) return true;
if(m%2 != 0 && hasOdd && totalCols >= m) return true;
return false;
}
public static void solve() {
int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();
int[] arr = sc.readArrayInt(k);
reverseSort(arr);
if(check(n, m, arr, k) || check(m, n, arr, k)){
out.println("YES");
}else{
out.println("NO");
}
}
public static long gcd(long a, long b) {
while (b != 0) {
long rem = a % b;
a = b;
b = rem;
}
return a;
}
static long MOD = 1000000007;
static void reverseSort(int[] arr){List<Integer> list = new ArrayList<>();for (int i=0; i<arr.length; i++){list.add(arr[i]);}Collections.sort(list, Collections.reverseOrder());for (int i = 0; i < arr.length; i++){arr[i] = list.get(i);}}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static void sort(long[] a){
ArrayList<Long> l=new ArrayList<>();
for (long i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class Kioken {
// FileInputStream br = new FileInputStream("input.txt");
BufferedReader br;
StringTokenizer st;
Kioken(File filename) {
try {
FileReader fr = new FileReader(filename);
br = new BufferedReader(fr);
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
Kioken() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
public String next() {
while (!st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public boolean hasNext() {
String next = null;
try {
next = br.readLine();
} catch (Exception e) {
}
if (next == null || next.length() == 0) {
return false;
}
st = new StringTokenizer(next);
return true;
}
public int[] readArrayInt(int n){
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = nextInt();
}
return arr;
}
public long[] readArrayLong(int n){
long[] arr = new long[n];
for(int i = 0; i < n; i++){
arr[i] = nextLong();
}
return arr;
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 21dfa13d5af6b086944f14401556bb9b | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.lang.*;
import java.util.*;
public class ComdeFormces {
static pair dp[][];
static boolean mnans;
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
// Reader.init(System.in);
FastReader sc=new FastReader();
BufferedWriter log = new BufferedWriter(new OutputStreamWriter(System.out));
// OutputStream out = new BufferedOutputStream ( System.out );
int t=sc.nextInt();
int tc=1;
while(t--!=0) {
long n=sc.nextInt();
long m=sc.nextInt();
int k=sc.nextInt();
long a[]=new long[k];
for(int i=0;i<k;i++) {
a[i]=sc.nextLong();
}
boolean ans=false;
boolean fg=false,fg2=false;
long nd=n;
long nd2=m;
for(int i=0;i<k;i++) {
if(n*m<=a[i]) {
ans=true;
break;
}
long p=a[i]/n;
long p2=a[i]/m;
if(p2>1) {
nd-=p2;
}
if(p>1) {
nd2-=p;
}
if(p2>2) {
fg=true;
}
if(p>2) {
fg2=true;
}
}
if(n%2!=0 ) {
if(nd==0 || (nd<0 && fg))ans=true;
}
else if(nd<=0)ans=true;
if(m%2!=0) {
if(nd2==0 || (nd2<0 && fg2))ans=true;
}
else if(nd2<=0)ans=true;
log.write((ans?"Yes":"No")+"\n");
}
log.flush();
}
static int[] lps(int a[],String s) {
int i=1;
int j=0;
a[0]=0;
while(i<s.length()) {
if(s.charAt(i)==s.charAt(j)) {
a[i]=j+1;
i++;
j++;
}
else {
if(j!=0) {
j=a[j-1];
}
else {
a[i]=0;
i++;
}
}
}
return a;
}
static int[] zed(char a[]) {
int z[]=new int[a.length];
int l=0;
int r=0;
for(int i=1;i<a.length;i++) {
if(i>r) {
l=r=i;
while(r<a.length && a[r]==a[r-l])r++;
z[i]=r-l;
r--;
}
else {
int k1=i-l;
if(z[k1]<r-i+1) {
z[i]=z[k1];
}
else {
l=i;
while(r<a.length && a[r]==a[r-l])r++;
z[i]=r-l;
r--;
}
}
}
return z;
}
public static class pair2{
int a,b,c,d;
public pair2(int a,int b,int c,int d) {
this.a=a;
this.b=b;
this.c=c;
this.d=d;
}
}
static boolean dfs(ArrayList<ArrayList<Integer>> ar,int src,int pr,HashSet<Integer> hs) {
int cnt=0;
boolean an=false;
for(int k:ar.get(src)) {
if(k==pr)continue;
boolean p=dfs(ar,k,src,hs);
an|=p;
if(p)cnt++;
}
if(cnt>1)mnans=false;
if(hs.contains(src))an=true;
return an;
}
static int find(int el,int p[]) {
if(p[el]<0)return el;
return p[el]=find(p[el],p);
}
static boolean union(int a,int b,int p[]) {
int p1=find(a,p);
int p2=find(b,p);
if(p1>=0 && p1==p2)return false;
else {
if(p[p1]<p[p2]) {
p[p1]+=p[p2];
p[p2]=p1;
}
else {
p[p2]+=p[p1];
p[p1]=p2;
}
return true;
}
}
public static void build(int a[][],int b[]) {
for(int i=0;i<b.length;i++) {
a[i][0]=b[i];
}
int jmp=2;
while(jmp<=b.length) {
for(int j=0;j<b.length;j++) {
int ind=(int)(Math.log(jmp/2)/Math.log(2));
int ind2=(int)(Math.log(jmp)/Math.log(2));
if(j+jmp-1<b.length) {
a[j][ind2]=Math.max(a[j][ind],a[j+(jmp/2)][ind]);
}
}
jmp*=2;
}
// for(int i=0;i<a.length;i++) {
// for(int j=0;j<33;j++) {
// System.out.print(a[i][j]+" ");
// }
// System.out.println();
// }
}
public static void build2(int a[][],int b[]) {
for(int i=0;i<b.length;i++) {
a[i][0]=b[i];
}
int jmp=2;
while(jmp<=b.length) {
for(int j=0;j<b.length;j++) {
int ind=(int)(Math.log(jmp/2)/Math.log(2));
int ind2=(int)(Math.log(jmp)/Math.log(2));
if(j+jmp-1<b.length) {
a[j][ind2]=Math.min(a[j][ind],a[j+(jmp/2)][ind]);
}
}
jmp*=2;
}
// for(int i=0;i<a.length;i++) {
// for(int j=0;j<33;j++) {
// System.out.print(a[i][j]+" ");
// }
// System.out.println();
// }
}
public static int serst(int a[][],int i,int j) {
int len=j-i+1;
int hp=1;
int tlen=len>>=1;
// System.out.println(tlen);
while(tlen!=0) {
tlen>>=1;
hp<<=1;
}
// System.out.println(hp);
int ind=(int)(Math.log(hp)/Math.log(2));
int i2=j+1-hp;
return Math.max(a[i][ind], a[i2][ind]);
}
public static int serst2(int a[][],int i,int j) {
int len=j-i+1;
int hp=1;
int tlen=len>>=1;
// System.out.println(tlen);
while(tlen!=0) {
tlen>>=1;
hp<<=1;
}
// System.out.println(hp);
int ind=(int)(Math.log(hp)/Math.log(2));
int i2=j+1-hp;
return Math.min(a[i][ind], a[i2][ind]);
}
static void update(long f[],long upd,int ind) {
int vl=ind;
while(vl<f.length) {
f[vl]+=upd;
int tp=~vl;
tp++;
tp&=vl;
vl+=tp;
}
}
static long ser(long f[],int ind) {
int vl=ind;
long sm=0;
while(vl!=0) {
sm+=f[vl];
int tp=~vl;
tp++;
tp&=vl;
vl-=tp;
}
return sm;
}
public static void radixSort(int a[]) {
int n=a.length;
int res[]=new int[n];
int p=1;
for(int i=0;i<=8;i++) {
int cnt[]=new int[10];
for(int j=0;j<n;j++) {
a[j]=res[j];
cnt[(a[j]/p)%10]++;
}
for(int j=1;j<=9;j++) {
cnt[j]+=cnt[j-1];
}
for(int j=n-1;j>=0;j--) {
res[cnt[(a[j]/p)%10]-1]=a[j];
cnt[(a[j]/p)%10]--;
}
p*=10;
}
}
static int bits(long n) {
int ans=0;
while(n!=0) {
if((n&1)==1)ans++;
n>>=1;
}
return ans;
}
public static int kadane(int a[]) {
int sum=0,mx=Integer.MIN_VALUE;
for(int i=0;i<a.length;i++) {
sum+=a[i];
mx=Math.max(mx, sum);
if(sum<0) sum=0;
}
return mx;
}
public static int m=(int)(1e9+7);
public static int mul(int a, int b) {
return ((a%m)*(b%m))%m;
}
public static long mul(long a, long b) {
return ((a%m)*(b%m))%m;
}
public static int add(int a, int b) {
return ((a%m)+(b%m))%m;
}
public static long add(long a, long b) {
return ((a%m)+(b%m))%m;
}
//debug
public static <E> void p(E[][] a,String s) {
System.out.println(s);
for(int i=0;i<a.length;i++) {
for(int j=0;j<a[0].length;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
public static void p(int[] a,String s) {
System.out.print(s+"=");
for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");
System.out.println();
}
public static void p(long[] a,String s) {
System.out.print(s+"=");
for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");
System.out.println();
}
public static <E> void p(E a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(ArrayList<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(LinkedList<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(HashSet<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(Stack<E> a,String s){
System.out.println(s+"="+a);
}
public static <E> void p(Queue<E> a,String s){
System.out.println(s+"="+a);
}
//utils
static ArrayList<Integer> divisors(int n){
ArrayList<Integer> ar=new ArrayList<>();
for (int i=2; i<=Math.sqrt(n); i++){
if (n%i == 0){
if (n/i == i) {
ar.add(i);
}
else {
ar.add(i);
ar.add(n/i);
}
}
}
return ar;
}
static ArrayList<Integer> prime(int n){
ArrayList<Integer> ar=new ArrayList<>();
int cnt=0;
boolean pr=false;
while(n%2==0) {
ar.add(2);
n/=2;
}
for(int i=3;i*i<=n;i+=2) {
pr=false;
while(n%i==0) {
n/=i;
ar.add(i);
pr=true;
}
}
if(n>2) ar.add(n);
return ar;
}
static long gcd(long a,long b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static int gcd(int a,int b) {
if(b==0)return a;
else return gcd(b,a%b);
}
static long factmod(long n,long mod) {
if(n==0)return 0;
long ans=1;
long temp=1;
while(temp<=n) {
ans=((ans%mod)*((temp)%mod))%mod;
temp++;
}
return ans%mod;
}
static int ncr(int n, int r){
if(r>n-r)r=n-r;
int ans=1;
for(int i=0;i<r;i++){
ans*=(n-i);
ans/=(i+1);
}
return ans;
}
public static class trip{
int a;
int b;
int c;
public trip(int a,int b,int c) {
this.a=a;
this.b=b;
this.c=c;
}
// public int compareTo(trip q) {
// return this.b-q.b;
// }
}
static void mergesort(int[] a,int start,int end) {
if(start>=end)return ;
int mid=start+(end-start)/2;
mergesort(a,start,mid);
mergesort(a,mid+1,end);
merge(a,start,mid,end);
}
static void merge(int[] a, int start,int mid,int end) {
int ptr1=start;
int ptr2=mid+1;
int b[]=new int[end-start+1];
int i=0;
while(ptr1<=mid && ptr2<=end) {
if(a[ptr1]<=a[ptr2]) {
b[i]=a[ptr1];
ptr1++;
i++;
}
else {
b[i]=a[ptr2];
ptr2++;
i++;
}
}
while(ptr1<=mid) {
b[i]=a[ptr1];
ptr1++;
i++;
}
while(ptr2<=end) {
b[i]=a[ptr2];
ptr2++;
i++;
}
for(int j=start;j<=end;j++) {
a[j]=b[j-start];
}
}
public static class FastReader {
BufferedReader b;
StringTokenizer s;
public FastReader() {
b=new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while(s==null ||!s.hasMoreElements()) {
try {
s=new StringTokenizer(b.readLine());
}
catch(IOException e) {
e.printStackTrace();
}
}
return s.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str="";
try {
str=b.readLine();
}
catch(IOException e) {
e.printStackTrace();
}
return str;
}
boolean hasNext() {
if (s != null && s.hasMoreTokens()) {
return true;
}
String tmp;
try {
b.mark(1000);
tmp = b.readLine();
if (tmp == null) {
return false;
}
b.reset();
} catch (IOException e) {
return false;
}
return true;
}
}
public static class pair{
int a;
int b;
public pair(int a,int b) {
this.a=a;
this.b=b;
}
// public int compareTo(pair b) {
// return this.a-b.a;
//
// }
// // public int compareToo(pair b) {
// return this.b-b.b;
// }
@Override
public String toString() {
return "{"+this.a+" "+this.b+"}";
}
}
static long pow(long a, long pw) {
long temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
public static int md=998244353;
static long mpow(long a, long pw) {
long temp;
if(pw==0)return 1;
temp=pow(a,pw/2)%md;
if(pw%2==0)return mul(temp,temp);
return mul(a,mul(temp,temp));
}
static int pow(int a, int pw) {
int temp;
if(pw==0)return 1;
temp=pow(a,pw/2);
if(pw%2==0)return temp*temp;
return a*temp*temp;
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | f537eb8dc1812e749be81ff15b5d1c16 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class C810 {
public static int n;
static BufferedReader inp = new BufferedReader(new InputStreamReader(System.in));
public static void make(int n) throws IOException {
while (n > 0) {
String line = inp.readLine();
String[] strs = line.trim().split("\\s+");
int row = Integer.parseInt(strs[0]), col = Integer.parseInt(strs[1]), pigm = Integer.parseInt(strs[2]);
int[] arr = new int[pigm];
line = inp.readLine();
strs = line.trim().split("\\s+");
for (int i = 0; i < pigm; i++) {
arr[i] = Integer.parseInt(strs[i]);
}
if (row < 2 || col < 2) {
System.out.println("No");
} else {
long sum = 0;
int i = 0;
boolean b = false;
while (i < pigm) {
if (!b && (arr[i] / col) > 2) {
b = true;
}
if((arr[i]/col) >= 2){
sum += arr[i] / col;
}
i++;
}
if (sum >= row && (row % 2 == 0 || b)) {
System.out.println("Yes");
} else {
sum = 0;
i = 0;
while (i < pigm) {
if (!b && (arr[i] / row) > 2) {
b = true;
}
if((arr[i]/row) >= 2){
sum += arr[i] / row;
}
i++;
}
if (sum >= col && (col % 2 == 0 || b)) {
System.out.println("Yes");
} else {
System.out.println("No");
}
}
}
n--;
}
}
public static void main(String[] args) throws IOException {
String li = inp.readLine();
String[] str = li.trim().split("\\s+");
n = Integer.parseInt(str[0]);
make(n);
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 296dc00b05ade20dfa1387efc4646f03 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static long mod = (int)1e9+7;
static PrintWriter out=new PrintWriter(new BufferedOutputStream(System.out));
public static void main (String[] args) throws java.lang.Exception
{
FastReader sc =new FastReader();
int t=sc.nextInt();
// int t=1;
O : while(t-->0)
{
int N = sc.nextInt();
int M = sc.nextInt();
int K = sc.nextInt();
List<Integer> arr = new ArrayList<>();
for (int i = 0; i < K; i++) {
arr.add(sc.nextInt());
}
long current = 0;
for (int i : arr) {
if (i / N >= 2 && current + 2 <= M) {
current += 2;
}
}
for (int i : arr) {
if (i / N > 2) {
current += i / N - 2;
}
}
if (current >= M) {
out.println("YES");
continue;
}
current = 0;
for (int i : arr) {
if (i / M >= 2 && current + 2 <= N) {
current += 2;
}
}
for (int i : arr) {
if (i / M > 2) {
current += i / M - 2;
}
}
if (current >= N) {
out.println("YES");
} else {
out.println("NO");
}
}
out.flush();
}
static void printN()
{
System.out.println("NO");
}
static void printY()
{
System.out.println("YES");
}
static int findfrequencies(int a[],int n)
{
int count=0;
for(int i=0;i<a.length;i++)
{
if(a[i]==n)
{
count++;
}
}
return count;
}
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());
}
float nextFloat()
{
return Float.parseFloat(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long[] readArrayLong(int n) {
long[] a=new long[n];
for (int i=0; i<n; i++) a[i]=nextLong();
return a;
}
}
public static int[] radixSort2(int[] a)
{
int n = a.length;
int[] c0 = new int[0x101];
int[] c1 = new int[0x101];
int[] c2 = new int[0x101];
int[] c3 = new int[0x101];
for(int v : a) {
c0[(v&0xff)+1]++;
c1[(v>>>8&0xff)+1]++;
c2[(v>>>16&0xff)+1]++;
c3[(v>>>24^0x80)+1]++;
}
for(int i = 0;i < 0xff;i++) {
c0[i+1] += c0[i];
c1[i+1] += c1[i];
c2[i+1] += c2[i];
c3[i+1] += c3[i];
}
int[] t = new int[n];
for(int v : a)t[c0[v&0xff]++] = v;
for(int v : t)a[c1[v>>>8&0xff]++] = v;
for(int v : a)t[c2[v>>>16&0xff]++] = v;
for(int v : t)a[c3[v>>>24^0x80]++] = v;
return a;
}
static int[] EvenOddArragement(int nums[])
{
int i1=0,i2=nums.length-1;
while(i1<i2){
while(nums[i1]%2==0 && i1<i2){
i1++;
}
while(nums[i2]%2!=0 && i2>i1){
i2--;
}
int temp=nums[i1];
nums[i1]=nums[i2];
nums[i2]=temp;
}
return nums;
}
static int gcd(int a, int b) {
while (b != 0) {
int t = a;
a = b;
b = t % b;
}
return a;
}
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
public static HashMap<Integer, Integer> sortByValue(HashMap<Integer, Integer> hm)
{
// Create a list from elements of HashMap
List<Map.Entry<Integer, Integer> > list =
new LinkedList<Map.Entry<Integer, Integer> >(hm.entrySet());
// Sort the list
Collections.sort(list, new Comparator<Map.Entry<Integer, Integer> >() {
public int compare(Map.Entry<Integer, Integer> o1,
Map.Entry<Integer, Integer> o2)
{
return (o1.getValue()).compareTo(o2.getValue());
}
});
// put data from sorted list to hashmap
HashMap<Integer, Integer> temp = new LinkedHashMap<Integer, Integer>();
for (Map.Entry<Integer, Integer> aa : list) {
temp.put(aa.getKey(), aa.getValue());
}
return temp;
}
static int DigitSum(int n)
{
int r=0,sum=0;
while(n>=0)
{
r=n%10;
sum=sum+r;
n=n/10;
}
return sum;
}
static boolean checkPerfectSquare(int number)
{
double sqrt=Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static boolean isPowerOfTwo(int n)
{
if(n==0)
return false;
return (int)(Math.ceil((Math.log(n) / Math.log(2)))) == (int)(Math.floor(((Math.log(n) / Math.log(2)))));
}
static boolean isPrime2(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;
}
static String minLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for(int i=0;i<n;i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[0];
}
static String maxLexRotation(String str)
{
int n = str.length();
String arr[] = new String[n];
String concat = str + str;
for (int i = 0; i < n; i++)
{
arr[i] = concat.substring(i, i + n);
}
Arrays.sort(arr);
return arr[arr.length-1];
}
static class P implements Comparable<P> {
int i, j;
public P(int i, int j) {
this.i=i;
this.j=j;
}
public int compareTo(P o) {
return Integer.compare(i, o.i);
}
}
static class pair{
int i,j;
pair(int x,int y){
i=x;
j=y;
}
}
static int binary_search(int a[],int value)
{
int start=0;
int end=a.length-1;
int mid=start+(end-start)/2;
while(start<=end)
{
if(a[mid]==value)
{
return mid;
}
if(a[mid]>value)
{
end=mid-1;
}
else
{
start=mid+1;
}
mid=start+(end-start)/2;
}
return -1;
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | fb4db8a3efc00db9cbf592189378652a | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
public class Solution
{
boolean isPossible(long main,long sec,long A[])
{
long total=0,count=0;
for(int i=0;i<A.length;i++)
{
long num=A[i]/sec;
if(num>1)
{
if(num>2)
count++;
total+=num;
}
}
if(total<main)
return false;
if(main%2==1 && count==0)
return false;
return true;
}
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int T=sc.nextInt();
for(int k=1;k<=T;k++)
{
long n=sc.nextLong();
long m=sc.nextLong();
int K=sc.nextInt();
long A[]=new long[K];
for(int i=0;i<K;i++)
A[i]=sc.nextLong();
Solution ob=new Solution();
boolean flag=ob.isPossible(n,m,A)||ob.isPossible(m,n,A);
if(flag)
System.out.println("YES");
else
System.out.println("NO");
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 39501635fc12b43a264800356d817ac5 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Random;
import java.util.StringTokenizer;
/*
1
3 3 2
8 8
0 0 0
0 0 0
0 0 0
*/
public class A {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
PrintWriter out=new PrintWriter(System.out);
int T=fs.nextInt();
// int T=1;
for (int tt=0; tt<T; tt++) {
boolean solved = false;
int n = fs.nextInt(), m = fs.nextInt(), k = fs.nextInt();
int[] c = fs.readArray(k);
sort(c);
long stripes = 0;
boolean hasOdd = false;
for (int i = k - 1; i >= 0; i--) {
if (c[i] / n > 1) {
stripes += c[i] / n;
if (c[i] / n > 2) hasOdd = true;
}
}
if (m % 2 == 1 && hasOdd && stripes >= m) {
out.println("YES");
continue;
} else if (m % 2 == 0 && stripes >= m) {
out.println("YES");
continue;
}
stripes = 0;
hasOdd = false;
for (int i = k - 1; i >= 0; i--) {
if (c[i] / m > 1) {
stripes += c[i] / m;
if (c[i] / m > 2) hasOdd = true;
}
}
if (n % 2 == 1 && hasOdd && stripes >= n) {
out.println("YES");
continue;
} else if (n % 2 == 0 && stripes >= n) {
out.println("YES");
continue;
}
out.println("NO");
}
out.close();
}
static final Random random=new Random();
static final int mod=1_000_000_007;
static void ruffleSort(int[] a) {
int n=a.length;//shuffle, then sort
for (int i=0; i<n; i++) {
int oi=random.nextInt(n), temp=a[oi];
a[oi]=a[i]; a[i]=temp;
}
Arrays.sort(a);
}
static long add(long a, long b) {
return (a+b)%mod;
}
static long sub(long a, long b) {
return ((a-b)%mod+mod)%mod;
}
static long mul(long a, long b) {
return (a*b)%mod;
}
static long exp(long base, long exp) {
if (exp==0) return 1;
long half=exp(base, exp/2);
if (exp%2==0) return mul(half, half);
return mul(half, mul(half, base));
}
static long[] factorials=new long[2_000_001];
static long[] invFactorials=new long[2_000_001];
static void precompFacts() {
factorials[0]=invFactorials[0]=1;
for (int i=1; i<factorials.length; i++) factorials[i]=mul(factorials[i-1], i);
invFactorials[factorials.length-1]=exp(factorials[factorials.length-1], mod-2);
for (int i=invFactorials.length-2; i>=0; i--)
invFactorials[i]=mul(invFactorials[i+1], i+1);
}
static long nCk(int n, int k) {
return mul(factorials[n], mul(invFactorials[k], invFactorials[n-k]));
}
static void sort(int[] a) {
ArrayList<Integer> l=new ArrayList<>();
for (int i:a) l.add(i);
Collections.sort(l);
for (int i=0; i<a.length; i++) a[i]=l.get(i);
}
static class FastScanner {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer("");
String next() {
while (!st.hasMoreTokens())
try {
st=new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a=new int[n];
for (int i=0; i<n; i++) a[i]=nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 3973343d0cf35c2b26f776a63868174e | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes |
import java.io.*;
import java.lang.reflect.Array;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.*;
import java.util.Scanner;
import java.util.StringTokenizer;
public class copy {
static int log=30;
static int[][] ancestor;
static int[] depth;
static void sieveOfEratosthenes(int n, ArrayList<Integer> arr) {
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
// If prime[p] is not changed, then it is a
// prime
if (prime[p]) {
// Update all multiples of p
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
// Print all prime numbers
for (int i = 2; i <= n; i++) {
if (prime[i]) {
arr.add(i);
}
}
}
public static long fac(long N, long mod) {
if (N == 0)
return 1;
if(N==1)
return 1;
return ((N % mod) * (fac(N - 1, mod) % mod)) % mod;
}
static long power(long x, long y, long p) {
// Initialize result
long res = 1;
// Update x if it is more than or
// equal to p
x = x % p;
while (y > 0) {
// If y is odd, multiply x
// with result
if (y % 2 == 1)
res = (res * x) % p;
// y must be even now
y = y >> 1; // y = y/2
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static long modInverse(long n, long p) {
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static long nCrModPFermat(long n, long r,
long p) {
if (n < r)
return 0;
// Base case
if (r == 0)
return 1;
return ((fac(n, p) % p * (modInverse(fac(r, p), p)
% p)) % p * (modInverse(fac(n - r, p), p)
% p)) % p;
}
public static int find(int[] parent, int x) {
if (parent[x] == x)
return x;
return find(parent, parent[x]);
}
public static void merge(int[] parent, int[] rank, int x, int y,int[] child) {
int x1 = find(parent, x);
int y1 = find(parent, y);
if (rank[x1] > rank[y1]) {
parent[y1] = x1;
child[x1]+=child[y1];
} else if (rank[y1] > rank[x1]) {
parent[x1] = y1;
child[y1]+=child[x1];
} else {
parent[y1] = x1;
child[x1]+=child[y1];
rank[x1]++;
}
}
public static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static int power(int x, int y, int p)
{
int res = 1;
x = x % p;
while (y > 0) {
if (y % 2 == 1)
res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
// Returns n^(-1) mod p
static int modInverse(int n, int p)
{
return power(n, p - 2, p);
}
// Returns nCr % p using Fermat's
// little theorem.
static int nCrModPFermat(int n, int r,
int p)
{
if (n<r)
return 0;
// Base case
if (r == 0)
return 1;
// Fill factorial array so that we
// can find all factorial of r, n
// and n-r
int[] fac = new int[n + 1];
fac[0] = 1;
for (int i = 1; i <= n; i++)
fac[i] = fac[i - 1] * i % p;
return (fac[n] * modInverse(fac[r], p)
% p * modInverse(fac[n - r], p)
% p)
% p;
}
public static long[][] ncr(int n,int r)
{
long[][] dp=new long[n+1][r+1];
for(int i=0;i<=n;i++)
dp[i][0]=1;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=r;j++)
{
if(j>i)
continue;
dp[i][j]=dp[i-1][j-1]+dp[i-1][j];
}
}
return dp;
}
public static boolean prime(long N)
{
int c=0;
for(int i=2;i*i<=N;i++)
{
if(N%i==0)
++c;
}
return c==0;
}
public static int sparse_ancestor_table(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] child)
{
int c=0;
for(int i:arr.get(x))
{
if(i!=parent)
{
// System.out.println(i+" hello "+x);
depth[i]=depth[x]+1;
ancestor[i][0]=x;
// if(i==2)
// System.out.println(parent+" hello");
for(int j=1;j<log;j++)
ancestor[i][j]=ancestor[ancestor[i][j-1]][j-1];
c+=sparse_ancestor_table(arr,i,x,child);
}
}
child[x]=1+c;
return child[x];
}
public static int lca(int x,int y)
{
if(depth[x]<depth[y])
{
int temp=x;
x=y;
y=temp;
}
x=get_kth_ancestor(depth[x]-depth[y],x);
if(x==y)
return x;
// System.out.println(x+" "+y);
for(int i=log-1;i>=0;i--)
{
if(ancestor[x][i]!=ancestor[y][i])
{
x=ancestor[x][i];
y=ancestor[y][i];
}
}
return ancestor[x][0];
}
public static int get_kth_ancestor(int K,int x)
{
if(K==0)
return x;
int node=x;
for(int i=0;i<log;i++)
{
if(K%2!=0)
{
node=ancestor[node][i];
}
K/=2;
}
return node;
}
public static ArrayList<Integer> primeFactors(int n)
{
// Print the number of 2s that divide n
ArrayList<Integer> factors=new ArrayList<>();
if(n%2==0)
factors.add(2);
while (n%2==0)
{
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if(n%i==0)
factors.add(i);
while (n%i == 0)
{
n /= i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
{
factors.add(n);
}
return factors;
}
static long ans=1,mod=1000000007;
public static void recur(long X,long N,int index,ArrayList<Integer> temp)
{
// System.out.println(X);
if(index==temp.size())
{
System.out.println(X);
ans=((ans%mod)*(X%mod))%mod;
return;
}
for(int i=0;i<=60;i++)
{
if(X*Math.pow(temp.get(index),i)<=N)
recur(X*(long)Math.pow(temp.get(index),i),N,index+1,temp);
else
break;
}
}
public static int upper(ArrayList<Integer> temp,int X)
{
int l=0,r=temp.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(temp.get(mid)==X)
return mid;
// System.out.println(mid+" "+temp.get(mid));
if(temp.get(mid)<X)
l=mid+1;
else
{
if(mid-1>=0 && temp.get(mid-1)>=X)
r=mid-1;
else
return mid;
}
}
return -1;
}
public static int lower(ArrayList<Integer> temp,int X)
{
int l=0,r=temp.size()-1;
while(l<=r)
{
int mid=(l+r)/2;
if(temp.get(mid)==X)
return mid;
// System.out.println(mid+" "+temp.get(mid));
if(temp.get(mid)>X)
r=mid-1;
else
{
if(mid+1<temp.size() && temp.get(mid+1)<=X)
l=mid+1;
else
return mid;
}
}
return -1;
}
public static int[] check(String shelf,int[][] queries)
{
int[] arr=new int[queries.length];
ArrayList<Integer> indices=new ArrayList<>();
for(int i=0;i<shelf.length();i++)
{
char ch=shelf.charAt(i);
if(ch=='|')
indices.add(i);
}
for(int i=0;i<queries.length;i++)
{
int x=queries[i][0]-1;
int y=queries[i][1]-1;
int left=upper(indices,x);
int right=lower(indices,y);
if(left<=right && left!=-1 && right!=-1)
{
arr[i]=indices.get(right)-indices.get(left)+1-(right-left+1);
}
else
arr[i]=0;
}
return arr;
}
static boolean check;
public static void check(ArrayList<ArrayList<Integer>> arr,int x,int[] color,boolean[] visited)
{
visited[x]=true;
PriorityQueue<Integer> pq=new PriorityQueue<>();
for(int i:arr.get(x))
{
if(color[i]<color[x])
pq.add(color[i]);
if(color[i]==color[x])
check=false;
if(!visited[i])
check(arr,i,color,visited);
}
int start=1;
while(pq.size()>0)
{
int temp=pq.poll();
if(temp==start)
++start;
else
break;
}
if(start!=color[x])
check=false;
}
static boolean cycle;
public static void cycle(boolean[] stack,boolean[] visited,int x,ArrayList<ArrayList<Integer>> arr)
{
if(stack[x])
{
cycle=true;
return;
}
visited[x]=true;
for(int i:arr.get(x))
{
if(!visited[x])
{
cycle(stack,visited,i,arr);
}
}
stack[x]=false;
}
public static int check(char[][] ch,int x,int y)
{
int cnt=0;
int c=0;
int N=ch.length;
int x1=x,y1=y;
while(c<ch.length)
{
if(ch[x][y]=='1')
++cnt;
// if(x1==0 && y1==3)
// System.out.println(x+" "+y+" "+cnt);
x=(x+1)%N;
y=(y+1)%N;
++c;
}
return cnt;
}
public static void s(char[][] arr,int x)
{
char start=arr[arr.length-1][x];
for(int i=arr.length-1;i>0;i--)
{
arr[i][x]=arr[i-1][x];
}
arr[0][x]=start;
}
public static void shuffle(char[][] arr,int x,int down)
{
int N= arr.length;
down%=N;
char[] store=new char[N-down];
for(int i=0;i<N-down;i++)
store[i]=arr[i][x];
for(int i=0;i<arr.length;i++)
{
if(i<down)
{
// Printing rightmost
// kth elements
arr[i][x]=arr[N + i - down][x];
}
else
{
// Prints array after
// 'k' elements
arr[i][x]=store[i-down];
}
}
}
public static String form(int C1,char ch1,char ch2)
{
char ch=ch1;
String s="";
for(int i=1;i<=C1;i++)
{
s+=ch;
if(ch==ch1)
ch=ch2;
else
ch=ch1;
}
return s;
}
public static void printArray(long[] arr)
{
for(int i=0;i<arr.length;i++)
System.out.print(arr[i]+" ");
System.out.println();
}
public static boolean check(long mid,long[] arr,long K)
{
long[] arr1=Arrays.copyOfRange(arr,0,arr.length);
long ans=0;
for(int i=0;i<arr1.length-1;i++)
{
if(arr1[i]+arr1[i+1]>=mid)
{
long check=(arr1[i]+arr1[i+1])/mid;
// if(mid==5)
// System.out.println(check);
long left=check*mid;
left-=arr1[i];
if(left>=0)
arr1[i+1]-=left;
ans+=check;
}
// if(mid==5)
// printArray(arr1);
}
// if(mid==5)
// System.out.println(ans);
ans+=arr1[arr1.length-1]/mid;
return ans>=K;
}
public static long search(long sum,long[] arr,long K)
{
long l=1,r=sum/K;
while(l<=r)
{
long mid=(l+r)/2;
if(check(mid,arr,K))
{
if(mid+1<=sum/K && check(mid+1,arr,K))
l=mid+1;
else
return mid;
}
else
r=mid-1;
}
return -1;
}
public static void primeFactors(int n,HashSet<Integer> hp)
{
// Print the number of 2s that divide n
ArrayList<Integer> factors=new ArrayList<>();
if(n%2==0)
hp.add(2);
while (n%2==0)
{
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
if(n%i==0)
hp.add(i);
while (n%i == 0)
{
n /= i;
}
}
// This condition is to handle the case when
// n is a prime number greater than 2
if (n > 2)
{
hp.add(n);
}
}
public static boolean check(String s)
{
HashSet<Character> hp=new HashSet<>();
char ch=s.charAt(0);
for(int i=1;i<s.length();i++)
{
// System.out.println(hp+" "+s.charAt(i));
if(hp.contains(s.charAt(i)))
{
// System.out.println(i);
// System.out.println(hp);
// System.out.println(s.charAt(i));
return false;
}
if(s.charAt(i)!=ch)
{
hp.add(ch);
ch=s.charAt(i);
}
}
return true;
}
public static int check_end(String[] arr,boolean[] st,char ch)
{
for(int i=0;i<arr.length;i++)
{
if(ch==arr[i].charAt(0) && !st[i] && ch==arr[i].charAt(arr[i].length()-1))
return i;
}
for(int i=0;i<arr.length;i++)
{
if(ch==arr[i].charAt(0) && !st[i])
return i;
}
return -1;
}
public static int check_start(String[] arr,boolean[] st,char ch)
{
for(int i=0;i<arr.length;i++)
{
// if(ch=='B')
// {
// if(!st[i])
// System.out.println(arr[i]+" hello");
// }
if(ch==arr[i].charAt(arr[i].length()-1) && !st[i] && ch==arr[i].charAt(0))
return i;
}
for(int i=0;i<arr.length;i++)
{
// if(ch=='B')
// {
// if(!st[i])
// System.out.println(arr[i]+" hello");
// }
if(ch==arr[i].charAt(arr[i].length()-1) && !st[i])
return i;
}
return -1;
}
public static boolean palin(int N)
{
String s="";
while(N>0)
{
s+=N%10;
N/=10;
}
int l=0,r=s.length()-1;
while(l<=r)
{
if(s.charAt(l)!=s.charAt(r))
return false;
++l;
--r;
}
return true;
}
public static boolean check(long org_s,long org_d,long org_n,long check_ele)
{
if(check_ele<org_s)
return false;
if((check_ele-org_s)%org_d!=0)
return false;
long num=(check_ele-org_s)/org_d;
// if(check_ele==5)
// System.out.println(num+" "+org_n);
return num+1<=org_n;
}
public static long check(long c,long c_diff,long mod,long b_start,long c_start, long c_end,long b_end,long b_diff)
{
// System.out.println(c);
long max=Math.max(c,b_diff);
long min=Math.min(c,b_diff);
long lcm=(max/gcd(max,min))*min;
// System.out.println(lcm);
// System.out.println(c);
// System.out.println(c_diff);
// if(b_diff>c)
// {
long start_point=c_diff/c-c_diff/lcm;
// System.out.println(start_point);
// }
// else
// {
// start_point=c_diff/b_diff-c_diff/c;
// }
// System.out.println(c+" "+start_point);
return (start_point%mod*start_point%mod)%mod;
}
public static boolean check_bounds(int x,int y,int[][] arr,int N,int M)
{
return x>=0 && x<N && y>=0 && y<M && (arr[x][y]==1 || arr[x][y]==9);
}
static boolean found=false;
public static void check(int x,int y,int[][] arr,boolean status[][])
{
if(arr[x][y]==9)
{
found=true;
return;
}
status[x][y]=true;
if(check_bounds(x-1,y,arr,arr.length,arr[0].length)&& !status[x-1][y])
check(x-1,y,arr,status);
if(check_bounds(x+1,y,arr,arr.length,arr[0].length)&& !status[x+1][y])
check(x+1,y,arr,status);
if(check_bounds(x,y-1,arr,arr.length,arr[0].length)&& !status[x][y-1])
check(x,y-1,arr,status);
if(check_bounds(x,y+1,arr,arr.length,arr[0].length)&& !status[x][y+1])
check(x,y+1,arr,status);
}
public static int check(String s1,String s2,int M)
{
int ans=0;
for(int i=0;i<M;i++)
{
ans+=Math.abs(s1.charAt(i)-s2.charAt(i));
}
return ans;
}
public static int check(int[][] arr,int dir1,int dir2,int x1,int y1)
{
int sum=0,N=arr.length,M=arr[0].length;
int x=x1+dir1,y=y1+dir2;
while(x<N && x>=0 && y<M && y>=0)
{
sum+=arr[x][y];
x=x+dir1;
y+=dir2;
}
return sum;
}
public static int check(long[] pref,long X,int N)
{
if(X>pref[N-1])
return -1;
// System.out.println(pref[0]);
if(X<=pref[0])
return 1;
int l=0,r=N-1;
while(l<=r)
{
int mid=(l+r)/2;
if(pref[mid]>=X)
{
if(mid-1>=0 && pref[mid-1]<X)
return mid+1;
else
r=mid-1;
}
else
l=mid+1;
}
return -1;
}
private static long mergeAndCount(long[] arr, int l,
int m, int r)
{
// Left subarray
long[] left = Arrays.copyOfRange(arr, l, m + 1);
// Right subarray
long[] right = Arrays.copyOfRange(arr, m + 1, r + 1);
int i = 0, j = 0, k = l;long swaps = 0;
while (i < left.length && j < right.length) {
if (left[i] < right[j])
arr[k++] = left[i++];
else {
arr[k++] = right[j++];
swaps += (m + 1) - (l + i);
}
}
while (i < left.length)
arr[k++] = left[i++];
while (j < right.length)
arr[k++] = right[j++];
return swaps;
}
// Merge sort function
private static long mergeSortAndCount(long[] arr, int l,
int r)
{
// Keeps track of the inversion count at a
// particular node of the recursion tree
long count = 0;
if (l < r) {
int m = (l + r) / 2;
// Total inversion count = left subarray count
// + right subarray count + merge count
// Left subarray count
count += mergeSortAndCount(arr, l, m);
// Right subarray count
count += mergeSortAndCount(arr, m + 1, r);
// Merge count
count += mergeAndCount(arr, l, m, r);
}
return count;
}
public static long check(long L,long R)
{
long ans=0;
for(int i=1;i<=Math.pow(10,8);i++)
{
long A=i*(long)i;
if(A<L)
continue;
long upper=(long)Math.floor(Math.sqrt(A-L));
long lower=(long)Math.ceil(Math.sqrt(Math.max(A-R,0)));
if(upper>=lower)
ans+=upper-lower+1;
}
return ans;
}
public static int check(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[]store)
{
int index=0;
ArrayList<Integer> temp=arr.get(x);
for(int i:temp)
{
if(i!=parent)
{
index+=check(arr,i,x,store);
}
}
store[x]=index;
return index+1;
}
public static void finans(int[][] store,ArrayList<ArrayList<Integer>> arr,int x,int parent)
{
// ++delete;
// System.out.println(x);
if(store[x][0]==0 && store[x][1]==0)
return;
if(store[x][0]!=0 && store[x][1]==0)
{
++delete;
ans+=store[x][0];
return;
}
if(store[x][0]==0 && store[x][1]!=0)
{
++delete;
ans+=store[x][1];
return;
}
ArrayList<Integer> temp=arr.get(x);
if(store[x][0]!=0 && store[x][1]!=0)
{
++delete;
if(store[x][0]>store[x][1])
{
ans+=store[x][0];
for(int i=temp.size()-1;i>=0;i--)
{
if(temp.get(i)!=parent)
{
finans(store,arr,temp.get(i),x);
break;
}
}
}
else
{
ans+=store[x][1];
for(int i=0;i<temp.size();i++)
{
if(temp.get(i)!=parent)
{
finans(store,arr,temp.get(i),x);
break;
}
}
}
}
}
public static int dfs(ArrayList<ArrayList<Integer>> arr,int x,int parent,int[] store)
{
int index1=-1,index2=-1;
for(int i=0;i<arr.get(x).size();i++)
{
if(arr.get(x).get(i)!=parent)
{
if(index1==-1)
{
index1=i;
}
else
index2=i;
}
}
if(index1==-1)
{
return 0;
}
if(index2==-1)
{
return store[arr.get(x).get(index1)];
}
// System.out.println(x);
// System.out.println();;
return Math.max(store[arr.get(x).get(index1)]+dfs(arr,arr.get(x).get(index2),x,store),store[arr.get(x).get(index2)]+dfs(arr,arr.get(x).get(index1),x,store));
}
static int delete=0;
public static boolean bounds(int x,int y,int N,int M)
{
return x>=0 && x<N && y>=0 && y<M;
}
public static int gcd_check(ArrayList<Integer> temp,char[] ch, int[] arr)
{
ArrayList<Integer> ini=new ArrayList<>(temp);
for(int i=0;i<temp.size();i++)
{
for(int j=0;j<temp.size();j++)
{
int req=temp.get(j);
temp.set(j,arr[req-1]);
}
boolean status=true;
for(int j=0;j<temp.size();j++)
{
if(ch[ini.get(j)-1]!=ch[temp.get(j)-1])
status=false;
}
if(status)
return i+1;
}
return temp.size();
}
static long LcmOfArray(int[] arr, int idx)
{
// lcm(a,b) = (a*b/gcd(a,b))
if (idx == arr.length - 1){
return arr[idx];
}
int a = arr[idx];
long b = LcmOfArray(arr, idx+1);
return (a*b/gcd(a,b)); //
}
public static boolean check(ArrayList<Integer> arr,int sum)
{
for(int i=0;i<arr.size();i++)
{
for(int j=i+1;j<arr.size();j++)
{
for(int k=j+1;k<arr.size();k++)
{
if(arr.get(i)+arr.get(j)+arr.get(k)==sum)
return true;
}
}
}
return false;
}
public static void check(ArrayList<ArrayList<Integer>> arr,int x,int parent,long[] l,long[] r,int[] left,int[] right)
{
if(arr.get(x).size()==1 && x!=0)
{
l[x]=left[x];
r[x]=right[x];
++ans;
return;
}
long l1=0,r1=0;
for(int i:arr.get(x))
{
if(i!=parent)
{
check(arr,i,x,l,r,left,right);
l1+=l[i];
r1+=r[i];
}
}
// System.out.println(l1+" "+r1);
if(r1<left[x])
{
l[x]=left[x];
r[x]=right[x];
++ans;
}
else
{
l[x]=Math.max(l1,left[x]);
r[x]=Math.min(r1,right[x]);
}
}
// Returns true if str1 is smaller than str2.
static boolean isSmaller(String str1, String str2)
{
// Calculate lengths of both string
int n1 = str1.length(), n2 = str2.length();
if (n1 < n2)
return true;
if (n2 < n1)
return false;
for (int i = 0; i < n1; i++)
if (str1.charAt(i) < str2.charAt(i))
return true;
else if (str1.charAt(i) > str2.charAt(i))
return false;
return false;
}
public static void transform(List<Integer> arr)
{
}
public static int check(List<String> history)
{
int[][] arr=new int[history.size()][history.get(0).length()];
for(int i=0;i<arr.length;i++)
{
for(int j=0;j<arr[0].length;j++)
{
arr[i][j]=history.get(i).charAt(j)-48;
}
}
for(int i=0;i<arr.length;i++)
Arrays.sort(arr[i]);
int sum=0;
for(int i=0;i<arr[0].length;i++)
{
int max=0;
for(int j=0;j<arr.length;j++)
max=Math.max(max,arr[j][i]);
sum+=max;
}
return sum;
}
// Function for find difference of larger numbers
static String findDiff(String str1, String str2)
{
// Before proceeding further, make sure str1
// is not smaller
if (isSmaller(str1, str2)) {
String t = str1;
str1 = str2;
str2 = t;
}
// Take an empty string for storing result
String str = "";
// Calculate length of both string
int n1 = str1.length(), n2 = str2.length();
// Reverse both of strings
str1 = new StringBuilder(str1).reverse().toString();
str2 = new StringBuilder(str2).reverse().toString();
int carry = 0;
// Run loop till small string length
// and subtract digit of str1 to str2
for (int i = 0; i < n2; i++) {
// Do school mathematics, compute difference of
// current digits
int sub
= ((int)(str1.charAt(i) - '0')
- (int)(str2.charAt(i) - '0') - carry);
// If subtraction is less then zero
// we add then we add 10 into sub and
// take carry as 1 for calculating next step
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str += (char)(sub + '0');
}
// subtract remaining digits of larger number
for (int i = n2; i < n1; i++) {
int sub = ((int)(str1.charAt(i) - '0') - carry);
// if the sub value is -ve, then make it
// positive
if (sub < 0) {
sub = sub + 10;
carry = 1;
}
else
carry = 0;
str += (char)(sub + '0');
}
// reverse resultant string
return new StringBuilder(str).reverse().toString();
}
static int nCr(int n, int r)
{
return fact(n) / (fact(r) *
fact(n - r));
}
// Returns factorial of n
static int fact(int n)
{
int res = 1;
for (int i = 2; i <= n; i++)
res = res * i;
return res;
}
public static boolean answer(int[] arr,int x,int y)
{
long tot=0,max=0;
boolean status=true;
for (int j : arr) {
if (j / x > 1) {
tot += j / x;
if(j/x!=2)
status=false;
max = Math.max(max, j / x);
}
}
if(y%2!=0 && status)
{
return false;
}
return tot>=y;
}
public static void main(String[] args) throws IOException{
Reader.init(System.in);
BufferedWriter output = new BufferedWriter(new OutputStreamWriter(System.out));
int T=Reader.nextInt();
for(int m=1;m<=T;m++)
{
int N=Reader.nextInt();
int M=Reader.nextInt();
int k=Reader.nextInt();
int[] color=new int[k];
for(int i=0;i<k;i++)
color[i]=Reader.nextInt();
if(answer(color,N,M ) || answer(color,M,N))
output.write("YES"+"\n");
else
output.write("NO"+"\n");
}
output.flush();
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input));
tokenizer = new StringTokenizer("");
}
static String next() throws IOException {
while (!tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(
reader.readLine());
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
}
class TreeNode
{
int data;
TreeNode left;
TreeNode right;
TreeNode(int data)
{
left=null;
right=null;
this.data=data;
}
}
class div {
int x;
int y;
div(int x,int y) {
this.x=x;
this.y=y;
}
}
class trie_node
{
trie_node[] arr;
trie_node()
{
arr=new trie_node[26];
}
public static void insert(trie_node root,String s)
{
trie_node tmp=root;
for(int i=0;i<s.length();i++)
{
if(tmp.arr[s.charAt(i)-97]!=null)
{
tmp=tmp.arr[s.charAt(i)-97];
}
else
{
tmp.arr[s.charAt(i)-97]=new trie_node();
tmp=tmp.arr[s.charAt(i)-97];
}
}
}
public static boolean search(trie_node root,String s)
{
trie_node tmp=root;
for(int i=0;i<s.length();i++)
{
if(tmp.arr[s.charAt(i)-97]!=null)
{
tmp=tmp.arr[s.charAt(i)-97];
}
else
{
return false;
}
}
return true;
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | b957aac0015e056455b7d62d27971105 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
/**
*
* @author xpeng
*/
public class codeforces {
public static boolean test(int row,int col,int[] arr){
long sum=0;
for (int i = 0; i < arr.length; i++) {
if (Math.floor(arr[i]/(double)row)>1) {
sum+=Math.floor(arr[i]/(double)row);
}
}
if (sum>=col) {
if (col%2==0) {
return true;
}else{
//boolean flag = false;
int maxe=1;
int maxo=1;
for (int i = 0; i < arr.length; i++) {
if (Math.floor(arr[i]/(double)row)%2==0) {
if (Math.floor(arr[i]/(double)row)>maxe) {
maxe=(int) Math.floor(arr[i]/(double)row);
}
}else{
if (Math.floor(arr[i]/(double)row)>maxo) {
maxo=(int) Math.floor(arr[i]/(double)row);
}
}
}
if (maxe-1+sum>=col&&maxe-1!=1) {
return true;
}
if (maxo+sum>=col&&maxo!=1) {
return true;
}
return false;
}
}else{
return false;
}
}
public static void main(String[] args) {
//System.out.println(3&1);
Scanner scanner = new Scanner(System.in);
int t = scanner.nextInt();
for (int i = 0; i < t; i++) {
int n = scanner.nextInt(),m=scanner.nextInt(),k=scanner.nextInt();
int[] arr = new int[k];
for (int j = 0; j < k; j++) {
arr[j]=scanner.nextInt();
}
if (test(n,m,arr)) {
System.out.println("YES");
}else{
if (test(m,n,arr)) {
System.out.println("YES");
}else{
System.out.println("NO");
}
}
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | f02e719f336e3fdcee4e9b7e3af36e9a | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | /* package codechef; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
//------------------------------------------------input class---------------------------------------//
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;
}
}
//-------------------------------------------segment tree-----------------------------------------//
static int[] segment;
static void constructSt(int n, int[] arr){
segment = new int[n*4+1];
formSt(arr, 1,0,n-1);
}
public static void formSt(int[] arr, int node, int s, int e){
if(s==e){
segment[node]= arr[s];
return;
}
formSt(arr, node*2,s,s+(e-s)/2);
formSt(arr, node*2+1,s+(e-s)/2+1,e);
segment[node]=Math.max(segment[node*2],segment[node*2+1]);
}
public static int findMax( int node, int s, int e,int l , int r){
if(l>e||s>r) return -1;
if(s==e) return segment[node];
if(l<=s&&r>=e) return segment[node];
int mid = s+(e-s)/2;
return Math.max(findMax(node*2,s,mid,l,r),findMax(node*2+1,mid+1,e,l,r));
}
//-----------------------------------------dsu-----------------------------------------//
static int[] parent,rank;
public static void dsu(int n){
parent = new int[n]; rank = new int[n];
for(int i =0;i<n;i++) parent[i]=i;
}
public static int find(int i){
if(i==parent[i] ) return i;
return parent[i]=find(parent[i]);
}
public static void merge(int i, int j){
if(rank[i]>=rank[j]){
rank[i]+=rank[j];
parent[j]=i;
}
else {
rank[j]+=rank[i];
parent[i]=j;
}
}
//-------------------------------------------topological sort--------------------------------------//
public static int[] topo(List<List<Integer>> a , int n , int[] in){
int[] ans = new int[n+1];
PriorityQueue<Integer> p = new PriorityQueue<>((x,y)->a.get(x).size()-a.get(y).size());
for(int i =1;i<=n;i++) if(in[i]==0) p.add(i);
int i =1;
while(p.size()>0){
int e = p.poll();
ans[i++]= e;
for(int temp : a.get(e)){
in[temp]--;
if(in[temp]==0)
p.add(temp);
}
}
return ans;
}
//-------------------------------------------max--------------------------------------------------//
public static int max (int a, int b){
return a>=b?a:b;
}
public static long max (long a, long b){
return a>=b?a:b;
}
public static int max (int a, int b, int c){
return Math.max(a,Math.max(b,c));
}
public static long max (long a, long b, long c){
return Math.max(a,Math.max(b,c));
}
//------------------------------------------min---------------------------------------------------//
public static int min (int a, int b){
return a<=b?a:b;
}
public static long min (long a, long b){
return a<=b?a:b;
}
public static int min (int a, int b, int c){
return Math.min(a,Math.min(b,c));
}
public static long min (long a, long b, long c){
return Math.min(a,Math.min(b,c));
}
//----------------------------------------- gcd-----------------------------------------//
public static int gcd(int a, int b){
if(a==0) return b;
if(b==0 ) return a;
if(a<b) return gcd(b%a,a);
return gcd(a%b,b);
}
public static long gcd(long a, long b){
if(a==0) return b;
if(b==0 ) return a;
if(a<b) return gcd(b%a,a);
return gcd(a%b,b);
}
//-------------------------------------lcm------------------------------------------------------//
public static int lcm(int a, int b){
return (a*b)/gcd(a,b);
}
public static long lcm(long a, long b){
return (a*b)/gcd(a,b);
}
//---------------------------------------------binary search--------------------------------------//
public static int binary(int arr[], int x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x) return m;
if (arr[m] < x) l = m + 1;
else r = m - 1;
}
return -1;
}
public static int binary(long arr[], long x)
{
int l = 0, r = arr.length - 1;
while (l <= r) {
int m = l + (r - l) / 2;
if (arr[m] == x) return m;
if (arr[m] < x) l = m + 1;
else r = m - 1;
}
return -1;
}
//---------------------------------------helping methods---------------------------------------------//
//------------------------------------------main------------------------------------------------------//
public static void main (String[] args) throws java.lang.Exception
{
OutputStream outputStream =System.out;
PrintWriter out =new PrintWriter(outputStream);
FastReader sc =new FastReader();
int t =sc.nextInt();
while(t-->0){
int n = sc.nextInt(), m = sc.nextInt(), k = sc.nextInt();
long[] a = new long[k];
for(int i =0;i<k;i++) a[i]=sc.nextLong();
boolean c = false, g=false;
long sum = 0, max =0;
for(long e:a){
max = max(max,e);
e= e/m;
g|= e>2;
if(e>=2) sum+= e;
}
c|= (sum>=n)&&(g||n%2==0);
sum =0;
g=false;
for(long e:a){
e= e/n;
g|= e>2;
if(e>=2) sum+= e;
}
c|= (sum>=m)&&(g||m%2==0);
out.print(c?"Yes":"No");
out.print("\n");
}
out.close();
// your code goes here"
}
}
class pair
{
long x;
long y;
public pair(long x , long y)
{
this.x= x;
this.y= y;
}
}
class node{
int x, y;
public node(int x , int y)
{
this.x= x;
this.y= y;
}
}
class solution{
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 2b9bc8ea3f5db65e97f60960256f7444 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
static final PrintWriter out =new PrintWriter(System.out);
static final FastReader sc = new FastReader();
//I invented a new word!Plagiarism!
//Did you hear about the mathematician who’s afraid of negative numbers?He’ll stop at nothing to avoid them
//What do Alexander the Great and Winnie the Pooh have in common? Same middle name.
//I finally decided to sell my vacuum cleaner. All it was doing was gathering dust!
//ArrayList<Integer> a=new ArrayList <Integer>();
//PriorityQueue<Integer> pq=new PriorityQueue<>();
//char[] a = s.toCharArray();
// char s[]=sc.next().toCharArray();
public static boolean sorted(int a[])
{
int n=a.length,i;
int b[]=new int[n];
for(i=0;i<n;i++)
b[i]=a[i];
Arrays.sort(b);
for(i=0;i<n;i++)
{
if(a[i]!=b[i])
return false;
}
return true;
}
public static void main (String[] args) throws java.lang.Exception
{
int tes=sc.nextInt();
label: while(tes-->0)
{
int n=sc.nextInt();
int m=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[k];
int i,min=Math.min(m,n),max=Math.max(m,n);
long sum=0;
boolean flag=false;
for(i=0;i<k;i++)
{
int p=sc.nextInt();
a[i]=p;
if(p/n > 1){
sum+=(p/n);
if(p/n>2)
flag=true;
}
}
if(sum>=m && (m%2==0 || flag))
{
System.out.println("YES");
continue;
}
flag=false;
sum=0;
for(i=0;i<k;i++)
{
int p=a[i];
if(p/m > 1){
sum+=(p/m);
if(p/m > 2)
flag=true;
}
}
if(sum>=n && (n%2==0 || flag))
{
System.out.println("YES");
continue;
}
System.out.println("NO");
}
}
public static int first(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == 0 || x > arr.get(mid-1)) && arr.get(mid) == x)
return mid;
else if (x > arr.get(mid))
return first(arr, (mid + 1), high, x, n);
else
return first(arr, low, (mid - 1), x, n);
}
return -1;
}
public static int last(ArrayList<Integer> arr, int low, int high, int x, int n)
{
if (high >= low) {
int mid = low + (high - low) / 2;
if ((mid == n - 1 || x < arr.get(mid+1)) && arr.get(mid) == x)
return mid;
else if (x < arr.get(mid))
return last(arr, low, (mid - 1), x, n);
else
return last(arr, (mid + 1), high, x, n);
}
return -1;
}
public static int lis(int[] arr) {
int n = arr.length;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(arr[0]);
for(int i = 1 ; i<n;i++) {
int x = al.get(al.size()-1);
if(arr[i]>=x) {
al.add(arr[i]);
}else {
int v = upper_bound(al, 0, al.size(), arr[i]);
al.set(v, arr[i]);
}
}
return al.size();
}
public static int lower_bound(ArrayList<Long> ar,int lo , int hi , long k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get((int)mid) <k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
public static int upper_bound(ArrayList<Integer> ar,int lo , int hi, int k)
{
Collections.sort(ar);
int s=lo;
int e=hi;
while (s !=e)
{
int mid = s+e>>1;
if (ar.get(mid) <=k)
{
s=mid+1;
}
else
{
e=mid;
}
}
if(s==ar.size())
{
return -1;
}
return s;
}
static boolean isPrime(long N)
{
if (N<=1) return false;
if (N<=3) return true;
if (N%2 == 0 || N%3 == 0) return false;
for (int i=5; i*i<=N; i=i+6)
if (N%i == 0 || N%(i+2) == 0)
return false;
return true;
}
static int countBits(long a)
{
return (int)(Math.log(a)/Math.log(2)+1);
}
static long fact(long N)
{
long mod=1000000007;
long n=2;
if(N<=1)return 1;
else
{
for(int i=3; i<=N; i++)n=(n*i)%mod;
}
return n;
}
private static boolean isInteger(String s) {
try {
Integer.parseInt(s);
} catch (NumberFormatException e) {
return false;
} catch (NullPointerException e) {
return false;
}
return true;
}
private static long gcd(long a, long b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
private static long lcm(long a, long b) {
return (a / gcd(a, b)) * b;
}
private static boolean isPalindrome(String str) {
int i = 0, j = str.length() - 1;
while (i < j)
if (str.charAt(i++) != str.charAt(j--))
return false;
return true;
}
private static String reverseString(String str) {
StringBuilder sb = new StringBuilder(str);
return sb.reverse().toString();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(
new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() { return Integer.parseInt(next()); }
long nextLong() { return Long.parseLong(next()); }
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 194adea074c738ce87c6bc46d8405dc1 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | /*
Rating: 1378
Date: 12-08-2022
Time: 23-18-31
Author: Kartik Papney
Linkedin: https://www.linkedin.com/in/kartik-papney-4951161a6/
Leetcode: https://leetcode.com/kartikpapney/
Codechef: https://www.codechef.com/users/kartikpapney
----------------------------Jai Shree Ram----------------------------
*/
import java.util.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class A_Color_the_Picture {
public static void s() {
long n = sc.nextLong(), m = sc.nextLong(), k = sc.nextLong();
long[] arr = sc.readLongArray((int)k);
boolean check = false;
long ans = 0;
for(int i=0; i<k; i++) {
if(arr[i]/m > 1) ans += (arr[i]/m);
if(arr[i]/m > 2) check = true;
}
if(ans >= n &&(n%2 == 0 || check)) {
p.writeln("Yes");
return;
}
ans = 0;
for(int i=0; i<k; i++) {
if(arr[i]/n > 1) ans += (arr[i]/n);
if(arr[i]/n > 2) check = true;
}
if(ans >= m &&(m%2 == 0 || check)){
p.writeln("Yes");
return;
}
p.writeln("No");
}
public static void main(String[] args) {
int t = 1;
t = sc.nextInt();
while (t-- != 0) {
s();
}
p.print();
}
public static boolean debug = false;
static void debug(String st) {
if(debug) p.writeln(st);
}
static final Integer MOD = (int) 1e9 + 7;
static final FastReader sc = new FastReader();
static final Print p = new Print();
static class Functions {
static void sort(int... a) {
ArrayList<Integer> l = new ArrayList<>();
for (int i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static void sort(long... a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static int max(int... a) {
int max = Integer.MIN_VALUE;
for (int val : a) max = Math.max(val, max);
return max;
}
static int min(int... a) {
int min = Integer.MAX_VALUE;
for (int val : a) min = Math.min(val, min);
return min;
}
static long min(long... a) {
long min = Long.MAX_VALUE;
for (long val : a) min = Math.min(val, min);
return min;
}
static long max(long... a) {
long max = Long.MIN_VALUE;
for (long val : a) max = Math.max(val, max);
return max;
}
static long sum(long... a) {
long sum = 0;
for (long val : a) sum += val;
return sum;
}
static int sum(int... a) {
int sum = 0;
for (int val : a) sum += val;
return sum;
}
public static long mod_add(long a, long b) {
return (a % MOD + b % MOD + MOD) % MOD;
}
public static long pow(long a, long b) {
long res = 1;
while (b > 0) {
if ((b & 1) != 0)
res = mod_mul(res, a);
a = mod_mul(a, a);
b >>= 1;
}
return res;
}
public static long mod_mul(long a, long b) {
long res = 0;
a %= MOD;
while (b > 0) {
if ((b & 1) > 0) {
res = mod_add(res, a);
}
a = (2 * a) % MOD;
b >>= 1;
}
return res;
}
public static long gcd(long a, long b) {
if (a == 0) return b;
return gcd(b % a, a);
}
public static long factorial(long n) {
long res = 1;
for (int i = 1; i <= n; i++) {
res = (i % MOD * res % MOD) % MOD;
}
return res;
}
public static int count(int[] arr, int x) {
int count = 0;
for (int val : arr) if (val == x) count++;
return count;
}
public static ArrayList<Integer> generatePrimes(int n) {
boolean[] primes = new boolean[n];
for (int i = 2; i < primes.length; i++) primes[i] = true;
for (int i = 2; i < primes.length; i++) {
if (primes[i]) {
for (int j = i * i; j < primes.length; j += i) {
primes[j] = false;
}
}
}
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 0; i < primes.length; i++) {
if (primes[i]) arr.add(i);
}
return arr;
}
}
static class Print {
StringBuffer strb = new StringBuffer();
public void write(Object str) {
strb.append(str);
}
public void writes(Object str) {
char c = ' ';
strb.append(str).append(c);
}
public void writeln(Object str) {
char c = '\n';
strb.append(str).append(c);
}
public void writeln() {
char c = '\n';
strb.append(c);
}
public void yes() {
char c = '\n';
writeln("YES");
}
public void no() {
writeln("NO");
}
public void writes(int... arr) {
for (int val : arr) {
write(val);
write(' ');
}
}
public void writes(long... arr) {
for (long val : arr) {
write(val);
write(' ');
}
}
public void writeln(int... arr) {
for (int val : arr) {
writeln(val);
}
}
public void print() {
System.out.print(strb);
}
public void println() {
System.out.println(strb);
}
}
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());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
double[] readArrayDouble(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
String nextLine() {
String str = new String();
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 84d17c74acc4743173dfc61b15648604 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.lang.Math;
import java.util.*;
import javax.management.ValueExp;
public final class Codechef {
static BufferedReader br = new BufferedReader(
new InputStreamReader(System.in)
);
static BufferedWriter bw = new BufferedWriter(
new OutputStreamWriter(System.out)
);
static StringTokenizer st;
static int mod = 1000000007;
/*write your constructor and global variables here*/
static class sortCond implements Comparator<Pair<Integer, Integer>> {
@Override
public int compare(Pair<Integer, Integer> p1, Pair<Integer, Integer> p2) {
if (p1.a <= p2.a) {
return -1;
} else {
return 1;
}
}
}
static class sortCond1 implements Comparator<Integer> {
@Override
public int compare(Integer p1, Integer p2) {
if (p1 <= p2) {
return 1;
} else {
return -1;
}
}
}
static class Rec {
int a;
int b;
long c;
Rec(int a, int b, long c) {
this.a = a;
this.b = b;
this.c = c;
}
}
static class Pair<f, s> {
f a;
s b;
Pair(f a, s b) {
this.a = a;
this.b = b;
}
}
interface modOperations {
int mod(int a, int b, int mod);
}
static int findBinaryExponentian(int a, int pow, int mod) {
if (pow == 1) {
return a;
} else if (pow == 0) {
return 1;
} else {
int retVal = findBinaryExponentian(a, (int) pow / 2, mod);
int val = (pow % 2 == 0) ? 1 : a;
return modMul.mod(modMul.mod(retVal, retVal, mod), val, mod);
}
}
static int findPow(int a, int b, int mod) {
if (b == 1) {
return a % mod;
} else if (b == 0) {
return 1;
} else {
int res = findPow(a, (int) b / 2, mod);
return modMul.mod(res, modMul.mod(res, (b % 2 == 1 ? a : 1), mod), mod);
}
}
static int bleft(int ele, int[] arr) {
int l = 0;
int h = arr.length - 1;
int ans = -1;
while (l <= h) {
int mid = l + (int) (h - l) / 2;
int val = arr[mid];
if (ele > val) {
l = mid + 1;
} else if (ele < val) {
h = mid - 1;
} else {
ans = mid;
h = mid - 1;
}
}
return ans;
}
static int gcd(int a, int b) {
int div = b;
int rem = a % b;
while (rem != 0) {
int temp = rem;
rem = div % rem;
div = temp;
}
return div;
}
static long[] log(long no, long n) {
long i = 1;
int cnt = 0;
long sum = 0l;
long arr[] = new long[2];
while (i < no) {
sum += i;
cnt++;
if (sum == n) {
arr[0] = 1l * cnt;
arr[1] = sum;
break;
}
i *= 2l;
}
if (arr[0] == 0) {
arr[0] = cnt;
arr[1] = sum;
}
return arr;
}
static modOperations modAdd = (int a, int b, int mod) -> {
return (a % mod + b % mod) % mod;
};
static modOperations modSub = (int a, int b, int mod) -> {
return (int) ((1l * a % mod - 1l * b % mod + 1l * mod) % mod);
};
static modOperations modMul = (int a, int b, int mod) -> {
return (int) ((1l * (a % mod) * 1l * (b % mod)) % (1l * mod));
};
static modOperations modDiv = (int a, int b, int mod) -> {
return modMul.mod(a, findBinaryExponentian(b, mod - 1, mod), mod);
};
static HashSet<Integer> primeList(int MAXI) {
int[] prime = new int[MAXI + 1];
HashSet<Integer> obj = new HashSet<>();
for (int i = 2; i <= (int) Math.sqrt(MAXI) + 1; i++) {
if (prime[i] == 0) {
obj.add(i);
for (int j = i * i; j <= MAXI; j += i) {
prime[j] = 1;
}
}
}
for (int i = (int) Math.sqrt(MAXI) + 1; i <= MAXI; i++) {
if (prime[i] == 0) {
obj.add(i);
}
}
return obj;
}
static int[] factorialList(int MAXI, int mod) {
int[] factorial = new int[MAXI + 1];
factorial[0] = factorial[1] = 1;
factorial[2] = 2;
for (int i = 3; i < MAXI + 1; i++) {
factorial[i] = modMul.mod(factorial[i - 1], i, mod);
}
return factorial;
}
static void put(HashMap<Integer, Integer> cnt, int key) {
if (cnt.containsKey(key)) {
cnt.replace(key, cnt.get(key) + 1);
} else {
cnt.put(key, 1);
}
}
static long arrSum(ArrayList<Long> arr) {
long tot = 0;
for (int i = 0; i < arr.size(); i++) {
tot += arr.get(i);
}
return tot;
}
static int ord(char b) {
return (int) b - (int) 'a';
}
static int optimSearch(int[] cnt, int lower_bound, int pow, int n) {
int l = lower_bound + 1;
int h = n;
int ans = 0;
while (l <= h) {
int mid = l + (h - l) / 2;
if (cnt[mid] - cnt[lower_bound] == pow) {
return mid;
} else if (cnt[mid] - cnt[lower_bound] < pow) {
ans = mid;
l = mid + 1;
} else {
h = mid - 1;
}
}
return ans;
}
static Pair<Long, Integer> ret_ans(ArrayList<Integer> ans) {
int size = ans.size();
int mini = 1000000000 + 1;
long tit = 0l;
for (int i = 0; i < size; i++) {
tit += 1l * ans.get(i);
mini = Math.min(mini, ans.get(i));
}
return new Pair<>(tit - mini, mini);
}
static int factorList(
HashMap<Integer, Integer> maps,
int no,
int maxK,
int req
) {
int i = 1;
while (i * i <= no) {
if (no % i == 0) {
if (i != no / i) {
put(maps, no / i);
}
put(maps, i);
if (maps.get(i) == req) {
maxK = Math.max(maxK, i);
}
if (maps.get(no / i) == req) {
maxK = Math.max(maxK, no / i);
}
}
i++;
}
return maxK;
}
static ArrayList<Integer> getKeys(HashMap<Integer, Integer> maps) {
ArrayList<Integer> vals = new ArrayList<>();
for (Map.Entry<Integer, Integer> map : maps.entrySet()) {
vals.add(map.getKey());
}
return vals;
}
static ArrayList<Integer> getValues(HashMap<Integer, Integer> maps) {
ArrayList<Integer> vals = new ArrayList<>();
for (Map.Entry<Integer, Integer> map : maps.entrySet()) {
vals.add(map.getValue());
}
return vals;
}
static int getMax(ArrayList<Integer> arr) {
int max = arr.get(0);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) > max) {
max = arr.get(i);
}
}
return max;
}
static int getMin(ArrayList<Integer> arr) {
int max = arr.get(0);
for (int i = 1; i < arr.size(); i++) {
if (arr.get(i) < max) {
max = arr.get(i);
}
}
return max;
}
static void bitRep(int n, int no) throws IOException {
int curr = (int) Math.pow(2, n - 1);
for (int i = n - 1; i >= 0; i--) {
if ((curr & no) != 0) {
bw.write("1");
} else {
bw.write("0");
}
curr /= 2;
}
bw.write("\n");
}
static ArrayList<Integer> retPow(int MAXI) {
long curr = 1l;
ArrayList<Integer> arr = new ArrayList<>();
for (int i = 1; i <= MAXI; i++) {
curr *= 2l;
curr = curr % mod;
arr.add((int) curr);
}
return arr;
}
static String is(int no) {
return Integer.toString(no);
}
static String ls(long no) {
return Long.toString(no);
}
static int pi(String s) {
return Integer.parseInt(s);
}
static long pl(String s) {
return Long.parseLong(s);
}
/*write your methods and classes here*/
static boolean ret_ans(int n, int m, int k, int arr[]) {
boolean have = false;
long tot = 0l;
for (int i = k - 1; i >= 0; i--) {
if (arr[i] >= 2 * m) {
tot += 1l * (arr[i] / m);
}
if (arr[i] >= 3 * m) {
have = true;
}
}
if (tot >= n && ((tot - n) % 2 == 0 || have)) {
return true;
}
return false;
}
public static void main(String[] args) throws IOException {
int cases = pi(br.readLine()), n, m, k;
while (cases-- != 0) {
st = new StringTokenizer(br.readLine());
n = pi(st.nextToken());
m = pi(st.nextToken());
k = pi(st.nextToken());
int arr[] = new int[k];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < k; i++) {
arr[i] = pi(st.nextToken());
}
Arrays.sort(arr);
boolean r1 = ret_ans(n, m, k, arr);
boolean r2 = ret_ans(m, n, k, arr);
if (r1 || r2) {
bw.write("Yes\n");
} else {
bw.write("No\n");
}
}
bw.flush();
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | eb98c32675dd52bd6cfa0f0b2b326db5 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ColorthePicture {
static int MAXN = 1123456;
static boolean[] prime = new boolean[MAXN];
static boolean[] used = new boolean[MAXN];
public static void seive() {
for (int i = 2; i < MAXN; ++i) if (!used[i]){
prime[i] = true;
for (int j = i; j < MAXN; j += i) {
used[j] = true;
}
}
prime[1] = false;
}
// pair
// class Pair implements Comparable<Pair>{
// int ind,val;
// Pair(int i,int v){ ind=i;val=v;}
// @Override
// public int compareTo(Pair o) {return o.val-val ;}
// }
public static void main (String[] args ) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int t = Integer.parseInt(br.readLine());
while (t-- > 0) {
String[] st = br.readLine().split(" ");
int n = Integer.parseInt(st[0]);
long m = Long.parseLong(st[1]);
long k = Long.parseLong(st[2]);
String[] str = br.readLine().split(" ");
int a[] = new int[(int) k];
for(int i = 0; i < k; i++) {
a[i] = Integer.parseInt(str[i]);
}
// Row wise
boolean flag = false;
long total = 0l;
for(int i = 0; i < k; i++) {
if(a[i]/n > 2) flag = true;
if(a[i]/n >= 2) total += a[i]/n;
}
if(total >= m && (flag || m%2 == 0)) {
System.out.println("YES");
continue;
}
flag = false;
total = 0l;
for(int i = 0; i < k; i++) {
if(a[i]/m > 2) flag = true;
if(a[i]/m >= 2) total += a[i]/m;
}
if(total >= n && (flag || n%2 == 0)) {
System.out.println("YES");
continue;
}
System.out.println("NO");
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 6d4f5a39f3cae8b69b71d3cc593db334 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | public class A {
public Object solve () {
int R = sc.nextInt(), C = sc.nextInt();
sc.nextInt();
long [] A = sc.nextLongs();
if (calc(R, C, A.clone()))
return "Yes";
if (calc(C, R, A.clone()))
return "Yes";
return "No";
}
boolean calc (int R, int C, long [] A) {
int N = A.length;
for (int i : rep(N))
A[i] /= C;
long S = 0;
for (int i : rep(N))
if (A[i] >= 2) {
S += 2;
A[i] -= 2;
}
else
A[i] = 0;
long T = 0;
for (long a : A)
T += a;
if (R % 2 == 1 && T == 0)
return false;
boolean res = S + T >= R;
return res;
}
private static final int CONTEST_TYPE = 2;
private static void init () {
}
private static int [] rep (int N) { return rep(0, N); }
private static int [] rep (int S, int T) { if (S >= T) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
//////////////////////////////////////////////////////////////////////////////////// OFF
private static IOUtils.MyScanner sc = new IOUtils.MyScanner();
private static class IOUtils {
public static class MyScanner {
public String next () { newLine(); return line[index++]; }
public int nextInt () { return Integer.parseInt(next()); }
public String nextLine () { line = null; return readLine(); }
public String [] nextStrings () { return split(nextLine()); }
public long [] nextLongs () {
String [] S = nextStrings(); int N = S.length;
long [] res = new long [N];
for (int i = 0; i < N; ++i)
res[i] = Long.parseLong(S[i]);
return res;
}
//////////////////////////////////////////////
private boolean eol () { return index == line.length; }
private String readLine () {
try {
int b;
StringBuilder res = new StringBuilder();
while ((b = System.in.read()) < ' ')
continue;
res.append((char)b);
while ((b = System.in.read()) >= ' ')
res.append((char)b);
return res.toString();
} catch (Exception e) {
throw new Error (e);
}
}
private MyScanner () {
try {
while (System.in.available() == 0)
Thread.sleep(1);
start();
} catch (Exception e) {
throw new Error(e);
}
}
private String [] line;
private int index;
private void newLine () {
if (line == null || eol()) {
line = split(readLine());
index = 0;
}
}
private String [] split (String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
}
private static String build (Object o, Object ... A) { return buildDelim(" ", o, A); }
private static String buildDelim (String delim, Object o, Object ... A) {
java.util.List<String> str = new java.util.ArrayList<>();
append(str, o);
for (Object p : A)
append(str, p);
return String.join(delim, str.toArray(new String [0]));
}
//////////////////////////////////////////////////////////////////////////////////
private static java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########");
private static void start () { if (t == 0) t = millis(); }
private static void append (java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, Object o) {
if (o.getClass().isArray()) {
int len = java.lang.reflect.Array.getLength(o);
for (int i = 0; i < len; ++i)
f.accept(java.lang.reflect.Array.get(o, i));
}
else if (o instanceof Iterable<?>)
((Iterable<?>)o).forEach(f::accept);
else
g.accept(o instanceof Double ? formatter.format(o) : o);
}
private static void append (java.util.List<String> str, Object o) {
append(x -> append(str, x), x -> str.add(x.toString()), o);
}
private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
private static void flush () { pw.flush(); System.out.flush(); }
private static void print (Object o, Object ... A) {
String res = build(o, A);
if (DEBUG == 2)
err(res, '(', time(), ')');
if (res.length() > 0)
pw.println(res);
if (DEBUG == 1)
flush();
}
private static void err (Object o, Object ... A) { System.err.println(build(o, A)); }
private static int DEBUG;
private static void exit () {
String end = "------------------" + System.lineSeparator() + time();
switch(DEBUG) {
case 1: print(end); break;
case 2: err(end); break;
}
IOUtils.pw.close();
System.out.flush();
System.exit(0);
}
private static long t;
private static long millis () { return System.currentTimeMillis(); }
private static String time () { return "Time: " + (millis() - t) / 1000.0; }
private static void run (int N) {
try { DEBUG = Integer.parseInt(System.getProperties().get("DEBUG").toString()); }
catch (Throwable t) {}
for (int n = 1; n <= N; ++n) {
Object res = new A().solve();
if (res != null) {
@SuppressWarnings("all")
Object o = CONTEST_TYPE == 0 ? "Case #" + n + ": " + build(res) : res;
print(o);
}
}
exit();
}
}
////////////////////////////////////////////////////////////////////////////////////
public static void main (String[] args) {
@SuppressWarnings("all")
int N = CONTEST_TYPE == 1 ? 1 : sc.nextInt();
init();
IOUtils.run(N);
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 29c2b5b0c29261bf8b1fd6eb6be33497 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import static java.lang.Math.max;
import static java.lang.Math.min;
import static java.lang.Math.abs;
import java.util.*;
import java.io.*;
import java.math.*;
public class A_Color_the_Picture {
static long mod = Long.MAX_VALUE;
public static void main(String[] args) {
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
FastReader f = new FastReader();
int t = f.nextInt();
while(t-- > 0){
solve(f, out);
}
out.close();
}
public static void solve(FastReader f, PrintWriter out) {
long n = f.nextLong();
long m = f.nextLong();
int k = f.nextInt();
long arr[] = f.nextArray(k);
// int rowArr[] = new int[k];
ArrayList<Long> rowArrli = new ArrayList<>();
// int colArr[] = new int[k];
ArrayList<Long> colArrli = new ArrayList<>();
for(int i = 0; i < k; i++) {
if(arr[i]/n >= 2) {
rowArrli.add(arr[i]/n);
}
if(arr[i]/m >= 2) {
colArrli.add(arr[i]/m);
}
}
long row = 0;
boolean flag = false;
Collections.sort(rowArrli, Collections.reverseOrder());
for(int i = 0; i < rowArrli.size(); i++) {
long req = Math.min(m-row, rowArrli.get(i));
if(req >= 2) {
row += req;
if(req > 2) {
flag = true;
}
} else if(req == 1) {
if(flag) {
out.println("Yes");
return;
}
break;
}
}
if(row == m) {
out.println("Yes");
return;
}
long col = 0;
flag = false;
Collections.sort(colArrli, Collections.reverseOrder());
for(int i = 0; i < colArrli.size(); i++) {
long req = Math.min(n-col, colArrli.get(i));
if(req >= 2) {
col += req;
if(req > 2) {
flag = true;
}
} else if(req == 1) {
if(flag) {
out.println("Yes");
return;
}
break;
}
}
if(col == n) {
out.println("Yes");
} else {
out.println("No");
}
}
// Sort an array
public static void sort(int arr[]) {
ArrayList<Integer> al = new ArrayList<>();
for(int i: arr) {
al.add(i);
}
Collections.sort(al, Collections.reverseOrder());
for(int i = 0; i < arr.length; i++) {
arr[i] = al.get(i);
}
}
// Find all divisors of n
public static void allDivisors(int n) {
for(int i = 1; i*i <= n; i++) {
if(n%i == 0) {
System.out.println(i + " ");
if(i != n/i) {
System.out.println(n/i + " ");
}
}
}
}
// Check if n is prime or not
public static boolean isPrime(int n) {
if(n < 1) return false;
if(n == 2 || n == 3) return true;
if(n % 2 == 0 || n % 3 == 0) return false;
for(int i = 5; i*i <= n; i += 6) {
if(n % i == 0 || n % (i+2) == 0) {
return false;
}
}
return true;
}
// Find gcd of a and b
public static long gcd(long a, long b) {
long dividend = a > b ? a : b;
long divisor = a < b ? a : b;
while(divisor > 0) {
long reminder = dividend % divisor;
dividend = divisor;
divisor = reminder;
}
return dividend;
}
// Find lcm of a and b
public static long lcm(long a, long b) {
long lcm = gcd(a, b);
long hcf = (a * b) / lcm;
return hcf;
}
// Find factorial in O(n) time
public static long fact(int n) {
long res = 1;
for(int i = 2; i <= n; i++) {
res = res * i;
}
return res;
}
// Find power in O(logb) time
public static long power(long a, long b) {
long res = 1;
while(b > 0) {
if((b&1) == 1) {
res = (res * a)%mod;
}
a = (a * a)%mod;
b >>= 1;
}
return res;
}
// Find nCr
public static long nCr(int n, int r) {
if(r < 0 || r > n) {
return 0;
}
long ans = fact(n) / (fact(r) * fact(n-r));
return ans;
}
// Find nPr
public static long nPr(int n, int r) {
if(r < 0 || r > n) {
return 0;
}
long ans = fact(n) / fact(r);
return ans;
}
// sort all characters of a string
public static String sortString(String inputString) {
char tempArray[] = inputString.toCharArray();
Arrays.sort(tempArray);
return new String(tempArray);
}
// User defined class for fast I/O
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();
}
boolean hasNext() {
if (st != null && st.hasMoreTokens()) {
return true;
}
String tmp;
try {
br.mark(1000);
tmp = br.readLine();
if (tmp == null) {
return false;
}
br.reset();
} catch (IOException e) {
return false;
}
return true;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
float nextFloat() {
return Float.parseFloat(next());
}
boolean nextBoolean() {
return Boolean.parseBoolean(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
long[] nextArray(int n) {
long[] a = new long[n];
for(int i=0; i<n; i++) {
a[i] = nextInt();
}
return a;
}
}
}
/**
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- ----------
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
2 STX (start of text) 34 " 66 B 98 b
3 ETX (end of text) 35 # 67 C 99 c
4 EOT (end of transmission) 36 $ 68 D 100 d
5 ENQ (enquiry) 37 % 69 E 101 e
6 ACK (acknowledge) 38 & 70 F 102 f
7 BEL (bell) 39 ' 71 G 103 g
8 BS (backspace) 40 ( 72 H 104 h
9 TAB (horizontal tab) 41 ) 73 I 105 i
10 LF (NL line feed, new line) 42 * 74 J 106 j
11 VT (vertical tab) 43 + 75 K 107 k
12 FF (NP form feed, new page) 44 , 76 L 108 l
13 CR (carriage return) 45 - 77 M 109 m
14 SO (shift out) 46 . 78 N 110 n
15 SI (shift in) 47 / 79 O 111 o
16 DLE (data link escape) 48 0 80 P 112 p
17 DC1 (device control 1) 49 1 81 Q 113 q
18 DC2 (device control 2) 50 2 82 R 114 r
19 DC3 (device control 3) 51 3 83 S 115 s
20 DC4 (device control 4) 52 4 84 T 116 t
21 NAK (negative acknowledge) 53 5 85 U 117 u
22 SYN (synchronous idle) 54 6 86 V 118 v
23 ETB (end of trans. block) 55 7 87 W 119 w
24 CAN (cancel) 56 8 88 X 120 x
25 EM (end of medium) 57 9 89 Y 121 y
26 SUB (substitute) 58 : 90 Z 122 z
27 ESC (escape) 59 ; 91 [ 123 {
28 FS (file separator) 60 < 92 \ 124 |
29 GS (group separator) 61 = 93 ] 125 }
30 RS (record separator) 62 > 94 ^ 126 ~
31 US (unit separator) 63 ? 95 _ 127 DEL
*/
// (a/b)%mod == (a * moduloInverse(b)) % mod;
// moduloInverse(b) = power(b, mod-2);
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 502c46bc03d407494f87dc568cffb99e | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
import java.text.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception {
FastReader sc = new FastReader();
PrintWriter writer = new PrintWriter(System.out);
int t = sc.nextInt();
while(t-->0) {
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
int arr[] = readIntArray(k,sc);
long v1 = 0;
long v2 = 0;
boolean greaterThanTwo1 = false;
boolean greaterThanTwo2 = false;
boolean exist1 = false;
boolean exist2 = false;
for(int i = 0; i < k; i++) {
int div = arr[i]/m;
int div2 = arr[i]/n;
if(div>=2) {
v1+=div;
exist1=true;
if(div>=3)greaterThanTwo1 = true;
}
if(div2>=2) {
v2+=div2;
exist2=true;
if(div2>=3)greaterThanTwo2 = true;
}
}
if(v1<n)exist1=false;
if(v2<m)exist2=false;
if(n%2==1 && !greaterThanTwo1)exist1 = false;
if(m%2==1 && !greaterThanTwo2)exist2 = false;
if(exist1 || exist2)writer.println("YES");
else writer.println("NO");
}
writer.flush();
writer.close();
}
private static long power (long a, long n, long p) {
long res = 1;
while(n!=0) {
if(n%2==1) {
res=(res*a)%p;
n--;
}else {
a= (a*a)%p;
n/=2;
}
}
return res;
}
private static boolean isPrime(int c) {
for (int i = 2; i*i <= c; i++) {
if(c%i==0)return false;
}
return true;
}
private static int find(int a , int arr[]) {
if(arr[a] != a) return arr[(int)a] = find(arr[(int)a], arr);
return arr[(int)a];
}
private static void union(int a, int b, int arr[]) {
int aa = find(a,arr);
int bb = find(b, arr);
arr[aa] = bb;
}
private static int gcd(int a, int b) {
if(a==0)return b;
return gcd(b%a, a);
}
public static int[] readIntArray(int size, FastReader s) {
int[] array = new int[size];
for (int i = 0; i < size; i++) {
array[i] = s.nextInt();
}
return array;
}
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;
}
}
}
class SegmentTree{
int size;
long arr[];
SegmentTree(int n){
size = 1;
while(size<n)size*=2;
arr = new long[size*2];
}
public void build(int input[]) {
build(input, 0,0, size);
}
public void set(int i, int v) {
set(i,v,0,0,size);
}
// sum from l + r-1
public long sum(int l, int r) {
return sum(l,r,0,0,size);
}
private void build(int input[], int x, int lx, int rx) {
if(rx-lx==1) {
if(lx < input.length )
arr[x] = input[lx];
return;
}
int mid = (lx+rx)/2;
build(input, 2*x+1, lx, mid);
build(input,2*x+2,mid,rx);
arr[x] = arr[2*x+1] + arr[2*x+2];
}
private void set(int i, int v, int x, int lx, int rx) {
if(rx-lx==1) {
arr[x] = v;
return;
}
int mid = (lx+rx)/2;
if(i < mid) set(i, v, 2*x+1, lx, mid);
else set(i,v,2*x+2,mid,rx);
arr[x] = arr[2*x+1] + arr[2*x+2];
}
private long sum(int l, int r, int x, int lx, int rx) {
if(lx>=r || rx <= l)return 0;
if(lx>=l && rx <= r)return arr[x];
int mid = (lx+rx)/2;
long s1 = sum(l,r,2*x+1,lx,mid);
long s2 = sum(l,r,2*x+2,mid,rx);
return s1+s2;
}
}
class Trie{
Trie arr[] = new Trie[26];
boolean isEnd;
Trie(){
for(int i = 0; i < 26; i++)arr[i] = null;
isEnd = false;
}
}
class Pair implements Comparable<Pair>{
long a;
long b;
long c;
Pair(long a, long b, long c){
this.a = a;
this.b = b;
this.c = c;
}
Pair(long a, long b){
this.a = a;
this.b = b;
this.c = 0;
}
@Override
public boolean equals(Object obj) {
if(this == obj) return true;
if(obj == null || obj.getClass()!= this.getClass()) return false;
Pair pair = (Pair) obj;
return (pair.a == this.a && pair.b == this.b && pair.c == this.c);
}
@Override
public int hashCode()
{
// return Objects.hash(a,b);
return new Long(a).hashCode() * 31 + new Long(b).hashCode();
}
@Override
public int compareTo(Pair o) {
if(o.a != this.a) return Long.compare(this.a, o.a);
else
return Long.compare(this.b, o.b);
}
@Override
public String toString() {
return this.a + " " + this.b;
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 04dd454f77f61be59b4904ff7b113a15 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static int INF = 0x3f3f3f3f;
public static int mod = 1000000007;
public static int mod9 = 998244353;
public static void main(String args[]){
try {
PrintWriter o = new PrintWriter(System.out);
boolean multiTest = true;
// init
if(multiTest) {
int t = fReader.nextInt(), loop = 0;
while (loop < t) {loop++;solve(o);}
} else solve(o);
o.close();
} catch (Exception e) {e.printStackTrace();}
}
static void solve(PrintWriter o) {
try {
int n = fReader.nextInt(), m = fReader.nextInt(), k = fReader.nextInt();
int[] a = new int[k];
for(int i=0;i<k;i++) a[i] = fReader.nextInt();
boolean upper2 = false;
long cnt = 0;
for(int i=0;i<k;i++) {
if(a[i]/m > 2) upper2 = true;
if(a[i]/m >= 2) cnt += a[i]/m;
}
if(cnt >= n && (n%2 == 0 || upper2)) {
o.println("Yes");
return;
}
upper2 = false;
cnt = 0;
for(int i=0;i<k;i++) {
if(a[i]/n > 2) upper2 = true;
if(a[i]/n >= 2) cnt += a[i]/n;
}
if(cnt >= m && (m%2 == 0 || upper2)) {
o.println("Yes");
return;
}
o.println("No");
} catch (Exception e){e.printStackTrace();}
}
public static int upper_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) <= val) l = mid + 1;
else r = mid;
}
return l;
}
public static int lower_bound(List<Integer> a, int val){
int l = 0, r = a.size();
while(l < r){
int mid = l + (r - l) / 2;
if(a.get(mid) < val) l = mid + 1;
else r = mid;
}
return l;
}
public static long gcd(long a, long b){
return b == 0 ? a : gcd(b, a%b);
}
public static long lcm(long a, long b){
return a / gcd(a,b)*b;
}
public static boolean isPrime(long x){
boolean ok = true;
for(long i=2;i<=Math.sqrt(x);i++){
if(x % i == 0){
ok = false;
break;
}
}
return ok;
}
public static void reverse(int[] array){
reverse(array, 0 , array.length-1);
}
public static void reverse(int[] array, int left, int right) {
if (array != null) {
int i = left;
for(int j = right; j > i; ++i) {
int tmp = array[j];
array[j] = array[i];
array[i] = tmp;
--j;
}
}
}
public static long qpow(long a, long n){
long ret = 1l;
while(n > 0){
if((n & 1) == 1){
ret = ret * a % mod;
}
n >>= 1;
a = a * a % mod;
}
return ret;
}
public static class DSU {
int[] parent;
int[] size;
int n;
public DSU(int n){
this.n = n;
parent = new int[n];
size = new int[n];
for(int i=0;i<n;i++){
parent[i] = i;
size[i] = 1;
}
}
public int find(int p){
while(parent[p] != p){
parent[p] = parent[parent[p]];
p = parent[p];
}
return p;
}
public void union(int p, int q){
int root_p = find(p);
int root_q = find(q);
if(root_p == root_q) return;
if(size[root_p] >= size[root_q]){
parent[root_q] = root_p;
size[root_p] += size[root_q];
size[root_q] = 0;
}
else{
parent[root_p] = root_q;
size[root_q] += size[root_p];
size[root_p] = 0;
}
n--;
}
public int getComNum(){
return n;
}
public int[] getSize(){
return size;
}
}
public static class FenWick {
int n;
long[] tree;
public FenWick(int n){
this.n = n;
tree = new long[n+1];
}
private void add(int x, long val){
while(x <= n){
tree[x] += val;
x += x&-x;
}
}
private long query(int x){
long ret = 0l;
while(x > 0){
ret += tree[x];
x -= x&-x;
}
return ret;
}
}
public static class fReader {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static StringTokenizer tokenizer = new StringTokenizer("");
private static String next() throws IOException{
while(!tokenizer.hasMoreTokens()){tokenizer = new StringTokenizer(reader.readLine());}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {return Integer.parseInt(next());}
public static Long nextLong() throws IOException {return Long.parseLong(next());}
public static double nextDouble() throws IOException {return Double.parseDouble(next());}
public static char nextChar() throws IOException {return next().toCharArray()[0];}
public static String nextString() throws IOException {return next();}
public static String nextLine() throws IOException {return reader.readLine();}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 369127f171bb02033d26767a3b550633 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.*;
// import static java.lang.Math.*;
public class Round810_Div1_A implements Runnable {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args) {
new Thread(new Round810_Div1_A()).start();
}
void init() throws IOException {
if (ONLINE_JUDGE) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
@Override
public void run() {
try {
long t1 = System.currentTimeMillis();
init();
solve();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
void solve() throws IOException {
int test_cases = readInt();
for (int i = 0; i < test_cases; i++) {
// out.println("Test Case #:" + i);
int n = readInt();
int m = readInt();
int k = readInt();
Integer[] pigmentCells = new Integer[k];
for (int j = 0; j < k; j++) {
pigmentCells[j] = readInt();
}
solution(n, m, k, pigmentCells);
}
}
void solution(int n, int m, int k, Integer[] pigmentCells) {
Arrays.sort(pigmentCells, (a, b) -> b - a);
// out.println(Arrays.toString(pigmentCells));
if (check(n, m, pigmentCells) || check(m, n, pigmentCells))
out.println("Yes");
else
out.println("No");
}
boolean check(int n, int m, Integer[] pigmentCells) {
boolean hasSpareColumn = false;
for (int pc : pigmentCells) {
if ((!hasSpareColumn && m == 1) || m <= 0 || pc < n * 2)
break;
int i = pc / n;
if (!hasSpareColumn && i > 2) {
hasSpareColumn = true;
}
m -= i;
}
return m <= 0;
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 4bf4119d6e96861078bb61cd57a1a4ff | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class cd810div1A {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int tt = in.nextInt();
for(int w=0; w<tt; w++) {
int n=in.nextInt();
int m=in.nextInt();
int k =in.nextInt();
long[] niz = new long[k];
for(int i=0; i<k; i++) {
niz[i]=in.nextLong();
}
Arrays.sort(niz);
long suma = 0;
int flag=0;
boolean ok = false;
for(int i=k-1; i>=0; i--) {
if(niz[i]/n > 2) {
flag=1;
}
if(niz[i]/n>=2) {
suma+=niz[i]/n;
}
}
if(suma>=m && (flag==1 || m%2==0)) {
System.out.println("Yes");
ok=true;
}
if(ok==false) {
flag=0;
suma=0;
for(int i=k-1; i>=0; i--) {
if(niz[i]/m > 2) {
flag=1;
}
if(niz[i]/m >=2) {
suma+=niz[i]/m;
}
}
if(suma>=n && (flag==1 || n%2==0)) {
System.out.println("Yes");
ok=true;
}
}
if(ok==false) {
System.out.println("No");
}
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | fe8a460be853a8d66c65de83e150a5fa | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.StringTokenizer;
public class A {
static FastScanner in;
static PrintWriter out;
public static void main(String[] args) {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
int[] a = new int[k];
int[] an = new int[k];
int[] am = new int[k];
long nSum = 0;
long mSum = 0;
boolean nOdd = false;
boolean mOdd = false;
for (int i = 0; i < k; i++) {
a[i] = in.nextInt();
an[i] = a[i] / m;
am[i] = a[i] / n;
if (an[i] >= 2) {
nSum += an[i];
if (an[i] >= 3) {
nOdd = true;
}
}
if (am[i] >= 2) {
mSum += am[i];
if (am[i] >= 3) {
mOdd = true;
}
}
}
boolean yes = (nSum >= n && (n % 2 == 0 || nOdd)) || (mSum >= m && (m % 2 == 0 || mOdd));
out.println(yes ? "Yes" : "No");
}
out.close();
}
static class FastScanner {
BufferedReader reader;
StringTokenizer st = null;
FastScanner(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 1 << 15);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(reader.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 8fcf821e4d20a7d8bb3019760ca78509 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
//BufferedReader f = new BufferedReader(new FileReader("uva.in"));
//PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("milkvisits.out")));
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int t = Integer.parseInt(f.readLine());
while(t-- > 0) {
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int m = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
st = new StringTokenizer(f.readLine());
long rows = 0;
long cols = 0;
boolean flagRow = n%2 == 0;
boolean flagCol = m%2 == 0;
for(int i = 0; i < k; i++) {
int a = Integer.parseInt(st.nextToken());
if(a/m > 1) {
rows += a/m;
if(a/m > 2) {
flagRow = true;
}
}
if(a/n > 1) {
cols += a/n;
if(a/n > 2) {
flagCol = true;
}
}
}
out.println((rows >= n && flagRow) || (cols >= m && flagCol) ? "Yes" : "No");
}
f.close();
out.close();
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 08b203936648c411166ebc0ed78e7e9a | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.*;
public class A {
static IOHandler sc = new IOHandler();
public static void main(String[] args) {
// TODO Auto-generated method stub
int testCases = sc.nextInt();
for (int i = 1; i <= testCases; ++i) {
solve(i);
}
}
private static void solve(int t) {
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
List<Integer> list = new ArrayList<>();
for (int i = 0; i < k; ++i) {
list.add(sc.nextInt());
}
Collections.sort(list);
boolean result = canUse(n, m, list) || canUse(m, n, list);
System.out.println(result ? "Yes" : "No");
}
private static boolean canUse(int n, int m, List<Integer> list) {
int prev = 0;
int val;
int max = -1;
for (int num : list) {
if (n == 0)
break;
if (num / m >= 2) {
max = Math.min(n, num / m);
if (max == 1 && num / m < 3) {
continue;
}
n -= max;
}
}
return n == 0;
}
private static class IOHandler {
BufferedReader br;
StringTokenizer st;
public IOHandler() {
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());
}
int [] readArray(int n) {
int [] res = new int [n];
for (int i = 0; i < n; ++i)
res[i] = nextInt();
return res;
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 11 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 646edfe545d5dbafcd293c7b1dff15f5 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | // When I wrote this code, only God & I knew what it did. Now only God knows !!
import java.util.*;
import java.io.*;
public class Main {
static class FastReader
{
private InputStream mIs;private byte[] buf = new byte[1024];private int curChar,numChars;public FastReader() { this(System.in); }public FastReader(InputStream is) { mIs = is;}
public int read() {if (numChars == -1) throw new InputMismatchException();if (curChar >= numChars) {curChar = 0;try { numChars = mIs.read(buf);} catch (IOException e) { throw new InputMismatchException();}if (numChars <= 0) return -1; }return buf[curChar++];}
public String nextLine(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isEndOfLine(c));return res.toString() ;}
public String next(){int c = read();while (isSpaceChar(c)) c = read();StringBuilder res = new StringBuilder();do {res.appendCodePoint(c);c = read();}while (!isSpaceChar(c));return res.toString();}
public long l(){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 int i(){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 double d() throws IOException {return Double.parseDouble(next()) ;}
public boolean isSpaceChar(int c) { return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1; }
public boolean isEndOfLine(int c) { return c == '\n' || c == '\r' || c == -1; }
public void scanIntArr(int [] arr){ for(int li=0;li<arr.length;++li){ arr[li]=i();}}
public void scanLongArr(long [] arr){for (int i=0;i<arr.length;++i){arr[i]=l();}}
public void shuffle(long [] arr){ for(int i=arr.length;i>0;--i) { int r=(int)(Math.random()*i); long temp=arr[i-1]; arr[i-1]=arr[r]; arr[r]=temp; } }
}
static int mod = (int)(1e9 + 7);
public static void main(String[] args) throws IOException {
FastReader fr = new FastReader();
PrintWriter pw = new PrintWriter(System.out);
/*
inputCopy
6
4 6 3
12 9 8
3 3 2
8 8
3 3 2
9 5
4 5 2
10 11
5 4 2
9 11
10 10 3
11 45 14
outputCopy
Yes
No
Yes
Yes
No
No
*/
int t = fr.i();
outer: for (int ti = 0; ti < t; ++ti) {
long n = fr.l();
long m = fr.l();
long k = fr.l();
long[] arr = new long[(int) k];
fr.scanLongArr(arr);
long extra1 = 0;
long extra2 = 0;
long [] column1 = new long[(int) k];
long [] column2 = new long[(int) k];
for(int i=0;i<k;++i) {
column1[i] = (arr[i] / n) > 1 ? (arr[i] / n) : 0;
extra1 += Math.max(0, column1[i] - 2);
column2[i] = (arr[i] / m) > 1 ? (arr[i] / m) : 0;
extra2 += Math.max(0, column2[i] - 2);
}
fr.shuffle(column1);
fr.shuffle(column2);
Arrays.sort(column1);
Arrays.sort(column2);
boolean isPossible = helper(column1, m, extra1) || helper(column2, n, extra2);
pw.println(isPossible ? "Yes" : "No");
}
pw.flush();
pw.close();
}
private static boolean helper(long[] arr, long req, long extra) {
long sum = 0;
for(int i = arr.length - 1; i >= 0; --i) {
sum += arr[i];
if(sum >= req) {
long extraReq = sum - req;
return extra >= extraReq;
}
}
return false;
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 17 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 48f68e60e75a6faa9f27f3e65e198537 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | /******************************************************************************
Online Java Compiler.
Code, Compile, Run and Debug java program online.
Write your code in this editor and press "Run" button to execute it.
*******************************************************************************/
import java.util.*;
public class Main
{
public static void main(String[] args) {
Scanner scn = new Scanner(System.in);
int t = scn.nextInt();
while(t-->0){
int n = scn.nextInt();
int m = scn.nextInt();
int k = scn.nextInt();
int max = 0;
int []arr = new int[k];
for(int i = 0;i<k;i++){
arr[i] = scn.nextInt();
max = Math.max(arr[i],max);
}
// check for Rows
long m1 = 0;
long c = 0;
for(int i = 0;i<k;i++){
int val = (arr[i]/n);
if(val!=1){
c+=val;
m1 = Math.max(m1,val);
}
}
// check for column
long m2 = 0;
long c2 = 0;
for(int i = 0;i<k;i++){
int val = (arr[i]/m);
if(val!=1){
c2+=val;
m2 = Math.max(val,m2);
}
}
boolean f1 = false;
if(c>=m){
if(m%2 == 0){
f1 = true;
}
else if(m1>2){
f1 = true;
}
}
boolean f2 = false;
if(c2>=n){
if(n%2 == 0){
f2 = true;
}
else if(m2>2){
f2 = true;
}
}
if(f1 || f2){
System.out.println("YES");
}
else{
System.out.println("NO");
}
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 17 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 30814b47bb439c9cfd5f6f101e5e6c1a | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.lang.*;
public class main1{
FastScanner in;
PrintWriter out;
public static void main(String[] arg) {
new main1().run();
}
//////////////SOLVE QUESTIONS HERE/////////////
public void solve() throws IOException {
int tc=in.nextInt();
while(tc-->0)
{
int n=in.nextInt();
int m=in.nextInt();
int k=in.nextInt();
int[] arr=new int[k];
for(int i=0;i<k;i++)
arr[i]=in.nextInt();
//check column wise first
boolean greaterThan3=false;
long sum=0;
boolean ans=false;
for(int i=0;i<k;i++)
{
if(arr[i]/n >2)
greaterThan3=true;
if(arr[i]/n >=2)
sum+=arr[i]/n;
}
if(sum>=m && (greaterThan3 || m%2==0))
{
ans=true;
}
//now check row wise first
greaterThan3=false;
sum=0;
for(int i=0;i<k;i++)
{
if(arr[i]/m >2)
greaterThan3=true;
if(arr[i]/m >=2)
sum+=arr[i]/m;
}
if(sum>=n && (greaterThan3 || n%2==0))
{
ans=true;
}
if(ans)
{
out.println("Yes");
}
else
out.println("No");
}
}
public void run() {
try {
in = new FastScanner(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(Reader f) {
br = new BufferedReader(f);
}
String next() {
while (st == null || !st.hasMoreTokens()) {
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().trim();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 17 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 901a783bcf230dea394bc1cff8135ce9 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
public class color {
public static boolean solve(long sum,int n,int m,int flag){
if (m%2==0){
return (sum>=m);
}
else{
return (sum>=m && flag==1);
}
}
public static void main(String[] args){
Scanner sc=new Scanner (System.in);
int t=sc.nextInt();
for (int z=1;z<=t;z++){
int n=sc.nextInt();
int m=sc.nextInt();
int k=sc.nextInt();
long n_sum=0;
int n_sum_flag=0;
int m_sum_flag=0;
long m_sum=0;
for (int i=0;i<k;i++){
int y=sc.nextInt();
if (y/n>=2){
n_sum+=y/n;
if (y/n>=3){
n_sum_flag=1;
}
}
if (y/m>=2){
m_sum+=y/m;
if (y/m>=3){
m_sum_flag=1;
}
}
}
if (solve(n_sum, n, m, n_sum_flag) || solve(m_sum, m, n, m_sum_flag)){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
sc.close();
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 17 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | f477aa313496ff001dd461e48c0e159d | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
public class ColorThePicture {
public static void solve(Scanner sc) {
long n = sc.nextLong();
long m = sc.nextLong();
int k = sc.nextInt();
long[] c = new long[k];
long sum = 0;
long cols = 0;
long rows = 0;
long colodd = 0;
long colmax = 0;
long rowodd = 0;
long rowmax = 0;
for(int i = 0; i < k; i++) {
c[i] = sc.nextInt();
sum+=c[i];
long colorc = (long)Math.floor(c[i]/n);
long colorm = (long)Math.floor(c[i]/m);
if(colorc >=2 ) {
cols+=colorc;
if(colorc % 2 == 1) {
colodd = 1;
}
if(colorc >= 3) {
colmax = 1;
}
}
if(colorm >=2 ) {
rows+=colorm;
if(colorm % 2 == 1) {
rowodd = 1;
}
if(colorm >= 3) {
rowmax = 1;
}
}
}
if(n*m > sum) {
System.out.println("no");
return;
}
if(cols < m && rows <n) {
System.out.println("no");
}else {
if(cols >= m) {
if(m%2 == 0) {
System.out.println("yes");
return;
}
if(colodd == 1) {
System.out.println("yes");
return;
}
if(colmax == 1) {
cols--;
if(cols >= m) {
System.out.println("yes");
return;
}
}
}
if(rows >= n) {
if(n % 2 == 0) {
System.out.println("yes");
return;
}
if(rowodd == 1) {
System.out.println("yes");
return;
}
if(rowmax == 1) {
rows--;
if(rows >= n) {
System.out.println("yes");
return;
}
}
}
System.out.println("no");
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0) {
solve(sc);
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 17 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | e1e8a75934703f600196eeb646937380 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes |
import java.util.*;
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
while(tc-->0) {
int row = sc.nextInt(), col = sc.nextInt(), k = sc.nextInt();
int[] arr = new int[k];
for(int i=0;i<k;i++) arr[i] = sc.nextInt();
Arrays.sort(arr);
boolean ok = util(row, col, arr) | util(col, row, arr);
String ans = ok? "Yes" : "No";
System.out.println(ans);
}
// ####################
}
// ####################
static boolean util(int row, int col, int[] arr) {
int n = arr.length, colLeft = col;
boolean extra = false;
for(int i=n-1;i>=0 && colLeft > 0;i--) {
int x = arr[i], pillars = x / row;
if(pillars <= 1) return false;
if(pillars >= 3) extra = true;
if(colLeft == 1) return extra;
colLeft -= pillars;
}
return colLeft <= 0;
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 17 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | e05a4fda235842bb587ca2541e77bab8 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
public class color {
public static boolean solve(long sum,int n,int m,int flag){
if (m%2==0){
return (sum>=m);
}
else{
return (sum>=m && flag==1);
}
}
public static void main(String[] args){
Scanner sc=new Scanner (System.in);
int t=sc.nextInt();
for (int z=1;z<=t;z++){
int n=sc.nextInt();
int m=sc.nextInt();
int k=sc.nextInt();
long n_sum=0;
int n_sum_flag=0;
int m_sum_flag=0;
long m_sum=0;
for (int i=0;i<k;i++){
int y=sc.nextInt();
if (y/n>=2){
n_sum+=y/n;
if (y/n>=3){
n_sum_flag=1;
}
}
if (y/m>=2){
m_sum+=y/m;
if (y/m>=3){
m_sum_flag=1;
}
}
}
if (solve(n_sum, n, m, n_sum_flag) || solve(m_sum, m, n, m_sum_flag)){
System.out.println("Yes");
}
else{
System.out.println("No");
}
}
sc.close();
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 17 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 6844d3a80096114d7f5d75d75552e87c | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
public class Sol {
public static void main(String[] args) {
Scanner read = new Scanner(System.in);
int tt = read.nextInt();
while(tt-- > 0){
long n = read.nextLong();
long m = read.nextLong();
long k = read.nextLong();
int f = 0, f1 = 0;
int a[] = new int[(int) (k + 1)];
long sum = 0;
long sum1 = 0;
for(int i = 1; i <= k; ++i){
a[i] = read.nextInt();
long x = a[i] / n;
long y = a[i] / m;
if(x > 2)f = 1;
if(y > 2)f1 = 1;
if(x > 1)sum += x;
if(y > 1)sum1 += y;
}
if(m % 2 == 1 && f == 1 && sum >= m){
System.out.println("Yes");
}
else if(m % 2 == 0 && sum >= m){
System.out.println("Yes");
}
else if(n % 2 == 1 && f1 == 1 && sum1 >= n){
System.out.println("Yes");
}
else if(n % 2 == 0 && sum1 >= n){
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 17 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 08fcc8cb1ca65c4f547fd5f950a341c9 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.Scanner;
public class A1710 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t=0; t<T; t++) {
int N = in.nextInt();
int M = in.nextInt();
int K = in.nextInt();
long columnCount = 0;
long rowCount = 0;
int columnMax = 0;
int rowMax = 0;
for (int k=0; k<K; k++) {
int a = in.nextInt();
int columns = a/N;
int rows = a/M;
if (columns >= 2) {
columnCount += columns;
columnMax = Math.max(columnMax, columns);
}
if (rows >= 2) {
rowCount += rows;
rowMax = Math.max(rowMax, rows);
}
}
boolean possible = (columnCount >= M && (M%2 == 0 || columnMax > 2))
|| (rowCount >= N && (N%2 == 0 || rowMax > 2));
System.out.println(possible ? "Yes" : "No");
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | c18bafb91dc9f6a4b0f6b240f62c003d | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | //---#ON_MY_WAY---
//---#THE_SILENT_ONE---
import static java.lang.Math.*;
import java.io.*;
import java.math.*;
import java.util.*;
public class apples {
static FastReader x = new FastReader();
static OutputStream outputStream = System.out;
static PrintWriter out = new PrintWriter(outputStream);
/*---------------------------------------CODE STARTS HERE-------------------------*/
public static void main(String[] args) throws NumberFormatException, IOException {
long startTime = System.nanoTime();
int mod = 1000000007;
int t = x.nextInt();
StringBuilder str = new StringBuilder();
while (t > 0) {
long n = x.nextInt(), m = x.nextInt(), k = x.nextInt();
int a[] = new int[(int) k];
long cell = n*m;
boolean f = false;
for(int i = 0; i < k; i++) {
a[i] = x.nextInt();
if(a[i]>=cell) f = true;
}
revsort(a);
if(check(a, n, m)|check(a, m, n)) str.append("YES");
else str.append("NO");
str.append("\n");
t--;
}
out.println(str);
out.flush();
long endTime = System.nanoTime();
//System.out.println((endTime-startTime)/1000000000.0);
}
static boolean check(int a[], long n, long m) {
long marked = 0, f = 0;
for(int i = 0; i < a.length; i++) {
long c = a[i]/n;
if(c>1) marked += c;
if(m%2==1&&c>2) f = 1;
}
if(marked>=m&&((m%2==0)||(m%2==1&&f==1))) return true;
else return false;
}
/*--------------------------------------------FAST I/O-------------------------------*/
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;
}
char nextchar() {
char ch = ' ';
try {
ch = (char) br.read();
} catch (IOException e) {
e.printStackTrace();
}
return ch;
}
}
/*--------------------------------------------HELPER---------------------------------*/
static class pair implements Comparable<pair> {
int x, y;
public pair(int a, int b) {
x = a;
y = b;
}
@Override
public int hashCode() {
int hash = 3;
hash = 47 * hash + this.x;
hash = 47 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final pair other = (pair) obj;
if (this.x != other.x) {
return false;
}
if (this.y != other.y) {
return false;
}
return true;
}
@Override
public int compareTo(apples.pair o) {
if(this.x==o.x) return this.y-o.y;
return this.x-o.x;
}
}
/*--------------------------------------------BOILER PLATE---------------------------*/
static int[] readarr(int n) {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = x.nextInt();
}
return arr;
}
static int[] sortint(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static long[] sortlong(long a[]) {
ArrayList<Long> al = new ArrayList<>();
for (long i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < al.size(); i++) {
a[i] = al.get(i);
}
return a;
}
static long pow(long x, long y) {
long result = 1;
while (y > 0) {
if (y % 2 == 0) {
x = x * x;
y = y / 2;
} else {
result = result * x;
y = y - 1;
}
}
return result;
}
static long pow(long x, long y, long mod) {
long result = 1;
x %= mod;
while (y > 0) {
if (y % 2 == 0) {
x = (x % mod * x % mod) % mod;
y /= 2;
} else {
result = (result % mod * x % mod) % mod;
y--;
}
}
return result;
}
static int[] revsort(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al, Comparator.reverseOrder());
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static int[] gcd(int a, int b, int ar[]) {
if (b == 0) {
ar[0] = a;
ar[1] = 1;
ar[2] = 0;
return ar;
}
ar = gcd(b, a % b, ar);
int t = ar[1];
ar[1] = ar[2];
ar[2] = t - (a / b) * ar[2];
return ar;
}
static boolean[] esieve(int n) {
boolean p[] = new boolean[n + 1];
Arrays.fill(p, true);
for (int i = 2; i * i <= n; i++) {
if (p[i] == true) {
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
return p;
}
static ArrayList<Integer> primes(int n) {
boolean p[] = new boolean[n + 1];
ArrayList<Integer> al = new ArrayList<>();
Arrays.fill(p, true);
int i = 0;
for (i = 2; i * i <= n; i++) {
if (p[i] == true) {
al.add(i);
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
for (i = i; i <= n; i++) {
if (p[i] == true) {
al.add(i);
}
}
return al;
}
static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
static long gcd(long a, long b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | f8c4e9d7afb46b849f61406e765d143b | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | //---#ON_MY_WAY---
//---#THE_SILENT_ONE---
import static java.lang.Math.*;
import java.io.*;
import java.math.*;
import java.util.*;
public class apples {
static FastReader x = new FastReader();
static OutputStream outputStream = System.out;
static PrintWriter out = new PrintWriter(outputStream);
/*---------------------------------------CODE STARTS HERE-------------------------*/
public static void main(String[] args) throws NumberFormatException, IOException {
long startTime = System.nanoTime();
int mod = 1000000007;
int t = x.nextInt();
StringBuilder str = new StringBuilder();
while (t > 0) {
long n = x.nextInt(), m = x.nextInt(), k = x.nextInt();
int a[] = new int[(int) k];
long cell = n*m;
boolean f = false;
for(int i = 0; i < k; i++) {
a[i] = x.nextInt();
if(a[i]>=cell) f = true;
}
revsort(a);
if(check(a, n, m)|check(a, m, n)) str.append("YES");
else str.append("NO");
str.append("\n");
t--;
}
out.println(str);
out.flush();
long endTime = System.nanoTime();
//System.out.println((endTime-startTime)/1000000000.0);
}
static boolean check(int a[], long n, long m) {
long marked = 0, f = 0;
for(int i = 0; i < a.length; i++) {
long c = a[i]/n;
if(c>1) marked += c;
if(c>2) f = 1;
}
if(f==1) return marked>=m;
else {
if(m%2==1) return false;
else return marked>=m;
}
}
/*--------------------------------------------FAST I/O-------------------------------*/
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;
}
char nextchar() {
char ch = ' ';
try {
ch = (char) br.read();
} catch (IOException e) {
e.printStackTrace();
}
return ch;
}
}
/*--------------------------------------------HELPER---------------------------------*/
static class pair implements Comparable<pair> {
int x, y;
public pair(int a, int b) {
x = a;
y = b;
}
@Override
public int hashCode() {
int hash = 3;
hash = 47 * hash + this.x;
hash = 47 * hash + this.y;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final pair other = (pair) obj;
if (this.x != other.x) {
return false;
}
if (this.y != other.y) {
return false;
}
return true;
}
@Override
public int compareTo(apples.pair o) {
if(this.x==o.x) return this.y-o.y;
return this.x-o.x;
}
}
/*--------------------------------------------BOILER PLATE---------------------------*/
static int[] readarr(int n) {
int arr[] = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = x.nextInt();
}
return arr;
}
static int[] sortint(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static long[] sortlong(long a[]) {
ArrayList<Long> al = new ArrayList<>();
for (long i : a) {
al.add(i);
}
Collections.sort(al);
for (int i = 0; i < al.size(); i++) {
a[i] = al.get(i);
}
return a;
}
static long pow(long x, long y) {
long result = 1;
while (y > 0) {
if (y % 2 == 0) {
x = x * x;
y = y / 2;
} else {
result = result * x;
y = y - 1;
}
}
return result;
}
static long pow(long x, long y, long mod) {
long result = 1;
x %= mod;
while (y > 0) {
if (y % 2 == 0) {
x = (x % mod * x % mod) % mod;
y /= 2;
} else {
result = (result % mod * x % mod) % mod;
y--;
}
}
return result;
}
static int[] revsort(int a[]) {
ArrayList<Integer> al = new ArrayList<>();
for (int i : a) {
al.add(i);
}
Collections.sort(al, Comparator.reverseOrder());
for (int i = 0; i < a.length; i++) {
a[i] = al.get(i);
}
return a;
}
static int[] gcd(int a, int b, int ar[]) {
if (b == 0) {
ar[0] = a;
ar[1] = 1;
ar[2] = 0;
return ar;
}
ar = gcd(b, a % b, ar);
int t = ar[1];
ar[1] = ar[2];
ar[2] = t - (a / b) * ar[2];
return ar;
}
static boolean[] esieve(int n) {
boolean p[] = new boolean[n + 1];
Arrays.fill(p, true);
for (int i = 2; i * i <= n; i++) {
if (p[i] == true) {
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
return p;
}
static ArrayList<Integer> primes(int n) {
boolean p[] = new boolean[n + 1];
ArrayList<Integer> al = new ArrayList<>();
Arrays.fill(p, true);
int i = 0;
for (i = 2; i * i <= n; i++) {
if (p[i] == true) {
al.add(i);
for (int j = i * i; j <= n; j += i) {
p[j] = false;
}
}
}
for (i = i; i <= n; i++) {
if (p[i] == true) {
al.add(i);
}
}
return al;
}
static int gcd(int a, int b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
static long gcd(long a, long b) {
if (a == 0) {
return b;
}
return gcd(b % a, a);
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 1ca3de9e30a2544bb7dffd0a5e38155d | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class cd810div1A {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
for (int j = 0; j < tc; j++) {
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
long[] color = new long[k];
for (int i = 0; i < k; i++) {
color[i] = sc.nextLong();
}
Arrays.sort(color);
long total = 0;
int flag = 0;
boolean ans = false;
for (int i = 0; i < k; i++) {
if (color[i] / n > 2) {
flag = 1;
}
if (color[i] / n >= 2) {
total += color[i] / n;
}
}
if (total >= m && (flag == 1 || m % 2 == 0)) {
System.out.println("Yes");
ans = true;
}
if (!ans) {
flag = 0;
total = 0;
for (int i = 0; i < k; i++) {
if (color[i] / m > 2) {
flag = 1;
}
if (color[i] / m >= 2) {
total += color[i] / m;
}
}
if (total >= n && (flag == 1 || n % 2 == 0)) {
System.out.println("Yes");
ans = true;
}
}
if (!ans) {
System.out.println("No");
}
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | d7e72c3d5fa6dcf434f8eb6de2421954 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.*;
public class Main {
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args){
MyScanner scanner = new MyScanner();
int numOfTests = scanner.nextInt();
for(int t = 1; t <= numOfTests; t++){
int m = scanner.nextInt(), n = scanner.nextInt();
int k = scanner.nextInt();
long[] arr = new long[k];
for(int i = 0; i < k; i++){
arr[i] = scanner.nextLong();
}
boolean res = solve(m, n, arr) || solve(n, m, arr);
System.out.println(res? "Yes":"No");
}
}
private static boolean solve(long m, long n, long[] arr){
boolean flag = false;
long needEachColor = m;
long count = 0;
for(int i = 0; i < arr.length; i++){
if(arr[i] / needEachColor > 2){
flag = true;
}
if(arr[i] / needEachColor >= 2) {
count += arr[i] / needEachColor;
}
}
if(count >= n && (flag || n%2==0)){
return true;
}
return false;
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | d4ca4074ea37129fd9e77f77a7520ece | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | // Problem: A. Color the Picture
// Contest: Codeforces - Codeforces Round #810 (Div. 1)
// URL: https://codeforces.com/problemset/problem/1710/A
// Memory Limit: 256 MB
// Time Limit: 1000 ms
import java.util.*;
import java.io.*;
public class Main {
static FastReader sc=new FastReader();
static PrintWriter out=new PrintWriter(System.out);
static int dx[]={0,0,-1,1},dy[]={-1,1,0,0};
static final double pi=3.1415926536;
static long mod=1000000007;
// static long mod=998244353;
static int MAX=Integer.MAX_VALUE;
static int MIN=Integer.MIN_VALUE;
static long MAXL=Long.MAX_VALUE;
static long MINL=Long.MIN_VALUE;
static ArrayList<Integer> graph[];
static long fact[];
static long seg[];
// static int dp[][];
static long dp[][];
public static void main (String[] args) throws java.lang.Exception {
// code goes here
int t = I();
for(int i = 0; i < t; i++){
long[] nmk = IL(3);
long n = nmk[0];
long m = nmk[1];
long k = nmk[2];
if(n == 1 || m == 1){
out.println("No");
continue;
}
long[] kArr = IL((int)k);
long sum = 0;
long aMax = 0;
int aMaxIdx = 0;
for(int x = 0; x < k; x++){
sum += kArr[x];
if(kArr[x] > aMax){
aMax = kArr[x];
aMaxIdx = x;
}
}
if(sum < n * m){
out.println("No");
continue;
}
if(n == 2 || m == 2){
if(aMax >= n * m){
out.println("Yes");
}else{
out.println("No");
}
continue;
}
if(f(kArr, n, m, k, aMax, aMaxIdx) || f(kArr, m, n, k, aMax, aMaxIdx)){
out.println("Yes");
}else{
out.println("No");
}
}
out.close();
}
static boolean f(long[] kArr, long n, long m, long k, long aMax, int aMaxIdx){
long canMax = 0;
if(n % 2 == 0){
//看每钟颜料能支持多少
if(aMax < (2 * m)){
return false;
}
for(int x = 0; x < k; x++){
if(kArr[x] / m > 1){
canMax += kArr[x] / m;
}
}
return canMax >= n;
}else{
if(aMax < (3 * m)){
return false;
}
for(int x = 0; x < k; x++){
if(kArr[x] / m > 1){
canMax += kArr[x] / m;
}
}
return canMax >= n;
}
}
static long nps(int N) {
int nextN = (int)Math.floor(Math.sqrt(N)) + 1;
return 1L * nextN * nextN;
}
public static class pair {
int a;
int b;
public pair(int aa,int bb) {
a=aa;
b=bb;
}
}
//comparator
public static class myComp implements Comparator<pair> {
//sort in ascending order.
// public int compare(pair p1,pair p2)
// {
// if(p1.a==p2.a)
// return 0;
// else if(p1.a<p2.a)
// return -1;
// else
// return 1;
// }
// sort in descending order.
public int compare(pair p1,pair p2) {
if(p1.b==p2.b)
return 0;
else if(p1.b<p2.b)
return 1;
else
return -1;
}
}
//create graph
public static void setGraph(int n,int m)throws IOException {
graph=new ArrayList[n+1];
for(int i=0;i<=n;i++){
graph[i]=new ArrayList<>();
}
for(int i=0;i<m;i++){
int u=I(),v=I();
graph[u].add(v);
graph[v].add(u);
}
}
//LOWER_BOUND and UPPER_BOUND functions
//It returns answer according to zero based indexing.
public static int lower_bound(ArrayList<Long> arr,long X,int start, int end) { //start=0,end=n-1
if(start>end)return -1;
if(arr.get(end)<X)return end;
// if(arr[end]<X)return end;
if(arr.get(start)>X)return -1;
// if(arr[start]>X)return -1;
int left=start,right=end;
while(left<right){
int mid=(left+right)/2;
// if(arr[mid]==X){
if(arr.get(mid)==X){ //Returns last index of lower bound value.
// if(mid<end && arr[mid+1]==X){
if(mid<end && arr.get(mid+1)==X){
left=mid+1;
}else{
return mid;
}
}
// if(arr.get(mid)==X){ //Returns first index of lower bound value.
// if(arr[mid]==X){
// // if(mid>start && arr.get(mid-1)==X){
// if(mid>start && arr[mid-1]==X){
// right=mid-1;
// }else{
// return mid;
// }
// }
// else if(arr[mid]>X){
else if(arr.get(mid)>X){
// if(mid>start && arr[mid-1]<X){
if(mid>start && arr.get(mid-1)<X){
return mid-1;
}else{
right=mid-1;
}
}else{
// if(mid<end && arr[mid+1]>X){
if(mid<end && arr.get(mid+1)>X){
return mid;
}else{
left=mid+1;
}
}
}
return left;
}
//It returns answer according to zero based indexing.
public static int upper_bound(ArrayList<Integer> arr,int X,int start,int end){ //start=0,end=n-1
if(arr.get(0)>=X)return start;
if(arr.get(arr.size()-1)<X)return -1;
int left=start,right=end;
while(left<right){
int mid=(left+right)/2;
if(arr.get(mid)==X){ //returns first index of upper bound value.
if(mid>start && arr.get(mid-1)==X){
right=mid-1;
}else{
return mid;
}
}
// if(arr[mid]==X){ //returns last index of upper bound value.
// if(mid<end && arr[mid+1]==X){
// left=mid+1;
// }else{
// return mid;
// }
// }
else if(arr.get(mid)>X){
if(mid>start && arr.get(mid-1)<X){
return mid;
}else{
right=mid-1;
}
}else{
if(mid<end && arr.get(mid+1)>X){
return mid+1;
}else{
left=mid+1;
}
}
}
return left;
}
//END
//Segment Tree Code
public static void buildTree(long a[],int si,int ss,int se) {
if(ss==se){
seg[si]=a[ss];
return;
}
int mid=(ss+se)/2;
buildTree(a,2*si+1,ss,mid);
buildTree(a,2*si+2,mid+1,se);
seg[si]=max(seg[2*si+1],seg[2*si+2]);
}
// public static void update(int si,int ss,int se,int pos,int val)
// {
// if(ss==se){
// // seg[si]=val;
// return;
// }
// int mid=(ss+se)/2;
// if(pos<=mid){
// update(2*si+1,ss,mid,pos,val);
// }else{
// update(2*si+2,mid+1,se,pos,val);
// }
// // seg[si]=min(seg[2*si+1],seg[2*si+2]);
// if(seg[2*si+1].a < seg[2*si+2].a){
// seg[si].a=seg[2*si+1].a;
// seg[si].b=seg[2*si+1].b;
// }else{
// seg[si].a=seg[2*si+2].a;
// seg[si].b=seg[2*si+2].b;
// }
// }
public static long query1(int si,int ss,int se,int qs,int qe) {
if(qs>se || qe<ss)return 0;
if(ss>=qs && se<=qe)return seg[si];
int mid=(ss+se)/2;
long p1=query1(2*si+1,ss,mid,qs,qe);
long p2=query1(2*si+2,mid+1,se,qs,qe);
return max(p1,p2);
}
public static void merge(ArrayList<Integer> f,ArrayList<Integer> a,ArrayList<Integer> b) {
int i=0,j=0;
while(i<a.size() && j<b.size()){
if(a.get(i)<=b.get(j)){
f.add(a.get(i));
i++;
}else{
f.add(b.get(j));
j++;
}
}
while(i<a.size()){
f.add(a.get(i));
i++;
}
while(j<b.size()){
f.add(b.get(j));
j++;
}
}
//Segment Tree Code end
//Prefix Function of KMP Algorithm
public static int[] KMP(char c[],int n) {
int pi[]=new int[n];
for(int i=1;i<n;i++){
int j=pi[i-1];
while(j>0 && c[i]!=c[j]){
j=pi[j-1];
}
if(c[i]==c[j])j++;
pi[i]=j;
}
return pi;
}
//largest sum subarray
public static long kadane(long a[],int n){
long max_sum=Long.MIN_VALUE,max_end=0;
for(int i=0;i<n;i++){
max_end+=a[i];
if(max_sum<max_end){max_sum=max_end;}
if(max_end<0){max_end=0;}
}
return max_sum;
}
public static long nPr(int n,int r) {
long ans=divide(fact(n),fact(n-r),mod);
return ans;
}
public static long nCr(int n,int r) {
long ans=divide(fact[n],mul(fact[n-r],fact[r]),mod);
return ans;
}
//asc: true
public static boolean isSorted(int a[]) {
int n=a.length;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])return false;
}
return true;
}
public static boolean isSorted(long a[]) {
int n=a.length;
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1])return false;
}
return true;
}
//compute XOR of all numbers between 1 to n.
public static int computeXOR(int n){
if (n % 4 == 0)
return n;
if (n % 4 == 1)
return 1;
if (n % 4 == 2)
return n + 1;
return 0;
}
public static int np2(int x) {
x--;
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
x++;
return x;
}
public static int hp2(int x) {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static long hp2(long x) {
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return x ^ (x >> 1);
}
public static ArrayList<Integer> primeSieve(int n) {
ArrayList<Integer> arr=new ArrayList<>();
boolean prime[] = new boolean[n + 1];
for (int i = 0; i <= n; i++)
prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p)
prime[i] = false;
}
}
for (int i = 2; i <= n; i++) {
if (prime[i] == true)
arr.add(i);
}
return arr;
}
// Fenwick / BinaryIndexed Tree USE IT - FenwickTree ft1=new FenwickTree(n);
public static class FenwickTree {
int farr[];
int n;
public FenwickTree(int c) {
n=c+1;
farr=new int[n];
}
// public void update_range(int l,int r,long p)
// {
// update(l,p);
// update(r+1,(-1)*p);
// }
public void update(int x,int p) {
for(;x<n;x+=x&(-x))
{
farr[x]+=p;
}
}
public int get(int x) {
int ans=0;
for(;x>0;x-=x&(-x))
{
ans=ans+farr[x];
}
return ans;
}
}
//Disjoint Set Union
public static class DSU {
int par[],rank[];
public DSU(int c) {
par=new int[c+1];
rank=new int[c+1];
for(int i=0;i<=c;i++)
{
par[i]=i;
rank[i]=0;
}
}
public int find(int a) {
if(a==par[a])
return a;
return par[a]=find(par[a]);
}
public void union(int a,int b) {
int a_rep=find(a),b_rep=find(b);
if(a_rep==b_rep)
return;
if(rank[a_rep]<rank[b_rep])
par[a_rep]=b_rep;
else if(rank[a_rep]>rank[b_rep])
par[b_rep]=a_rep;
else
{
par[b_rep]=a_rep;
rank[a_rep]++;
}
}
}
public static HashMap<Integer,Integer> primeFact(int a) {
// HashSet<Long> arr=new HashSet<>();
HashMap<Integer,Integer> hm=new HashMap<>();
int p=0;
while(a%2==0){
// arr.add(2L);
p++;
a=a/2;
}
hm.put(2,hm.getOrDefault(2,0)+p);
for(int i=3;i*i<=a;i+=2){
p=0;
while(a%i==0){
// arr.add(i);
p++;
a=a/i;
}
hm.put(i,hm.getOrDefault(i,0)+p);
}
if(a>2){
// arr.add(a);
hm.put(a,hm.getOrDefault(a,0)+1);
}
// return arr;
return hm;
}
public static boolean isVowel(char c) {
if(c=='a' || c=='e' || c=='i' || c=='u' || c=='o')return true;
return false;
}
public static boolean isInteger(double N) {
int X = (int)N;
double temp2 = N - X;
if (temp2 > 0) {
return false;
}
return true;
}
public static boolean isPalindrome(String s) {
int n=s.length();
for(int i=0;i<=n/2;i++){
if(s.charAt(i)!=s.charAt(n-i-1)){
return false;
}
}
return true;
}
public static int gcd(int a,int b) {
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long gcd(long a,long b) {
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static long fact(long n) {
long fact=1;
for(long i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static long fact(int n) {
long fact=1;
for(int i=2;i<=n;i++){
fact=((fact%mod)*(i%mod))%mod;
}
return fact;
}
public static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
public static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
double sq=Math.sqrt(n);
for (int i = 5; i <= sq; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
//print array
public static void printArray(long a[]) {
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(int a[]) {
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(char a[]) {
for(int i=0;i<a.length;i++){
out.print(a[i]);
}
out.println();
}
public static void printArray(String a[]) {
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(boolean a[]) {
for(int i=0;i<a.length;i++){
out.print(a[i]+" ");
}
out.println();
}
public static void printArray(pair a[]) {
for(pair p:a){
out.println(p.a+"->"+p.b);
}
}
public static void printArray(int a[][]) {
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(boolean a[][]) {
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(long a[][]) {
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(char a[][]) {
for(int i=0;i<a.length;i++){
for(int j=0;j<a[i].length;j++){
out.print(a[i][j]+" ");
}out.println();
}
}
public static void printArray(ArrayList<?> arr) {
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}
out.println();
}
public static void printMapI(HashMap<?,?> hm){
for(Map.Entry<?,?> e:hm.entrySet()){
out.println(e.getKey()+"->"+e.getValue());
}out.println();
}
public static void printMap(HashMap<Long,ArrayList<Integer>> hm){
for(Map.Entry<Long,ArrayList<Integer>> e:hm.entrySet()){
out.print(e.getKey()+"->");
ArrayList<Integer> arr=e.getValue();
for(int i=0;i<arr.size();i++){
out.print(arr.get(i)+" ");
}out.println();
}
}
public static void printGraph(ArrayList<Integer> graph[]) {
int n=graph.length;
for(int i=0;i<n;i++){
out.print(i+"->");
for(int j:graph[i]){
out.print(j+" ");
}out.println();
}
}
//Modular Arithmetic
public static long add(long a,long b) {
a+=b;
if(a>=mod)a-=mod;
return a;
}
public static long sub(long a,long b) {
a-=b;
if(a<0)a+=mod;
return a;
}
public static long mul(long a,long b) {
return ((a%mod)*(b%mod))%mod;
}
public static long divide(long a,long b,long m) {
a=mul(a,modInverse(b,m));
return a;
}
public static long modInverse(long a,long m) {
int x=0,y=0;
own p=new own(x,y);
long g=gcdExt(a,m,p);
if(g!=1){
out.println("inverse does not exists");
return -1;
}else{
long res=((p.a%m)+m)%m;
return res;
}
}
public static long gcdExt(long a,long b,own p) {
if(b==0){
p.a=1;
p.b=0;
return a;
}
int x1=0,y1=0;
own p1=new own(x1,y1);
long gcd=gcdExt(b,a%b,p1);
p.b=p1.a - (a/b) * p1.b;
p.a=p1.b;
return gcd;
}
public static long pwr(long m,long n) {
long res=1;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m);
}
n=n>>1;
m=(m*m);
}
return res;
}
public static long modpwr(long m,long n) {
long res=1;
m=m%mod;
if(m==0)
return 0;
while(n>0)
{
if((n&1)!=0)
{
res=(res*m)%mod;
}
n=n>>1;
m=(m*m)%mod;
}
return res;
}
public static class own {
long a;
long b;
public own(long val,long index)
{
a=val;
b=index;
}
}
//Modular Airthmetic
//sort
public static void sort(int[] A) {
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i)
{
int tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void sort(char[] A) {
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i) {
char tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
public static void sort(long[] A) {
int n = A.length;
Random rnd = new Random();
for(int i=0; i<n; ++i) {
long tmp = A[i];
int randomPos = i + rnd.nextInt(n-i);
A[i] = A[randomPos];
A[randomPos] = tmp;
}
Arrays.sort(A);
}
//max & min
public static int max(int a,int b){return Math.max(a,b);}
public static int min(int a,int b){return Math.min(a,b);}
public static int max(int a,int b,int c){return Math.max(a,Math.max(b,c));}
public static int min(int a,int b,int c){return Math.min(a,Math.min(b,c));}
public static long max(long a,long b){return Math.max(a,b);}
public static long min(long a,long b){return Math.min(a,b);}
public static long max(long a,long b,long c){return Math.max(a,Math.max(b,c));}
public static long min(long a,long b,long c){return Math.min(a,Math.min(b,c));}
public static int maxinA(int a[]){int n=a.length;int mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;}
public static long maxinA(long a[]){int n=a.length;long mx=a[0];for(int i=1;i<n;i++){mx=max(mx,a[i]);}return mx;}
public static int mininA(int a[]){int n=a.length;int mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;}
public static long mininA(long a[]){int n=a.length;long mn=a[0];for(int i=1;i<n;i++){mn=min(mn,a[i]);}return mn;}
public static long suminA(int a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;}
public static long suminA(long a[]){int n=a.length;long sum=0;for(int i=0;i<n;i++){sum+=a[i];}return sum;}
//end
public static int[] I(int n){int a[]=new int[n];for(int i=0;i<n;i++){a[i]=I();}return a;}
public static long[] IL(int n){long a[]=new long[n];for(int i=0;i<n;i++){a[i]=L();}return a;}
public static long[] prefix(int a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;}
public static long[] prefix(long a[]){int n=a.length;long pre[]=new long[n];pre[0]=a[0];for(int i=1;i<n;i++){pre[i]=pre[i-1]+a[i];}return pre;}
public static int I(){return sc.I();}
public static long L(){return sc.L();}
public static String S(){return sc.S();}
public static double D(){return sc.D();}
}
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 I(){ return Integer.parseInt(next());}
long L(){ return Long.parseLong(next());}
double D(){return Double.parseDouble(next());}
String S(){
String str = "";
try {
str = br.readLine();
}
catch (IOException e){
e.printStackTrace();
}
return str;
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 7ebbe9006fba1bb91239d126a3f0e449 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.*;
import java.util.function.DoubleToLongFunction;
public class Codeforces810{
static long mod = 1000000007L;
static MyScanner sc = new MyScanner();
static void solve() {
int n = sc.nextInt();
if(n==1){
out.println(1);
return;
}
int arr[] = new int[n];
for(int i = 0;i<n;i++) arr[i] = i+1;
if(n%2==0){
for(int i = 0;i<n-1;i+=2){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}else{
for(int i = 1;i<n-1;i+=2){
int temp = arr[i];
arr[i] = arr[i+1];
arr[i+1] = temp;
}
}
for(int i:arr) out.print(i+" ");
out.println();
}
static void solve2(){
int n = sc.nextInt();
int m = sc.nextInt();
int arr[] = sc.readIntArray(n);
HashMap<Integer,HashSet<Integer>> map = new HashMap<>();
int aa[] = new int[m];
int bb[] = new int[m];
for(int i = 0;i<m;i++){
int a = sc.nextInt()-1;
int b = sc.nextInt()-1;
aa[i] = a;
bb[i] = b;
if(!map.containsKey(a)) map.put(a,new HashSet<>());
if(!map.containsKey(b)) map.put(b,new HashSet<>());
map.get(a).add(b);
map.get(b).add(a);
}
if(m%2==0){
out.println(0);
return;
}
long ans = Integer.MAX_VALUE;
for(int i = 0 ;i<m;i++){
if(map.get(aa[i]).size()%2==1) ans = Math.min(arr[aa[i]],ans);
if(map.get(bb[i]).size()%2==1) ans = Math.min(arr[bb[i]],ans);
if(map.get(aa[i]).size()%2==0 && map.get(bb[i]).size()%2==0){
ans = Math.min(ans,arr[aa[i]]+arr[bb[i]]);
}
}
out.println(ans);
}
static void solve3(){
int n = sc.nextInt();
int m = sc.nextInt();
int k = sc.nextInt();
int arr[]= sc.readIntArray(k);
long s1 = 0;
long s2 = 0;
boolean f1 = false;
boolean f2 = false;
for(int i = 0;i<k;i++){
int t = arr[i]/n;
if(t>1) s1+= t;
if(t>2) f1 = true;
}
for(int i = 0;i<k;i++){
int t = arr[i]/m;
if(t>1) s2+= t;
if(t>2) f2 = true;
}
if((s1>=m && (m%2==0 || f1)) || (s2>=n && (n%2==0 || f2))){
out.println("YES");
}else{
out.println("NO");
}
}
static void solve5(){
}
static void solve4(){
}
static void solve6(){
}
static void solve7(){
}
static void reverse(int arr[]){
int i= 0;
int j = arr.length-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
}
static void swap(char arr[][],int i,int j){
for(int k = j;k>0;k--){
if(arr[i][k]=='.'&& arr[i][k-1]=='*'){
char temp = arr[i][k];
arr[i][k] = arr[i][k-1];
arr[i][k-1] = temp;
}
}
}
static int search(int pre,int suf[],int i,int j){
while(i<=j){
int mid = (i+j)/2;
if(suf[mid]==pre) return mid;
else if(suf[mid]<pre) j = mid-1;
else i = mid+1;
}
return Integer.MIN_VALUE;
}
static long pow(long a, long b) {
if (b == 0) return 1;
long res = pow(a, b / 2);
res = (res * res) % 1_000_000_007;
if (b % 2 == 1) {
res = (res * a) % 1_000_000_007;
}
return res;
}
static int lis(int arr[],int n){
int lis[] = new int[n];
lis[0] = 1;
for(int i = 1;i<n;i++){
lis[i] = 1;
for(int j = 0;j<i;j++){
if(arr[i]>arr[j]){
lis[i] = Math.max(lis[i],lis[j]+1);
}
}
}
int max = Integer.MIN_VALUE;
for(int i = 0;i<n;i++){
max = Math.max(lis[i],max);
}
return max;
}
static boolean isPali(String str){
int i = 0;
int j = str.length()-1;
while(i<j){
if(str.charAt(i)!=str.charAt(j)){
return false;
}
i++;
j--;
}
return true;
}
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static String reverse(String str){
char arr[] = str.toCharArray();
int i = 0;
int j = arr.length-1;
while(i<j){
char temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
i++;
j--;
}
String st = new String(arr);
return st;
}
static boolean isprime(int n){
if(n==1) return false;
if(n==3 || n==2) return true;
if(n%2==0 || n%3==0) return false;
for(int i = 5;i*i<=n;i+= 6){
if(n%i== 0 || n%(i+2)==0){
return false;
}
}
return true;
}
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
// int t= 1;
while(t-- >0){
// solve();
// solve2();
solve3();
// solve5();
// solve6();
// solve7();
// solve8();
}
// Stop writing your solution here. -------------------------------------
out.close();
}
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
//-----------MyScanner class for faster input----------
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] readIntArray(int n){
int arr[] = new int[n];
for(int i = 0;i<n;i++){
arr[i] = Integer.parseInt(next());
}
return arr;
}
int[] reverse(int arr[]){
int n= arr.length;
int i = 0;
int j = n-1;
while(i<j){
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
j--;i++;
}
return arr;
}
long[] readLongArray(int n){
long arr[] = new long[n];
for(int i = 0;i<n;i++){
arr[i] = Long.parseLong(next());
}
return arr;
}
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;
}
}
private static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
private static void sort(long[] arr) {
List<Long> list = new ArrayList<>();
for (int i=0; i<arr.length; i++){
list.add(arr[i]);
}
Collections.sort(list); // collections.sort uses nlogn in backend
for (int i = 0; i < arr.length; i++){
arr[i] = list.get(i);
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 664582debdd2463854f71daa66ec981a | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes |
import java.io.*;
import java.util.*;
public final class Main {
//int 2e9 - long 9e18
static PrintWriter out = new PrintWriter(System.out);
static FastReader in = new FastReader();
static Pair[] moves = new Pair[]{new Pair(-1, 0), new Pair(0, 1), new Pair(1, 0), new Pair(0, -1)};
static Pair[] movesDiagonal = new Pair[]{new Pair(-1, -1), new Pair(-1, 1), new Pair(1, -1), new Pair(1, 1)};
static int mod = (int) (1e9 + 7);
static int mod2 = 998244353;
public static void main(String[] args) {
int tt = i();
while (tt-- > 0) {
solve();
}
out.flush();
}
public static void solve() {
int n = i();
int m = i();
int k = i();
int[] a = input(k);
Arrays.sort(a);
reverse(a);
if (use(n, m, k, a) || use(m, n, k, a)) {
out.println("YES");
} else {
out.println("NO");
}
}
private static boolean use(int i, int target, int k, int[] a) {
long sum = 0;
long more = 0;
for (int j = 0; j < k; j++) {
int x = a[j] / i;
if (x >= 2) {
more += (x - 2);
sum += x;
if (sum >= target && more >= (sum - target)) {
return true;
}
}
}
return false;
}
// (10,5) = 2 ,(11,5) = 3
static long upperDiv(long a, long b) {
return (a / b) + ((a % b == 0) ? 0 : 1);
}
static long sum(int[] a) {
long sum = 0;
for (int x : a) {
sum += x;
}
return sum;
}
static int[] preint(int[] a) {
int[] pre = new int[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static long[] pre(int[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static long[] post(int[] a) {
long[] post = new long[a.length + 1];
post[0] = 0;
for (int i = 0; i < a.length; i++) {
post[i + 1] = post[i] + a[a.length - 1 - i];
}
return post;
}
static long[] pre(long[] a) {
long[] pre = new long[a.length + 1];
pre[0] = 0;
for (int i = 0; i < a.length; i++) {
pre[i + 1] = pre[i] + a[i];
}
return pre;
}
static void print(char A[]) {
for (char c : A) {
out.print(c);
}
out.println();
}
static void print(boolean A[]) {
for (boolean c : A) {
out.print(c + " ");
}
out.println();
}
static void print(int A[]) {
for (int c : A) {
out.print(c + " ");
}
out.println();
}
static void print(long A[]) {
for (long i : A) {
out.print(i + " ");
}
out.println();
}
static void print(List<Integer> A) {
for (int a : A) {
out.print(a + " ");
}
}
static int i() {
return in.nextInt();
}
static long l() {
return in.nextLong();
}
static double d() {
return in.nextDouble();
}
static String s() {
return in.nextLine();
}
static String c() {
return in.next();
}
static int[][] inputWithIdx(int N) {
int A[][] = new int[N][2];
for (int i = 0; i < N; i++) {
A[i] = new int[]{i, in.nextInt()};
}
return A;
}
static int[] input(int N) {
int A[] = new int[N];
for (int i = 0; i < N; i++) {
A[i] = in.nextInt();
}
return A;
}
static long[] inputLong(int N) {
long A[] = new long[N];
for (int i = 0; i < A.length; i++) {
A[i] = in.nextLong();
}
return A;
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long GCD(long a, long b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
static long LCM(int a, int b) {
return (long) a / GCD(a, b) * b;
}
static long LCM(long a, long b) {
return a / GCD(a, b) * b;
}
// find highest i which satisfy a[i]<=x
static int lowerbound(List<Long> a, long x) {
int l = 0;
int r = a.size() - 1;
while (l < r) {
int m = (l + r + 1) / 2;
if (a.get(m) <= x) {
l = m;
} else {
r = m - 1;
}
}
return l;
}
static void shuffle(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
}
static void shuffleAndSort(int[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static void shuffleAndSort(int[][] arr, Comparator<? super int[]> comparator) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
int[] temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr, comparator);
}
static void shuffleAndSort(long[] arr) {
for (int i = 0; i < arr.length; i++) {
int rand = (int) (Math.random() * arr.length);
long temp = arr[rand];
arr[rand] = arr[i];
arr[i] = temp;
}
Arrays.sort(arr);
}
static boolean isPerfectSquare(double number) {
double sqrt = Math.sqrt(number);
return ((sqrt - Math.floor(sqrt)) == 0);
}
static void swap(int A[], int a, int b) {
int t = A[a];
A[a] = A[b];
A[b] = t;
}
static void swap(char A[], int a, int b) {
char t = A[a];
A[a] = A[b];
A[b] = t;
}
static long pow(long a, long b, int mod) {
long pow = 1;
long x = a;
while (b != 0) {
if ((b & 1) != 0) {
pow = (pow * x) % mod;
}
x = (x * x) % mod;
b /= 2;
}
return pow;
}
static long pow(long a, long b) {
long pow = 1;
long x = a;
while (b != 0) {
if ((b & 1) != 0) {
pow *= x;
}
x = x * x;
b /= 2;
}
return pow;
}
static long modInverse(long x, int mod) {
return pow(x, mod - 2, mod);
}
static boolean isPrime(long N) {
if (N <= 1) {
return false;
}
if (N <= 3) {
return true;
}
if (N % 2 == 0 || N % 3 == 0) {
return false;
}
for (int i = 5; i * i <= N; i = i + 6) {
if (N % i == 0 || N % (i + 2) == 0) {
return false;
}
}
return true;
}
public static String reverse(String str) {
if (str == null) {
return null;
}
return new StringBuilder(str).reverse().toString();
}
public static void reverse(int[] arr) {
int l = 0;
int r = arr.length - 1;
while (l < r) {
swap(arr, l, r);
l++;
r--;
}
}
public static String repeat(char ch, int repeat) {
if (repeat <= 0) {
return "";
}
final char[] buf = new char[repeat];
for (int i = repeat - 1; i >= 0; i--) {
buf[i] = ch;
}
return new String(buf);
}
public static int[] manacher(String s) {
char[] chars = s.toCharArray();
int n = s.length();
int[] d1 = new int[n];
for (int i = 0, l = 0, r = -1; i < n; i++) {
int k = (i > r) ? 1 : Math.min(d1[l + r - i], r - i + 1);
while (0 <= i - k && i + k < n && chars[i - k] == chars[i + k]) {
k++;
}
d1[i] = k--;
if (i + k > r) {
l = i - k;
r = i + k;
}
}
return d1;
}
public static int[] kmp(String s) {
int n = s.length();
int[] res = new int[n];
for (int i = 1; i < n; ++i) {
int j = res[i - 1];
while (j > 0 && s.charAt(i) != s.charAt(j)) {
j = res[j - 1];
}
if (s.charAt(i) == s.charAt(j)) {
++j;
}
res[i] = j;
}
return res;
}
}
class Pair {
int i;
int j;
Pair(int i, int j) {
this.i = i;
this.j = j;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
Pair pair = (Pair) o;
return i == pair.i && j == pair.j;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
class ThreePair {
int i;
int j;
int k;
ThreePair(int i, int j, int k) {
this.i = i;
this.j = j;
this.k = k;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
ThreePair pair = (ThreePair) o;
return i == pair.i && j == pair.j && k == pair.k;
}
@Override
public int hashCode() {
return Objects.hash(i, j);
}
}
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;
}
}
class Node {
int val;
public Node(int val) {
this.val = val;
}
}
class ST {
int n;
Node[] st;
ST(int n) {
this.n = n;
st = new Node[4 * Integer.highestOneBit(n)];
}
void build(Node[] nodes) {
build(0, 0, n - 1, nodes);
}
private void build(int id, int l, int r, Node[] nodes) {
if (l == r) {
st[id] = nodes[l];
return;
}
int mid = (l + r) >> 1;
build((id << 1) + 1, l, mid, nodes);
build((id << 1) + 2, mid + 1, r, nodes);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
void update(int i, Node node) {
update(0, 0, n - 1, i, node);
}
private void update(int id, int l, int r, int i, Node node) {
if (i < l || r < i) {
return;
}
if (l == r) {
st[id] = node;
return;
}
int mid = (l + r) >> 1;
update((id << 1) + 1, l, mid, i, node);
update((id << 1) + 2, mid + 1, r, i, node);
st[id] = comb(st[(id << 1) + 1], st[(id << 1) + 2]);
}
Node get(int x, int y) {
return get(0, 0, n - 1, x, y);
}
private Node get(int id, int l, int r, int x, int y) {
if (x > r || y < l) {
return new Node(0);
}
if (x <= l && r <= y) {
return st[id];
}
int mid = (l + r) >> 1;
return comb(get((id << 1) + 1, l, mid, x, y), get((id << 1) + 2, mid + 1, r, x, y));
}
Node comb(Node a, Node b) {
if (a == null) {
return b;
}
if (b == null) {
return a;
}
return new Node(GCD(a.val, b.val));
}
static int GCD(int a, int b) {
if (b == 0) {
return a;
} else {
return GCD(b, a % b);
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 527861416f5b65bdd65ec0963c3bb5a3 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.*;
public class TaskB {
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public static void main(String[] args) throws IOException {
new TaskB().run();
}
public void solve() throws IOException {
int t = nextInt();
for (int tt = 0; tt < t; tt++) {
int n = nextInt();
int m = nextInt();
int k = nextInt();
int[] a = new int[k];
for (int i = 0; i < k; i++) {
a[i] = nextInt();
}
long filled = 0;
boolean has3 = false;
boolean rowPos;
for (int i = 0; i < k; i++) {
if (a[i] / m > 2) {
has3 = true;
}
if (a[i] / m >= 2) {
filled += a[i] / m;
}
}
rowPos = filled >= n && ((n % 2 == 0) || has3);
filled = 0;
has3 = false;
boolean colPos;
for (int i = 0; i < k; i++) {
if (a[i] / n > 2) {
has3 = true;
}
if (a[i] / n >= 2) {
filled += a[i] / n;
}
}
colPos = filled >= m && ((m % 2 == 0) || has3);
if (rowPos || colPos) {
out.println("Yes");
} else {
out.println("No");
}
}
}
public void run() {
try {
/* br = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");*/
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 56df726c677932c77601e1bf15cc07bf | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.*;
public class p1
{
BufferedReader br;
StringTokenizer st;
BufferedWriter bw;
public static void main(String[] args)throws Exception
{
new p1().run();
}
void run()throws IOException
{
br = new BufferedReader(new InputStreamReader(System.in));
bw=new BufferedWriter(new OutputStreamWriter(System.out));
solve();
}
void solve() throws IOException
{
int t=ni();
while(t-->0)
{
int n=ni();
int m=ni();
int k=ni();
int a[]=nai(k);
Integer b[]=new Integer[k];
for(int i=-1;++i<k;)
b[i]=a[i]/n;
Integer c[]=new Integer[k];
for(int i=-1;++i<k;)
c[i]=a[i]/m;
Arrays.sort(b);
Arrays.sort(c);
long sum=0;
int i1=-1,j1=-1;
for(;++i1<k && b[i1].intValue()<2;);
for(;++j1<k && c[j1].intValue()<2;);
int i=i1-1, j=j1-1;
for(;++i<k && sum<m;)
sum+=b[i].intValue();
long sum2=0;
for(;++j<k && sum2<n;)
sum2+=c[j].intValue();
if(sum==m || sum2==n)
System.out.println("Yes");
else
{
boolean x=false, y=false;
if(sum>m)
{
long x1=sum-b[i-1].intValue();
x1=m-x1;
if(x1>1 || b[i1].intValue()!=b[k-1].intValue())
x=true;
}
if(sum2>n)
{
long x1=sum2-c[j-1].intValue();
x1=n-x1;
if(x1>1 || c[j1].intValue()!=c[k-1].intValue())
y=true;
}
if(x||y)
System.out.println("Yes");
else
System.out.println("No");
}
}
}
public int gcd(int a, int b)
{
if(a==0)
return b;
else
return gcd(b%a,a);
}
int[] nai(int n) { int a[]=new int[n]; for(int i=-1;++i<n;)a[i]=ni(); return a;}
Integer[] naI(int n) { Integer a[]=new Integer[n]; for(int i=-1;++i<n;)a[i]=ni(); return a;}
long[] nal(int n) { long a[]=new long[n]; for(int i=-1;++i<n;)a[i]=nl(); return a;}
char[] nac() {char c[]=nextLine().toCharArray(); return c;}
char [][] nmc(int n) {char c[][]=new char[n][]; for(int i=-1;++i<n;)c[i]=nac(); return c;}
int[][] nmi(int r, int c) {int a[][]=new int[r][c]; for(int i=-1;++i<r;)a[i]=nai(c); return a;}
String next()
{
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int[] intArray(Integer[] a) {
int n=a.length;
int b[]=new int[n];
for(int i=-1;++i<n;)
b[i]=a[i].intValue();
return b;
}
int ni() { return Integer.parseInt(next()); }
byte nb() { return Byte.parseByte(next()); }
short ns() { return Short.parseShort(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String nextLine()
{
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
int countDigits(int l) {
if (l >= 1000000000) return 10;
if (l >= 100000000) return 9;
if (l >= 10000000) return 8;
if (l >= 1000000) return 7;
if (l >= 100000) return 6;
if (l >= 10000) return 5;
if (l >= 1000) return 4;
if (l >= 100) return 3;
if (l >= 10) return 2;
return 1;
}
int countDigits(long l) {
if (l >= 1000000000000000000L) return 19;
if (l >= 100000000000000000L) return 18;
if (l >= 10000000000000000L) return 17;
if (l >= 1000000000000000L) return 16;
if (l >= 100000000000000L) return 15;
if (l >= 10000000000000L) return 14;
if (l >= 1000000000000L) return 13;
if (l >= 100000000000L) return 12;
if (l >= 10000000000L) return 11;
if (l >= 1000000000L) return 10;
if (l >= 100000000L) return 9;
if (l >= 10000000L) return 8;
if (l >= 1000000L) return 7;
if (l >= 100000L) return 6;
if (l >= 10000L) return 5;
if (l >= 1000L) return 4;
if (l >= 100L) return 3;
if (l >= 10L) return 2;
return 1;
}
public int[] primeNO(long limit)
{
int size=(int)Math.sqrt(limit)+1;
int size1=size/2;
boolean composite[]=new boolean[size1];
int zz=(int)Math.sqrt(size-1);
for(int i=3;i<=zz;)
{
for(int j=(i*i)/2;j<size1;j+=i)
composite[j]=true;
for(i+=2;composite[i/2];i+=2);
}
int prime[]=new int[3401];
prime[0]=2;
int p=1;
for(int i=1;i<size1;i++)
{
if(!composite[i])
prime[p++]=2*i+1;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////
// System.out.println(p);
////////////////////////////////////////////////////////////////////////////////////////////////////////
prime=Arrays.copyOf(prime, p);
return prime;
}
public static class queue
{
public static class node
{
node next;
int val;
public node(int val)
{
this.val=val;
}
}
node head,tail;
public void add(int val)
{
node a=new node(val);
if(head==null)
{
head=tail=a;
return;
}
tail.next=a;
tail=a;
}
public int remove()
{
int a=head.val;
head=head.next;
if(head==null)
tail=null;
return a;
}
}
public static class stack
{
public static class node
{
node next;
int val;
public node(int val)
{
this.val=val;
}
}
node head;
public void add(int val)
{
node a=new node(val);
a.next=head;
head=a;
}
public int remove()
{
int a=head.val;
head=head.next;
return a;
}
}
public static class tree
{
public static class node
{
// node left, right;
int below;
// ArrayList<Integer> conn=new ArrayList<>(3);
// node parent;
HashMap<Integer, Boolean> conn;
// int parent;
// int val;
public node()
{
// this.val=val;
conn=new HashMap<>();
}
}
public void connections(data[] d, tree.node[] nodes)
{
int n=nodes.length;
for(int i=-1;++i<n-1;)
{
nodes[d[i].a].conn.put(d[i].b, true);
// nodes[d[i].b].parent=d[i].a;
}
}
public void connectionsNotInOrder(data[] d, node[] nodes, int root)
{
int n=nodes.length;
for(int i=-1;++i<n-1;)
{
nodes[d[i].a].conn.put(d[i].b, true);
nodes[d[i].b].conn.put(d[i].a, true);
}
queue q=new queue();
q.add(root);
int curr;
while(q.head!=null)
{
curr=q.remove();
Integer c[]=nodes[curr].conn.keySet().toArray(new Integer[nodes[curr].conn.size()]);
for(int i=-1;++i<c.length;)
{
nodes[c[i].intValue()].conn.remove(curr);
q.add(c[i].intValue());
}
}
}
public int noBelow(int curr, node[] nodes)
{
int a=0;
Integer c[]=nodes[curr].conn.keySet().toArray(new Integer[nodes[curr].conn.size()]);
for(int i=-1;++i<c.length;)
{
a+=noBelow(c[i].intValue(), nodes)+1;
}
nodes[curr].below=a;
return a;
}
}
public static class data
{
int a,b;
public data(int a, int b)
{
this.a=a;
this.b=b;
}
}
public data[] dataArray(int n)
{
data d[]=new data[n];
for(int i=-1;++i<n;)
d[i]=new data(ni(), ni());
return d;
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | ebd67903c8dcaf403af3fa8e5369f022 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* @author KaiXin
* @version 11
* @date 2022-07-12 13:03
*/
public class TaskA {
static final int SIZE = (int)2e5 + 5;
static int[] a = new int[SIZE];
static int[] b = new int[SIZE];
public static boolean check(int[]t,int goal,int k){
for(int i = 1; i <= k; ++i){
if(t[i] < 2) continue;
if(t[i] >= goal) return true;
if(goal - t[i] == 1){
if(t[i] > 2) goal = 2;
}
else goal -= t[i];
}
return goal <= 0;
}
public static void solve(InputReader in,PrintWriter out){
int n = in.nextInt(),m = in.nextInt(),k = in.nextInt();
for(int i = 1; i <= k; ++i) {
int t = in.nextInt();
a[i] = t / n;
b[i] = t / m;
}
Arrays.sort(a, 1, k + 1);
Arrays.sort(b, 1, k + 1);
if(check(a,m,k) || check(b,n,k)) out.println("Yes");
else out.println("No");
}
public static void main(String[] args) throws FileNotFoundException {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int T = 1;
T = in.nextInt();
for(int i = 1; i <= T; ++i){
solve(in,out);
}
out.close();
}
private static class InputReader {
private BufferedReader reader;
private 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 double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
String str = "";
try {
str = reader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
/*
1
5 4 2
9 11
*/ | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 16df7edd9a9ff40d87d302a1eeddb464 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
import java.io.*;
public class Color_The_Picture {
public static void main(String[]args){
Kattio io = new Kattio();
int t = io.nextInt();
for(int I = 0; I < t; I++) {
int n = io.nextInt();
int m = io.nextInt();
int k = io.nextInt();
int mval = 0;
int maxmval = 0;
int nval = 0;
int maxnval = 0;
for(int i = 0; i < k; i++) {
int a = io.nextInt();
if(a/n > 1) {
if(m - mval != 1 || maxmval > 2) {
mval = Math.min(mval+=(a/n), m);
}
maxmval = Math.max(maxmval, a/n);
}
if(a/m > 1) {
if(n - nval != 1 || maxnval > 2) {
nval = Math.min(nval+=(a/m), n);
}
maxnval = Math.max(maxnval, a/m);
}
}
if(nval == n || mval == m) {
io.println("Yes");
}
else {
io.println("No");
}
}
io.close();
}
static class Kattio extends PrintWriter {
private BufferedReader r;
private StringTokenizer st;
// standard input
public Kattio() { this(System.in, System.out); }
public Kattio(InputStream i, OutputStream o) {
super(o);
r = new BufferedReader(new InputStreamReader(i));
}
// USACO-style file input
public Kattio(String problemName) throws IOException {
super(problemName + ".out");
r = new BufferedReader(new FileReader(problemName + ".in"));
}
// returns null if no more input
public String next() {
try {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(r.readLine());
return st.nextToken();
} catch (Exception e) { }
return null;
}
public int nextInt() { return Integer.parseInt(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public long nextLong() { return Long.parseLong(next()); }
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 87fc4cad3659b5ddaf178c15e9b4fc8b | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
public class ColorThePicture {
public static void main(String[] args) throws IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
PrintWriter pr=new PrintWriter(System.out);
int t=Integer.parseInt(br.readLine());
while(t!=0){
solve(br,pr);
t--;
}
pr.flush();
pr.close();
}
public static void solve(BufferedReader br,PrintWriter pr) throws IOException{
String[] temp=br.readLine().split(" ");
int m=Integer.parseInt(temp[0]);
int n=Integer.parseInt(temp[1]);
int k=Integer.parseInt(temp[2]);
long sum=0;
int[] colors=new int[k];
temp=br.readLine().split(" ");
for(int i=0;i<k;i++){
colors[i]=Integer.parseInt(temp[i]);
sum+=colors[i];
}
boolean flag=check(m,n,colors)||check(n,m,colors);
pr.println(flag?"Yes":"No");
}
public static boolean check(int m,int n,int[] colors){
long count=0;
Set<Integer> set=new HashSet<>();
for(int i:colors){
int max=i/m;
if(max>=2){
count+=max;
set.add(max);
}
}
if(count<n){
return false;
}
if(set.size()>=2){
return true;
}
if(set.size()==1){
if(set.contains(2)){
return n%2==0?true:false;
}
else{
return true;
}
}
return true;
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | cb80ad6c8764fc6ea4d70e091be626ec | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | // package faltu;
import java.util.*;
import java.util.Map.Entry;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
public class Main {
// ***********************MATHS--STARTS************************************************* //
private static ArrayList<Long> get_divisor(long x) {
ArrayList<Long>a=new ArrayList<Long>();
for(long i=2;i*i<=x;i++) {
if(x%i==0) {
a.add((long) i);
if(x/i!=i)a.add(x/i);
}
}
return a;
}
private static int CntOfFactor(long x) {
ArrayList<Long>a=new ArrayList<Long>();
for(long i=1;i*i<=x;i++) {
if(x%i==0) {
a.add((long) i);
if(x/i!=i)a.add(x/i);
}
}
return a.size();
}
static long[] sieve;
static long[] smallestPrime;
public static void sieve()
{
int n=4000000+1;
sieve=new long[n];
smallestPrime=new long[n];
sieve[0]=1;
sieve[1]=1;
for(int i=2;i<n;i++){
sieve[i]=i;
smallestPrime[i]=i;
}
for(int i=2;i*i<n;i++){
if(sieve[i]==i){
for(int j=i*i;j<n;j+=i){
if(sieve[j]==j)sieve[j]=1;
if(smallestPrime[j]==j||smallestPrime[j]>i)smallestPrime[j]=i;
}
}
}
}
static long nCr(long n,long r,long MOD) {
computeFact(n, MOD);
if(n<r)return 0;
if(r==0)return 1;
return fact[(int) n]*mod_inv(fact[(int) r],MOD)%MOD*mod_inv(fact[(int) (n-r)],MOD)%MOD;
}
static long[]fact;
static void computeFact(long n,long MOD) {
fact=new long[(int)n+1];
fact[0]=1;
for(int i=1;i<=n;i++)fact[i]=(fact[i-1]*i%MOD)%MOD;
}
static long bin_expo(long a,long b,long MOD) {
if(b == 0)return 1;
long ans = bin_expo(a,b/2,MOD);
ans = (ans*ans)%MOD;
if(b % 2!=0){
ans = (ans*a)%MOD;
}
return ans%MOD;
}
static int ceil(int x, int y) {return (x % y == 0 ? x / y : (x / y + 1));}
static long ceil(long x, long y) {return (x % y == 0 ? x / y : (x / y + 1));}
static long mod_add(long a, long b, long m) {a = a % m; b = b % m; return (((a + b) % m) + m) % m;}
static long mod_mul(long a, long b, long m) {a = a % m; b = b % m; return (((a * b) % m) + m) % m;}
static long mod_sub(long a, long b, long m) {a = a % m; b = b % m; return (((a - b) % m) + m) % m;}
static long mod_inv(long n,long p) {return bin_expo(n,p-2,p);}
static long gcd(long a, long b){if (a == 0) {return b;}return gcd(b % a, a); }
static int gcd(int a, int b){if (a == 0) {return b; }return gcd(b % a, a); }
static long lcm(long a,long b){return (a / gcd(a, b)) * b;}
static long min(long x,long y) {return Math.min(x, y);}static long max(long x,long y) {return Math.max(x, y);}
static int min(int x,int y) {return Math.min(x, y);}static int max(int x,int y) {return Math.max(x, y);}
static ArrayList<String>powof2s;
static void powof2S() {
long i=1;
while(i<(long)2e18) {
powof2s.add(String.valueOf(i));
i*=2;
}
}
static long power(long a, long b){
a %=MOD;long out = 1;
while (b > 0) {
if((b&1)!=0)out = out * a % MOD;
a = a * a % MOD;
b >>= 1;
a*=a;
}
return out;
}
static boolean coprime(int a, long l){return (gcd(a, l) == 1);}
// ****************************MATHS-ENDS*****************************************************
// ***********************BINARY-SEARCH STARTS***********************************************
public static int upperBound(long[] arr, long m, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(arr[mid]<=m) l=mid+1;
else r=mid-1;
}
return l;
}
public static int lowerBound(long[] a, long m, int l, int r) {
while(l<=r) {
int mid=(l+r)/2;
if(a[mid]<m) l=mid+1;
else r=mid-1;
}
return l;
}
public static int lowerBound(ArrayList<Integer> ar,int k){
int s=0,e=ar.size();
while (s!=e){
int mid = s+e>>1;
if (ar.get(mid) <k)s=mid+1;
else e=mid;
}
if(s==ar.size())return -1;
return s;
}
public static int upperBound(ArrayList<Integer> ar,int k){
int s=0,e=ar.size();
while (s!=e){
int mid = s+e>>1;
if (ar.get(mid) <=k)s=mid+1;
else e=mid;
}
if(s==ar.size())return -1;
return s;
}
public static long getClosest(long val1, long val2,long target){if (target - val1 >= val2 - target)return val2; else return val1;}
static void ruffleSort(long[] a) {
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
long oi=r.nextInt(n), temp=a[i];
a[i]=a[(int)oi];
a[(int)oi]=temp;
}
Arrays.sort(a);
}
static void ruffleSort(int[] a){
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n), temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
Arrays.sort(a);
}
int ceilIndex(int input[], int T[], int end, int s){
int start = 0;
int middle;
int len = end;
while(start <= end){
middle = (start + end)/2;
if(middle < len && input[T[middle]] < s && s <= input[T[middle+1]]){
return middle+1;
}else if(input[T[middle]] < s){
start = middle+1;
}else{
end = middle-1;
}
}
return -1;
}
static int lowerLimitBinarySearch(ArrayList<Long> v,long k) {
int n =v.size();
int first = 0,second = n;
while(first <second) {
int mid = first + (second-first)/2;
if(v.get(mid) > k) {
second = mid;
}else {
first = mid+1;
}
}
if(first < n && v.get(first) < k) {
first++;
}
return first; //1 index
}
public static int searchindex(long arr[], long t){int index = Arrays.binarySearch(arr, t);return (index < 0) ? -1 : index;}
public static long[] sort(long[] a) {ArrayList<Long> al = new ArrayList<>();for(int i=0;i<a.length;i++) al.add(a[i]);Collections.sort(al);for(int i=0;i<a.length;i++) a[i]=al.get(i);return a;}
public static int[] sort(int[] a) {ArrayList<Integer> al = new ArrayList<>();for(int i=0;i<a.length;i++) al.add(a[i]);Collections.sort(al);for(int i=0;i<a.length;i++) a[i]=al.get(i);return a;}
// *******************************BINARY-SEARCH ENDS***********************************************
// *********************************GRAPHS-STARTS****************************************************
// *******----SEGMENT TREE IMPLEMENT---*****
// -------------START---------------
void buildTree (int[] arr,int[] tree,int start,int end,int treeNode){
if(start==end){
tree[treeNode]=arr[start];
return;
}
buildTree(arr,tree,start,end,2*treeNode);
buildTree(arr,tree,start,end,2*treeNode+1);
tree[treeNode]=tree[treeNode*2]+tree[2*treeNode+1];
}
void updateTree(int[] arr,int[] tree,int start,int end,int treeNode,int idx,int value){
if(start==end){
arr[idx]=value;
tree[treeNode]=value;
return;
}
int mid=(start+end)/2;
if(idx>mid)updateTree(arr,tree,mid+1,end,2*treeNode+1,idx,value);
else updateTree(arr,tree,start,mid,2*treeNode,idx,value);
tree[treeNode]=tree[2*treeNode]+tree[2*treeNode+1];
}
long query(int[]arr,int[]tree,int start,int end,int treeNode,int qleft,int qright) {
if(start>=qleft&&end<=qright)return tree[treeNode];
if(start>qright||end<qleft)return 0;
int mid=(start+end)/2;
long valLeft=query(arr,tree,start,mid-1,treeNode*2,qleft,qright);
long valRight=query(arr,tree,mid+1,end,treeNode*2+1,qleft,qright);
return valLeft+valRight;
}
// -------------ENDS---------------
//***********************DSU IMPLEMENT START*************************
static int parent[];
static int rank[];
static int[]Size;
static void makeSet(int n){
parent=new int[n];
rank=new int[n];
Size=new int[n];
for(int i=0;i<n;i++){
parent[i]=i;
rank[i]=0;
Size[i]=1;
}
}
static void union(int u,int v){
u=findpar(u);
v=findpar(v);
if(u==v)return;
if(rank[u]<rank[v]) {
parent[u]=v;
Size[v]+=Size[u];
}
else if(rank[v]<rank[u]) {
parent[v]=u;
Size[u]+=Size[v];
}
else{
parent[v]=u;
rank[u]++;
Size[u]+=Size[v];
}
}
private static int findpar(int node){
if(node==parent[node])return node;
return parent[node]=findpar(parent[node]);
}
// *********************DSU IMPLEMENT ENDS*************************
// ****__________PRIMS ALGO______________________****
private static int prim(ArrayList<node>[] adj,int N,int node) {
int key[] = new int[N+1];
int parent[] = new int[N+1];
boolean mstSet[] = new boolean[N+1];
for(int i = 0;i<N;i++) {
key[i] = 100000000;
mstSet[i] = false;
}
PriorityQueue<node> pq = new PriorityQueue<node>(N, new node());
key[node] = 0;
parent[node] = -1;
pq.add(new node( node,key[node]));
for(int i = 0;i<N-1;i++) {
int u = pq.poll().getV();
mstSet[u] = true;
for(node it: adj[u]) {
if(mstSet[it.getV()] == false && it.getW() < key[it.getV()]) {
parent[it.getV()] = u;
key[it.getV()] = (int) it.getW();
pq.add(new node(it.getV(), key[it.getV()]));
}
}
}
int sum=0;
for(int i=1;i<N;i++) {
System.out.println(key[i]);
sum+=key[i];
}
System.out.println(sum);
return sum;
}
// ****____________DIJKSTRAS ALGO___________****
static int[]dist;
static int dijkstra(int u,int n,ArrayList<node>adj[]) {
long[]path=new long[n];
dist=new int[n];
Arrays.fill(dist,Integer.MAX_VALUE);
dist[u]=0;
path[0]=1;
PriorityQueue<node>pq=new PriorityQueue<node>(new node());
pq.add(new node(u,0));
while(!pq.isEmpty()) {
node v=pq.poll();
if(dist[v.getV()]<v.getW())continue;
for(node it:adj[v.getV()]) {
if(dist[it.getV()]>it.getW()+dist[v.getV()]) {
dist[it.getV()]=(int) (it.getW()+dist[v.getV()]);
pq.add(new node(it.getV(),dist[it.getV()]));
path[it.getV()]=path[v.getV()];
}
else if(dist[it.getV()]==it.getW()+dist[v.getV()]) {
path[it.getV()]+=path[v.getV()];
}
}
}
int sum=0;
for(int i=1;i<n;i++){
System.out.println(dist[i]);
sum+=dist[i];
}
return sum;
}
private static void setGraph(int n,int m){
vis=new boolean[n+1];
indeg=new int[n+1];
// adj=new ArrayList<ArrayList<Integer>>();
// for(int i=0;i<=n;i++)adj.add(new ArrayList<>());
// for(int i=0;i<m;i++){
// int u=s.nextInt(),v=s.nextInt();
// adj.get(u).add(v);
// adj.get(v).add(u);
// }
adj=new ArrayList[n+1];
// backadj=new ArrayList[n+1];
for(int i=0;i<=n;i++){
adj[i]=new ArrayList<Integer>();
// backadj[i]=new ArrayList<Integer>();
}
for(int i=0;i<m;i++){
int u=s.nextInt(),v=s.nextInt();
adj[u].add(v);
adj[v].add(u);
// backadj[v].add(u);
indeg[v]++;
indeg[u]++;
}
// weighted adj
// adj=new ArrayList[n+1];
// for(int i=0;i<=n;i++){
// adj[i]=new ArrayList<node>();
// }
// for(int i=0;i<m;i++){
// int u=s.nextInt(),v=s.nextInt();
// long w=s.nextInt();
// adj[u].add(new node(v,w));
//// adj[v].add(new node(u,w));
// }
}
// *********************************GRAPHS-ENDS****************************************************
static int[][] dirs8 = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,1},{-1,-1}};
static int[][] dirs4 = {{1,0},{-1,0},{0,1},{0,-1}}; //d-u-r-l
static long MOD=(long) (1e9+7);
static int prebitsum[][];
static boolean[] vis;
static int[]indeg;
// static ArrayList<ArrayList<Integer>>adj;
static ArrayList<Integer> adj[];
static ArrayList<Integer> backadj[];
static FastReader s = new FastReader(System.in);
static PrintWriter out = new PrintWriter(System.out);
public static void main(String[] args) throws IOException
{
// sieve();
// computeFact((int)1e7+1,MOD);
// prebitsum=new int[2147483648][31];
// presumbit(prebitsum);
// powof2S();
//
// try {
int tt = s.nextInt();
// int tt=1;
for(int i=1;i<=tt;i++) {
solver();
}
out.close();
// catch(Exception e) {return;}
}
private static void solver() {
int n=s.nextInt();
int m=s.nextInt();
int k=s.nextInt();
long[]a=s.rdla(k);
long sumr=0;
long maxr=0;
for(long it:a) {
long x=it/n;
if(x>=2) {
maxr=max(maxr,x);
sumr+=x;
}
}
if(sumr>=m) {
if(m%2==0) {out.println("Yes");return;}
else if(maxr>2){out.println("Yes");return;}
}
long sumc=0;
long maxc=0;
for(long it:a) {
long x=it/m;
if(x>=2) {
maxc=max(maxc,x);
sumc+=x;
}
}
if(sumc>=n) {
if(n%2==0) {out.println("Yes");return;}
else if(maxc>2){out.println("Yes");return;}
}
out.println("No");
}
/* *********************BITS && TOOLS &&DEBUG STARTS***********************************************/
static boolean issafe(int i, int j, int r,int c,boolean[][]vis){
if (i < 0 || j < 0 || i >= r || j >= c||vis[i][j]==true)return false;
else return true;
}
static void presumbit(int[][]prebitsum) {
for(int i=1;i<=200000;i++) {
int z=i;
int j=0;
while(z>0) {
if((z&1)==1) {
prebitsum[i][j]+=(prebitsum[i-1][j]+1);
}else {
prebitsum[i][j]=prebitsum[i-1][j];
}
z=z>>1;
j++;
}
}
}
static void countOfSetBit(long[]a) {
for(int j=30;j>=0;j--) {
int cnt=0;
for(long i:a) {
if((i&1<<j)==1)cnt++;
}
// printing the current no set bit in all array element
System.out.println(cnt);
}
}
public static String revStr(String str){String input = str;StringBuilder input1 = new StringBuilder();input1.append(input);input1.reverse();return input1.toString();}
static void printA(long[] x) {for(int i=0;i<x.length;i++)System.out.print(x[i]+" ");System.out.println();}
static void printA(int[] x) {for(int i=0;i<x.length;i++)System.out.print(x[i]+" ");System.out.println();}
static void pc2d(boolean[][] vis) {
int n=vis.length;
int m=vis[0].length;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
System.out.print(vis[i][j]+" ");
}
System.out.println();
}
}
static void pi2d(char[][] a) {
int n=a.length;
int m=a[0].length;
for(int i=0;i<n;i++) {
for(int j=0;j<m;j++) {
System.out.print(a[i][j]+" ");
}
System.out.println();
}
}
static void p1d(int[]a) {
for(int i=0;i<a.length;i++)System.out.print(a[i]+" ");
System.out.println();
}
// *****************BITS && TOOLS &&DEBUG ENDS***********************************************
}
// **************************I/O*************************
class FastReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastReader(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 double nextDouble() {return Double.parseDouble(next());}
public String nextLine() {String str = "";try {str = reader.readLine();}catch (IOException e) {e.printStackTrace();}return str;}
public int[] rdia(int n) {int[] a = new int[n];for (int i = 0; i < n; i++) a[i] = nextInt();return a;}
public long[] rdla(int n) {long[] a = new long[n];for (int i = 0; i < n; i++) a[i] = nextLong();return a;}
public Integer[] rdIa(int n) {Integer[] a = new Integer[n];for (int i = 0; i < n; i++) a[i] =nextInt();return a;}
public Long[] rdLa(int n) {Long[] a = new Long[n];for (int i = 0; i < n; i++) a[i] = nextLong();return a;}
}
class dsu{
int n;
static int parent[];
static int rank[];
static int[]Size;
public dsu(int n) {this.n=n;this.parent=new int[n];this.rank=new int[n];this.Size=new int[n];
for(int i=0;i<n;i++){parent[i]=i;rank[i]=0;Size[i]=1;}
}
static int findpar(int node) {if(node==parent[node])return node;return parent[node]=findpar(parent[node]);}
static void union(int u,int v){
u=findpar(u);v=findpar(v);
if(u!=v) {
if(rank[u]<rank[v]) {parent[u]=v;Size[v]+=Size[u];}
else if(rank[v]<rank[u]) {parent[v]=u;Size[u]+=Size[v];}
else{parent[v]=u;rank[u]++;Size[u]+=Size[v];}
}
}
}
class pair{
int x;int y;
long u,v;
public pair(int x,int y){this.x=x;this.y=y;}
public pair(long u,long v) {this.u=u;this.v=v;}
}
class Tuple{
String str;int x;int y;
public Tuple(String str,int x,int y) {
this.str=str;
this.x=x;
this.y=y;
}
}
class node implements Comparator<node>{
private int v;
private long w;
node(int _v, long _w) { v = _v; w = _w; }
node() {}
int getV() { return v; }
long getW() { return w; }
@Override
public int compare(node node1, node node2) {
if (node1.w < node2.w) return -1;
if (node1.w > node2.w) return 1;
return 0;
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 486cbdb1ccef048af88d52e927ce04c5 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes |
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static int n,m,k;
public static int [] a;
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0){
n = sc.nextInt();
m = sc.nextInt();
k = sc.nextInt();
a = new int[k];
for (int i = 0; i < k; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
if (solve()) System.out.println("Yes");
else System.out.println("No");
}
}
private static boolean solve() {
long cnt = 0;
for (int i = 0; i < k; i++) {
int x = a[i] / n;
if (x >= 2){
if (m - (cnt + x) == 1) {
if (x > 2) cnt += x - 1;
}
else cnt += x;
}
}
if (cnt >= m) return true;
cnt = 0;
for (int i = 0; i < k; i++) {
int x = a[i] / m;
if (x >= 2){
if (n - (cnt + x) == 1) {
if (x > 2) cnt += x - 1;
}
else cnt += x;
}
}
if (cnt >= n) return true;
return false;
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 7ea5c13ca3b0cc92ae55aa5b6000e9b8 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.math.*;
import java.util.*;
/*
* Author: Atuer
*/
public class Main
{
// ==== Solve Code ====//
static int INF = 2000000010;
public static void csh()
{
}
public static void main(String[] args) throws IOException
{
// csh();
int t = in.nextInt();
while (t-- > 0)
{
solve();
out.flush();
}
out.close();
}
public static void solve()
{
int n = in.nextInt(), m = in.nextInt(), k = in.nextInt();
int[] a = new int[k];
for (int i = 0; i < k; i++)
a[i] = in.nextInt();
boolean flag = false;
long cot = 0;
boolean trd = false;
for (int i = 0; i < k; i++)
{
if (a[i] / n >= 2)
cot += a[i] / n;
if (a[i] / n >= 3)
trd = true;
}
if (cot >= m && m % 2 == 0)
flag = true;
else if (cot >= m && trd)
flag = true;
cot = 0;
trd = false;
for (int i = 0; i < k; i++)
{
if (a[i] / m >= 2)
cot += a[i] / m;
if (a[i] / m >= 3)
trd = true;
}
if (cot >= n && n % 2 == 0)
flag = true;
else if (cot >= n && trd)
flag = true;
out.println(flag ? "Yes" : "No");
}
public static class Node
{
int x, y, k;
public Node(int x, int y, int k)
{
this.x = x;
this.y = y;
this.k = k;
}
}
// ==== Solve Code ====//
// ==== Template ==== //
public static long cnm(int a, int b)
{
long sum = 1;
int i = a, j = 1;
while (j <= b)
{
sum = sum * i / j;
i--;
j++;
}
return sum;
}
public static int gcd(int a, int b)
{
return b == 0 ? a : gcd(b, a % b);
}
public static int lcm(int a, int b)
{
return (a * b) / gcd(a, b);
}
public static void gbSort(int[] a, int l, int r)
{
if (l < r)
{
int m = (l + r) >> 1;
gbSort(a, l, m);
gbSort(a, m + 1, r);
int[] t = new int[r - l + 1];
int idx = 0, i = l, j = m + 1;
while (i <= m && j <= r)
if (a[i] <= a[j])
t[idx++] = a[i++];
else
t[idx++] = a[j++];
while (i <= m)
t[idx++] = a[i++];
while (j <= r)
t[idx++] = a[j++];
for (int z = 0; z < t.length; z++)
a[l + z] = t[z];
}
}
// ==== Template ==== //
// ==== IO ==== //
static InputStream inputStream = System.in;
static InputReader in = new InputReader(inputStream);
static PrintWriter out = new PrintWriter(System.out);
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();
}
boolean hasNext()
{
while (tokenizer == null || !tokenizer.hasMoreTokens())
{
try
{
tokenizer = new StringTokenizer(reader.readLine());
} catch (Exception e)
{
return false;
// TODO: handle exception
}
}
return true;
}
public String nextLine()
{
String str = null;
try
{
str = reader.readLine();
} catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
return Integer.parseInt(next());
}
public long nextLong()
{
return Long.parseLong(next());
}
public Double nextDouble()
{
return Double.parseDouble(next());
}
public BigInteger nextBigInteger()
{
return new BigInteger(next());
}
public BigDecimal nextBigDecimal()
{
return new BigDecimal(next());
}
}
// ==== IO ==== //
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | c1281f549a6fc21785970039ed46d0d5 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
import java.io.*;
import java.math.*;
public class Main {
static long INF = 2000000000000000010l;
static int fx[][] = {{-1,0},{0,1},{1,0},{0,-1},{-1,-1},{1,1},{1,-1},{-1,1}};
public static void main(String[] args) throws IOException {
int t=1;
t = cin.nextInt();
while (t-- > 0)
{
// csh();
solve();
out.flush();
}
out.close();
}
public static void solve() {
int n=cin.nextInt();
int m=cin.nextInt();
int k=cin.nextInt();
int[]v=new int[k];
for(int i=0;i<k;i++){
v[i]=cin.nextInt();
}
long ls=0;
int ls1=0;
for(int i=0;i<k;i++){
if(v[i]/n>=3)
ls1=1;
if(v[i]/n>=2)
ls+=v[i]/n;
}
if(ls>=m){
if(m%2==0){
out.println("Yes");
return;
}else{
if(ls1!=0){
out.println("Yes");
return;
}
}
}
ls=0;
ls1=0;
for(int i=0;i<k;i++){
if(v[i]/m>=3)
ls1=1;
if(v[i]/m>=2)
ls+=v[i]/m;
}
if(ls>=n){
if(n%2==0){
out.println("Yes");
return;
}else{
if(ls1!=0){
out.println("Yes");
return;
}
}
}
out.println("No");
}
static class Node {
int x, y, k;
Node(){}
public Node(int x, int y, int k) {
this.x = x;
this.y = y;
this.k = k;
}
}
static void csh() {
}
static long cnm(int a, int b) {
long sum = 1;
int i = a, j = 1;
while (j <= b) {
sum = sum * i / j;
i--;
j++;
}
return sum;
}
static int gcd(int a, int b) {
return b == 0 ? a : gcd(b, a % b);
}
static int lcm(int a, int b) {
return (a * b) / gcd(a, b);
}
static void gbSort(int[] a, int l, int r) {
if (l < r) {
int m = (l + r) >> 1;
gbSort(a, l, m);
gbSort(a, m + 1, r);
int[] t = new int[r - l + 1];
int idx = 0, i = l, j = m + 1;
while (i <= m && j <= r){
//cnt++;
if (a[i] <= a[j])
t[idx++] = a[i++];
else{
t[idx++] = a[j++];
// cnt += m -i +1;//nx
}
}
while (i <= m)
t[idx++] = a[i++];
while (j <= r)
t[idx++] = a[j++];
for (int z = 0; z < t.length; z++)
a[l + z] = t[z];
}
}
static FastScanner cin = new FastScanner(System.in);
static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in), 16384);
eat("");
}
public void eat(String s) {
st = new StringTokenizer(s);
}
public String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
public boolean hasNext() {
while (!st.hasMoreTokens()) {
String s = nextLine();
if (s == null)
return false;
eat(s);
}
return true;
}
public String next() {
hasNext();
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public Double nextDouble() {
return Double.parseDouble(next());
}
public BigInteger nextBigInteger() {
return new BigInteger(next());
}
public BigDecimal nextBigDecimal() {
return new BigDecimal(next());
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 280b2d552437555e6149680a915ba323 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.Scanner;
public class A1710 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int T = in.nextInt();
for (int t=0; t<T; t++) {
int N = in.nextInt();
int M = in.nextInt();
int K = in.nextInt();
long columnCount = 0;
long rowCount = 0;
int columnMax = 0;
int rowMax = 0;
for (int k=0; k<K; k++) {
int a = in.nextInt();
int columns = a/N;
int rows = a/M;
if (columns >= 2) {
columnCount += columns;
columnMax = Math.max(columnMax, columns);
}
if (rows >= 2) {
rowCount += rows;
rowMax = Math.max(rowMax, rows);
}
}
boolean possible = (columnCount >= M && (M%2 == 0 || columnMax > 2))
|| (rowCount >= N && (N%2 == 0 || rowMax > 2));
System.out.println(possible ? "Yes" : "No");
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 7be77ef1dcf053ece97cf0bef542ce39 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.BufferedReader;
import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.time.Clock;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.Random;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import java.util.function.Function;
import java.util.function.Supplier;
public class Solution {
private void solve() throws IOException {
int n = nextInt();
int m = nextInt();
int k = nextInt();
int[] a = nextIntArray(k);
out.println((check(n, m, a) || check(m, n, a)) ? "Yes" : "No");
}
private boolean check(int n, int m, int[] a) {
long c2 = 0;
long c1 = 0;
int n2 = n * 2;
int mx = 0;
for (int i : a) {
mx = Math.max(mx, i / n2);
c2 += i / n2;
if (i > n2 && i % n2 >= n) c1++;
}
if (m % 2 == 0 || c1 > 0) {
return m <= c2 * 2 + c1;
} else {
return mx >= 2 && m <= c2 * 2 - 1;
}
}
private static final boolean runNTestsInProd = true;
private static final boolean printCaseNumber = false;
private static final boolean assertInProd = false;
private static final boolean logToFile = false;
private static final boolean readFromConsoleInDebug = false;
private static final boolean writeToConsoleInDebug = true;
private static final boolean testTimer = false;
private static Boolean isDebug = null;
private BufferedReader in;
private StringTokenizer line;
private PrintWriter out;
public static void main(String[] args) throws Exception {
isDebug = Arrays.asList(args).contains("DEBUG_MODE");
if (isDebug) {
log = logToFile ? new PrintWriter("logs/j_solution_" + System.currentTimeMillis() + ".log") : new PrintWriter(System.out);
clock = Clock.systemDefaultZone();
}
new Solution().run();
}
private void run() throws Exception {
in = new BufferedReader(new InputStreamReader(!isDebug || readFromConsoleInDebug ? System.in : new FileInputStream("input.txt")));
out = !isDebug || writeToConsoleInDebug ? new PrintWriter(System.out) : new PrintWriter("output.txt");
try (Timer totalTimer = new Timer("total")) {
int t = runNTestsInProd || isDebug ? nextInt() : 1;
for (int i = 0; i < t; i++) {
if (printCaseNumber) {
out.print("Case #" + (i + 1) + ": ");
}
if (testTimer) {
try (Timer testTimer = new Timer("test #" + (i + 1))) {
solve();
}
} else {
solve();
}
if (isDebug) {
out.flush();
}
}
}
in.close();
out.flush();
out.close();
}
private void println(Object... objects) {
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst) {
out.print(" ");
} else {
isFirst = false;
}
out.print(o.toString());
}
out.println();
}
private int[] nextIntArray(int n) throws IOException {
return nextIntArray(n, 0);
}
private int[] nextIntArray(int n, int delta) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt() + delta;
}
return res;
}
private long[] nextLongArray(int n) throws IOException {
long[] res = new long[n];
for (int i = 0; i < n; i++) {
res[i] = nextLong();
}
return res;
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private char[] nextTokenChars() throws IOException {
return nextToken().toCharArray();
}
private String nextToken() throws IOException {
while (line == null || !line.hasMoreTokens()) {
line = new StringTokenizer(in.readLine());
}
return line.nextToken();
}
private static void assertPredicate(boolean p) {
if ((isDebug || assertInProd) && !p) {
throw new RuntimeException();
}
}
private static void assertPredicate(boolean p, String message) {
if ((isDebug || assertInProd) && !p) {
throw new RuntimeException(message);
}
}
private static <T> void assertNotEqual(T unexpected, T actual) {
if ((isDebug || assertInProd) && Objects.equals(actual, unexpected)) {
throw new RuntimeException("assertNotEqual: " + unexpected + " == " + actual);
}
}
private static <T> void assertEqual(T expected, T actual) {
if ((isDebug || assertInProd) && !Objects.equals(actual, expected)) {
throw new RuntimeException("assertEqual: " + expected + " != " + actual);
}
}
private static PrintWriter log = null;
private static Clock clock = null;
private static void log(Object... objects) {
log(true, objects);
}
private static void logNoDelimiter(Object... objects) {
log(false, objects);
}
private static void log(boolean printDelimiter, Object[] objects) {
if (isDebug) {
StringBuilder sb = new StringBuilder();
sb.append(LocalDateTime.now(clock)).append(" - ");
boolean isFirst = true;
for (Object o : objects) {
if (!isFirst && printDelimiter) {
sb.append(" ");
} else {
isFirst = false;
}
sb.append(o.toString());
}
log.println(sb);
log.flush();
}
}
private static class Timer implements Closeable {
private final String label;
private final long startTime = isDebug ? System.nanoTime() : 0;
public Timer(String label) {
this.label = label;
}
@Override
public void close() throws IOException {
if (isDebug) {
long executionTime = System.nanoTime() - startTime;
String fraction = Long.toString(executionTime / 1000 % 1_000_000);
logNoDelimiter("Timer[", label, "]: ", executionTime / 1_000_000_000, '.', "00000".substring(0, 6 - fraction.length()), fraction, 's');
}
}
}
private static <T> T timer(String label, Supplier<T> f) throws Exception {
if (isDebug) {
try (Timer timer = new Timer(label)) {
return f.get();
}
} else {
return f.get();
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | fadc34ac63da35d0273ee91b187d70e7 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
import java.io.*;
public class ColorThePicture {
public static void main(String[] args) throws IOException {
Reader in = new Reader();
PrintWriter out = new PrintWriter(System.out);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int N = in.nextInt(), M = in.nextInt(), K = in.nextInt();
List<Integer> arr = new ArrayList<>();
for (int i = 0; i < K; i++) {
arr.add(in.nextInt());
}
long current = 0;
for (int i : arr) {
if (i / N >= 2 && current + 2 <= M) {
current += 2;
}
}
for (int i : arr) {
if (i / N > 2) {
current += i / N - 2;
}
}
if (current >= M) {
out.println("YES");
continue;
}
current = 0;
for (int i : arr) {
if (i / M >= 2 && current + 2 <= N) {
current += 2;
}
}
for (int i : arr) {
if (i / M > 2) {
current += i / M - 2;
}
}
if (current >= N) {
out.println("YES");
} else {
out.println("NO");
}
}
out.close();
}
static class Reader {
BufferedReader in;
StringTokenizer st;
public Reader() {
in = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
public String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
}
public static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i : arr) {
list.add(i);
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | e485b666ba2c161dc147247dd2e2fc78 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.BitSet;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.SortedSet;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* #
*
* @author pttrung
*/
public class A_Round_810_Div1 {
public static long MOD = 1000000007;
public static void main(String[] args) throws FileNotFoundException {
// PrintWriter out = new PrintWriter(new FileOutputStream(new File(
// "output.txt")));
PrintWriter out = new PrintWriter(System.out);
Scanner in = new Scanner();
int T = in.nextInt();
for (int Z = 0; Z < T; Z++) {
int n = in.nextInt();
int m = in.nextInt();
int k = in.nextInt();
int[] data = new int[k];
for (int i = 0; i < k; i++) {
data[i] = in.nextInt();
}
boolean ok = false;
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < k; i++) {
int v = data[i] / n;
if (v >= 2) {
list.add(v);
}
}
Collections.sort(list);
LinkedList<Integer> q = new LinkedList<>();
//System.out.println(list);
int left = m;
for (int i = list.size() - 1; i >= 0 && left > 0; i--) {
int v = list.get(i);
if (left == 1) {
if (q.isEmpty() || q.peekFirst() == 2) {
break;
}
q.pollFirst();
left++;
}
v = Integer.min(left, v);
left -= v;
q.add(v);
}
ok = left == 0;
if (ok) {
out.println("Yes");
continue;
}
list.clear();
for (int i = 0; i < k; i++) {
int v = data[i] / m;
if (v >= 2) {
list.add(v);
}
}
Collections.sort(list);
q.clear();
//System.out.println(list);
left = n;
for (int i = list.size() - 1; i >= 0 && left > 0; i--) {
int v = list.get(i);
if (left == 1) {
if (q.isEmpty() || q.peekFirst() == 2) {
break;
}
q.pollFirst();
left++;
}
v = Integer.min(left, v);
left -= v;
q.add(v);
}
ok = left == 0;
out.println(ok ? "Yes" : "No");
}
out.close();
}
static long cal(long n) {
return n * (n + 1) / 2 + n * (n - 1) / 2;
}
static int find(int index, int[] u) {
if (u[index] != index) {
return u[index] = find(u[index], u);
}
return index;
}
static int abs(int a) {
return a < 0 ? -a : a;
}
public static int[] KMP(String val) {
int i = 0;
int j = -1;
int[] result = new int[val.length() + 1];
result[0] = -1;
while (i < val.length()) {
while (j >= 0 && val.charAt(j) != val.charAt(i)) {
j = result[j];
}
j++;
i++;
result[i] = j;
}
return result;
}
public static boolean nextPer(int[] data) {
int i = data.length - 1;
while (i > 0 && data[i] < data[i - 1]) {
i--;
}
if (i == 0) {
return false;
}
int j = data.length - 1;
while (data[j] < data[i - 1]) {
j--;
}
int temp = data[i - 1];
data[i - 1] = data[j];
data[j] = temp;
Arrays.sort(data, i, data.length);
return true;
}
public static int digit(long n) {
int result = 0;
while (n > 0) {
n /= 10;
result++;
}
return result;
}
public static double dist(long a, long b, long x, long y) {
double val = (b - a) * (b - a) + (x - y) * (x - y);
val = Math.sqrt(val);
double other = x * x + a * a;
other = Math.sqrt(other);
return val + other;
}
public static class Point {
int x;
int y;
public Point(int start, int end) {
this.x = start;
this.y = end;
}
public String toString() {
return x + " " + y;
}
}
public static class FT {
long[] data;
FT(int n) {
data = new long[n];
}
public void update(int index, long value) {
while (index < data.length) {
data[index] += value;
index += (index & (-index));
}
}
public long get(int index) {
long result = 0;
while (index > 0) {
result += data[index];
index -= (index & (-index));
}
return result;
}
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
public static long pow(long a, int b) {
if (b == 0) {
return 1;
}
if (b == 1) {
return a;
}
long val = pow(a, b / 2);
if (b % 2 == 0) {
return val * val;
} else {
return val * (val * a);
}
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() throws FileNotFoundException {
// System.setOut(new PrintStream(new BufferedOutputStream(System.out), true));
br = new BufferedReader(new InputStreamReader(System.in));
//br = new BufferedReader(new InputStreamReader(new FileInputStream(new File("input.txt"))));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
throw new RuntimeException();
}
}
return st.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
public int nextInt() {
return Integer.parseInt(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
st = null;
try {
return br.readLine();
} catch (Exception e) {
throw new RuntimeException();
}
}
public boolean endLine() {
try {
String next = br.readLine();
while (next != null && next.trim().isEmpty()) {
next = br.readLine();
}
if (next == null) {
return true;
}
st = new StringTokenizer(next);
return st.hasMoreTokens();
} catch (Exception e) {
throw new RuntimeException();
}
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 29b38cef68f9d9421beacc92ab4e1c89 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes |
import javax.swing.*;
import java.math.BigInteger;
import java.util.*;
import java.io.*;
public class Vaibhav {
/*-----------==============-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/
static long bit[];
static boolean prime[];
public static long lcm(long x, long y) {
return (x * y) / gcd(x, y);
}
static class Pair {//implements Comparable<Pair> {
int peldo;
int bachalo;
Pair(int peldo,int bachalo) {
this.peldo = peldo;
this.bachalo=bachalo;
}
/* //public int compareTo(Pair o){
return this.cqm-o.cqm;
}*/
}
public static void update(long bit[], int i, int x) {
for (; i < bit.length; i += (i & (-i))) {
bit[i] += x;
}
}
public static long sum(int i) {
long sum = 0;
for (; i > 0; i -= (i & (-i))) {
sum += bit[i];
}
return sum;
}
public static int CeilIndex(int A[], int l, int r, int key) {
while (r - l > 1) {
int m = l + (r - l) / 2;
if (A[m] >= key) r = m;
else l = m;
}
return r;
}
public static int LongestIncreasingSubsequenceLength(int A[], int size) {
int[] tailTable = new int[size];
int len;
tailTable[0] = A[0];
len = 1;
for (int i = 1; i < size; i++) {
if (A[i] < tailTable[0]) tailTable[0] = A[i];
else if (A[i] > tailTable[len - 1]) tailTable[len++] = A[i];
else tailTable[CeilIndex(tailTable, -1, len - 1, A[i])] = A[i];
}
return len;
}
static long power(long x, long y, long p) {
if (y == 0) return 1;
if (x == 0) return 0;
long res = 1l;
x = x % p;
while (y > 0) {
if (y % 2 == 1) res = (res * x) % p;
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static long power(long x, long y) {
if (y == 0) return 1;
if (x == 0) return 0;
long res = 1;
while (y > 0) {
if (y % 2 == 1) res = (res * x);
y = y >> 1;
x = (x * x);
}
return res;
}
static void sort(long[] a) {
ArrayList<Long> l = new ArrayList<>();
for (long i : a) l.add(i);
Collections.sort(l);
for (int i = 0; i < a.length; i++) a[i] = l.get(i);
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] readArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++) a[i] = nextInt();
return a;
}
long[] readlongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++) a[i] = nextLong();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
}
static void sieveOfEratosthenes(int n) {
prime = new boolean[n + 1];
for (int i = 0; i <= n; i++) prime[i] = true;
for (int p = 2; p * p <= n; p++) {
if (prime[p] == true) {
for (int i = p * p; i <= n; i += p) prime[i] = false;
}
}
}
public static int log2(int x) {
return (int) (Math.log(x) / Math.log(2));
}
public static long gcd(long a, long b) {
if (b == 0) {
return a;
}
return gcd(b, a % b);
}
static long nCk(int n, int k) {
long res = 1;
for (int i = n - k + 1; i <= n; ++i) res *= i;
for (int i = 2; i <= k; ++i) res /= i;
return res;
}
static BigInteger bi(String str) {
return new BigInteger(str);
}
/*------------=========------------------------------------===================================================------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------===============================================------=================-=-------=============--------*/
static long max;
static long res;
static long total;
static FastScanner fs = null;
static long ans;
//static long col;
static Pair val[];
static long val1[];
// static long mod = 1_000_000_007;
static long count=0;
static long dp[] ;
static ArrayList<Integer> al[];
public static void main(String[] args) {
fs = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = fs.nextInt();
outer: while (t-- > 0) {
int n = fs.nextInt();
int m = fs.nextInt();
int k = fs.nextInt();
int a[] = fs.readArray(k);
long tot=0;
boolean flag = false;
for(int i=0;i<k;i++){
if(a[i]/n > 2){
flag=true;
}
if(a[i]/n >= 2){
tot += a[i]/n;
}
}
if(tot >= m && (flag || m%2==0)){
out.println("Yes");
continue outer;
}
flag = false;
tot = 0;
for(int i=0;i<k;i++){
if(a[i]/m > 2){
flag=true;
}
if(a[i]/m >= 2){
tot += a[i]/m;
}
}
if(tot >= n && (flag || n%2==0)){
out.println("Yes");
continue outer;
}
out.println("No");
}
out.close();
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 0d25fdd057656a068670e74f4d783e24 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.io.Writer;
import java.io.IOException;
import java.util.InputMismatchException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Nipuna Samarasekara
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
FastPrinter out = new FastPrinter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, FastScanner in, FastPrinter out) {
int t = in.nextInt();
while (t-- > 0) {
int n = in.nextInt(), m = in.nextInt(), k = in.nextInt();
int[] a = in.readIntArray(k);
boolean pos = false;
long tt = 0;
int xx = m;
int mx = 0;
for (int i = 0; i < k; i++) {
if (a[i] >= 2 * xx) {
tt += (a[i] / xx);
mx = Math.max(mx, a[i] / xx);
}
}
if (tt >= n) {
if (n % 2 == 0 || mx > 2)
pos = true;
}
tt = 0;
xx = n;
mx = 0;
for (int i = 0; i < k; i++) {
if (a[i] >= 2 * xx) {
tt += (a[i] / xx);
mx = Math.max(mx, a[i] / xx);
}
}
if (tt >= m) {
if (m % 2 == 0 || mx > 2)
pos = true;
}
if (pos)
out.println("Yes");
else
out.println("No");
}
}
}
static class FastScanner extends BufferedReader {
public FastScanner(InputStream is) {
super(new InputStreamReader(is));
}
public int read() {
try {
int ret = super.read();
// if (isEOF && ret < 0) {
// throw new InputMismatchException();
// }
// isEOF = ret == -1;
return ret;
} catch (IOException e) {
throw new InputMismatchException();
}
}
static boolean isWhiteSpace(int c) {
return c >= 0 && c <= 32;
}
public int nextInt() {
int c = read();
while (isWhiteSpace(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int ret = 0;
while (c >= 0 && !isWhiteSpace(c)) {
if (c < '0' || c > '9') {
throw new NumberFormatException("digit expected " + (char) c
+ " found");
}
ret = ret * 10 + c - '0';
c = read();
}
return ret * sgn;
}
public String readLine() {
try {
return super.readLine();
} catch (IOException e) {
return null;
}
}
public int[] readIntArray(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = nextInt();
}
return ret;
}
}
static class FastPrinter extends PrintWriter {
public FastPrinter(OutputStream out) {
super(out);
}
public FastPrinter(Writer out) {
super(out);
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 4b9a9f337f2f85748e46c89d58956895 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.*;
import java.util.*;
// import static java.lang.Math.*;
public class Round810_Div1_A implements Runnable {
final boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args) {
new Thread(new Round810_Div1_A()).start();
}
void init() throws IOException {
if (ONLINE_JUDGE) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
@Override
public void run() {
try {
long t1 = System.currentTimeMillis();
init();
solve();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}
}
void solve() throws IOException {
int test_cases = readInt();
for (int i = 0; i < test_cases; i++) {
// out.println("Test Case #:" + i);
int n = readInt();
int m = readInt();
int k = readInt();
Integer[] pigmentCells = new Integer[k];
for (int j = 0; j < k; j++) {
pigmentCells[j] = readInt();
}
solution(n, m, k, pigmentCells);
}
}
void solution(int n, int m, int k, Integer[] pigmentCells) {
Arrays.sort(pigmentCells, (a, b) -> b - a);
// out.println(Arrays.toString(pigmentCells));
if (check(n, m, pigmentCells) || check(m, n, pigmentCells))
out.println("Yes");
else
out.println("No");
}
boolean check(int n, int m, Integer[] pigmentCells) {
boolean hasSpareColumn = false;
for (int pc : pigmentCells) {
if ((!hasSpareColumn && m == 1) || m <= 0 || pc < n * 2)
break;
int i = pc / n;
if (!hasSpareColumn && i > 2) {
hasSpareColumn = true;
}
m -= i;
}
return m <= 0;
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | bdd2c7ef303452302e03f417147bbc64 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.Scanner;
public class C {
public static void main(String[] args) {
int casenum = 1;
Scanner in = new Scanner(System.in);
casenum = in.nextInt();
for (int i = 1; i <= casenum; i++) {
work(in);
}
}
static final int MAXN = 100010;
public static int n, m, k;
public static int[] a = new int[MAXN];
public static void work(Scanner in) {
n = in.nextInt();
m = in.nextInt();
k = in.nextInt();
for (int i = 1; i <= k; i++) {
a[i] = in.nextInt();
}
boolean flag = false;
long tot = 0;
for (int i = 1; i <= k; i++) {
if (a[i] / n > 2) {
flag = true;
}
if (a[i] / n >= 2) {
tot += a[i] / n;
}
}
if (tot >= m && (flag || m % 2 == 0)) {
System.out.println("Yes");
return;
}
flag = false;
tot = 0;
for (int i = 1; i <= k; i++) {
if (a[i] / m > 2) {
flag = true;
}
if (a[i] / m >= 2) {
tot += a[i] / m;
}
}
if (tot >= n && (flag || n % 2 == 0)) {
System.out.println("Yes");
return;
}
System.out.println("No");
}
} | Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 67bffc029954cc8ce686e2b560e2e493 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.util.*;
import java.io.*;
public class codeforces1710A {
public static void main(String[] args) throws IOException {
FastReader in = new FastReader();
int numCases = in.nextInt();
while (numCases-->0)
{
long n = in.nextLong();
long m = in.nextLong();
int k = in.nextInt();
int [] arr = new int[k];
for (int i = 0; i<k;i++)
{
arr[i] = in.nextInt();
}
//System.out.println(arr[0]);
boolean ok = false;
boolean three = false;
long count = 0;
for (int i = 0; i<k;i++)
{
long a = arr[i] /m;
if (a>2) three = true;
if (a>=2)count+=a;
}
if (count>=n && (three || n%2==0)) ok = true;
three = false;
count = 0;
for (int i = 0; i<k;i++)
{
long a = arr[i] / n;
if (a>2) three = true;
if (a>=2)count+=a;
}
if (count>=m && (three || m%2==0)) ok = true;
if (ok) System.out.println("Yes");
else System.out.println("No");
}
}
static class FastReader {
StringTokenizer st;
BufferedReader br;
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 s = "";
while (st == null || st.hasMoreElements()) {
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
}
return s;
}
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | c35803ef3ab502993d564da1eea828e1 | train_108.jsonl | 1658673300 | A picture can be represented as an $$$n\times m$$$ grid ($$$n$$$ rows and $$$m$$$ columns) so that each of the $$$n \cdot m$$$ cells is colored with one color. You have $$$k$$$ pigments of different colors. You have a limited amount of each pigment, more precisely you can color at most $$$a_i$$$ cells with the $$$i$$$-th pigment.A picture is considered beautiful if each cell has at least $$$3$$$ toroidal neighbors with the same color as itself.Two cells are considered toroidal neighbors if they toroidally share an edge. In other words, for some integers $$$1 \leq x_1,x_2 \leq n$$$ and $$$1 \leq y_1,y_2 \leq m$$$, the cell in the $$$x_1$$$-th row and $$$y_1$$$-th column is a toroidal neighbor of the cell in the $$$x_2$$$-th row and $$$y_2$$$-th column if one of following two conditions holds: $$$x_1-x_2 \equiv \pm1 \pmod{n}$$$ and $$$y_1=y_2$$$, or $$$y_1-y_2 \equiv \pm1 \pmod{m}$$$ and $$$x_1=x_2$$$. Notice that each cell has exactly $$$4$$$ toroidal neighbors. For example, if $$$n=3$$$ and $$$m=4$$$, the toroidal neighbors of the cell $$$(1, 2)$$$ (the cell on the first row and second column) are: $$$(3, 2)$$$, $$$(2, 2)$$$, $$$(1, 3)$$$, $$$(1, 1)$$$. They are shown in gray on the image below: The gray cells show toroidal neighbors of $$$(1, 2)$$$. Is it possible to color all cells with the pigments provided and create a beautiful picture? | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Locale;
import java.util.StringTokenizer;
public class Solution implements Runnable {
private PrintStream out;
private BufferedReader in;
private StringTokenizer st;
public void solve() throws IOException {
long time0 = System.currentTimeMillis();
int t = nextInt();
for (int test = 1; test <= t; test++) {
int n = nextInt();
int m = nextInt();
int k = nextInt();
int[] a = new int[k];
for (int i = 0; i < k; i++) {
a[i] = nextInt();
}
boolean answer = solve(n, m, k, a);
if (answer) {
out.println("Yes");
} else {
out.println("No");
}
}
System.err.println("time: " + (System.currentTimeMillis() - time0));
}
private boolean solve(int n, int m, int k, int[] a) {
if (solveHorizontal(n, m, k, a)) {
return true;
}
if (solveHorizontal(m, n, k, a)) {
return true;
}
return false;
}
private boolean solveHorizontal(int n, int m, int k, int[] a) {
long sum = 0;
boolean freedom = false;
for (int i = 0; i < k; i++) {
int add = a[i] / m;
if (add >= 2) {
if (add > 2) {
freedom = true;
}
sum += add;
}
}
if ((n > 1) && (!freedom) && ((n % 2) != 0)) {
return false;
}
return sum >= n;
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
@Override
public void run() {
try {
solve();
out.close();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public Solution(String name) throws IOException {
Locale.setDefault(Locale.US);
if (name == null) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintStream(new BufferedOutputStream(System.out));
} else {
in = new BufferedReader(new InputStreamReader(new FileInputStream(name + ".in")));
out = new PrintStream(new BufferedOutputStream(new FileOutputStream(name + ".out")));
}
st = new StringTokenizer("");
}
public static void main(String[] args) throws IOException {
new Thread(new Solution(null)).start();
}
}
| Java | ["6\n\n4 6 3\n\n12 9 8\n\n3 3 2\n\n8 8\n\n3 3 2\n\n9 5\n\n4 5 2\n\n10 11\n\n5 4 2\n\n9 11\n\n10 10 3\n\n11 45 14"] | 1 second | ["Yes\nNo\nYes\nYes\nNo\nNo"] | NoteIn the first test case, one possible solution is as follows: In the third test case, we can color all cells with pigment $$$1$$$. | Java 8 | standard input | [
"constructive algorithms",
"greedy",
"math"
] | 002129eec704af8976a2bf02cc532d59 | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains three integers $$$n$$$, $$$m$$$, and $$$k$$$ ($$$3 \leq n,m \leq 10^9$$$, $$$1 \leq k \leq 10^5$$$) — the number of rows and columns of the picture and the number of pigments. The next line contains $$$k$$$ integers $$$a_1,a_2,\dots, a_k$$$ ($$$1 \leq a_i \leq 10^9$$$) — $$$a_i$$$ is the maximum number of cells that can be colored with the $$$i$$$-th pigment. It is guaranteed that the sum of $$$k$$$ over all test cases does not exceed $$$10^5$$$. | 1,500 | For each test case, print "Yes" (without quotes) if it is possible to color a beautiful picture. Otherwise, print "No" (without quotes). | standard output | |
PASSED | 6b94187a8f41bba4b5de071cc7410f76 | train_108.jsonl | 1658673300 | You are the owner of a harvesting field which can be modeled as an infinite line, whose positions are identified by integers.It will rain for the next $$$n$$$ days. On the $$$i$$$-th day, the rain will be centered at position $$$x_i$$$ and it will have intensity $$$p_i$$$. Due to these rains, some rainfall will accumulate; let $$$a_j$$$ be the amount of rainfall accumulated at integer position $$$j$$$. Initially $$$a_j$$$ is $$$0$$$, and it will increase by $$$\max(0,p_i-|x_i-j|)$$$ after the $$$i$$$-th day's rain.A flood will hit your field if, at any moment, there is a position $$$j$$$ with accumulated rainfall $$$a_j>m$$$.You can use a magical spell to erase exactly one day's rain, i.e., setting $$$p_i=0$$$. For each $$$i$$$ from $$$1$$$ to $$$n$$$, check whether in case of erasing the $$$i$$$-th day's rain there is no flood. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
static Solution2 admin = new Solution2();
public static void main(String[] args) {
admin.start();
}
}
class Solution2 {
//---------------------------------INPUT READER-----------------------------------------//
public BufferedReader br;
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try { st = new StringTokenizer(br.readLine());} catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String ns() { return next(); }
int[] na(long n) {int[]ret=new int[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;}
long[] nal(long n) {long[]ret=new long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;}
Integer[] nA(long n) {Integer[]ret=new Integer[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;}
Long[] nAl(long n) {Long[]ret=new Long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;}
//--------------------------------------PRINTER------------------------------------------//
PrintWriter w;
void p(int i) {w.println(i);} void p(long l) {w.println(l);}
void p(double d) {w.println(d);} void p(String s) { w.println(s);}
void pr(int i) {w.print(i);} void pr(long l) {w.print(l);}
void pr(double d) {w.print(d);} void pr(String s) { w.print(s);}
void pl() {w.println();}
//--------------------------------------VARIABLES-----------------------------------------//
long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
long mod = 1000000007;
{
w = new PrintWriter(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
try {if(new File(System.getProperty("user.dir")).getName().equals("LOCAL")) {
w = new PrintWriter(new OutputStreamWriter(new FileOutputStream("output.txt")));
br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));}
} catch (Exception ignore) { }
}
//----------------------START---------------------//
void start() {
int t = ni(); while(t-- > 0)
solve();
w.close();
}
pr getIntersection (pr a, pr b) {
long tx = Math.max(a.x + a.p, b.x + b.p);
long ty = Math.max(a.p - a.x, b.p - b.x);
return new pr((tx - ty) / 2, (tx + ty) / 2);
}
void solve() {
int n = ni(), m = ni() * 2;
List<pr> li = new ArrayList<>();
List<pr> rains = new ArrayList<>();
pr intersection = null;
for(int i = 0; i < n; i++) {
long x = nl() * 2, p = nl() * 2;
li.add(new pr(x-p, 1));
li.add(new pr(x, -2));
li.add(new pr(x+p, 1));
rains.add(new pr(x, p));
}
li.sort(Comparator.comparingLong(a -> a.x));
long prev_height = 0, prev_multiple = 0, prev_x = 0;
for(int i = 0; i < li.size(); i++) {
pr curr = li.get(i);
prev_height = prev_height + (curr.x - prev_x) * prev_multiple;
if(prev_height > m) {
if(intersection == null) intersection = new pr(curr.x, prev_height-m);
else intersection = getIntersection(intersection, new pr(curr.x, prev_height-m));
}
prev_multiple += curr.p;
prev_x = curr.x;
}
if(intersection == null) {
for(int i = 0; i < n; i++) {
pr(1);
}
pl();
return;
}
for(int i = 0; i < n; i++) {
pr rain = rains.get(i);
pr curr_intersection = getIntersection(intersection, rain);
if(curr_intersection.x == rain.x && curr_intersection.p == rain.p) pr(1);
else pr(0);
}
pl();
}
class pr {
long x, p;
public pr(long x, long p) {
this.x = x;
this.p = p;
}
}
} | Java | ["4\n\n3 6\n\n1 5\n\n5 5\n\n3 4\n\n2 3\n\n1 3\n\n5 2\n\n2 5\n\n1 6\n\n10 6\n\n6 12\n\n4 5\n\n1 6\n\n12 5\n\n5 5\n\n9 7\n\n8 3"] | 4 seconds | ["001\n11\n00\n100110"] | NoteIn the first test case, if we do not use the spell, the accumulated rainfall distribution will be like this: If we erase the third day's rain, the flood is avoided and the accumulated rainfall distribution looks like this: In the second test case, since initially the flood will not happen, we can erase any day's rain.In the third test case, there is no way to avoid the flood. | Java 8 | standard input | [
"binary search",
"brute force",
"data structures",
"geometry",
"greedy",
"implementation",
"math"
] | 268cf03c271d691c3c1e3922e884753e | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq m \leq 10^9$$$) — the number of rainy days and the maximal accumulated rainfall with no flood occurring. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains two integers $$$x_i$$$ and $$$p_i$$$ ($$$1 \leq x_i,p_i \leq 10^9$$$) — the position and intensity of the $$$i$$$-th day's rain. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 2,100 | For each test case, output a binary string $$$s$$$ length of $$$n$$$. The $$$i$$$-th character of $$$s$$$ is 1 if after erasing the $$$i$$$-th day's rain there is no flood, while it is 0, if after erasing the $$$i$$$-th day's rain the flood still happens. | standard output | |
PASSED | 87f25e6d8330fec08250944176e3a893 | train_108.jsonl | 1658673300 | You are the owner of a harvesting field which can be modeled as an infinite line, whose positions are identified by integers.It will rain for the next $$$n$$$ days. On the $$$i$$$-th day, the rain will be centered at position $$$x_i$$$ and it will have intensity $$$p_i$$$. Due to these rains, some rainfall will accumulate; let $$$a_j$$$ be the amount of rainfall accumulated at integer position $$$j$$$. Initially $$$a_j$$$ is $$$0$$$, and it will increase by $$$\max(0,p_i-|x_i-j|)$$$ after the $$$i$$$-th day's rain.A flood will hit your field if, at any moment, there is a position $$$j$$$ with accumulated rainfall $$$a_j>m$$$.You can use a magical spell to erase exactly one day's rain, i.e., setting $$$p_i=0$$$. For each $$$i$$$ from $$$1$$$ to $$$n$$$, check whether in case of erasing the $$$i$$$-th day's rain there is no flood. | 256 megabytes | import java.io.*;
import java.util.*;
public class Solution {
static Solution2 admin = new Solution2();
public static void main(String[] args) {
admin.start();
}
}
class Solution2 {
//---------------------------------INPUT READER-----------------------------------------//
public BufferedReader br;
StringTokenizer st = new StringTokenizer("");
String next() {
while (!st.hasMoreTokens()) {
try { st = new StringTokenizer(br.readLine());} catch (IOException e) { e.printStackTrace(); }
}
return st.nextToken();
}
int ni() { return Integer.parseInt(next()); }
long nl() { return Long.parseLong(next()); }
double nd() { return Double.parseDouble(next()); }
String ns() { return next(); }
int[] na(long n) {int[]ret=new int[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;}
long[] nal(long n) {long[]ret=new long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;}
Integer[] nA(long n) {Integer[]ret=new Integer[(int)n]; for(int i=0;i<n;i++) ret[i]=ni(); return ret;}
Long[] nAl(long n) {Long[]ret=new Long[(int)n]; for(int i=0;i<n;i++) ret[i]=nl(); return ret;}
//--------------------------------------PRINTER------------------------------------------//
PrintWriter w;
void p(int i) {w.println(i);} void p(long l) {w.println(l);}
void p(double d) {w.println(d);} void p(String s) { w.println(s);}
void pr(int i) {w.print(i);} void pr(long l) {w.print(l);}
void pr(double d) {w.print(d);} void pr(String s) { w.print(s);}
void pl() {w.println();}
//--------------------------------------VARIABLES-----------------------------------------//
long lma = Long.MAX_VALUE, lmi = Long.MIN_VALUE;
int ima = Integer.MAX_VALUE, imi = Integer.MIN_VALUE;
long mod = 1000000007;
{
w = new PrintWriter(System.out);
br = new BufferedReader(new InputStreamReader(System.in));
try {if(new File(System.getProperty("user.dir")).getName().equals("LOCAL")) {
w = new PrintWriter(new OutputStreamWriter(new FileOutputStream("output.txt")));
br = new BufferedReader(new InputStreamReader(new FileInputStream("input.txt")));}
} catch (Exception ignore) { }
}
//----------------------START---------------------//
void start() {
int t = ni(); while(t-- > 0)
solve();
w.close();
}
pr getIntersection (pr a, pr b) {
long tx = Math.max(a.x + a.p, b.x + b.p);
long ty = Math.max(a.p - a.x, b.p - b.x);
return new pr((tx - ty) / 2, (tx + ty) / 2);
}
void solve() {
int n = ni(), m = ni();
List<pr> li = new ArrayList<>();
List<pr> rains = new ArrayList<>();
pr intersection = null;
for(int i = 0; i < n; i++) {
long x = nl(), p = nl();
li.add(new pr(x-p, 1));
li.add(new pr(x, -2));
li.add(new pr(x+p, 1));
rains.add(new pr(x, p));
}
li.sort(Comparator.comparingLong(a -> a.x));
long prev_height = 0, prev_multiple = 0, prev_x = 0;
for(int i = 0; i < li.size(); i++) {
pr curr = li.get(i);
prev_height = prev_height + (curr.x - prev_x) * prev_multiple;
if(prev_height > m) {
if(intersection == null) intersection = new pr(2 * curr.x, 2 * (prev_height-m));
else intersection = getIntersection(intersection, new pr(2 * curr.x, 2 * (prev_height-m)));
}
prev_multiple += curr.p;
prev_x = curr.x;
}
if(intersection == null) {
for(int i = 0; i < n; i++) {
pr(1);
}
pl();
return;
}
for(int i = 0; i < n; i++) {
pr rain = rains.get(i);
pr curr_intersection = getIntersection(intersection, new pr(2 * rain.x, 2 * rain.p));
if(curr_intersection.x == 2 * rain.x && curr_intersection.p == 2 * rain.p) pr(1);
else pr(0);
}
pl();
}
class pr {
long x, p;
public pr(long x, long p) {
this.x = x;
this.p = p;
}
}
} | Java | ["4\n\n3 6\n\n1 5\n\n5 5\n\n3 4\n\n2 3\n\n1 3\n\n5 2\n\n2 5\n\n1 6\n\n10 6\n\n6 12\n\n4 5\n\n1 6\n\n12 5\n\n5 5\n\n9 7\n\n8 3"] | 4 seconds | ["001\n11\n00\n100110"] | NoteIn the first test case, if we do not use the spell, the accumulated rainfall distribution will be like this: If we erase the third day's rain, the flood is avoided and the accumulated rainfall distribution looks like this: In the second test case, since initially the flood will not happen, we can erase any day's rain.In the third test case, there is no way to avoid the flood. | Java 8 | standard input | [
"binary search",
"brute force",
"data structures",
"geometry",
"greedy",
"implementation",
"math"
] | 268cf03c271d691c3c1e3922e884753e | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq m \leq 10^9$$$) — the number of rainy days and the maximal accumulated rainfall with no flood occurring. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains two integers $$$x_i$$$ and $$$p_i$$$ ($$$1 \leq x_i,p_i \leq 10^9$$$) — the position and intensity of the $$$i$$$-th day's rain. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 2,100 | For each test case, output a binary string $$$s$$$ length of $$$n$$$. The $$$i$$$-th character of $$$s$$$ is 1 if after erasing the $$$i$$$-th day's rain there is no flood, while it is 0, if after erasing the $$$i$$$-th day's rain the flood still happens. | standard output | |
PASSED | db048384d261c1ba2088bcbb23e7d245 | train_108.jsonl | 1658673300 | You are the owner of a harvesting field which can be modeled as an infinite line, whose positions are identified by integers.It will rain for the next $$$n$$$ days. On the $$$i$$$-th day, the rain will be centered at position $$$x_i$$$ and it will have intensity $$$p_i$$$. Due to these rains, some rainfall will accumulate; let $$$a_j$$$ be the amount of rainfall accumulated at integer position $$$j$$$. Initially $$$a_j$$$ is $$$0$$$, and it will increase by $$$\max(0,p_i-|x_i-j|)$$$ after the $$$i$$$-th day's rain.A flood will hit your field if, at any moment, there is a position $$$j$$$ with accumulated rainfall $$$a_j>m$$$.You can use a magical spell to erase exactly one day's rain, i.e., setting $$$p_i=0$$$. For each $$$i$$$ from $$$1$$$ to $$$n$$$, check whether in case of erasing the $$$i$$$-th day's rain there is no flood. | 256 megabytes | import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run() {
for(int q=ni();q>0;q--){
work();
}
out.flush();
}
long mod=1000000007;
long inf=Long.MAX_VALUE/3;
long gcd(long a,long b) {
return a==0?b:gcd(b%a,a);
}
void work(){
int n=ni(),m=ni();
long[][] rec=new long[n][];
TreeMap<Integer,Long> map=new TreeMap<>();
for(int i=0;i<n;i++){
int x=ni(),d=ni();
rec[i]=new long[]{x,d};
map.put(x-d,map.getOrDefault(x-d,0L)+1L);
map.put(x,map.getOrDefault(x,0L)-2L);
map.put(x+d,map.getOrDefault(x+d,0L)+1L);
}
long l=inf,r=-inf;
int pre=Integer.MIN_VALUE/2;
long val=0,d=0;
for(Integer x:map.keySet()){
int w=x-pre;
val+=d*w;
if(val>m){
l=Math.min(l,x-(val-m));
r=Math.max(r,x+(val-m));
}
d+=map.get(x);
pre=x;
}
StringBuilder sb=new StringBuilder();
for(int i=0;i<n;i++){
long x=rec[i][0],d1=rec[i][1];
if(x-d1<=l&&x+d1>=r){
sb.append(1);
}else{
sb.append(0);
}
}
out.println(sb.toString());
}
@SuppressWarnings("unused")
private ArrayList<Integer>[] ng(int n, int m) {
ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
int s=in.nextInt()-1,e=in.nextInt()-1;
graph[s].add(e);
graph[e].add(s);
}
return graph;
}
private ArrayList<long[]>[] ngw(int n, int m) {
ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n];
for(int i=0;i<n;i++) {
graph[i]=new ArrayList<>();
}
for(int i=1;i<=m;i++) {
long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong();
graph[(int)s].add(new long[] {e,w});
graph[(int)e].add(new long[] {s,w});
}
return graph;
}
private int ni() {
return in.nextInt();
}
private long nl() {
return in.nextLong();
}
private double nd() {
return in.nextDouble();
}
private String ns() {
return in.next();
}
private long[] na(int n) {
long[] A=new long[n];
for(int i=0;i<n;i++) {
A[i]=in.nextLong();
}
return A;
}
private int[] nia(int n) {
int[] A=new int[n];
for(int i=0;i<n;i++) {
A[i]=in.nextInt();
}
return A;
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
InputStreamReader input;//no buffer
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(boolean isBuffer)
{
if(!isBuffer){
input=new InputStreamReader(System.in);
}else{
br=new BufferedReader(new InputStreamReader(System.in));
}
}
public boolean hasNext(){
try{
String s=br.readLine();
if(s==null){
return false;
}
st=new StringTokenizer(s);
}catch(IOException e){
e.printStackTrace();
}
return true;
}
public String next()
{
if(input!=null){
try {
StringBuilder sb=new StringBuilder();
int ch=input.read();
while(ch=='\n'||ch=='\r'||ch==32){
ch=input.read();
}
while(ch!='\n'&&ch!='\r'&&ch!=32){
sb.append((char)ch);
ch=input.read();
}
return sb.toString();
}catch (Exception e){
e.printStackTrace();
}
}
while(st==null || !st.hasMoreElements())//回车,空行情况
{
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt()
{
return (int)nextLong();
}
public long nextLong() {
try {
if(input!=null){
long ret=0;
int b=input.read();
while(b<'0'||b>'9'){
b=input.read();
}
while(b>='0'&&b<='9'){
ret=ret*10+(b-'0');
b=input.read();
}
return ret;
}
}catch (Exception e){
e.printStackTrace();
}
return Long.parseLong(next());
}
public double nextDouble()
{
return Double.parseDouble(next());
}
} | Java | ["4\n\n3 6\n\n1 5\n\n5 5\n\n3 4\n\n2 3\n\n1 3\n\n5 2\n\n2 5\n\n1 6\n\n10 6\n\n6 12\n\n4 5\n\n1 6\n\n12 5\n\n5 5\n\n9 7\n\n8 3"] | 4 seconds | ["001\n11\n00\n100110"] | NoteIn the first test case, if we do not use the spell, the accumulated rainfall distribution will be like this: If we erase the third day's rain, the flood is avoided and the accumulated rainfall distribution looks like this: In the second test case, since initially the flood will not happen, we can erase any day's rain.In the third test case, there is no way to avoid the flood. | Java 8 | standard input | [
"binary search",
"brute force",
"data structures",
"geometry",
"greedy",
"implementation",
"math"
] | 268cf03c271d691c3c1e3922e884753e | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq m \leq 10^9$$$) — the number of rainy days and the maximal accumulated rainfall with no flood occurring. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains two integers $$$x_i$$$ and $$$p_i$$$ ($$$1 \leq x_i,p_i \leq 10^9$$$) — the position and intensity of the $$$i$$$-th day's rain. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 2,100 | For each test case, output a binary string $$$s$$$ length of $$$n$$$. The $$$i$$$-th character of $$$s$$$ is 1 if after erasing the $$$i$$$-th day's rain there is no flood, while it is 0, if after erasing the $$$i$$$-th day's rain the flood still happens. | standard output | |
PASSED | a0f87f3c6b3d47918cbceaf8351a8a62 | train_108.jsonl | 1658673300 | You are the owner of a harvesting field which can be modeled as an infinite line, whose positions are identified by integers.It will rain for the next $$$n$$$ days. On the $$$i$$$-th day, the rain will be centered at position $$$x_i$$$ and it will have intensity $$$p_i$$$. Due to these rains, some rainfall will accumulate; let $$$a_j$$$ be the amount of rainfall accumulated at integer position $$$j$$$. Initially $$$a_j$$$ is $$$0$$$, and it will increase by $$$\max(0,p_i-|x_i-j|)$$$ after the $$$i$$$-th day's rain.A flood will hit your field if, at any moment, there is a position $$$j$$$ with accumulated rainfall $$$a_j>m$$$.You can use a magical spell to erase exactly one day's rain, i.e., setting $$$p_i=0$$$. For each $$$i$$$ from $$$1$$$ to $$$n$$$, check whether in case of erasing the $$$i$$$-th day's rain there is no flood. | 256 megabytes | import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.Locale;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
public class Solution implements Runnable {
private PrintStream out;
private BufferedReader in;
private StringTokenizer st;
public void solve() throws IOException {
long time0 = System.currentTimeMillis();
int t = nextInt();
for (int test = 1; test <= t; test++) {
int n = nextInt();
int m = nextInt();
int[] x = new int[n];
int[] p = new int[n];
for (int i = 0; i < n; i++) {
x[i] = nextInt();
p[i] = nextInt();
}
boolean[] answer = solve(n, m, x, p);
for (int i = 0; i < n; i++) {
if (answer[i]) {
out.print('1');
} else {
out.print('0');
}
}
out.println();
}
System.err.println("time: " + (System.currentTimeMillis() - time0));
}
private boolean[] solve(int n, int m, int[] x, int[] p) {
TreeSet<Integer> x0s = new TreeSet<>();
for (int i = 0; i < n; i++) {
x0s.add(x[i] - p[i]);
x0s.add(x[i]);
x0s.add(x[i] + p[i]);
}
x0s.add(x0s.first() - 1);
int[] x0 = new int[x0s.size()];
TreeMap<Integer, Integer> x0m = new TreeMap<Integer, Integer>();
int done = 0;
for (Integer pos : x0s) {
x0[done] = pos;
x0m.put(x0[done], done);
done++;
}
int[] a = new int[x0.length];
for (int i = 0; i < n; i++) {
a[x0m.get(x[i] - p[i])] += 1;
a[x0m.get(x[i])] -= 2;
a[x0m.get(x[i] + p[i])] += 1;
}
int[] v = new int[x0.length];
long[] h = new long[x0.length];
for (int i = 1; i < x0.length; i++) {
v[i] = v[i - 1] + a[i];
h[i] = h[i - 1] + 1L * v[i - 1] * (x0[i] - x0[i - 1]);
}
long[] hp = new long[x0.length];
long[] hm = new long[x0.length];
for (int i = 0; i < x0.length; i++) {
hp[i] = h[i] + x0[i];
hm[i] = h[i] - x0[i];
}
MaxTree t0 = new MaxTree(h);
MaxTree tp = new MaxTree(hp);
MaxTree tm = new MaxTree(hm);
boolean[] answer = new boolean[n];
for (int i = 0; i < n; i++) {
int left = x0m.get(x[i] - p[i]);
int mid = x0m.get(x[i]);
int right = x0m.get(x[i] + p[i]);
long maxLeft = t0.get(0, left + 1);
long maxMidLeft = tm.get(left, mid + 1) + x0[left];
long maxMidRight = tp.get(mid, right + 1) - x0[right];
long maxRight = t0.get(right, x0.length);
answer[i] = (maxLeft <= m) && (maxMidLeft <= m) && (maxMidRight <= m) && (maxRight <= m);
}
return answer;
}
public static class MaxTree {
int base;
long[] a;
public MaxTree(long[] v) {
base = 1;
while (base < v.length) {
base = base * 2;
}
a = new long[base * 2];
for (int i = 0; i < v.length; i++) {
a[base + i] = v[i];
}
for (int i = base - 1; i >= 1; i--) {
a[i] = Math.max(a[2 * i + 0], a[2 * i + 1]);
}
}
public long get(int from, int to) {
if (from >= to) {
return 0;
}
int i = base + from;
int j = base + to - 1;
long result = Math.max(a[i], a[j]);
while (i + 1 < j) {
result = Math.max(result, a[i + 1]);
result = Math.max(result, a[j - 1]);
i = i / 2;
j = j / 2;
}
return result;
}
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line);
}
return st.nextToken();
}
@Override
public void run() {
try {
solve();
out.close();
} catch (Throwable e) {
throw new RuntimeException(e);
}
}
public Solution(String name) throws IOException {
Locale.setDefault(Locale.US);
if (name == null) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintStream(new BufferedOutputStream(System.out));
} else {
in = new BufferedReader(new InputStreamReader(new FileInputStream(name + ".in")));
out = new PrintStream(new BufferedOutputStream(new FileOutputStream(name + ".out")));
}
st = new StringTokenizer("");
}
public static void main(String[] args) throws IOException {
new Thread(new Solution(null)).start();
}
}
| Java | ["4\n\n3 6\n\n1 5\n\n5 5\n\n3 4\n\n2 3\n\n1 3\n\n5 2\n\n2 5\n\n1 6\n\n10 6\n\n6 12\n\n4 5\n\n1 6\n\n12 5\n\n5 5\n\n9 7\n\n8 3"] | 4 seconds | ["001\n11\n00\n100110"] | NoteIn the first test case, if we do not use the spell, the accumulated rainfall distribution will be like this: If we erase the third day's rain, the flood is avoided and the accumulated rainfall distribution looks like this: In the second test case, since initially the flood will not happen, we can erase any day's rain.In the third test case, there is no way to avoid the flood. | Java 8 | standard input | [
"binary search",
"brute force",
"data structures",
"geometry",
"greedy",
"implementation",
"math"
] | 268cf03c271d691c3c1e3922e884753e | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq m \leq 10^9$$$) — the number of rainy days and the maximal accumulated rainfall with no flood occurring. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains two integers $$$x_i$$$ and $$$p_i$$$ ($$$1 \leq x_i,p_i \leq 10^9$$$) — the position and intensity of the $$$i$$$-th day's rain. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 2,100 | For each test case, output a binary string $$$s$$$ length of $$$n$$$. The $$$i$$$-th character of $$$s$$$ is 1 if after erasing the $$$i$$$-th day's rain there is no flood, while it is 0, if after erasing the $$$i$$$-th day's rain the flood still happens. | standard output | |
PASSED | e90b286b3d48413e3be81a9b1aa714c5 | train_108.jsonl | 1658673300 | You are the owner of a harvesting field which can be modeled as an infinite line, whose positions are identified by integers.It will rain for the next $$$n$$$ days. On the $$$i$$$-th day, the rain will be centered at position $$$x_i$$$ and it will have intensity $$$p_i$$$. Due to these rains, some rainfall will accumulate; let $$$a_j$$$ be the amount of rainfall accumulated at integer position $$$j$$$. Initially $$$a_j$$$ is $$$0$$$, and it will increase by $$$\max(0,p_i-|x_i-j|)$$$ after the $$$i$$$-th day's rain.A flood will hit your field if, at any moment, there is a position $$$j$$$ with accumulated rainfall $$$a_j>m$$$.You can use a magical spell to erase exactly one day's rain, i.e., setting $$$p_i=0$$$. For each $$$i$$$ from $$$1$$$ to $$$n$$$, check whether in case of erasing the $$$i$$$-th day's rain there is no flood. | 256 megabytes | import java.util.*;
import java.io.*;
public class Rain {
public static void main(String[] args) throws IOException {
Reader in = new Reader();
PrintWriter out = new PrintWriter(System.out);
int T = in.nextInt();
for (int t = 0; t < T; t++) {
int N = in.nextInt(), K = in.nextInt();
int[] p = new int[N], x = new int[N];
for (int i = 0; i < N; i++) {
x[i] = in.nextInt();
p[i] = in.nextInt();
}
List<Integer> compress = new ArrayList<>();
for (int i = 0; i < N; i++) {
compress.add(x[i]);
compress.add(x[i] - p[i]);
compress.add(x[i] + p[i]);
}
Collections.sort(compress);
int[][] map = new int[N][3];
for (int i = 0; i < N; i++) {
map[i][0] = Collections.binarySearch(compress, x[i] - p[i]);
map[i][1] = Collections.binarySearch(compress, x[i]);
map[i][2] = Collections.binarySearch(compress, x[i] + p[i]);
}
long[] change = new long[compress.size()];
for (int i = 0; i < N; i++) {
change[map[i][0]]++;
change[map[i][1]] -= 2;
change[map[i][2]]++;
}
for (int i = 1; i < compress.size(); i++) {
change[i] += change[i - 1];
}
long[] height = new long[compress.size()];
for (int i = 1; i < compress.size(); i++) {
height[i] = height[i - 1] + change[i - 1] * (compress.get(i) - compress.get(i - 1));
}
long[] height1 = new long[compress.size()], height2 = new long[compress.size()];
for (int i = 1; i < compress.size(); i++) {
height1[i] = height1[i - 1] + (change[i - 1] - 1) * (compress.get(i) - compress.get(i - 1));
height2[i] = height2[i - 1] + (change[i - 1] + 1) * (compress.get(i) - compress.get(i - 1));
}
int M = 1;
while (M < compress.size()) {
M <<= 1;
}
long[] tree = new long[M * 2], tree1 = new long[M * 2], tree2 = new long[M * 2];
for (int i = 0; i < compress.size(); i++) {
tree[i + M] = height[i];
tree1[i + M] = height1[i];
tree2[i + M] = height2[i];
}
for (int i = M - 1; i > 0; i--) {
tree[i] = Math.max(tree[i * 2], tree[i * 2 + 1]);
tree1[i] = Math.max(tree1[i * 2], tree1[i * 2 + 1]);
tree2[i] = Math.max(tree2[i * 2], tree2[i * 2 + 1]);
}
int[] res = new int[N];
for (int i = 0; i < N; i++) {
int l = map[i][0] + M, r = map[i][1] + M;
long dif = -((long)x[i] - p[i] - compress.get(0));
long max = 0;
while (l <= r) {
if (l % 2 == 1) {
max = Math.max(max, tree1[l] - dif);
l >>= 1;
l++;
} else {
l >>= 1;
}
if (r % 2 == 0) {
max = Math.max(max, tree1[r] - dif);
r >>= 1;
r--;
} else {
r >>= 1;
}
}
l = map[i][1] + M;
r = map[i][2] + M;
dif = ((long)x[i] - compress.get(0)) + (x[i] - ((long)x[i] - p[i]));
while (l <= r) {
if (l % 2 == 1) {
max = Math.max(max, tree2[l] - dif);
l >>= 1;
l++;
} else {
l >>= 1;
}
if (r % 2 == 0) {
max = Math.max(max, tree2[r] - dif);
r >>= 1;
r--;
} else {
r >>= 1;
}
}
l = map[i][2] + M;
r = M * 2 - 1;
dif = 0;
while (l <= r) {
if (l % 2 == 1) {
max = Math.max(max, tree[l] - dif);
l >>= 1;
l++;
} else {
l >>= 1;
}
if (r % 2 == 0) {
max = Math.max(max, tree[r] - dif);
r >>= 1;
r--;
} else {
r >>= 1;
}
}
l = M;
r = map[i][0] + M;
dif = 0;
while (l <= r) {
if (l % 2 == 1) {
max = Math.max(max, tree[l] - dif);
l >>= 1;
l++;
} else {
l >>= 1;
}
if (r % 2 == 0) {
max = Math.max(max, tree[r] - dif);
r >>= 1;
r--;
} else {
r >>= 1;
}
}
res[i] = max > K ? 0 : 1;
}
for (int i = 0; i < N; i++) {
out.print(res[i]);
}
out.println();
}
out.close();
}
static class Reader {
BufferedReader in;
StringTokenizer st;
public Reader() {
in = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
}
public String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
public String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
}
public static void sort(int[] arr) {
List<Integer> list = new ArrayList<>();
for (int i : arr) {
list.add(i);
}
Collections.sort(list);
for (int i = 0; i < arr.length; i++) {
arr[i] = list.get(i);
}
}
} | Java | ["4\n\n3 6\n\n1 5\n\n5 5\n\n3 4\n\n2 3\n\n1 3\n\n5 2\n\n2 5\n\n1 6\n\n10 6\n\n6 12\n\n4 5\n\n1 6\n\n12 5\n\n5 5\n\n9 7\n\n8 3"] | 4 seconds | ["001\n11\n00\n100110"] | NoteIn the first test case, if we do not use the spell, the accumulated rainfall distribution will be like this: If we erase the third day's rain, the flood is avoided and the accumulated rainfall distribution looks like this: In the second test case, since initially the flood will not happen, we can erase any day's rain.In the third test case, there is no way to avoid the flood. | Java 8 | standard input | [
"binary search",
"brute force",
"data structures",
"geometry",
"greedy",
"implementation",
"math"
] | 268cf03c271d691c3c1e3922e884753e | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq m \leq 10^9$$$) — the number of rainy days and the maximal accumulated rainfall with no flood occurring. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains two integers $$$x_i$$$ and $$$p_i$$$ ($$$1 \leq x_i,p_i \leq 10^9$$$) — the position and intensity of the $$$i$$$-th day's rain. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 2,100 | For each test case, output a binary string $$$s$$$ length of $$$n$$$. The $$$i$$$-th character of $$$s$$$ is 1 if after erasing the $$$i$$$-th day's rain there is no flood, while it is 0, if after erasing the $$$i$$$-th day's rain the flood still happens. | standard output | |
PASSED | 7b7fa12c8bb14f3846a09cd4d7e308ed | train_108.jsonl | 1658673300 | You are the owner of a harvesting field which can be modeled as an infinite line, whose positions are identified by integers.It will rain for the next $$$n$$$ days. On the $$$i$$$-th day, the rain will be centered at position $$$x_i$$$ and it will have intensity $$$p_i$$$. Due to these rains, some rainfall will accumulate; let $$$a_j$$$ be the amount of rainfall accumulated at integer position $$$j$$$. Initially $$$a_j$$$ is $$$0$$$, and it will increase by $$$\max(0,p_i-|x_i-j|)$$$ after the $$$i$$$-th day's rain.A flood will hit your field if, at any moment, there is a position $$$j$$$ with accumulated rainfall $$$a_j>m$$$.You can use a magical spell to erase exactly one day's rain, i.e., setting $$$p_i=0$$$. For each $$$i$$$ from $$$1$$$ to $$$n$$$, check whether in case of erasing the $$$i$$$-th day's rain there is no flood. | 256 megabytes | import static java.lang.Math.*;
import static java.util.Arrays.*;
import java.util.*;
public class B {
public Object solve () {
int N = sc.nextInt(), M = sc.nextInt();
int [] X = new int [N], P = new int [N];
for (int i : rep(N)) {
X[i] = sc.nextInt();
P[i] = sc.nextInt();
}
LinkedList<int[]> E = new LinkedList<>();
for (int i : rep(N)) {
E.add(new int [] { X[i] - P[i], 1 });
E.add(new int [] { X[i], -2 });
E.add(new int [] { X[i] + P[i], 1 });
}
E.sort(by(0));
long L = LINF, R = -LINF, U = -LINF, V = -M, D = 0;
while (!E.isEmpty()) {
int [] e = E.poll();
int x = e[0], d = e[1];
while (!E.isEmpty() && E.peek()[0] == x)
d += E.poll()[1];
V += D * (x - U);
U = x;
D += d;
if (V > 0) {
L = min(L, x - V);
R = max(R, x + V);
}
}
char [] res = new char [N];
fill(res, '0');
for (int i : rep(N)) {
long a = X[i] - P[i], b = X[i] + P[i];
if (a <= L && b >= R)
res[i] = '1';
}
return new String(res);
}
private static final int CONTEST_TYPE = 2;
private static void init () {
}
private static final long LINF = (long) 1e18 + 10;
private static Comparator<int[]> by (int j, int ... J) {
Comparator<int[]> res = (x, y) -> Integer.compare(x[j], y[j]);
for (int i : J)
res = res.thenComparing((x, y) -> Integer.compare(x[i], y[i]));
return res;
}
private static int [] rep (int N) { return rep(0, N); }
private static int [] rep (int S, int T) { if (S >= T) return new int [0]; int [] res = new int [T-S]; for (int i = S; i < T; ++i) res[i-S] = i; return res; }
//////////////////////////////////////////////////////////////////////////////////// OFF
private static IOUtils.MyScanner sc = new IOUtils.MyScanner();
private static class IOUtils {
public static class MyScanner {
public String next () { newLine(); return line[index++]; }
public int nextInt () { return Integer.parseInt(next()); }
//////////////////////////////////////////////
private boolean eol () { return index == line.length; }
private String readLine () {
try {
int b;
StringBuilder res = new StringBuilder();
while ((b = System.in.read()) < ' ')
continue;
res.append((char)b);
while ((b = System.in.read()) >= ' ')
res.append((char)b);
return res.toString();
} catch (Exception e) {
throw new Error (e);
}
}
private MyScanner () {
try {
while (System.in.available() == 0)
Thread.sleep(1);
start();
} catch (Exception e) {
throw new Error(e);
}
}
private String [] line;
private int index;
private void newLine () {
if (line == null || eol()) {
line = split(readLine());
index = 0;
}
}
private String [] split (String s) { return s.length() > 0 ? s.split(" ") : new String [0]; }
}
private static String build (Object o, Object ... A) { return buildDelim(" ", o, A); }
private static String buildDelim (String delim, Object o, Object ... A) {
java.util.List<String> str = new java.util.ArrayList<>();
append(str, o);
for (Object p : A)
append(str, p);
return String.join(delim, str.toArray(new String [0]));
}
//////////////////////////////////////////////////////////////////////////////////
private static java.text.DecimalFormat formatter = new java.text.DecimalFormat("#.#########");
private static void start () { if (t == 0) t = millis(); }
private static void append (java.util.function.Consumer<Object> f, java.util.function.Consumer<Object> g, Object o) {
if (o.getClass().isArray()) {
int len = java.lang.reflect.Array.getLength(o);
for (int i = 0; i < len; ++i)
f.accept(java.lang.reflect.Array.get(o, i));
}
else if (o instanceof Iterable<?>)
((Iterable<?>)o).forEach(f::accept);
else
g.accept(o instanceof Double ? formatter.format(o) : o);
}
private static void append (java.util.List<String> str, Object o) {
append(x -> append(str, x), x -> str.add(x.toString()), o);
}
private static java.io.PrintWriter pw = new java.io.PrintWriter(System.out);
private static void flush () { pw.flush(); System.out.flush(); }
private static void print (Object o, Object ... A) {
String res = build(o, A);
if (DEBUG == 2)
err(res, '(', time(), ')');
if (res.length() > 0)
pw.println(res);
if (DEBUG == 1)
flush();
}
private static void err (Object o, Object ... A) { System.err.println(build(o, A)); }
private static int DEBUG;
private static void exit () {
String end = "------------------" + System.lineSeparator() + time();
switch(DEBUG) {
case 1: print(end); break;
case 2: err(end); break;
}
IOUtils.pw.close();
System.out.flush();
System.exit(0);
}
private static long t;
private static long millis () { return System.currentTimeMillis(); }
private static String time () { return "Time: " + (millis() - t) / 1000.0; }
private static void run (int N) {
try { DEBUG = Integer.parseInt(System.getProperties().get("DEBUG").toString()); }
catch (Throwable t) {}
for (int n = 1; n <= N; ++n) {
Object res = new B().solve();
if (res != null) {
@SuppressWarnings("all")
Object o = CONTEST_TYPE == 0 ? "Case #" + n + ": " + build(res) : res;
print(o);
}
}
exit();
}
}
////////////////////////////////////////////////////////////////////////////////////
public static void main (String[] args) {
@SuppressWarnings("all")
int N = CONTEST_TYPE == 1 ? 1 : sc.nextInt();
init();
IOUtils.run(N);
}
}
| Java | ["4\n\n3 6\n\n1 5\n\n5 5\n\n3 4\n\n2 3\n\n1 3\n\n5 2\n\n2 5\n\n1 6\n\n10 6\n\n6 12\n\n4 5\n\n1 6\n\n12 5\n\n5 5\n\n9 7\n\n8 3"] | 4 seconds | ["001\n11\n00\n100110"] | NoteIn the first test case, if we do not use the spell, the accumulated rainfall distribution will be like this: If we erase the third day's rain, the flood is avoided and the accumulated rainfall distribution looks like this: In the second test case, since initially the flood will not happen, we can erase any day's rain.In the third test case, there is no way to avoid the flood. | Java 11 | standard input | [
"binary search",
"brute force",
"data structures",
"geometry",
"greedy",
"implementation",
"math"
] | 268cf03c271d691c3c1e3922e884753e | Each test contains multiple test cases. The first line contains the number of test cases $$$t$$$ ($$$1 \leq t \leq 10^4$$$). The description of the test cases follows. The first line of each test case contains two integers $$$n$$$ and $$$m$$$ ($$$1 \leq n \leq 2 \cdot 10^5$$$, $$$1 \leq m \leq 10^9$$$) — the number of rainy days and the maximal accumulated rainfall with no flood occurring. Then $$$n$$$ lines follow. The $$$i$$$-th of these lines contains two integers $$$x_i$$$ and $$$p_i$$$ ($$$1 \leq x_i,p_i \leq 10^9$$$) — the position and intensity of the $$$i$$$-th day's rain. The sum of $$$n$$$ over all test cases does not exceed $$$2 \cdot 10^5$$$. | 2,100 | For each test case, output a binary string $$$s$$$ length of $$$n$$$. The $$$i$$$-th character of $$$s$$$ is 1 if after erasing the $$$i$$$-th day's rain there is no flood, while it is 0, if after erasing the $$$i$$$-th day's rain the flood still happens. | standard output |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.