src
stringlengths 95
64.6k
| complexity
stringclasses 7
values | problem
stringlengths 6
50
| from
stringclasses 1
value |
---|---|---|---|
import java.util.*;
public class algo121
{
public static void main(String args[])
{
Scanner ex=new Scanner(System.in);
int n=ex.nextInt();
String a[]=new String[n];
String b[]=new String[n];
for(int i=0;i<n;i++)
a[i]=ex.next();
for(int i=0;i<n;i++)
b[i]=ex.next();
String valid[]={"S","M","L","XS","XL","XXS","XXL","XXXS","XXXL"};
int ai[]=new int[9];
int bi[]=new int[9];
for(int i=0;i<n;i++)
{
for(int j=0;j<9;j++)
{
if(a[i].equals(valid[j]))
ai[j]++;
if(b[i].equals(valid[j]))
bi[j]++;
}
}
int ans=0;
for(int i=0;i<9;i++)
{
if(ai[i]>bi[i])
ans=ans+ai[i]-bi[i];
}
System.out.println(ans);
}
} | linear | 1000_A. Codehorses T-shirts | CODEFORCES |
import java.io.*;
import java.util.*;
public class A
{
public static void main(String[] args) throws IOException
{
Scanner sc=new Scanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
int n=sc.nextInt();
String []a=new String[n];
String []b=new String[n];
TreeMap<String,Integer> map1=new TreeMap(),map2=new TreeMap();
for(int i=0;i<n;i++)
{
String s=sc.next();
map1.put(s, map1.getOrDefault(s, 0)+1);
}
for(int i=0;i<n;i++)
{
String s=sc.next();
map2.put(s, map2.getOrDefault(s, 0)+1);
}
int ans=0;
for(String s:map2.keySet())
{
int cnt=map1.getOrDefault(s, 0);
ans+=Math.max(0, map2.get(s)-cnt);
}
pw.println(ans);
pw.close();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public Scanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(s));
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | linear | 1000_A. Codehorses T-shirts | CODEFORCES |
import java.util.Scanner;
public class GenerateLogin {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String a = scan.next();
String b = scan.next();
char last = b.charAt(0);
String ans = ""+a.charAt(0);
for(int i = 1;i<a.length();i++){
if(a.charAt(i)>=last)break;
ans+=a.charAt(i);
}
ans+=last;
System.out.println(ans);
}
}
| linear | 909_A. Generate Login | CODEFORCES |
import java.util.*;
import java.io.*;
public class SonyaExhibition {
static BufferedReader br;
static StringTokenizer tokenizer;
public static void main(String[] args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
int n = nextInt();
int[] arr = {0,1};
for(int i = 0; i < n; i++) {
System.out.print(arr[i % 2]);
}
System.out.println();
}
public static String next() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
String line = br.readLine();
if (line == null)
throw new IOException();
tokenizer = new StringTokenizer(line);
}
return tokenizer.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
}
| linear | 1004_B. Sonya and Exhibition | CODEFORCES |
import java.util.*;
import java.io.*;
public class FirstClass {
public static void main(String[] args)throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
int n = Integer.parseInt(br.readLine());
int arr[] = new int [n];
StringTokenizer st1 = new StringTokenizer(br.readLine());
for(int i = 0 ; i < n ; i++)
{
arr[i] = Integer.parseInt(st1.nextToken());
}
int max = -1;
boolean flag = true;
for(int i = 0 ; i < n ; i++)
{
if(arr[i] > max+1)
{
flag = false;
out.println(i+1);
break;
}
else
{
max = Math.max(max, arr[i]);
}
}
if(flag)
out.println(-1);
out.flush();
out.close();
}
}
| linear | 1054_B. Appending Mex | CODEFORCES |
//package contese_476;
import java.util.*;
public class q1
{
int m=(int)1e9+7;
public class Node
{
int a;
int b;
public void Node(int a,int b)
{
this.a=a;
this.b=b;
}
}
public int mul(int a ,int b)
{
a=a%m;
b=b%m;
return((a*b)%m);
}
public int pow(int a,int b)
{
int x=1;
while(b>0)
{
if(b%2!=0)
x=mul(x,a);
a=mul(a,a);
b=b/2;
}
return x;
}
public static long gcd(long a,long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
HashMap<Integer,Integer> h=new HashMap();
//HashMap<Integer,Integer> h1=new HashMap();
int[] a=new int[n];
int x=sc.nextInt();
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
if(h.get(a[i])==null)
{
h.put(a[i], 1);
//h1.put(a[i],i);
}
else
{
System.out.print(0);
System.exit(0);
}
}
for(int i=0;i<n;i++)
{
int num=a[i]&x;
if(num==a[i])
continue;
if(h.get(num)==null)
continue;
else
{
System.out.print(1);
System.exit(0);
}
}
for(int i=0;i<n;i++)
{
int num=a[i]&x;
if(num==a[i])
continue;
if(h.get(num)==null)
h.put(num, 1);
else
{
System.out.print(2);
System.exit(0);
}
}
System.out.print(-1);
}
} | linear | 1013_B. And | CODEFORCES |
//package contese_476;
import java.util.*;
public class q1
{
int m=(int)1e9+7;
public class Node
{
int a;
int b;
public void Node(int a,int b)
{
this.a=a;
this.b=b;
}
}
public int mul(int a ,int b)
{
a=a%m;
b=b%m;
return((a*b)%m);
}
public int pow(int a,int b)
{
int x=1;
while(b>0)
{
if(b%2!=0)
x=mul(x,a);
a=mul(a,a);
b=b/2;
}
return x;
}
public static long gcd(long a,long b)
{
if(b==0)
return a;
else
return gcd(b,a%b);
}
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
HashMap<Integer,Integer> h=new HashMap();
//HashMap<Integer,Integer> h1=new HashMap();
int[] a=new int[n];
int x=sc.nextInt();
for(int i=0;i<n;i++)
{
a[i]=sc.nextInt();
if(h.get(a[i])==null)
{
h.put(a[i], 1);
//h1.put(a[i],i);
}
else
{
System.out.print(0);
System.exit(0);
}
}
for(int i=0;i<n;i++)
{
int num=a[i]&x;
if(num==a[i])
continue;
if(h.get(num)==null)
continue;
else
{
System.out.print(1);
System.exit(0);
}
}
for(int i=0;i<n;i++)
{
int num=a[i]&x;
if(num==a[i])
continue;
if(h.get(num)==null)
h.put(num, 1);
else
{
System.out.print(2);
System.exit(0);
}
}
System.out.print(-1);
}
} | linear | 1013_B. And | CODEFORCES |
// LM10: The next Ballon d'or
import java.util.*;
import java.io.*;
import java.math.*;
import javafx.util.Pair;
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(int [] arr){ for(int i=arr.length;i>0;--i) { int r=(int)(Math.random()*i); int temp=arr[i-1]; arr[i-1]=arr[r]; arr[r]=temp; } }
}
public static void main(String[] args)throws IOException {
/*
inputCopy
4
2 1 2 1
outputCopy
4
inputCopy
5
0 -1 -1 -1 -1
outputCopy
4
*/
PrintWriter pw = new PrintWriter(System.out);
FastReader fr = new FastReader();
int n=fr.i();
int [] arr=new int[n];
fr.scanIntArr(arr);
int min=Integer.MAX_VALUE;
int max=Integer.MIN_VALUE;
long sum=0;
if(n==1)
{
pw.println(arr[0]);
pw.flush();
pw.close();
return;
}
for(int i=0;i<n;++i)
{
if(arr[i]<min)
min=arr[i];
if(arr[i]>max)
max=arr[i];
sum+=Math.abs(arr[i]);
}
if(min>0)
{
sum-=2*min;
}
if(max<0)
{
sum+=2*max;
}
pw.println(sum);
pw.flush();
pw.close();
}
} | linear | 1038_D. Slime | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main {
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(br.readLine());
} catch (Exception e){e.printStackTrace();}
}
public String next() {
if (st.hasMoreTokens()) return st.nextToken();
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() {
String line = "";
if(st.hasMoreTokens()) line = st.nextToken();
else try {return br.readLine();}catch(IOException e){e.printStackTrace();}
while(st.hasMoreTokens()) line += " "+st.nextToken();
return line;
}
}
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int best = 1;
int bestTime = Integer.MAX_VALUE;
for(int i=0;i<n;i++) {
int time;
int a = sc.nextInt();
time = (a%n==0 || a%n<=i) ? a/n : (a+n)/n;
if(time < bestTime) {
best = i + 1;
bestTime = time;
}
}
pw.println(best);
pw.close();
}
}
| linear | 996_B. World Cup | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main {
public void solve() {
int n = ni();
int a = ni();
int b = ni();
long ans = 0;
HashMap<Long, Long> m = new HashMap<>();
HashMap<String, Long> s = new HashMap<>();
for (int i = 0; i < n; i++) {
ni();
long vx = ni();
long vy = ni();
long v = (long) a * vx - vy;
String k = vx + "|" + vy;
long cs = s.getOrDefault(k, 0L);
long c = m.getOrDefault(v, 0L);
ans += c - cs;
m.put(v, c + 1);
s.put(k, cs + 1);
}
write (ans * 2 + "\n");
}
public static void main(String[] args) {
Main m = new Main();
m.solve();
try {
m.out.close();
} catch (IOException e) {}
}
BufferedReader in;
BufferedWriter out;
StringTokenizer tokenizer;
public Main() {
in = new BufferedReader(new InputStreamReader(System.in));
out = new BufferedWriter(new OutputStreamWriter(System.out));
}
public String n() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(in.readLine());
} catch (IOException e) {}
}
return tokenizer.nextToken();
}
public int ni() {
return Integer.parseInt(n());
}
public long nl() {
return Long.parseLong(n());
}
public void write(String s) {
try {
out.write(s);
} catch (IOException e) {}
}
} | linear | 975_D. Ghosts | CODEFORCES |
import java.util.*;
import java.io.*;
public class MinimumDiameterTree{
public static void main(String[] args) {
InputReader in = new InputReader (System.in);
PrintWriter out = new PrintWriter (System.out);
int n = in.nextInt();
int s = in.nextInt();
int deg[] = new int [n];
for (int i = 1; i < n; ++i) {
deg[in.nextInt() - 1] ++;
deg[in.nextInt() - 1] ++;
}
int l = 0;
for (int i = 0; i < n; ++i)
if (deg[i] == 1) l ++;
out.println((double) 2 * s / l);
out.close();
}
public static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch(IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| linear | 1086_B. Minimum Diameter Tree | CODEFORCES |
import java.io.*;
import java.util.*;
public class CF1027D {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] cc = new int[n];
for (int i = 0; i < n; i++)
cc[i] = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int[] aa = new int[n];
for (int i = 0; i < n; i++)
aa[i] = Integer.parseInt(st.nextToken()) - 1;
int[] used = new int[n];
int ans = 0;
for (int i = 0; i < n; i++) {
if (used[i] == 2)
continue;
int j = i;
while (used[j] == 0) {
used[j] = 1;
j = aa[j];
}
if (used[j] == 1) {
int c = cc[j];
while (used[j] == 1) {
used[j] = 2;
c = Math.min(c, cc[j]);
j = aa[j];
}
ans += c;
}
j = i;
while (used[j] == 1) {
used[j] = 2;
j = aa[j];
}
}
System.out.println(ans);
}
}
| linear | 1027_D. Mouse Hunt | CODEFORCES |
import java.util.*;
//201920181
public class Polycarp{
public static void main(String args[]){
Scanner s = new Scanner(System.in);
int rem[] = new int[3];
Arrays.fill(rem,-1);
rem[0] = 0;
char ch[] = s.next().toCharArray();
int n = ch.length;
long dp[] = new long[n];
int sum = 0;
for(int i=0;i<ch.length;i++){
sum = sum + (ch[i]-48);
if(rem[sum%3] != -1)
if(i>0){
dp[i] = Math.max(dp[i-1],dp[rem[sum%3]]+1);}
else
dp[i] = 1;
else
if(i>0)
dp[i] = dp[i-1];
rem[sum%3] = i;
sum = sum%3;
}
System.out.println(dp[n-1]);
}
}
| linear | 1005_D. Polycarp and Div 3 | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
/**
*
* @author Haya
*/
public class CommentaryBoxes {
public static void main(String[] args) {
FastReader in = new FastReader();
long n = in.nextLong();
long m = in.nextLong();
long a = in.nextLong();
long b = in.nextLong();
long total = 0;
long val =(n%m);
if (n%m != 0){
long x = (val)*b;
long y = (m-val)*a;
total = Math.min(x, y);
}
System.out.println(Math.abs(total));
}
public 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 string = "";
try {
string = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return string;
}
}
}
| linear | 990_A. Commentary Boxes | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SFly {
public static void main(String[] args) throws IOException {
BufferedReader lector = new BufferedReader(new InputStreamReader(System.in));
int planet = Integer.parseInt(lector.readLine());
int ini = Integer.parseInt(lector.readLine());
double peso = ini;
int[] desp = new int[planet];
int[] ater = new int[planet];
String[] temp = lector.readLine().split(" ");
for(int i=0; i<planet; i++) {
desp[i] = Integer.parseInt(temp[i]);
if(desp[i] == 1) {
System.out.println(-1);
lector.close();
return;
}
}
temp = lector.readLine().split(" ");
for(int i=0; i<planet; i++) {
ater[i] = Integer.parseInt(temp[i]);
if(ater[i] == 1) {
System.out.println(-1);
lector.close();
return;
}
}
temp = null;
int i=planet-1;
peso = (peso*ater[0])/(ater[0]-1);
while(i>0) {
peso = (peso*desp[i])/(desp[i]-1);
peso = (peso*ater[i])/(ater[i]-1);
i--;
}
peso = (peso*desp[0])/(desp[0]-1);
peso = peso - ini;
System.out.println(peso);
lector.close();
}
} | linear | 1010_A. Fly | CODEFORCES |
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
/* spar5h */
public class cf1 implements Runnable{
public void run() {
InputReader s = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int t = 1;
while(t-- > 0) {
int n = s.nextInt(), m = s.nextInt();
int[] a = new int[n + 1];
for(int i = 1; i <= n; i++)
a[i] = s.nextInt();
int[] b = new int[n + 1];
for(int i = 1; i <= n; i++)
b[i] = s.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(a[1]);
for(int i = 2; i <= n; i++) {
list.add(b[i]); list.add(a[i]);
}
list.add(b[1]);
double wt = m;
boolean check = true;
for(int i = list.size() - 1; i >= 0; i--) {
if(list.get(i) <= 1) {
check = false; break;
}
double x = wt / (list.get(i) - 1);
wt += x;
}
if(check)
w.println(wt - m);
else
w.println(-1);
}
w.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception
{
new Thread(null, new cf1(),"cf1",1<<26).start();
}
} | linear | 1010_A. Fly | CODEFORCES |
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
/* spar5h */
public class cf1 implements Runnable{
public void run() {
InputReader s = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int t = 1;
while(t-- > 0) {
int n = s.nextInt(), m = s.nextInt();
int[] a = new int[n + 1];
for(int i = 1; i <= n; i++)
a[i] = s.nextInt();
int[] b = new int[n + 1];
for(int i = 1; i <= n; i++)
b[i] = s.nextInt();
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(a[1]);
for(int i = 2; i <= n; i++) {
list.add(b[i]); list.add(a[i]);
}
list.add(b[1]);
double wt = m;
boolean check = true;
for(int i = list.size() - 1; i >= 0; i--) {
if(list.get(i) <= 1) {
check = false; break;
}
double x = wt / (list.get(i) - 1);
wt += x;
}
if(check)
w.println(wt - m);
else
w.println(-1);
}
w.close();
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars)
{
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
public int nextInt()
{
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
int res = 0;
do
{
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
long res = 0;
do
{
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble()
{
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-')
{
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.')
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.')
{
c = read();
double m = 1;
while (!isSpaceChar(c))
{
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString()
{
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do
{
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c)
{
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next()
{
return readString();
}
public interface SpaceCharFilter
{
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception
{
new Thread(null, new cf1(),"cf1",1<<26).start();
}
} | linear | 1010_A. Fly | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class SFly {
public static void main(String[] args) throws IOException {
BufferedReader lector = new BufferedReader(new InputStreamReader(System.in));
int planet = Integer.parseInt(lector.readLine());
int ini = Integer.parseInt(lector.readLine());
double peso = ini;
int[] desp = new int[planet];
int[] ater = new int[planet];
String[] temp = lector.readLine().split(" ");
for(int i=0; i<planet; i++) {
desp[i] = Integer.parseInt(temp[i]);
if(desp[i] == 1) {
System.out.println(-1);
lector.close();
return;
}
}
temp = lector.readLine().split(" ");
for(int i=0; i<planet; i++) {
ater[i] = Integer.parseInt(temp[i]);
if(ater[i] == 1) {
System.out.println(-1);
lector.close();
return;
}
}
temp = null;
int i=planet-1;
peso = (peso*ater[0])/(ater[0]-1);
while(i>0) {
peso = (peso*desp[i])/(desp[i]-1);
peso = (peso*ater[i])/(ater[i]-1);
i--;
}
peso = (peso*desp[0])/(desp[0]-1);
peso = peso - ini;
System.out.println(peso);
lector.close();
}
} | linear | 1010_A. Fly | CODEFORCES |
import java.util.*;
public class Main{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[n];
for(int i=0;i<n;i++)
a[i]=sc.nextInt();
Arrays.sort(a);
//boolean
int t=1,c=0;
for(int i=1;i<n;i++){
if(a[i]==a[i-1])
{
if(i-2>=0&&a[i-2]==a[i-1]-1){
System.out.println("cslnb");
return;
}
c++;
}
if(a[i]==a[i-1]&&a[i]==0){
System.out.println("cslnb");
return;
}
}
if(c>1)
{
System.out.println("cslnb");
return;
}
for(int i=0;i<n;i++){
if((a[i]-i)%2!=0)
t=t^1;
}
if(t==1)
System.out.println("cslnb");
else
System.out.println("sjfnb");
}
} | linear | 1190_B. Tokitsukaze, CSL and Stone Game | CODEFORCES |
import java.util.*;
import java.io.*;
public class C{
static PrintWriter out;
static InputReader in;
public static void main(String args[]){
out = new PrintWriter(System.out);
in = new InputReader();
new C();
out.flush(); out.close();
}
C(){
int w = solve();
out.print(w == 0 ? "sjfnb" : "cslnb");
}
int n;
long a[];
int solve(){
n = in.nextInt(); a = new long[n];
long sum = 0;
for(int i = 0; i < n; i++)sum += a[i] = in.nextLong();
if(sum == 0){
return 1;
}
Arrays.sort(a);
int c = 0, c0 = 0; long p = -1, max = 0;
int f = 0;
long t = -1; int pp = -1;
for(int i = 0; i < n; i++){
if(a[i] == p){
c++;
}else{
if(p == 0)c0 = c;
if(c >= 2){f++; t = p; pp = i - 2;}
max = Math.max(max, c);
p = a[i];
c = 1;
}
}
max = Math.max(max, c);
sum = 0;
if(c >= 2){f++; t = p; pp = n - 2;}
if(max > 2 || c0 > 1 || f > 1)return 1;
if(f == 1){
long v = Arrays.binarySearch(a, t - 1);
if(v >= 0)return 1;
a[pp]--; sum = 1;
}
p = -1;
for(int i = 0; i < n; i++){
sum += a[i] - (p + 1);
a[i] = p + 1;
p = a[i];
}
return 1 - (int)(sum % 2);
}
public static class InputReader{
BufferedReader br;
StringTokenizer st;
InputReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public int nextInt(){
return Integer.parseInt(next());
}
public long nextLong(){
return Long.parseLong(next());
}
public double nextDouble(){
return Double.parseDouble(next());
}
public String next(){
while(st == null || !st.hasMoreTokens()){
try{
st = new StringTokenizer(br.readLine());
}catch(IOException e){}
}
return st.nextToken();
}
}
}
| linear | 1190_B. Tokitsukaze, CSL and Stone Game | CODEFORCES |
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class B {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = Integer.parseInt(bf.readLine());
StringTokenizer st = new StringTokenizer(bf.readLine());
Integer[] a = new Integer[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken());
Arrays.sort(a);
int[] b = new int[n];
for(int i=0; i<n; i++) b[i] = a[i].intValue();
boolean diff = false;
boolean diff2 = false;
Set<Integer> vals = new HashSet<Integer>();
vals.add(b[0]);
int valval = 0;
for(int i=1; i<n; i++) {
vals.add(b[i]);
if(b[i] == b[i-1]) {
if(!diff) {
diff = true;
valval = b[i];
}
else diff2 = true;
}
}
long sum = 0;
for(int i : b) sum += i;
sum -= 1L*n*(n-1)/2;
if(diff && !diff2) {
if(!vals.contains((valval-1)) && (valval > 0)) {
if(sum%2 == 0) out.println("cslnb"); else out.println("sjfnb");
}
else out.println("cslnb");
}
else if(diff2) out.println("cslnb");
else if(sum%2 == 0) out.println("cslnb"); else out.println("sjfnb");
// int n = Integer.parseInt(st.nextToken());
out.close(); System.exit(0);
}
} | linear | 1190_B. Tokitsukaze, CSL and Stone Game | CODEFORCES |
import java.util.*;
import java.io.*;
public class X
{
public static void main(String args[])throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String s1 = br.readLine();
String s2 = br.readLine();
int i=0;
char c1,c2;
int cost = 0;
while(i<n)
{
c1 = s1.charAt(i);
c2 = s2.charAt(i);
if(c1 != c2)
{
if((i+1)<n && s1.charAt(i+1) != s2.charAt(i+1) && s1.charAt(i) != s1.charAt(i+1))
{
cost +=1;
i++;
}
else
{
cost +=1;
}
}
i++;
}
System.out.println(cost);
}
} | linear | 1037_C. Equalize | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
import static java.lang.Math.*;
public class Main {
private FastScanner scanner = new FastScanner();
public static void main(String[] args) {
new Main().solve();
}
private void solve() {
int n = scanner.nextInt();
Map<Integer, Integer> cnt = new HashMap<>();
for (int i = 0; i < n; i++) {
String s = scanner.nextLine();
LinkedList<Character> st = new LinkedList<>();
for (char c : s.toCharArray()) {
if (c == ')' && !st.isEmpty() && st.getLast() == '(') {
st.pollLast();
continue;
}
st.addLast(c);
}
int t = st.size();
Set<Character> set = new HashSet<>(st);
if (set.size() > 1) {
continue;
}
if (set.isEmpty()) {
cnt.put(0, cnt.getOrDefault(0, 0) + 1);
continue;
}
if (st.getLast() == '(') {
cnt.put(t, cnt.getOrDefault(t, 0) + 1);
} else {
cnt.put(-t, cnt.getOrDefault(-t, 0) + 1);
}
}
long ans = 0;
for (int next : cnt.keySet()) {
if (next == 0) {
ans += (long) cnt.get(next) * (cnt.get(next) - 1) + cnt.get(next);
} else if (next > 0) {
int t = next * -1;
if (cnt.containsKey(t)) {
ans += (long) cnt.get(next) * cnt.get(t);
}
}
}
System.out.print(ans);
}
class FastScanner {
BufferedReader reader;
StringTokenizer tokenizer;
FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in), 32768);
tokenizer = null;
}
String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
Integer[] nextA(int n) {
Integer a[] = new Integer[n];
for (int i = 0; i < n; i++) {
a[i] = scanner.nextInt();
}
return a;
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
public String nextLine() {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
} | linear | 990_C. Bracket Sequences Concatenation Problem | CODEFORCES |
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;
public class Practice {
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
sc.nextLine();
String s=sc.nextLine();
//System.out.println(s);
char c[]=s.toCharArray();
ArrayList a =new ArrayList();
for(int i=0;i<c.length;i++)
{
//System.out.println(c[i]);
a.add(c[i]);
}
int x=Collections.frequency(a,'0' );
int y=Collections.frequency(a,'1');
//System.out.println(x+ " "+y );
if(y==0 || y==1)
{
System.out.println(s);
}
else
{
if(y>=2)
{
String s1="1";
for(int i=0;i<x;i++)
{
s1=s1+"0";
}
System.out.println(s1);
}
}
}
} | linear | 976_A. Minimum Binary Number | CODEFORCES |
import java.util.*;
import java.io.*;
public class Waw{
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] a = new long[n];
for(int i=0;i<n;i++) a[i] = sc.nextLong();
long[] p = new long[n];
p[n-1] = a[n-1];
for(int i=n-2;i>=0;i--){
if(a[i]<p[i+1]) p[i] = p[i+1]-1;
else p[i] = a[i];
}
long max = p[0];
long res = p[0] - a[0];
for(int i=1;i<n;i++){
if(max < p[i]) max = p[i];
res += max - a[i];
}
System.out.println(res);
}
} | linear | 924_C. Riverside Curio | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Code {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
HashMap<Double,Integer>h = new HashMap<>();
double [] temp = new double[n];
int m = 0;
for(int i=0;i<n;i++) {
String l = br.readLine();
int[] x = new int[4];
int k=0;
boolean t = false;
for(int j=0;j<l.length();j++) {
if(l.charAt(j)=='(' || l.charAt(j)=='+' || l.charAt(j)==')' || l.charAt(j)=='/')
x[k++] = j;
}
double a = Integer.parseInt(l.substring(x[0]+1,x[1]));
double b = Integer.parseInt(l.substring(x[1]+1, x[2]));
double c = Integer.parseInt(l.substring(x[3]+1));
temp[m++] = (a+b)/c;
//System.out.print((a+b)/c + " ");
if(h.containsKey((a+b)/c))
h.put((a+b)/c, h.get((a+b)/c)+1);
else
h.put((a+b)/c, 1);
}
//System.out.println(h);
for(int i=0;i<n;i++) {
System.out.print(h.get(temp[i]) + " ");
}
}
}
| linear | 958_D1. Hyperspace Jump (easy) | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.OutputStream;
import java.util.List;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastReader in = new FastReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, FastReader in, PrintWriter out) {
Debug debug = new Debug();
int n = in.nextInt();
int K = in.nextInt();
List<Integer>[] g = GraphUtils.nextU(in, n, n - 1, true);
int[] center = GraphUtils.getCenter(g);
if (center.length == 2) {
out.println("No");
return;
}
int[][] pars = GraphUtils.parents3(g, center[0]);
int[] par = pars[0], ord = pars[1], depth = pars[2];
int[] deg = new int[n];
for (int i = 0; i < n; ++i) deg[i] = g[i].size();
if (deg[center[0]] < 3) {
out.println("No");
return;
}
// all leaves depth = K
for (int i = 0; i < n; ++i) {
if (deg[i] == 1) {
if (depth[i] != K) {
out.println("No");
return;
}
} else if (i != center[0]) {
if (deg[i] < 4) {
out.println("No");
return;
}
}
}
out.println("Yes");
}
}
static class GraphUtils {
public static List<Integer>[] nextU(FastReader in, int n, int m, boolean oneIndexed) {
int diff = oneIndexed ? 1 : 0;
List<Integer>[] g = new List[n];
for (int i = 0; i < n; ++i) g[i] = new ArrayList<>();
for (int i = 0; i < m; ++i) {
int u = in.nextInt() - diff;
int v = in.nextInt() - diff;
g[u].add(v);
g[v].add(u);
}
return g;
}
public static int[][] parents3(List<Integer>[] g, int root) {
int n = g.length;
int[] par = new int[n];
ArrayUtils.fill(par, -1);
int[] depth = new int[n];
depth[0] = 0;
int[] q = new int[n];
q[0] = root;
for (int p = 0, r = 1; p < r; p++) {
int cur = q[p];
for (int nex : g[cur]) {
if (par[cur] != nex) {
q[r++] = nex;
par[nex] = cur;
depth[nex] = depth[cur] + 1;
}
}
}
return new int[][]{par, q, depth};
}
public static int[] getCenter(List<Integer>[] g) {
int n = g.length;
int[] q = new int[n];
int[] deg = new int[n];
int p = 0;
for (int i = 0; i < n; i++) {
deg[i] = g[i].size();
if (g[i].size() <= 1) { // < for n=1
q[p++] = i;
}
}
int bound = p == n ? 0 : p;
for (int z = 0; z < p; z++) {
if (bound == z && p < n) bound = p;
int cur = q[z];
deg[cur]--;
for (int e : g[cur]) {
if (--deg[e] == 1) q[p++] = e;
}
}
assert p == n;
assert bound >= n - 2 && bound < n;
if (bound == n - 2) {
return new int[]{q[n - 2], q[n - 1]};
} else {
return new int[]{q[n - 1]};
}
}
}
static class FastReader {
private InputStream stream;
private byte[] buf = new byte[8192];
private int curChar;
private int pnumChars;
public FastReader(InputStream stream) {
this.stream = stream;
}
private int pread() {
if (pnumChars == -1) {
throw new InputMismatchException();
}
if (curChar >= pnumChars) {
curChar = 0;
try {
pnumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (pnumChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = pread();
while (isSpaceChar(c))
c = pread();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = pread();
}
int res = 0;
do {
if (c == ',') {
c = pread();
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = pread();
} while (!isSpaceChar(c));
return res * sgn;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
static class ArrayUtils {
public static void fill(int[] array, int value) {
Arrays.fill(array, value);
}
}
static class Debug {
PrintWriter out;
boolean oj;
boolean system;
long timeBegin;
Runtime runtime;
public Debug(PrintWriter out) {
oj = System.getProperty("ONLINE_JUDGE") != null;
this.out = out;
this.timeBegin = System.currentTimeMillis();
this.runtime = Runtime.getRuntime();
}
public Debug() {
system = true;
oj = System.getProperty("ONLINE_JUDGE") != null;
OutputStream outputStream = System.out;
this.out = new PrintWriter(outputStream);
this.timeBegin = System.currentTimeMillis();
this.runtime = Runtime.getRuntime();
}
}
}
| linear | 1067_B. Multihedgehog | CODEFORCES |
import java.io.*;
import java.util.Arrays;
import java.util.StringJoiner;
import java.util.StringTokenizer;
import java.util.function.Function;
public class Main {
static String S;
public static void main(String[] args) {
FastScanner sc = new FastScanner(System.in);
S = sc.next();
System.out.println(solve());
}
static int solve() {
int ans = -1;
int time = 1;
int n = S.length();
for (int i = 1; i < n*2; i++) {
if( S.charAt((i-1)%n) != S.charAt(i%n) ) {
time++;
} else {
ans = Math.max(time, ans);
time = 1;
}
}
ans = Math.max(time, ans);
if( ans == n*2 ) {
return n;
} else {
return ans;
}
}
@SuppressWarnings("unused")
static class FastScanner {
private BufferedReader reader;
private StringTokenizer tokenizer;
FastScanner(InputStream in) {
reader = new BufferedReader(new InputStreamReader(in));
tokenizer = null;
}
String next() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
String nextLine() {
if (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
return reader.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken("\n");
}
long nextLong() {
return Long.parseLong(next());
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
int[] nextIntArray(int n, int delta) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt() + delta;
return a;
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
}
static <A> void writeLines(A[] as, Function<A, String> f) {
PrintWriter pw = new PrintWriter(System.out);
for (A a : as) {
pw.println(f.apply(a));
}
pw.flush();
}
static void writeLines(int[] as) {
PrintWriter pw = new PrintWriter(System.out);
for (int a : as) pw.println(a);
pw.flush();
}
static void writeLines(long[] as) {
PrintWriter pw = new PrintWriter(System.out);
for (long a : as) pw.println(a);
pw.flush();
}
static int max(int... as) {
int max = Integer.MIN_VALUE;
for (int a : as) max = Math.max(a, max);
return max;
}
static int min(int... as) {
int min = Integer.MAX_VALUE;
for (int a : as) min = Math.min(a, min);
return min;
}
static void debug(Object... args) {
StringJoiner j = new StringJoiner(" ");
for (Object arg : args) {
if (arg instanceof int[]) j.add(Arrays.toString((int[]) arg));
else if (arg instanceof long[]) j.add(Arrays.toString((long[]) arg));
else if (arg instanceof double[]) j.add(Arrays.toString((double[]) arg));
else if (arg instanceof Object[]) j.add(Arrays.toString((Object[]) arg));
else j.add(arg.toString());
}
System.err.println(j.toString());
}
}
| linear | 1025_C. Plasticine zebra | CODEFORCES |
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String str[]) throws Exception{
Scanner sc = new Scanner(System.in);
BufferedWriter output = new BufferedWriter(
new OutputStreamWriter(System.out));
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
if((n%2==0 && Math.pow(n/2,0.5)%1.0==0) || (n%4==0 && Math.pow(n/4,0.5)%1.0==0) ) output.write("YES\n");
else {
output.write("NO\n");
}
}
output.flush();
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
//package global14;
import java.io.*;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.Queue;
public class B {
InputStream is;
FastWriter out;
String INPUT = "";
void solve()
{
for(int T = ni();T > 0;T--)go();
}
void go()
{
int n = ni();
if(n % 2 == 0){
int u = (int)Math.sqrt(n/2);
if(u*u == n/2){
out.println("YES");
return;
}
}
if(n % 4 == 0){
int u = (int)Math.sqrt(n/4);
if(u*u == n/4){
out.println("YES");
return;
}
}
out.println("NO");
}
void run() throws Exception
{
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new FastWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception { new B().run(); }
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private long[] nal(int n)
{
long[] a = new long[n];
for(int i = 0;i < n;i++)a[i] = nl();
return a;
}
private char[][] nm(int n, int m) {
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[][] nmi(int n, int m) {
int[][] map = new int[n][];
for(int i = 0;i < n;i++)map[i] = na(m);
return map;
}
private int ni() { return (int)nl(); }
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
public static class FastWriter
{
private static final int BUF_SIZE = 1<<13;
private final byte[] buf = new byte[BUF_SIZE];
private final OutputStream out;
private int ptr = 0;
private FastWriter(){out = null;}
public FastWriter(OutputStream os)
{
this.out = os;
}
public FastWriter(String path)
{
try {
this.out = new FileOutputStream(path);
} catch (FileNotFoundException e) {
throw new RuntimeException("FastWriter");
}
}
public FastWriter write(byte b)
{
buf[ptr++] = b;
if(ptr == BUF_SIZE)innerflush();
return this;
}
public FastWriter write(char c)
{
return write((byte)c);
}
public FastWriter write(char[] s)
{
for(char c : s){
buf[ptr++] = (byte)c;
if(ptr == BUF_SIZE)innerflush();
}
return this;
}
public FastWriter write(String s)
{
s.chars().forEach(c -> {
buf[ptr++] = (byte)c;
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 FastWriter write(int x)
{
if(x == Integer.MIN_VALUE){
return write((long)x);
}
if(ptr + 12 >= BUF_SIZE)innerflush();
if(x < 0){
write((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 FastWriter write(long x)
{
if(x == Long.MIN_VALUE){
return write("" + x);
}
if(ptr + 21 >= BUF_SIZE)innerflush();
if(x < 0){
write((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 FastWriter write(double x, int precision)
{
if(x < 0){
write('-');
x = -x;
}
x += Math.pow(10, -precision)/2;
// if(x < 0){ x = 0; }
write((long)x).write(".");
x -= (long)x;
for(int i = 0;i < precision;i++){
x *= 10;
write((char)('0'+(int)x));
x -= (int)x;
}
return this;
}
public FastWriter writeln(char c){
return write(c).writeln();
}
public FastWriter writeln(int x){
return write(x).writeln();
}
public FastWriter writeln(long x){
return write(x).writeln();
}
public FastWriter writeln(double x, int precision){
return write(x, precision).writeln();
}
public FastWriter write(int... xs)
{
boolean first = true;
for(int x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter write(long... xs)
{
boolean first = true;
for(long x : xs) {
if (!first) write(' ');
first = false;
write(x);
}
return this;
}
public FastWriter writeln()
{
return write((byte)'\n');
}
public FastWriter writeln(int... xs)
{
return write(xs).writeln();
}
public FastWriter writeln(long... xs)
{
return write(xs).writeln();
}
public FastWriter writeln(char[] line)
{
return write(line).writeln();
}
public FastWriter writeln(char[]... map)
{
for(char[] line : map)write(line).writeln();
return this;
}
public FastWriter writeln(String s)
{
return write(s).writeln();
}
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");
}
}
public FastWriter print(byte b) { return write(b); }
public FastWriter print(char c) { return write(c); }
public FastWriter print(char[] s) { return write(s); }
public FastWriter print(String s) { return write(s); }
public FastWriter print(int x) { return write(x); }
public FastWriter print(long x) { return write(x); }
public FastWriter print(double x, int precision) { return write(x, precision); }
public FastWriter println(char c){ return writeln(c); }
public FastWriter println(int x){ return writeln(x); }
public FastWriter println(long x){ return writeln(x); }
public FastWriter println(double x, int precision){ return writeln(x, precision); }
public FastWriter print(int... xs) { return write(xs); }
public FastWriter print(long... xs) { return write(xs); }
public FastWriter println(int... xs) { return writeln(xs); }
public FastWriter println(long... xs) { return writeln(xs); }
public FastWriter println(char[] line) { return writeln(line); }
public FastWriter println(char[]... map) { return writeln(map); }
public FastWriter println(String s) { return writeln(s); }
public FastWriter println() { return writeln(); }
}
public void trnz(int... o)
{
for(int i = 0;i < o.length;i++)if(o[i] != 0)System.out.print(i+":"+o[i]+" ");
System.out.println();
}
// print ids which are 1
public void trt(long... o)
{
Queue<Integer> stands = new ArrayDeque<>();
for(int i = 0;i < o.length;i++){
for(long x = o[i];x != 0;x &= x-1)stands.add(i<<6|Long.numberOfTrailingZeros(x));
}
System.out.println(stands);
}
public void tf(boolean... r)
{
for(boolean x : r)System.out.print(x?'#':'.');
System.out.println();
}
public void tf(boolean[]... b)
{
for(boolean[] r : b) {
for(boolean x : r)System.out.print(x?'#':'.');
System.out.println();
}
System.out.println();
}
public void tf(long[]... b)
{
if(INPUT.length() != 0) {
for (long[] r : b) {
for (long x : r) {
for (int i = 0; i < 64; i++) {
System.out.print(x << ~i < 0 ? '#' : '.');
}
}
System.out.println();
}
System.out.println();
}
}
public void tf(long... b)
{
if(INPUT.length() != 0) {
for (long x : b) {
for (int i = 0; i < 64; i++) {
System.out.print(x << ~i < 0 ? '#' : '.');
}
}
System.out.println();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
//2021 global14
// jzzhao
import java.util.*;
import java.io.*;
public class Solution {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
int tc = sc.ni();
for (int rep = 0; rep < tc; rep++) {
pw.println(solve(sc,pw));
//solve(sc,pw);
}
pw.close();
}
/*
FS Methods:
- next(): next element in string
- nextLine(): nextline in string
- ni(): next Integer
- nd(): next Double
- nl(): next Long
- intArray(n):
- longArray(n):
- to2di(m,n):
- to2dl(m,n):
*/
public static String solve(FastScanner sc, PrintWriter pw) {
int n = sc.ni();
long cur1 = 2;
long cur2 = 4;
long block = 2;
long block2 = 4;
int tmp = 3;
while(cur1<=n||cur2<=n){
if(cur1==n||cur2==n){
return "YES";
}
if(cur1<n){
cur1+=block*tmp;
}
if(cur2<n){
cur2+=block2*tmp;
}
tmp+=2;
}
return "NO";
//return "NO";
}
/*
- The following are helping method so pls do not do anything to them.
*/
// public static int[][] to2d(Scanner scanner, int m, int n){
// int[][] ans = new int[m][n];
// for(int i = 0;i<m;i++){
// String[] r = scanner.nextLine().split("[ ]");
// for(int j = 0;j<n;j++){
// ans[i][j] = stoi(r[j]);
// }
// }
// return ans;
// }
// public static int[] toArray(Scanner scanner, int m){
// int[] ans = new int[m];
// String[] r = scanner.nextLine().split("[ ]");
// for(int i = 0;i<m;i++){
// ans[i] = stoi(r[i]);
// }
// return ans;
// }
public static void printArr(PrintWriter pw,int[] a){
for(int i = 0;i<a.length;i++){
pw.print(a[i]);
if(i!=a.length-1){
pw.print(" ");
}
}
pw.println();
}
public static void print2d(PrintWriter pw,int[][] a){
for(int j=0;j<a.length;j++){
for(int i = 0;i<a[j].length;i++){
pw.print(a[j][i]);
if(i!=a[j].length-1){
pw.print(" ");
}
}
pw.println(" ");
}
pw.println();
}
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);
}
public static int stoi(String s){
return Integer.parseInt(s);
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in), 32768);
st = null;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
int[] intArray(int N) {
int[] ret = new int[N];
for (int i = 0; i < N; i++)
ret[i] = ni();
return ret;
}
int[][] to2di(int m, int n){
int[][] ans = new int[m][n];
for(int i = 0;i<m;i++){
String[] r = nextLine().split("[ ]");
for(int j = 0;j<n;j++){
ans[i][j] = Integer.parseInt(r[j]);
}
}
return ans;
}
long[][] to2dl(int m, int n){
long[][] ans = new long[m][n];
for(int i = 0;i<m;i++){
String[] r = nextLine().split("[ ]");
for(int j = 0;j<n;j++){
ans[i][j] = Long.parseLong(r[j]);
}
}
return ans;
}
long nl() {
return Long.parseLong(next());
}
long[] longArray(int N) {
long[] ret = new long[N];
for (int i = 0; i < N; i++)
ret[i] = nl();
return ret;
}
double nd() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.*;
public class AA{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=0;
if(sc.hasNextInt()) {
t=sc.nextInt();
}
while(t>0) {
t--;
int n=sc.nextInt();
String ans="NO";
if(n%2==0) {
int p=n/2;
if(Math.ceil(Math.sqrt((double)p)) == Math.floor(Math.sqrt((double)p))){
ans="YES";
}
else {
if(n%4==0) {
p=n/4;
if(Math.ceil(Math.sqrt((double)p)) == Math.floor(Math.sqrt((double)p))){
ans="YES";
}
}
}
}
System.out.println(ans);
}
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
//package Round14Global;
import java.util.*;
import java.io.*;
public class phoenixandpuzzle {
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();
HashSet<Integer> dict = new HashSet<Integer>();
int pointer = 1;
int area = 0;
while(area >= 0) {
area = (pointer * pointer) * 2;
dict.add(area);
pointer ++;
}
pointer = 1;
area = 0;
while(area >= 0) {
area = (pointer * pointer) * 4;
dict.add(area);
pointer ++;
}
while(t-- > 0) {
int n = Integer.parseInt(fin.readLine());
if(dict.contains(n)) {
fout.append("YES\n");
}
else {
fout.append("NO\n");
}
}
System.out.print(fout);
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
//بسم الله الرحمن الرحيم
import java.util.*;
import java.lang.*;
import java.nio.*;
import java.io.*;
public class as {
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[100000000]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
/////////////////////////////////////////////////////////////
public static void main(String[] args) throws Exception {
Reader sc=new Reader();
StringBuilder finalAnswer=new StringBuilder();
//finalAnswer.append(1).append('\n');
int t=sc.nextInt();
while(t-->0) {
int count=0;
int n=sc.nextInt();
if(n==2 || n==4) {
finalAnswer.append("YES").append('\n');
count++;
}
if(n%2==0 && count==0){
n/=2;
if((int)Math.sqrt(n)*(int)Math.sqrt(n)==n) {
finalAnswer.append("YES").append('\n');
count++;
}
else {
n*=2;
}
}
if(n%4==0 && count==0) {
n/=4;
if((int)Math.sqrt(n)*(int)Math.sqrt(n)==n) {
finalAnswer.append("YES").append('\n');
count++;
}
}
if(count==0){
finalAnswer.append("NO").append('\n');
}
}
System.out.println(finalAnswer);
}
/////////////////////////////////////////////////////////
public static long gcd(long a, long b)
{
while (b > 0)
{
long temp = b;
b = a % b; // % is remainder
a = temp;
}
return a;
}
public static long lcm(long a, long b)
{
return a * (b / gcd(a, b));
}
static boolean containsDigit(int number, int digit)
{
while (number > 0)
{
int curr_digit = number % 10;
if (curr_digit == digit) return true;
number /= 10;
}
return false;
}
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;
}
void sieveOfEratosthenes(int n)
{
// Create a boolean array
// "prime[0..n]" and
// initialize all entries
// it as true. A value in
// prime[i] will finally be
// false if i is Not a
// prime, else true.
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] == true)
{
// 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] == true)
System.out.print(i + " ");
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.PrintWriter;
import java.util.Scanner;
public class Main {
static Scanner sc;
static PrintWriter out;
public static void main(String[] args) {
sc = new Scanner(System.in);
out = new PrintWriter(System.out);
int t = 1;
if (true) {
t = sc.nextInt();
}
for(int i=0; i<t; i++) {
new Main().solve();
}
out.flush();
}
public void solve() {
long n = sc.nextInt();
if(n % 2 == 1) {
out.println("NO");
return;
}
for(long i=1; i*i*2 <= n; i++) {
if(i * i * 2 == n || i * i * 4 == n) {
out.println("YES");
return;
}
}
out.println("NO");
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
///package round_global_14;
import java.io.*;
import java.util.*;
public class B {
static class InputReader {
BufferedReader buffreader;
StringTokenizer strtokenizer;
public InputReader(InputStream inputstr) {
buffreader = new BufferedReader(new InputStreamReader(inputstr), 1000000);strtokenizer = null; }
//////////////////////////////////////////////////////////////////////////////////////////
String next() {
while (strtokenizer == null || !strtokenizer.hasMoreTokens()) {
try { strtokenizer = new StringTokenizer(buffreader.readLine()); }
catch (IOException e) { throw new RuntimeException(e); }
}
return strtokenizer.nextToken(); }
///////////////////////////////////////////////////////////////////////////////////////////
public int nextInt() { return Integer.parseInt(next()); }
public long nextLong() { return Long.parseLong(next()); }
public double nextDouble() { return Double.parseDouble(next()); }
public int[] nextIntArr(int n){
int[] arr=new int[n];
for (int i=0;i<n;i++){arr[i]=nextInt();}
return arr;}
public long[] nextLongArr(int n){
long[] arr=new long[n];
for (int i=0;i<n;i++){arr[i]=nextLong();}
return arr;}
public String[] nextStringArr(int n){
String[] arr=new String[n];
for (int i=0;i<n;i++){arr[i]=next();}
return arr;}
}
static InputReader r = new InputReader(System.in);
static PrintWriter pw = new PrintWriter(System.out);
public static void main(String[] args){
int t=r.nextInt();
for (int v=0;v<t;v++){
int n=r.nextInt();
if (n%2==1){
pw.println("NO");
}
else{
int x=n/2;
boolean check=false;
for (int i=1;i<=(int)Math.sqrt(x)+1;i++){
if (i*i*2==x||i*i==x){
pw.println("YES");check=true;break;
}
}
if (!check){
pw.println("NO");
}
}
}
pw.close();
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.*;
public class B {
int mod=1000_000_007;
public static void main(String[] args) throws Exception {
PrintWriter out=new PrintWriter(System.out);
FastScanner fs=new FastScanner();
int t=fs.nextInt();
while(t-->0) {
double n=fs.nextInt();
if(isp(n/2)||isp(n/4)) {
System.out.println("YES");
}
else System.out.println("NO");
}
}
static boolean isp(double n) {
if(n==0) return false;
double a=Math.ceil(Math.sqrt(n));
double b=Math.floor(Math.sqrt(n));
return a==b;
}
static void mysort(long[] a) {
//suffle
int n=a.length;
Random r=new Random();
for (int i=0; i<a.length; i++) {
int oi=r.nextInt(n);
long temp=a[i];
a[i]=a[oi];
a[oi]=temp;
}
//then sort
Arrays.sort(a);
}
// Use this to input code since it is faster than a Scanner
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());
}
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Math.round;
import static java.lang.Math.sqrt;
import static java.lang.System.exit;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class B {
static long sqr(long a) {
return a * a;
}
static void solve() throws Exception {
int tests = scanInt();
for (int test = 0; test < tests; test++) {
int n = scanInt();
out.println(n == 2 * sqr(round(sqrt(n / 2))) || n == 4 * sqr(round(sqrt(n / 4))) ? "YES" : "NO");
}
}
static int scanInt() throws IOException {
return parseInt(scanString());
}
static long scanLong() throws IOException {
return parseLong(scanString());
}
static String scanString() throws IOException {
while (tok == null || !tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
static BufferedReader in;
static PrintWriter out;
static StringTokenizer tok;
public static void main(String[] args) {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
exit(1);
}
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
import java.util.*;
public class Solution {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
String nextLine() throws IOException {
return br.readLine();
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
float nextFloat() {
return Float.parseFloat(nextToken());
}
}
static FastReader f = new FastReader();
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st;
static StringBuilder sb = new StringBuilder();
static long[] fact;
static int[] inputArray(int n) throws IOException {
int[] a = new int[n];
for(int i = 0 ; i < n ; i++) {
// a[i] = (int) (Math.random()*1e5 + 1);
a[i] = f.nextInt();
}
return a;
}
static long[] inputLongArray(int n) throws IOException {
long[] a = new long[n];
for(int i = 0 ; i < n ; i++) {
a[i] = f.nextLong();
// a[i] = (long) (Math.random() * 1e9 + 1);
}
return a;
}
static long gcd(long a , long b) {
if(a == 0 || b == 0) {
return Math.max(a , b);
}
//System.out.println("a - " + a + " b - " + b);
if(a % b == 0) {
return b;
}
return gcd(b , a % b);
}
static void initializeFact() {
fact = new long[MAX_N];
for(int i = 0 ; i < fact.length ; i++) {
if(i == 0) {
fact[i] = 1;
}
else {
fact[i] = fact[i-1] * i % mod;
}
}
}
static long longModulus(long x , long m) {
if(x < m) {
return x;
}
long d = x / m;
return x - d * m;
}
static BitSet sieveOfEratosthenes(int n)
{
BitSet isPrime = new BitSet(n+1);
isPrime.set(0, n + 1);
isPrime.set(0);
isPrime.set(1);
for(int i = 2; i * i <= n ; i++)
{
if(isPrime.get(i))
for(int j = i * i ; j <= n; j += i)
isPrime.clear(j);
}
return isPrime;
}
static long moduloInversePrime(long a) {
//System.out.println("modulo inverse of " + a + " -> " + ans);
return modPow(a , mod - 2);
}
static long mult(long a, long b)
{
return (a * b % mod);
}
static long modPow(long a, int step)
{
long ans = 1;
while(step != 0)
{
if((step & 1) != 0)
ans = mult(ans , a);
a = mult(a , a);
step >>= 1;
}
return ans;
}
static int query(int l , int r) {
System.out.println("? " + l + " " + r);
System.out.flush();
return f.nextInt();
}
static int sum(int n) {
return n * (n + 1) / 2;
}
private static final int mod = (int) (1e9 + 7);
static int MAX_N = (int) Math.sqrt(1e9);
public static void main(String[] args) throws IOException {
int test = f.nextInt();
// System.out.println(MAX_N);
TreeSet<Integer> set = new TreeSet<>();
for(int i = 1 ; i <= MAX_N ; i++) {
set.add(i*i*2);
set.add(i*i*4);
}
// System.out.println(set);
for(int t = 1 ; t <= test ; t++) {
int n = f.nextInt();
if(set.contains(n)) {
sb.append("YES").append("\n");
}
else {
sb.append("NO").append("\n");
}
}
System.out.println(sb);
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class p2 {
final static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
private static String ss() throws IOException {return br.readLine();}
private static int stoi(String x) {return Integer.parseInt(x);}
private static int si() throws IOException {return stoi(ss());}
private static long sl() throws IOException {return Long.parseLong(ss());}
private static int[] sia(int len) throws IOException {
int[] ret = new int[len];
final StringTokenizer st = new StringTokenizer(ss());
for (int i = 0; i < len; ++i) {ret[i] = stoi(st.nextToken());}
return ret;
}
private static long[] sla(int len) throws IOException {
long[] ret = new long[len];
final StringTokenizer st = new StringTokenizer(ss());
for (int i = 0; i < len; ++i) {ret[i] = Long.parseLong(st.nextToken());}
return ret;
}
public static void main (final String[] args) throws IOException {
//goal is to always be higher
Set<Integer> poss = new HashSet<>();
for (int i = 1; 2 * (i*i) <= 1000000000; ++i) {
poss.add(2 * (i*i));
poss.add(4 * (i*i));
}
int t = si();
for (int i = 0; i < t; ++i) {
int n = si();
if (poss.contains(n)) System.out.println("YES");
else System.out.println("NO");
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
public class Main{
static long MOD = 1_000_000_007L;
//static long MOD = 998_244_353L;
//static long MOD = 1_000_000_033L;
static long inv2 = (MOD + 1) / 2;
static int[][] dir = new int[][]{{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
static long lMax = 0x3f3f3f3f3f3f3f3fL;
static int iMax = 0x3f3f3f3f;
static HashMap <Long, Long> memo = new HashMap();
static MyScanner sc = new MyScanner();
//static ArrayList <Integer> primes;
static int nn = 300000;
static long[] pow2;
static long [] fac;
static long [] pow;
static long [] inv;
static long [] facInv;
static int[] base;
static int[] numOfDiffDiv;
static int[] numOfDiv;
static ArrayList <Integer> primes;
//static int[] primes;
static int ptr = 0;
static boolean[] isPrime;
//-----------PrintWriter for faster output---------------------------------
public static PrintWriter out;
public static void main(String[] args) {
out = new PrintWriter(new BufferedOutputStream(System.out));
// Start writing your solution here. -------------------------------------
/*fac = new long[nn + 1];
fac[1] = 1;
for(int i = 2; i <= nn; i++)
fac[i] = fac[i - 1] * i % MOD;*/
/*pow2 = new long[nn + 1];
pow2[0] = 1L;
for(int i = 1; i <= nn; i++)
pow2[i] = pow2[i - 1] * 2L;*/
/*inv = new long[nn + 1];
inv[1] = 1;
for (int i = 2; i <= nn; ++i)
inv[i] = (MOD - MOD / i) * inv[(int)(MOD % i)] % MOD;*/
/*facInv = new long[nn + 1];
facInv[0] = facInv[1] = 1;
for (int i = 2; i <= nn; ++i)
facInv[i] = facInv[i - 1] * inv[i] % MOD;*/
/*numOfDiffDiv = new int[nn + 1];
for(int i = 2; i <= nn; i++)
if(numOfDiffDiv[i] == 0)
for(int j = i; j <= nn; j += i)
numOfDiv[j] ++;*/
/*numOfDiv = new int[nn + 1];
numOfDiv[1] = 1;
for(int i = 2; i <= nn; i++) {
for(int j = 2; j * j <= i; j++) {
if(i % j == 0) {
numOfDiv[i] = numOfDiv[i / j] + 1;
break;
}
}
}*/
//primes = sieveOfEratosthenes(100001);
/*
int t = 1;
//t = sc.ni();
while(t-- > 0) {
//boolean res = solve();
//out.println(res ? "YES" : "NO");
long res = solve();
out.println(res);
}*/
int t = 1, tt = 0;
t = sc.ni();
for(int i = 1; i <40000; i++) squares.add(i * i);
while(tt++ < t) {
boolean res = solve();
//out.println("Case #" + tt + ": " + res);
out.println(res ? "YES" : "NO");
}
out.close();
}
static HashSet <Integer> squares = new HashSet();
static boolean solve() {
/*String s = sc.nextLine();
char[] c = s.toCharArray();
int n = c.length;*/
//int n = sc.ni();
//long[] a = new long[n];
//for(int i = 0; i < n; i++) a[i] = sc.nl();
long res = 0;
int n = sc.ni();
if(n % 2 == 0 && squares.contains(n / 2)) return true;
if(n % 4 == 0 && squares.contains(n / 4)) return true;
return false;
}
// edges to adjacency list by uwi
public static int[][] packU(int n, int[] from, int[] to) {
return packU(n, from, to, from.length);
}
public static int[][] packU(int n, int[] from, int[] to, int sup) {
int[][] g = new int[n][];
int[] p = new int[n];
for (int i = 0; i < sup; i++) p[from[i]]++;
for (int i = 0; i < sup; i++) p[to[i]]++;
for (int i = 0; i < n; i++) g[i] = new int[p[i]];
for (int i = 0; i < sup; i++) {
g[from[i]][--p[from[i]]] = to[i];
g[to[i]][--p[to[i]]] = from[i];
}
return g;
}
// tree diameter by uwi
public static int[] diameter(int[][] g) {
int n = g.length;
int f0 = -1, f1 = -1, d01 = -1;
int[] q = new int[n];
boolean[] ved = new boolean[n];
{
int qp = 0;
q[qp++] = 0; ved[0] = true;
for(int i = 0;i < qp;i++){
int cur = q[i];
for(int e : g[cur]){
if(!ved[e]){
ved[e] = true;
q[qp++] = e;
continue;
}
}
}
f0 = q[n-1];
}
{
int[] d = new int[n];
int qp = 0;
Arrays.fill(ved, false);
q[qp++] = f0; ved[f0] = true;
for(int i = 0;i < qp;i++){
int cur = q[i];
for(int e : g[cur]){
if(!ved[e]){
ved[e] = true;
q[qp++] = e;
d[e] = d[cur] + 1;
continue;
}
}
}
f1 = q[n-1];
d01 = d[f1];
}
return new int[]{d01, f0, f1};
}
public static long c(int n, int k) {
return (fac[n] * facInv[k] % MOD) * facInv[n - k] % MOD;
}
// SegmentTree range min/max query by uwi
public static class SegmentTreeRMQ {
public int M, H, N;
public int[] st;
public SegmentTreeRMQ(int n)
{
N = n;
M = Integer.highestOneBit(Math.max(N-1, 1))<<2;
H = M>>>1;
st = new int[M];
Arrays.fill(st, 0, M, Integer.MAX_VALUE);
}
public SegmentTreeRMQ(int[] a)
{
N = a.length;
M = Integer.highestOneBit(Math.max(N-1, 1))<<2;
H = M>>>1;
st = new int[M];
for(int i = 0;i < N;i++){
st[H+i] = a[i];
}
Arrays.fill(st, H+N, M, Integer.MAX_VALUE);
for(int i = H-1;i >= 1;i--)propagate(i);
}
public void update(int pos, int x)
{
st[H+pos] = x;
for(int i = (H+pos)>>>1;i >= 1;i >>>= 1)propagate(i);
}
private void propagate(int i)
{
st[i] = Math.min(st[2*i], st[2*i+1]);
}
public int minx(int l, int r){
int min = Integer.MAX_VALUE;
if(l >= r)return min;
while(l != 0){
int f = l&-l;
if(l+f > r)break;
int v = st[(H+l)/f];
if(v < min)min = v;
l += f;
}
while(l < r){
int f = r&-r;
int v = st[(H+r)/f-1];
if(v < min)min = v;
r -= f;
}
return min;
}
public int min(int l, int r){ return l >= r ? 0 : min(l, r, 0, H, 1);}
private int min(int l, int r, int cl, int cr, int cur)
{
if(l <= cl && cr <= r){
return st[cur];
}else{
int mid = cl+cr>>>1;
int ret = Integer.MAX_VALUE;
if(cl < r && l < mid){
ret = Math.min(ret, min(l, r, cl, mid, 2*cur));
}
if(mid < r && l < cr){
ret = Math.min(ret, min(l, r, mid, cr, 2*cur+1));
}
return ret;
}
}
}
public static char[] rev(char[] a){char[] b = new char[a.length];for(int i = 0;i < a.length;i++)b[a.length-1-i] = a[i];return b;}
public static double dist(double a, double b){
return Math.sqrt(a * a + b * b);
}
public static long inv(long a){
return quickPOW(a, MOD - 2);
}
public class Interval {
int start;
int end;
public Interval(int start, int end) {
this.start = start;
this.end = end;
}
}
public static ArrayList<Integer> sieveOfEratosthenes(int n) {
boolean prime[] = new boolean[n + 1];
Arrays.fill(prime, true);
for (int p = 2; p * p <= n; p++) {
if (prime[p]) {
for (int i = p * 2; i <= n; i += p) {
prime[i] = false;
}
}
}
ArrayList<Integer> primeNumbers = new ArrayList<>();
for (int i = 2; i <= n; i++) {
if (prime[i]) {
primeNumbers.add(i);
}
}
return primeNumbers;
}
public static int lowerBound(int[] a, int v){ return lowerBound(a, 0, a.length, v); }
public static int lowerBound(int[] a, int l, int r, int v)
{
if(l > r || l < 0 || r > a.length)throw new IllegalArgumentException();
int low = l-1, high = r;
while(high-low > 1){
int h = high+low>>>1;
if(a[h] >= v){
high = h;
}else{
low = h;
}
}
return high;
}
public static int rlowerBound(int[] a, int v){ return lowerBound(a, 0, a.length, v); }
public static int rlowerBound(int[] a, int l, int r, int v)
{
if(l > r || l < 0 || r > a.length)throw new IllegalArgumentException();
int low = l-1, high = r;
while(high-low > 1){
int h = high+low>>>1;
if(a[h] <= v){
high = h;
}else{
low = h;
}
}
return high;
}
public static long C(int n, int m)
{
if(m == 0 || m == n) return 1l;
if(m > n || m < 0) return 0l;
long res = fac[n] * quickPOW((fac[m] * fac[n - m]) % MOD, MOD - 2) % MOD;
return res;
}
public static long quickPOW(long n, long m)
{
long ans = 1l;
while(m > 0)
{
if(m % 2 == 1)
ans = (ans * n) % MOD;
n = (n * n) % MOD;
m >>= 1;
}
return ans;
}
public static long quickPOW(long n, long m, long mod)
{
long ans = 1l;
while(m > 0)
{
if(m % 2 == 1)
ans = (ans * n) % mod;
n = (n * n) % mod;
m >>= 1;
}
return ans;
}
public static int gcd(int a, int b)
{
if(a % b == 0) return b;
return gcd(b, a % b);
}
public static long gcd(long a, long b)
{
if(a % b == 0) return b;
return gcd(b, a % b);
}
static class Randomized {
public static void shuffle(int[] data) {
shuffle(data, 0, data.length - 1);
}
public static void shuffle(int[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
int tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static void shuffle(long[] data) {
shuffle(data, 0, data.length - 1);
}
public static void shuffle(long[] data, int from, int to) {
to--;
for (int i = from; i <= to; i++) {
int s = nextInt(i, to);
long tmp = data[i];
data[i] = data[s];
data[s] = tmp;
}
}
public static int nextInt(int l, int r) {
return RandomWrapper.INSTANCE.nextInt(l, r);
}
}
static class RandomWrapper {
private Random random;
public static final RandomWrapper INSTANCE = new RandomWrapper(new Random());
public RandomWrapper() {
this(new Random());
}
public RandomWrapper(Random random) {
this.random = random;
}
public int nextInt(int l, int r) {
return random.nextInt(r - l + 1) + l;
}
}
//-----------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 ni() {
return Integer.parseInt(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;
}
}
//--------------------------------------------------------
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
import java.util.*;
import java.math.*;
import java.awt.Point;
public class Main {
//static final long MOD = 1000000007L;
//static final long MOD2 = 1000000009L;
static final long MOD = 998244353L;
//static final long INF = 500000000000L;
static final int INF = 1000000005;
static final int NINF = -1000000005;
//static final long NINF = -1000000000000000000L;
static FastScanner sc;
static PrintWriter pw;
static final int[][] dirs = {{-1,0},{1,0},{0,-1},{0,1}};
public static void main(String[] args) {
sc = new FastScanner();
pw = new PrintWriter(System.out);
int Q = sc.ni();
for (int q = 0; q < Q; q++) {
int N = sc.ni();
String ans = "NO";
if (N%2==0 && isSquare(N/2))
ans = "YES";
if (N%4==0 && isSquare(N/4))
ans = "YES";
pw.println(ans);
}
pw.close();
}
public static boolean isSquare(int x) {
int s = (int)Math.round(Math.sqrt(x));
return s*s==x;
}
public static void sort(int[] arr) {
Random rgen = new Random();
for (int i = 0; i < arr.length; i++) {
int r = rgen.nextInt(arr.length);
int temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
}
Arrays.sort(arr);
}
public static void sort(long[] arr) {
Random rgen = new Random();
for (int i = 0; i < arr.length; i++) {
int r = rgen.nextInt(arr.length);
long temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
}
Arrays.sort(arr);
}
//Sort an array (immune to quicksort TLE)
public static void sort(int[][] arr) {
Random rgen = new Random();
for (int i = 0; i < arr.length; i++) {
int r = rgen.nextInt(arr.length);
int[] temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
}
Arrays.sort(arr, new Comparator<int[]>() {
@Override
public int compare(int[] a, int[] b) {
return a[0]-b[0];
}
});
}
public static void sort(long[][] arr) {
Random rgen = new Random();
for (int i = 0; i < arr.length; i++) {
int r = rgen.nextInt(arr.length);
long[] temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
}
Arrays.sort(arr, new Comparator<long[]>() {
@Override
public int compare(long[] a, long[] b) {
if (a[0] > b[0])
return 1;
else if (a[0] < b[0])
return -1;
else
return 0;
//Ascending order.
}
});
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in), 32768);
st = null;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
int[][] graph(int N, int[][] edges) {
int[][] graph = new int[N][];
int[] sz = new int[N];
for (int[] e: edges) {
sz[e[0]] += 1;
sz[e[1]] += 1;
}
for (int i = 0; i < N; i++) {
graph[i] = new int[sz[i]];
}
int[] cur = new int[N];
for (int[] e: edges) {
graph[e[0]][cur[e[0]]] = e[1];
graph[e[1]][cur[e[1]]] = e[0];
cur[e[0]] += 1;
cur[e[1]] += 1;
}
return graph;
}
int[] intArray(int N, int mod) {
int[] ret = new int[N];
for (int i = 0; i < N; i++)
ret[i] = ni()+mod;
return ret;
}
long nl() {
return Long.parseLong(next());
}
long[] longArray(int N, long mod) {
long[] ret = new long[N];
for (int i = 0; i < N; i++)
ret[i] = nl()+mod;
return ret;
}
double nd() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) {
FastScanner fs=new FastScanner();
int T=fs.nextInt();
for (int tt=0; tt<T; tt++) {
int n=fs.nextInt();
boolean isEven=n%2==0;
while (n%2==0) n/=2;
if (isSquare(n) && isEven) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
static boolean isSquare(long n) {
long sqrt=(long) Math.sqrt(n);
for (int i=(int) Math.max(1, sqrt-5); i<=sqrt+5; i++)
if (i*i==n)
return true;
return false;
}
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());
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
public class B14G {
public static void main(String[] args) throws IOException {
init_io();
int t = nint();
while(t-- > 0) {
int N = nint();
if (N % 2 != 0) {
out.println("NO"); continue;
}
N /= 2;
int sqrt = (int)(Math.round(Math.sqrt(N)));
int sqrt2 = (int)(Math.round(Math.sqrt(N/2)));
if (sqrt * sqrt == N || sqrt2 * sqrt2 * 2 == N) {
out.println("YES");
}
else {
out.println("NO");
}
}
out.close();
}
static StreamTokenizer in;
static PrintWriter out;
static BufferedReader br;
static int nint() throws IOException {
in.nextToken();
return (int) in.nval;
}
static void init_io() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
in = new StreamTokenizer(br);
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
import java.util.*;
public class B {
static FastScanner fs;
public static void main(String[] args) {
fs=new FastScanner();
int t = fs.nextInt();
while (t-->0)
solve();
}
public static void solve() {
int n = fs.nextInt()*2;
int sq = (int)Math.sqrt(n);
int sq2 = (int)Math.sqrt(n/2);
if (sq*sq==n) System.out.println("YES");
else if (sq2*sq2==n/2.0 && sq2%2==0) System.out.println("YES");
else System.out.println("NO");
}
static int gcd(int a, int b) {
if (a==0) return b;
return gcd(b%a, a);
}
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 final Random random =new Random();
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());
}
}
static int[] reverse(int[] a) {
int n=a.length;
int[] res=new int[n];
for (int i=0; i<n; i++) res[i]=a[n-1-i];
return res;
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author lucasr
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
MyScanner in = new MyScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
BPhoenixAndPuzzle solver = new BPhoenixAndPuzzle();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class BPhoenixAndPuzzle {
public static MyScanner sc;
public static PrintWriter out;
public void solve(int testNumber, MyScanner sc, PrintWriter out) {
BPhoenixAndPuzzle.sc = sc;
BPhoenixAndPuzzle.out = out;
long n = sc.nextLong();
boolean can = true;
if (n % 2 != 0) can = false;
n /= 2;
while (n > 1 && n % 2 == 0) n /= 2;
long sq = Math.round(Math.pow(n, 0.5));
if (sq * sq != n) can = false;
out.println(can ? "YES" : "NO");
}
}
static class MyScanner {
private BufferedReader br;
private StringTokenizer tokenizer;
public MyScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(br.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.*;
import java.io.*;
import java.text.*;
public class CF_1515_B{
//SOLUTION BEGIN
void pre() throws Exception{}
void solve(int TC) throws Exception{
long N = nl();
if(N%2 == 1){
pn("NO");
return;
}
N /= 2;
boolean yes = ps(N);
if(N%2 == 0)yes |= ps(N/2);
pn(yes?"YES":"NO");
}
boolean ps(long N){
long T = (long)Math.sqrt(N);
while(T*T > N)T--;
while (T*T < N)T++;
return (T*T == N);
}
//SOLUTION END
void hold(boolean b)throws Exception{if(!b)throw new Exception("Hold right there, Sparky!");}
void exit(boolean b){if(!b)System.exit(0);}
static void dbg(Object... o){System.err.println(Arrays.deepToString(o));}
final long IINF = (long)1e17;
final int INF = (int)1e9+2;
DecimalFormat df = new DecimalFormat("0.00000000000");
double PI = 3.141592653589793238462643383279502884197169399, eps = 1e-8;
static boolean multipleTC = true, memory = true, fileIO = false;
FastReader in;PrintWriter out;
void run() throws Exception{
long ct = System.currentTimeMillis();
if (fileIO) {
in = new FastReader("");
out = new PrintWriter("");
} else {
in = new FastReader();
out = new PrintWriter(System.out);
}
//Solution Credits: Taranpreet Singh
int T = multipleTC? ni():1;
pre();
for (int t = 1; t <= T; t++) solve(t);
out.flush();
out.close();
System.err.println(System.currentTimeMillis() - ct);
}
public static void main(String[] args) throws Exception{
if(memory)new Thread(null, new Runnable() {public void run(){try{new CF_1515_B().run();}catch(Exception e){e.printStackTrace();System.exit(1);}}}, "1", 1 << 28).start();
else new CF_1515_B().run();
}
int[][] make(int n, int e, int[] from, int[] to, boolean f){
int[][] g = new int[n][];int[]cnt = new int[n];
for(int i = 0; i< e; i++){
cnt[from[i]]++;
if(f)cnt[to[i]]++;
}
for(int i = 0; i< n; i++)g[i] = new int[cnt[i]];
for(int i = 0; i< e; i++){
g[from[i]][--cnt[from[i]]] = to[i];
if(f)g[to[i]][--cnt[to[i]]] = from[i];
}
return g;
}
int[][][] makeS(int n, int e, int[] from, int[] to, boolean f){
int[][][] g = new int[n][][];int[]cnt = new int[n];
for(int i = 0; i< e; i++){
cnt[from[i]]++;
if(f)cnt[to[i]]++;
}
for(int i = 0; i< n; i++)g[i] = new int[cnt[i]][];
for(int i = 0; i< e; i++){
g[from[i]][--cnt[from[i]]] = new int[]{to[i], i, 0};
if(f)g[to[i]][--cnt[to[i]]] = new int[]{from[i], i, 1};
}
return g;
}
int find(int[] set, int u){return set[u] = (set[u] == u?u:find(set, set[u]));}
int digit(long s){int ans = 0;while(s>0){s/=10;ans++;}return ans;}
long gcd(long a, long b){return (b==0)?a:gcd(b,a%b);}
int gcd(int a, int b){return (b==0)?a:gcd(b,a%b);}
int bit(long n){return (n==0)?0:(1+bit(n&(n-1)));}
void p(Object... o){for(Object oo:o)out.print(oo+" ");}
void pn(Object... o){for(int i = 0; i< o.length; i++)out.print(o[i]+(i+1 < o.length?" ":"\n"));}
void pni(Object... o){for(Object oo:o)out.print(oo+" ");out.println();out.flush();}
String n()throws Exception{return in.next();}
String nln()throws Exception{return in.nextLine();}
int ni()throws Exception{return Integer.parseInt(in.next());}
long nl()throws Exception{return Long.parseLong(in.next());}
double nd()throws Exception{return Double.parseDouble(in.next());}
class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader(){
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws Exception{
br = new BufferedReader(new FileReader(s));
}
String next() throws Exception{
while (st == null || !st.hasMoreElements()){
try{
st = new StringTokenizer(br.readLine());
}catch (IOException e){
throw new Exception(e.toString());
}
}
return st.nextToken();
}
String nextLine() throws Exception{
String str;
try{
str = br.readLine();
}catch (IOException e){
throw new Exception(e.toString());
}
return str;
}
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
import java.util.*;
public class codeforces2 {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
// Scanner scan = new Scanner(System.in);
//for (int i = 1; i <= 10; i++) {
// System.out.println(i + " " + divisors(i).toString());
//}
int tc = sc.ni();
for (int rep = 0; rep < tc; rep++) {
long n = sc.nl();
if (n % 2 == 1) {
pw.println("NO");
}
else {
n/= 2;
if (perfectSquare(n)) {
pw.println("YES");
}
else if (n % 2 == 0 && perfectSquare(n/2)) {
pw.println("YES");
}
else {
pw.println("NO");
}
}
}
pw.close();
}
//precondition: n <= m
public static boolean solve(int n,int m, int k) {
return false;
}
public static boolean perfectSquare(long n) {
long lo = 0;
long hi = n;
while (lo < hi) {
long k = (lo + hi) / 2;
if (k * k < n)
lo = k + 1;
else
hi = k;
}
return (lo * lo == n);
}
static Set<Integer> divisors(int n) {
Set<Integer> set = new HashSet();
for (int i=1; i<=Math.sqrt(n); i++)
{
if (n%i==0)
{
// If divisors are equal, print only one
if (n/i == i)
set.add(i);
else {// Otherwise print both
set.add(i);
set.add(n / i);
}
}
}
return set;
}
static Map<Integer, Integer> primeFactorization(int x) {
//first divide by 2
Map<Integer, Integer> map = new HashMap();
if (x == 0) return map;
int count = 0;
while (x % 2 == 0) {
x /=2;
count++;
}
//insert 2
if (count > 0) map.put(2, count);
for (int divisor = 3; divisor * divisor <= x; divisor += 2) {
int cnt = 0;
while (x % divisor == 0) {
x /= divisor;
cnt++;
}
if (cnt > 0) map.put(divisor, cnt);
}
if (x > 1) {
map.put(x, 1);
}
return map;
}
static boolean isPrime(int n)
{
// Check if number is less than
// equal to 1
if (n <= 1)
return false;
// Check if number is 2
else if (n == 2)
return true;
// Check if n is a multiple of 2
else if (n % 2 == 0)
return false;
// If not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2)
{
if (n % i == 0)
return false;
}
return true;
}
static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b % a, a);
}
// method to return LCM of two numbers
static int lcm(int a, int b)
{
return (a / gcd(a, b)) * b;
}
public static void sort(int[] arr) {
Random rgen = new Random();
for (int i = 0; i < arr.length; i++) {
int r = rgen.nextInt(arr.length);
int temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
}
Arrays.sort(arr);
}
public static void sort(long[] arr) {
Random rgen = new Random();
for (int i = 0; i < arr.length; i++) {
int r = rgen.nextInt(arr.length);
long temp = arr[i];
arr[i] = arr[r];
arr[r] = temp;
}
Arrays.sort(arr);
}
/* */
//printing methods
/* */
//WOW!
/* */
public static void printArr(PrintWriter pw, int[] arr) {
StringBuilder sb = new StringBuilder();
for (int x : arr) {
sb.append(x + "");
}
sb.setLength(sb.length() - 1);
pw.println(sb.toString());
}
public static void printArr2d(PrintWriter pw, int[][] arr) {
StringBuilder sb = new StringBuilder();
for (int[] row : arr) {
for (int x : row) {
sb.append(x + " ");
}
sb.setLength(sb.length() - 1);
sb.append("\n");
}
sb.setLength(sb.length() - 1);
pw.println(sb.toString());
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in), 32768);
st = null;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int ni() {
return Integer.parseInt(next());
}
int[] intArray(int N) {
int[] ret = new int[N];
for (int i = 0; i < N; i++)
ret[i] = ni();
return ret;
}
long nl() {
return Long.parseLong(next());
}
long[] longArray(int N) {
long[] ret = new long[N];
for (int i = 0; i < N; i++)
ret[i] = nl();
return ret;
}
double nd() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
class UnionFind {
int size;
private int[] id;
public UnionFind(int size) {
this.size = size;
id = new int[size];
for (int i = 0; i < id.length; i++) {
id[i] = i;
}
}
public int find(int a) {
if (id[a] != a) {
id[a] = find(id[a]);
}
return id[a];
}
public void union(int a, int b) {
int r1 = find(a);
int r2 = find(b);
if (r1 == r2) return;
size--;
id[r1] = r2;
}
@Override
public String toString() {
return Arrays.toString(id);
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Solution1515B {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
Solver1515B solver = new Solver1515B();
int n = in.nextInt();
for (int i = 0; i < n; i++) {
solver.solve(i, in, out);
}
out.close();
}
static class Solver1515B {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
boolean f = false;
if (n % 2 == 0) {
int s = (int) Math.sqrt(n / 2);
if (s * s == n / 2) {
f = true;
}
}
if (n % 4 == 0) {
int s = (int) Math.sqrt(n / 4);
if (s * s == n / 4) {
f = true;
}
}
if (f) {
out.println("YES");
} else {
out.println("NO");
}
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.*;
import java.io.*;
public class EdD {
static long[] mods = {1000000007, 998244353, 1000000009};
static long mod = mods[0];
public static MyScanner sc;
public static PrintWriter out;
public static void main(String[] havish) throws Exception{
// TODO Auto-generated method stub
sc = new MyScanner();
out = new PrintWriter(System.out);
int t = sc.nextInt();
for(int i = 0;i<t;i++){
int n =sc.nextInt();
if (n%2== 1)
out.println("NO");
else{
while(n%2 == 0){
n/=2;
}
verdict(isSquare(n) || isSquare(2*n));
}
}
out.close();
}
public static boolean isSquare(int n) {
int d = (int)Math.sqrt(n);
for(long s = d-2;s<=d+2;s++){
if (s*s == n)
return true;
}
return false;
}
public static void sort(int[] array){
ArrayList<Integer> copy = new ArrayList<>();
for (int i : array)
copy.add(i);
Collections.sort(copy);
for(int i = 0;i<array.length;i++)
array[i] = copy.get(i);
}
static String[] readArrayString(int n){
String[] array = new String[n];
for(int j =0 ;j<n;j++)
array[j] = sc.next();
return array;
}
static int[] readArrayInt(int n){
int[] array = new int[n];
for(int j = 0;j<n;j++)
array[j] = sc.nextInt();
return array;
}
static int[] readArrayInt1(int n){
int[] array = new int[n+1];
for(int j = 1;j<=n;j++){
array[j] = sc.nextInt();
}
return array;
}
static long[] readArrayLong(int n){
long[] array = new long[n];
for(int j =0 ;j<n;j++)
array[j] = sc.nextLong();
return array;
}
static double[] readArrayDouble(int n){
double[] array = new double[n];
for(int j =0 ;j<n;j++)
array[j] = sc.nextDouble();
return array;
}
static int minIndex(int[] array){
int minValue = Integer.MAX_VALUE;
int minIndex = -1;
for(int j = 0;j<array.length;j++){
if (array[j] < minValue){
minValue = array[j];
minIndex = j;
}
}
return minIndex;
}
static int minIndex(long[] array){
long minValue = Long.MAX_VALUE;
int minIndex = -1;
for(int j = 0;j<array.length;j++){
if (array[j] < minValue){
minValue = array[j];
minIndex = j;
}
}
return minIndex;
}
static int minIndex(double[] array){
double minValue = Double.MAX_VALUE;
int minIndex = -1;
for(int j = 0;j<array.length;j++){
if (array[j] < minValue){
minValue = array[j];
minIndex = j;
}
}
return minIndex;
}
static long power(long x, long y){
if (y == 0)
return 1;
if (y%2 == 1)
return (x*power(x, y-1))%mod;
return power((x*x)%mod, y/2)%mod;
}
static void verdict(boolean a){
out.println(a ? "YES" : "NO");
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try{
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
//StringJoiner sj = new StringJoiner(" ");
//sj.add(strings)
//sj.toString() gives string of those stuff w spaces or whatever that sequence is | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.*;import java.io.*;import java.math.*;
public class Main
{
public static void process()throws IOException
{
long n=nl();
if(n%2==0)
{
long a1=(long)Math.sqrt(n);
long a2=(long)Math.sqrt(n/2);
if(a1*a1==n || a2*a2*2==n)
{pn("YES");return;}
}
pn("NO");
}
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();}
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;}}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
FastScanner in = new FastScanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, FastScanner in, PrintWriter out) {
int numTests = in.nextInt();
for (int test = 0; test < numTests; test++) {
int n = in.nextInt();
boolean can = false;
if (n % 2 == 0 && isSquare(n / 2)) {
can = true;
}
if (n % 4 == 0 && isSquare(n / 4)) {
can = true;
}
out.println(can ? "YES" : "NO");
}
}
private boolean isSquare(int n) {
int x = (int) Math.sqrt(n);
while (x * x > n) {
--x;
}
while (x * x < n) {
++x;
}
return x * x == n;
}
}
static class FastScanner {
private BufferedReader in;
private StringTokenizer st;
public FastScanner(InputStream stream) {
in = new BufferedReader(new InputStreamReader(stream));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
//package sept;
import java.io.*;
import java.util.*;
public class TimePass {
InputStream is;
PrintWriter out;
String INPUT = "";
//boolean codechef=true;
// String prodKey = "Av#/lL{OyEKiLR+/Ce%(w]^J65;XZe8FVb]]<931_=80E[BVnU^@4xu*J%KG3,CRqIZrUN~JJ+*6QC*CyBd>'$;>O"onO.bQ%{L}";
boolean codechef=true;
void solve()
{
int t=ni();
while(t-->0) {
int n=ni();
int root=(int)Math.sqrt(n/2);
int rootn = (int)Math.sqrt(n);
if (n==1 || n%2!=0) {
out.println("NO");
continue;
}
if (root*root == n/2 || (rootn*rootn == n && rootn%2==0)) {
out.println("YES");
} else {
out.println("NO");
}
}
}
static int comp(int a,int b){
return a+1097*b;
}
static long printNcR(int n, int r)
{
// p holds the value of n*(n-1)*(n-2)...,
// k holds the value of r*(r-1)...
long p = 1, k = 1;
// C(n, r) == C(n, n-r),
// choosing the smaller value
if (n - r < r) {
r = n - r;
}
if (r != 0) {
while (r > 0) {
p *= n;
k *= r;
// gcd of p, k
long m = __gcd(p, k);
// dividing by gcd, to simplify
// product division by their gcd
// saves from the overflow
p /= m;
k /= m;
n--;
r--;
}
// k should be simplified to 1
// as C(n, r) is a natural number
// (denominator should be 1 ) .
}
else {
p = 1;
}
// if our approach is correct p = ans and k =1
return p;
}
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;
}
static long[][] dp;
static long desc(int[] a,int l,int r) {
if (l==r) return 0;
if (dp[l][r]!=-1) return dp[l][r];
dp[l][r] = a[r]-a[l] + Math.min(desc(a,l+1,r),desc(a,l,r-1));
return dp[l][r];
}
static int getMax(int arr[], int n)
{
int mx = arr[0];
for (int i = 1; i < n; i++)
if (arr[i] > mx)
mx = arr[i];
return mx;
}
// A function to do counting sort of arr[] according to
// the digit represented by exp.
static void countSort(int arr[], int n, int exp)
{
int output[] = new int[n]; // output array
int i;
int count[] = new int[10];
Arrays.fill(count, 0);
// Store count of occurrences in count[]
for (i = 0; i < n; i++)
count[(arr[i] / exp) % 10]++;
// Change count[i] so that count[i] now contains
// actual position of this digit in output[]
for (i = 1; i < 10; i++)
count[i] += count[i - 1];
// Build the output array
for (i = n - 1; i >= 0; i--) {
output[count[(arr[i] / exp) % 10] - 1] = arr[i];
count[(arr[i] / exp) % 10]--;
}
// Copy the output array to arr[], so that arr[] now
// contains sorted numbers according to curent digit
for (i = 0; i < n; i++)
arr[i] = output[i];
}
// The main function to that sorts arr[] of size n using
// Radix Sort
static void radixsort(int arr[], int n)
{
// Find the maximum number to know number of digits
int m = getMax(arr, n);
// Do counting sort for every digit. Note that
// instead of passing digit number, exp is passed.
// exp is 10^i where i is current digit number
for (int exp = 1; m / exp > 0; exp *= 10)
countSort(arr, n, exp);
}
static int MAX=1500;
static int prime[], countdiv[];
static int[] getDivisorsArray() {
int n=20000005;
int[] mind = new int[n];
Arrays.fill(mind, -1);
for(int i=2;i<n;i++){
if (mind[i]==-1){
for(int j=i;j<n;j+=i){
if (mind[j]==-1){
mind[j]=i;
}
}
}
}
// int[] nod = new int[n];
// for(int i=2;i<n;i++){
// int prod = i/mind[i];
// if (mind[i] != mind[prod]) {
// nod[i] = nod[prod] + 1;
// } else {
// nod[i] = nod[prod];
// }
// }
return mind;
}
// Simple sieve to find smallest prime factors of numbers
// smaller than MAX
void SieveOfEratosthenes()
{
for (int i = 2; i * i < MAX; ++i)
{
if (prime[i]==0)
for (int j = i * i; j < MAX; j += i)
prime[j] = i;
}
// Prime number will have same divisor
for (int i = 1; i < MAX; ++i)
if (prime[i]==0)
prime[i] = i;
}
// Returns length of the largest subsequence
// with GCD more than 1.
int largestGCDSubsequence(int arr[], int n)
{
int ans = 0;
for (int i=0; i < n; ++i)
{
int element = arr[i];
// Fetch total unique prime divisor of element
while (element > 1)
{
int div = prime[element];
// Increment count[] of Every unique divisor
// we get till now
++countdiv[div];
// Find maximum frequency of divisor
ans = Math.max(ans, countdiv[div]);
while (element % div==0)
element /= div;
}
}
return ans;
}
static boolean check(int x)
{
char[] a=(""+x).toCharArray();
int s=0;
for(int i=0;i<a.length;i++)
{
s+=a[i]-'0';
s%=3;
}
if(s==0)return true;
return false;
}
static int[][] packD(int n,int[] from,int[] to)
{
int[][] g=new int[n][];
int[] p=new int[n];
for(int f:from)
{
p[f]++;
}
int m=from.length;
for(int i=0;i<n;i++)
{
g[i]=new int[p[i]];
}
for(int i=0;i<m;i++)
{
g[from[i]][--p[from[i]]]=to[i];
}
return g;
}
static class Pair3
{
int a,b,c;
public Pair3(int a,int b,int c)
{
this.a=a;
this.b=b;
this.c=c;
}
}
static class Pair
{
int a,b;
public Pair(int a,int b)
{
this.a=a;
this.b=b;
}
}
static long lcm(long a,long b)
{
long val=a;
val*=b;
return (val/gcd(a,b));
}
static long gcd(long a,long b)
{
if(a==0)return b;
return gcd(b%a,a);
}
static int pow(int a, int b, int p)
{
long ans = 1, base = a;
while (b!=0)
{
if ((b & 1)!=0)
{
ans *= base;
ans%= p;
}
base *= base;
base%= p;
b >>= 1;
}
return (int)ans;
}
static int inv(int x, int p)
{
return pow(x, p - 2, p);
}
void run() throws Exception
{
if(codechef)oj=true;
is = oj ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
long s = System.currentTimeMillis();
solve();
out.flush();
tr(System.currentTimeMillis()-s+"ms");
}
public static void main(String[] args) throws Exception {new TimePass().run();}
private byte[] inbuf = new byte[1024];
public int lenbuf = 0, ptrbuf = 0;
private int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private double nd() { return Double.parseDouble(ns()); }
private char nc() { return (char)skip(); }
private String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ')
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private boolean oj = System.getProperty("ONLINE_JUDGE") != null;
private void tr(Object... o) { if(!oj)System.out.println(Arrays.deepToString(o)); }
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.*;
import java.util.Map.Entry;
import java.io.*;
import java.math.*;
import static java.util.stream.Collectors.*;
import static java.util.Map.Entry.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws IOException
{
final long mod=(long) (1e9+7);
Reader s=new Reader();
PrintWriter pt=new PrintWriter(System.out);
// BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int T=s.nextInt();
// int T=Integer.parseInt(br.readLine());
// int T=1;
while(T-->0)
{
long n=s.nextInt();
long sq1=n/2;
long sq2=n/4;
if(isPerfectSquare(sq1)&&sq1*2==n||isPerfectSquare(sq2)&&sq2*4==n)
pt.println("YES");
else
pt.println("NO");
}
pt.close();
}
static void divideBy2(int arr[], int n) {
for(int i=0;i<n;i++) {
arr[i]=arr[i]>>1;
}
}
static boolean isEven(int arr[], int n) {
for(int i=0;i<n;i++) {
if(arr[i]%2==1)
return false;
}
return true;
}
static boolean isPartition(int arr[], int n) {
int sum = 0;
int i, j;
// Calculate sum of all elements
for (i = 0; i < n; i++)
sum += arr[i];
if (sum % 2 != 0)
return false;
boolean part[][]=new boolean[sum/2+1][n+1];
// initialize top row as true
for (i = 0; i <= n; i++)
part[0][i] = true;
// initialize leftmost column, except part[0][0], as false
for (i = 1; i <= sum / 2; i++)
part[i][0] = false;
// Fill the partition table in bottom up manner
for (i = 1; i <= sum / 2; i++) {
for (j = 1; j <= n; j++) {
part[i][j] = part[i][j - 1];
if (i >= arr[j - 1])
part[i][j] = part[i][j]
|| part[i - arr[j - 1]][j - 1];
}
}
return part[sum / 2][n];
}
static int setBit(int S, int j) { return S | 1 << j; }
static int clearBit(int S, int j) { return S & ~(1 << j); }
static int toggleBit(int S, int j) { return S ^ 1 << j; }
static boolean isOn(int S, int j) { return (S & 1 << j) != 0; }
static int turnOnLastZero(int S) { return S | S + 1; }
static int turnOnLastConsecutiveZeroes(int S) { return S | S - 1; }
static int turnOffLastBit(int S) { return S & S - 1; }
static int turnOffLastConsecutiveBits(int S) { return S & S + 1; }
static int lowBit(int S) { return S & -S; }
static int setAll(int N) { return (1 << N) - 1; }
static int modulo(int S, int N) { return (S & N - 1); } //S%N, N is a power of 2
static boolean isPowerOfTwo(int S) { return (S & S - 1) == 0; }
static boolean isWithin(long x, long y, long d, long k) {
return x*k*x*k + y*k*y*k <= d*d;
}
static long modFact(long n,
long p)
{
if (n >= p)
return 0;
long result = 1;
for (int i = 1; i <= n; i++)
result = (result * i) % p;
return result;
}
static int sum(int[] arr, int n)
{
int inc[]=new int[n+1];
int dec[]=new int[n+1];
inc[0] = arr[0];
dec[0] = arr[0];
for (int i = 1; i < n; i++) {
for (int j = 0; j < i; j++) {
if (arr[j] > arr[i]) {
dec[i] = max(dec[i], inc[j] + arr[i]);
}
else if (arr[i] > arr[j]) {
inc[i] = max(inc[i], dec[j] + arr[i]);
}
}
}
return max(inc[n - 1], dec[n - 1]);
}
static long nc2(long a) {
return a*(a-1)/2;
}
public static int numberOfprimeFactors(int n)
{
// Print the number of 2s that divide n
HashSet<Integer> hs = new HashSet<Integer>();
while (n%2==0)
{
hs.add(2);
n /= 2;
}
// n must be odd at this point. So we can
// skip one element (Note i = i +2)
for (int i = 3; i <= Math.sqrt(n); i+= 2)
{
// While i divides n, print i and divide n
while (n%i == 0)
{
hs.add(i);
n /= i;
}
}
// This condition is to handle the case whien
// n is a prime number greater than 2
if (n > 2)
hs.add(n);
return hs.size();
}
static int gcd(int a, int b)
{
if (b == 0)
return a;
return gcd(b, a % b);
}
static void reverse(int arr[],int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static void reverse(long arr[],int start, int end)
{
long temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static boolean isPrime(int n)
{
// Corner cases
if (n <= 1) return false;
if (n <= 3) return true;
// This is checked so that we can skip
// middle five numbers in below loop
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 p2(int n) {
int k=0;
while(n>1) {
if(n%2!=0)
return k;
n/=2;
k++;
}
return k;
}
static boolean isp2(int n) {
while(n>1) {
if(n%2==1)
return false;
n/=2;
}
return true;
}
static int binarySearch(int arr[], int first, int last, int key){
int mid = (first + last)/2;
while( first <= last ){
if ( arr[mid] < key ){
first = mid + 1;
}else if ( arr[mid] == key ){
return mid;
}else{
last = mid - 1;
}
mid = (first + last)/2;
}
return -1;
}
static void print(int a[][]) {
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();
}
}
static int max (int x, int y) {
return (x > y)? x : y;
}
static int search(Pair[] p, Pair pair) {
int l=0, r=p.length;
while (l <= r) {
int m = l + (r - l) / 2;
if (p[m].compareTo(pair)==0)
return m;
if (p[m].compareTo(pair)<0)
l = m + 1;
else
r = m - 1;
}
return -1;
}
static void pa(int a[])
{
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
static void pa(long a[])
{
for(int i=0;i<a.length;i++)
System.out.print(a[i]+" ");
System.out.println();
}
static void reverseArray(int arr[],
int start, int end)
{
int temp;
while (start < end)
{
temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start++;
end--;
}
}
static boolean isPalindrome(String s) {
int l=s.length();
for(int i=0;i<l/2;i++)
{
if(s.charAt(i)!=s.charAt(l-i-1))
return false;
}
return true;
}
static long nc2(long n, long m) {
return (n*(n-1)/2)%m;
}
static long c(long a) {
return a*(a+1)/2;
}
static int next(long[] arr, long target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
// Move to right side if target is
// greater.
if (arr[mid] < target) {
start = mid + 1;
}
// Move left side.
else {
ans = mid;
end = mid - 1;
}
}
return ans;
}
static int prev(long[] arr, long target)
{
int start = 0, end = arr.length - 1;
int ans = -1;
while (start <= end) {
int mid = (start + end) / 2;
// Move to left side if target is
// lesser.
if (arr[mid] > target) {
end = mid - 1;
}
// Move right side.
else {
ans = mid;
start = mid + 1;
}
}
return ans;
}
static long power(long x, long y, long p)
{
long 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;
}
static long modInverse(long n, long p)
{
return power(n, p-2, p);
}
static long nCrModP(long n, long r,
long p)
{
if(r>n)
return 0;
if (r == 0)
return 1;
long[] fac = new long[(int) (n+1)];
fac[0] = 1;
for (int i = 1 ;i <= n; i++)
fac[i] = fac[i-1] * i % p;
return (fac[(int) n]* modInverse(fac[(int) r], p)
% p * modInverse(fac[(int) (n-r)], p)
% p) % p;
}
static String reverse(String str)
{
return new StringBuffer(str).reverse().toString();
}
static long fastpow(long x, long y, long m)
{
if (y == 0)
return 1;
long p = fastpow(x, y / 2, m) % m;
p = (p * p) % m;
if (y % 2 == 0)
return p;
else
return (x * p) % m;
}
static boolean isPerfectSquare(long l)
{
return Math.pow((long)Math.sqrt(l),2)==l;
}
static void merge(long[] arr, int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long [n1];
long R[] = new long [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(int arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static void merge(int arr[], int l, int m, int r)
{
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int [n1];
int R[] = new int [n2];
/*Copy data to temp arrays*/
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2)
{
if (L[i] <= R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
static void sort(long arr[], int l, int r)
{
if (l < r)
{
// Find the middle point
int m = (l+r)/2;
// Sort first and second halves
sort(arr, l, m);
sort(arr , m+1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
static class Pair implements Comparable<Pair>{
int a;
int b;
Pair(int a,int b){
this.a=a;
this.b=b;
}
public int compareTo(Pair p){
if(a>p.a)
return 1;
if(a==p.a)
return (b-p.b);
return -1;
}
}
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[128]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
import java.util.*;
/*
polyakoff
*/
public class Main {
static FastReader in;
static PrintWriter out;
static Random rand = new Random();
static final int oo = (int) 1e9 + 10;
static final long OO = (long) 1e18 + 10;
static final int MOD = (int) 1e9 + 7;
static boolean isSq(int x) {
int sq = (int) Math.sqrt(x);
return sq * sq == x;
}
static void solve() {
int n = in.nextInt();
if ((n % 2 == 0 && isSq(n / 2)) || (n % 4 == 0 && isSq(n / 4)))
out.println("YES");
else
out.println("NO");
}
public static void main(String[] args) {
in = new FastReader();
out = new PrintWriter(System.out);
int t = 1;
t = in.nextInt();
while (t-- > 0) {
solve();
}
out.flush();
out.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader() {
this(System.in);
}
FastReader(String file) throws FileNotFoundException {
this(new FileInputStream(file));
}
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(nextLine());
}
return st.nextToken();
}
String nextLine() {
String line;
try {
line = br.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
return line;
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
//I'm Whiplash99
import java.io.*;
import java.util.*;
public class B
{
public static void main(String[] args) throws Exception
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int i,N;
int T=Integer.parseInt(br.readLine().trim());
StringBuilder sb=new StringBuilder();
while (T-->0)
{
N=Integer.parseInt(br.readLine().trim());
boolean flag=false;
int sqrt=(int) Math.sqrt(N/2);
if(sqrt*sqrt==N/2&&N%2==0) flag = true;
sqrt=(int) Math.sqrt(N/4);
if(sqrt*sqrt==N/4&&N%4==0) flag = true;
sb.append(flag?"YES\n":"NO\n");
}
System.out.println(sb);
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
import java.util.*;
public class b {
public static void main(String[] args) throws Exception {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
StringTokenizer st = new StringTokenizer(f.readLine());
int T = Integer.parseInt(st.nextToken());
for (int t = 0; t < T; t++) {
st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int sqrt = (int)Math.sqrt(n);
int sqrt2 = (int)Math.sqrt(n/2);
if (sqrt*sqrt == n && sqrt%2 == 0) {
out.println("YES");
} else if (2*sqrt2*sqrt2 == n) {
out.println("YES");
} else {
out.println("NO");
}
}
out.close();
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
BPhoenixAndPuzzle solver = new BPhoenixAndPuzzle();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class BPhoenixAndPuzzle {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
if (n % 2 == 1) {
out.println("NO");
return;
}
n /= 2;
int h = (int) Math.sqrt(n + 0.5);
if (h * h == n) {
out.println("YES");
return;
}
if (n % 2 == 1) {
out.println("NO");
return;
}
n /= 2;
h = (int) Math.sqrt(n + 0.5);
if (h * h == n) {
out.println("YES");
} else out.println("NO");
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void println(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
writer.print('\n');
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String next() {
int c;
while (isSpaceChar(c = this.read())) {
;
}
StringBuilder result = new StringBuilder();
result.appendCodePoint(c);
while (!isSpaceChar(c = this.read())) {
result.appendCodePoint(c);
}
return result.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;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
import java.util.*;
public class B {
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
static FastReader s = new FastReader();
static PrintWriter out = new PrintWriter(System.out);
private static int[] rai(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextInt();
}
return arr;
}
private static int[][] rai(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] = s.nextInt();
}
}
return arr;
}
private static long[] ral(int n) {
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = s.nextLong();
}
return arr;
}
private static long[][] ral(int n, int m) {
long[][] arr = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
arr[i][j] = s.nextLong();
}
}
return arr;
}
private static int ri() {
return s.nextInt();
}
private static long rl() {
return s.nextLong();
}
private static String rs() {
return s.next();
}
static int gcd(int a,int b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
static long gcd(long a,long b)
{
if(b==0)
{
return a;
}
return gcd(b,a%b);
}
static boolean isPrime(int n) {
//check if n is a multiple of 2
if(n==1)
{
return false;
}
if(n==2)
{
return true;
}
if (n % 2 == 0) return false;
//if not, then just check the odds
for (int i = 3; i <= Math.sqrt(n); i += 2) {
if (n % i == 0)
return false;
}
return true;
}
static boolean[] sieveOfEratosthenes(int n)
{
// Create a boolean array "prime[0..n]" and initialize
// all entries it as true. A value in prime[i] will
// finally be false if i is Not a prime, else true.
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] == true)
{
// Update all multiples of p
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
return prime;
}
public static void main(String[] args) {
StringBuilder ans = new StringBuilder();
int t = ri();
// int t=1;
while (t-- > 0)
{
long n=rl();
if(n%2==1)
{
ans.append("NO\n");
continue;
}
if(n%4==0)
{
long val = n/4;
long sq = (long) Math.sqrt(val);
if(sq*sq == val)
{
ans.append("YES\n");
continue;
}
}
if(n%2==0)
{
long val = n/2;
long sq = (long) Math.sqrt(val);
if(sq*sq == val)
{
ans.append("YES\n");
continue;
}
}
ans.append("NO\n");
}
out.print(ans.toString());
out.flush();
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class B {
public static void main(String[] args) throws IOException {
// Scanner scan = new Scanner(System.in);
BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
// int t = scan.nextInt();
int t= Integer.parseInt(br.readLine());
while (t-->0)
{
String[] s1= br.readLine().split(" ");
int n= Integer.parseInt(s1[0]);
// Integer[] a= new Integer[n];
// String[] s2= br.readLine().split(" ");
// long sum=0;
// for (int i=0;i<n;i++){
// a[i]= Integer.parseInt(s2[i]);
// sum+= Math.abs(a[i]);
// }
int x= 1;
boolean ans=true;
while (n%2==0){
x*=2;
n/=2;
}
if (x==1) ans= false;
int z= (int)Math.sqrt(n);
if (z*z!=n) ans= false;
if (ans) System.out.println("YES");
else System.out.println("NO");
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
/*==========================================================================*/
/*
* AUTHOR: RonWonWon
* CREATED: 02.05.2021 19:58:57
* EMAIL: [email protected]
*/
/*==========================================================================*/
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) {
FastScanner in = new FastScanner();
PrintWriter out = new PrintWriter(System.out);
int t = in.nextInt(), tt = 0;
while(t-->0) {
int n = in.nextInt();
if(n%2!=0) out.println("NO");
else{
n/=2;
if(Math.sqrt(n)==Math.ceil(Math.sqrt(n))) out.println("YES");
else{
if(n%2!=0) out.println("NO");
else{
n/=2;
if(Math.sqrt(n)==Math.ceil(Math.sqrt(n))) out.println("YES");
else out.println("NO");
}
}
}
//tt++; out.println("Case #"+tt+": "+ans);
}
out.flush();
}
static class FastScanner {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer("");
String next() {
while(!st.hasMoreTokens())
try { st = new StringTokenizer(br.readLine()); }
catch(IOException e) {}
return st.nextToken();
}
String nextLine(){
try{ return br.readLine(); }
catch(IOException e) { } return "";
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
int[] readArray(int n) {
int a[] = new int[n];
for(int i=0;i<n;i++) a[i] = nextInt();
return a;
}
}
static final Random random = new Random();
static void ruffleSort(int[] a){
int n = a.length;
for(int i=0;i<n;i++){
int j = random.nextInt(n), temp = a[j];
a[j] = a[i]; a[i] = temp;
}
Arrays.sort(a);
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
PrintWriter out = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer("");
String next() throws IOException {
if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); }
return tok.nextToken();
}
int ni() throws IOException { return Integer.parseInt(next()); }
long nl() throws IOException { return Long.parseLong(next()); }
long mod=1000000007;
void solve() throws IOException {
for (int tc=ni();tc>0;tc--) {
long n=ni();
long q=1;
long p=1;
boolean f=false;
while (true) {
if (p*2==n || p*4==n) { f=true; break; }
q++;
p=q*q;
if (p>n) break;
}
if (f) out.println("YES");
else out.println("NO");
}
out.flush();
}
int gcd(int a,int b) { return(b==0?a:gcd(b,a%b)); }
long gcd(long a,long b) { return(b==0?a:gcd(b,a%b)); }
long mp(long a,long p) { long r=1; while(p>0) { if ((p&1)==1) r=(r*a)%mod; p>>=1; a=(a*a)%mod; } return r; }
public static void main(String[] args) throws IOException {
new Main().solve();
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
import java.util.*;
public class B {
public static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
public static void main(String[] args) throws IOException {
readInput();
out.close();
}
static boolean sq(long x) {
long l = 1;
long r = (int)Math.sqrt(1e16)+1;
while (l+1<r) {
long m = (l+r)>>1;
if (m * m > x) r = m;
else l = m;
}
return l*l == x;
}
static boolean solve(long x) {
if ((x&1)==1) return false;
if ((x & (x-1)) == 0) return true;
long num = 2;
while (num < x && x % num == 0) {
if (sq(x/num)) return true;
num*=2;
}
return false;
}
public static void readInput() throws IOException {
// br = new BufferedReader(new FileReader(".in"));
// out = new PrintWriter(new FileWriter(".out"));
int t = Integer.parseInt(br.readLine());
while (t-->0) {
int x = Integer.parseInt(br.readLine());
out.println(solve(x) ? "YES":"NO");
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Sandip Jana
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long x = in.readLong();
if (x % 2 == 1) {
out.println("NO");
return;
}
if (x % 2 == 0) {
long p = x / 2;
long square = (long) Math.sqrt(p);
if (square * 1L * square == p) {
out.println("YES");
return;
}
}
if (x % 4 == 0) {
long p = x / 4;
long square = (long) Math.sqrt(p);
if (square * 1L * square == p) {
out.println("YES");
return;
}
}
out.println("NO");
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public 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;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
/**
* @author Mubtasim Shahriar
*/
public class Cgr14 {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Solver solver = new Solver();
int t = sc.nextInt();
// int t = 1;
while (t-- != 0) {
solver.solve(sc, out);
}
out.close();
}
static class Solver {
public void solve(InputReader sc, PrintWriter out) {
long n = sc.nextInt();
long l = 1;
long r = (long)1e5;
while(l<=r) {
long mid = (l+r)/2;
long needed = (mid*mid)*2;
if(needed==n) {
out.println("YES");
return;
}
if(needed>n) {
r = mid-1;
} else {
l = mid+1;
}
}
l = 1;
r = (long)1e5;
while(l<=r) {
long mid = (l+r)/2;
long needed = (mid*mid)*4;
if(needed==n) {
out.println("YES");
return;
}
if(needed>n) {
r = mid-1;
} else {
l = mid+1;
}
}
out.println("NO");
}
}
static void sort(int[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
}
static void sort(long[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
}
static void sortDec(int[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
int l = 0;
int r = n - 1;
while (l < r) {
arr[l] ^= arr[r];
arr[r] ^= arr[l];
arr[l] ^= arr[r];
l++;
r--;
}
}
static void sortDec(long[] arr) {
Random rand = new Random();
int n = arr.length;
for (int i = 0; i < n; i++) {
int idx = rand.nextInt(n);
if (idx == i) continue;
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
Arrays.sort(arr);
int l = 0;
int r = n - 1;
while (l < r) {
arr[l] ^= arr[r];
arr[r] ^= arr[l];
arr[l] ^= arr[r];
l++;
r--;
}
}
static class InputReader {
private boolean finished = false;
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 peek() {
if (numChars == -1) {
return -1;
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
return -1;
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String nextString() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
StringBuilder res = new StringBuilder();
do {
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 BigInteger readBigInteger() {
try {
return new BigInteger(nextString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char nextCharacter() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
return (char) c;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E') {
return res * Math.pow(10, nextInt());
}
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public boolean isExhausted() {
int value;
while (isSpaceChar(value = peek()) && value != -1) {
read();
}
return value == -1;
}
public String next() {
return nextString();
}
public SpaceCharFilter getFilter() {
return filter;
}
public void setFilter(SpaceCharFilter filter) {
this.filter = filter;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
public int[] nextIntArray(int n) {
int[] array = new int[n];
for (int i = 0; i < n; ++i) array[i] = nextInt();
return array;
}
public int[] nextSortedIntArray(int n) {
int array[] = nextIntArray(n);
Arrays.sort(array);
return array;
}
public int[] nextSumIntArray(int n) {
int[] array = new int[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextLongArray(int n) {
long[] array = new long[n];
for (int i = 0; i < n; ++i) array[i] = nextLong();
return array;
}
public long[] nextSumLongArray(int n) {
long[] array = new long[n];
array[0] = nextInt();
for (int i = 1; i < n; ++i) array[i] = array[i - 1] + nextInt();
return array;
}
public long[] nextSortedLongArray(int n) {
long array[] = nextLongArray(n);
Arrays.sort(array);
return array;
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.*;
import java.io.*;
public class B {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
int T = sc.nextInt();
while(T-->0) {
int n = sc.nextInt();
if(n % 2 == 0 && issq(n/2)) {
System.out.println("YES");
}
else if(n % 4 == 0 && issq(n/4)) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
static boolean issq(long x) {
long rx = (long)Math.sqrt(x);
return rx * rx == x;
}
static class FastScanner {
public BufferedReader reader;
public StringTokenizer tokenizer;
public FastScanner() {
reader = new BufferedReader(new InputStreamReader(System.in), 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() {
try {
return reader.readLine();
} catch(IOException e) {
throw new RuntimeException(e);
}
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.*;
import java.io.*;
public class two {
public static void main(String[] args) throws IOException, FileNotFoundException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
//BufferedReader in = new BufferedReader(new FileReader("two"));
HashSet<Integer> good = new HashSet<>();
int i=1;
for (; i<= (int)(1e9);) {
i <<= 1;
good.add(i);
}
for (i=3; i*i*2 <= (int)(1e9); i++) {
good.add(i*i*2);
}
int beg = 4;
for (i=3; beg + i*4 <= (int)(1e9); i+=2) {
good.add(beg + i*4);
beg += i*4;
}
int t = Integer.parseInt(in.readLine());
while (t-- > 0) {
int n = Integer.parseInt(in.readLine());
if (good.contains(n)) {
System.out.println("YES");
}
else {
System.out.println("NO");
}
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.PrintWriter;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
var sc = new Scanner(System.in);
var pw = new PrintWriter(System.out);
int T = Integer.parseInt(sc.next());
for(int t = 0; t < T; t++){
int n = Integer.parseInt(sc.next());
boolean ok = false;
if(n%2 == 0){
int a = n/2;
int b = (int) Math.sqrt(a);
if(b*b == a){
ok = true;
}
}
if(n%4 == 0){
int a = n/4;
int b = (int) Math.sqrt(a);
if(b*b == a){
ok = true;
}
}
if(ok){
pw.println("YES");
}else{
pw.println("NO");
}
}
pw.flush();
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.Scanner;
public class B_14 {
@SuppressWarnings("resource")
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int t = input.nextInt();
for(int test = 0; test < t; test++){
int n = input.nextInt();
if(n % 2 == 0){
if(Math.sqrt(n / 2) == (int)(Math.sqrt(n / 2))){
System.out.println("YES");
}else if(n % 4 == 0 && Math.sqrt(n / 4) == (int)(Math.sqrt(n / 4))){
System.out.println("YES");
}else{
System.out.println("NO");
}
}else{
System.out.println("NO");
}
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.*;
import java.io.*;
public class Main{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int t=sc.nextInt();
while(t-->0)
{
int n=sc.nextInt();
if(n%2==1)
{
System.out.println("NO");
continue;
}
// squares of 2
int num=n/2;
int root = (int)Math.sqrt(num);
if(root*root==num)
{
System.out.println("YES");
continue;
}
// squares of 4
if(n%4!=0)
{
System.out.println("NO");
continue;
}
num = n/4;
root = (int) Math.sqrt(num);
if(root*root==num)
{
System.out.println("YES");
}
else
{
System.out.println("NO");
}
}
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
//package global14;
import java.util.Scanner;
public class B {
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int t = in.nextInt();
while(t > 0){
t --;
int n = in.nextInt();
if(n % 2 != 0){
System.out.println("NO");
continue;
}
int a = n / 2;
int x = (int)Math.sqrt(a);
if(x * x == a || (x + 1) * (x + 1) == a){
System.out.println("YES");
continue;
}
a = n / 4;
if(n % 4 != 0){
System.out.println("NO");
continue;
}
x = (int)Math.sqrt(a);
if(x * x == a || (x + 1) * (x + 1) == a){
System.out.println("YES");
continue;
}
System.out.println("NO");
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.io.BufferedReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
BPhoenixAndPuzzle solver = new BPhoenixAndPuzzle();
int testCount = Integer.parseInt(in.next());
for (int i = 1; i <= testCount; i++)
solver.solve(i, in, out);
out.close();
}
static class BPhoenixAndPuzzle {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
if (n % 2 == 1) {
out.println("NO");
return;
}
n /= 2;
if (n == 1 || (int) Math.sqrt(n) * (int) (Math.sqrt(n)) == n) {
out.println("YES");
} else {
if (n % 2 == 0) {
n /= 2;
if ((int) Math.sqrt(n) * (int) (Math.sqrt(n)) == n) {
out.println("YES");
return;
}
}
out.println("NO");
}
}
}
static class InputReader {
BufferedReader reader;
StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void println(Object... objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
//package codeforces;
import java.io.PrintWriter;
import java.util.*;
public class codeforces {
public static void main(String[] args) {
PrintWriter out=new PrintWriter(System.out);
Scanner s=new Scanner(System.in);
int t=s.nextInt();
for(int tt=0;tt<t;tt++) {
long n=s.nextInt();
long x=(long)Math.sqrt(n/2);
long y=(long)Math.sqrt(n/4);
if(x*x*2==n || y*y*4==n) {
out.println("YES");
}else {
out.println("NO");
}
}
out.close();
s.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);
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;
import java.io.IOException;import java.io.InputStream;import java.io.PrintWriter;
import java.lang.annotation.Retention;import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Array;import java.lang.reflect.Field;import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;import java.security.AccessControlException;import java.util.Arrays;
import java.util.Collection;import java.util.Comparator;import java.util.List;import java.util.Map;
import java.util.Objects;import java.util.TreeMap;import java.util.function.Function;
import java.util.stream.Collectors;import java.util.stream.IntStream;import java.util.stream.LongStream;
import java.util.stream.Stream;public class _B {static public void main(final String[] args)
throws IOException{B._main(args);}
static class B extends Solver{public B(){}@Override public void solve()throws IOException
{int n=sc.nextInt();sc.nextLine();boolean res=false;if(n%2==0){int n1=n/2;int sq
=(int)Math.ceil(Math.sqrt(n1));if(sq*sq==n1){res=true;}else if(n%4==0){n1=n/4;sq
=(int)Math.ceil(Math.sqrt(n1));if(sq*sq==n1){res=true;}}}pw.println(Datas.yesNo(res));
}static public void _main(String[]args)throws IOException{new B().run();}}static class
Datas{static class Pair<K,V>{private K k;private V v;public Pair(final K t,final
V u){this.k=t;this.v=u;}public K getKey(){return k;}public V getValue(){return v;
}}final static String SPACE=" ";public static TreeMap<Integer,Integer>mapc(final
int[]a){return IntStream.range(0,a.length).collect(()->new TreeMap<Integer,Integer>(),
(res,i)->{res.put(a[i],res.getOrDefault(a[i],0)+1);},Map::putAll);}public static
TreeMap<Long,Integer>mapc(final long[]a){return IntStream.range(0,a.length).collect(
()->new TreeMap<Long,Integer>(),(res,i)->{res.put(a[i],res.getOrDefault(a[i],0)+
1);},Map::putAll);}public static<T>TreeMap<T,Integer>mapc(final T[]a,Comparator<T>
cmp){return IntStream.range(0,a.length).collect(cmp!=null?()->new TreeMap<T,Integer>(cmp)
:()->new TreeMap<T,Integer>(),(res,i)->{res.put(a[i],res.getOrDefault(a[i],0)+1);
},Map::putAll);}public static<T>TreeMap<T,Integer>mapc(final T[]a){return mapc(a,
null);}public static TreeMap<Integer,Integer>mapc(final IntStream a){return a.collect(
()->new TreeMap<Integer,Integer>(),(res,v)->{res.put(v,res.getOrDefault(v,0)+1);
},Map::putAll);}public static TreeMap<Long,Integer>mapc(final LongStream a){return
a.collect(()->new TreeMap<Long,Integer>(),(res,v)->{res.put(v,res.getOrDefault(v,
0)+1);},Map::putAll);}public static<T>TreeMap<T,Integer>mapc(final Stream<T>a,Comparator<T>
cmp){return a.collect(cmp!=null?()->new TreeMap<T,Integer>(cmp):()->new TreeMap<T,
Integer>(),(res,v)->{res.put(v,res.getOrDefault(v,0)+1);},Map::putAll);}public static
<T>TreeMap<T,Integer>mapc(final Stream<T>a){return mapc(a,null);}public static<T>
TreeMap<T,Integer>mapc(final Collection<T>a,Comparator<T>cmp){return mapc(a.stream(),
cmp);}public static<T>TreeMap<T,Integer>mapc(final Collection<T>a){return mapc(a.stream());
}public static TreeMap<Integer,List<Integer>>mapi(final int[]a){return IntStream.range(0,
a.length).collect(()->new TreeMap<Integer,List<Integer>>(),(res,i)->{if(!res.containsKey(a[i]))
{res.put(a[i],Stream.of(i).collect(Collectors.toList()));}else{res.get(a[i]).add(i);
}},Map::putAll);}public static TreeMap<Long,List<Integer>>mapi(final long[]a){return
IntStream.range(0,a.length).collect(()->new TreeMap<Long,List<Integer>>(),(res,i)
->{if(!res.containsKey(a[i])){res.put(a[i],Stream.of(i).collect(Collectors.toList()));
}else{res.get(a[i]).add(i);}},Map::putAll);}public static<T>TreeMap<T,List<Integer>>
mapi(final T[]a,Comparator<T>cmp){return IntStream.range(0,a.length).collect(cmp
!=null?()->new TreeMap<T,List<Integer>>(cmp):()->new TreeMap<T,List<Integer>>(),
(res,i)->{if(!res.containsKey(a[i])){res.put(a[i],Stream.of(i).collect(Collectors.toList()));
}else{res.get(a[i]).add(i);}},Map::putAll);}public static<T>TreeMap<T,List<Integer>>
mapi(final T[]a){return mapi(a,null);}public static TreeMap<Integer,List<Integer>>
mapi(final IntStream a){int[]i=new int[]{0};return a.collect(()->new TreeMap<Integer,
List<Integer>>(),(res,v)->{if(!res.containsKey(v)){res.put(v,Stream.of(i[0]).collect(Collectors.toList()));
}else{res.get(v).add(i[0]);}i[0]++;},Map::putAll);}public static TreeMap<Long,List<Integer>>
mapi(final LongStream a){int[]i=new int[]{0};return a.collect(()->new TreeMap<Long,
List<Integer>>(),(res,v)->{if(!res.containsKey(v)){res.put(v,Stream.of(i[0]).collect(Collectors.toList()));
}else{res.get(v).add(i[0]);}i[0]++;},Map::putAll);}public static<T>TreeMap<T,List<Integer>>
mapi(final Stream<T>a,Comparator<T>cmp){int[]i=new int[]{0};return a.collect(cmp
!=null?()->new TreeMap<T,List<Integer>>(cmp):()->new TreeMap<T,List<Integer>>(),
(res,v)->{if(!res.containsKey(v)){res.put(v,Stream.of(i[0]).collect(Collectors.toList()));
}else{res.get(v).add(i[0]);}i[0]++;},Map::putAll);}public static<T>TreeMap<T,List<Integer>>
mapi(final Stream<T>a){return mapi(a,null);}public static<T>TreeMap<T,List<Integer>>
mapi(final Collection<T>a,Comparator<T>cmp){return mapi(a.stream(),cmp);}public
static<T>TreeMap<T,List<Integer>>mapi(final Collection<T>a){return mapi(a.stream());
}public static List<int[]>listi(final int[]a){return IntStream.range(0,a.length).mapToObj(i
->new int[]{a[i],i}).collect(Collectors.toList());}public static List<long[]>listi(final
long[]a){return IntStream.range(0,a.length).mapToObj(i->new long[]{a[i],i}).collect(Collectors.toList());
}public static<T>List<Pair<T,Integer>>listi(final T[]a){return IntStream.range(0,
a.length).mapToObj(i->new Pair<T,Integer>(a[i],i)).collect(Collectors.toList());
}public static List<int[]>listi(final IntStream a){int[]i=new int[]{0};return a.mapToObj(v
->new int[]{v,i[0]++}).collect(Collectors.toList());}public static List<long[]>listi(final
LongStream a){int[]i=new int[]{0};return a.mapToObj(v->new long[]{v,i[0]++}).collect(Collectors.toList());
}public static<T>List<Pair<T,Integer>>listi(final Stream<T>a){int[]i=new int[]{0};
return a.map(v->new Pair<T,Integer>(v,i[0]++)).collect(Collectors.toList());}public
static<T>List<Pair<T,Integer>>listi(final Collection<T>a){int[]i=new int[]{0};return
a.stream().map(v->new Pair<T,Integer>(v,i[0]++)).collect(Collectors.toList());}public
static String join(final int[]a){return Arrays.stream(a).mapToObj(Integer::toString).collect(Collectors.joining(SPACE));
}public static String join(final long[]a){return Arrays.stream(a).mapToObj(Long::toString).collect(Collectors.joining(SPACE));
}public static<T>String join(final T[]a){return Arrays.stream(a).map(v->Objects.toString(v)).collect(Collectors.joining(SPACE));
}public static<T>String join(final T[]a,final Function<T,String>toString){return
Arrays.stream(a).map(v->toString.apply(v)).collect(Collectors.joining(SPACE));}public
static<T>String join(final Collection<T>a){return a.stream().map(v->Objects.toString(v)).collect(Collectors.joining(SPACE));
}public static<T>String join(final Collection<T>a,final Function<T,String>toString)
{return a.stream().map(v->toString.apply(v)).collect(Collectors.joining(SPACE));
}public static<T>String join(final Stream<T>a){return a.map(v->Objects.toString(v)).collect(Collectors.joining(SPACE));
}public static<T>String join(final Stream<T>a,final Function<T,String>toString){
return a.map(v->toString.apply(v)).collect(Collectors.joining(SPACE));}public static
<T>String join(final IntStream a){return a.mapToObj(Integer::toString).collect(Collectors.joining(SPACE));
}public static<T>String join(final LongStream a){return a.mapToObj(Long::toString).collect(Collectors.joining(SPACE));
}public static List<Integer>list(final int[]a){return Arrays.stream(a).mapToObj(Integer::valueOf).collect(Collectors.toList());
}public static List<Integer>list(final IntStream a){return a.mapToObj(Integer::valueOf).collect(Collectors.toList());
}public static List<Long>list(final long[]a){return Arrays.stream(a).mapToObj(Long::valueOf).collect(Collectors.toList());
}public static List<Long>list(final LongStream a){return a.mapToObj(Long::valueOf).collect(Collectors.toList());
}public static<T>List<T>list(final Stream<T>a){return a.collect(Collectors.toList());
}public static<T>List<T>list(final Collection<T>a){return a.stream().collect(Collectors.toList());
}public static<T>List<T>list(final T[]a){return Arrays.stream(a).collect(Collectors.toList());
}public static String yesNo(final boolean res){return res?"YES":"NO";}public static
String dump(Object obj){String res="";if(obj!=null){Class cl=obj.getClass();String
cls=cl.getName();if(cls.startsWith("[")){res+="[";for(int i=0;;i++){try{Object o
=Array.get(obj,i);String s=dump(o);if(i>0){res+=", ";}res+=s;}catch(ArrayIndexOutOfBoundsException
ex){break;}}res+="]";}else if(Collection.class.isAssignableFrom(cl)){@SuppressWarnings("unchecked")final
Object s=((Collection)obj).stream().map(v->dump(v)).collect(Collectors.joining(", ",
"[","]"));res+=s.toString();}else if(Map.class.isAssignableFrom(cl)){@SuppressWarnings("unchecked")final
Object s=((Map)obj).entrySet().stream().map(v->dump(v)).collect(Collectors.joining(", ",
"{","}"));res+=s.toString();}else if(Character.class.isInstance(obj)|| Integer.class.isInstance(obj)
|| Long.class.isInstance(obj)|| Float.class.isInstance(obj)|| Double.class.isInstance(obj)|| String.class.isInstance(obj)
){res+=Objects.toString(obj);}else if(Map.Entry.class.isInstance(obj)){res+=dump(((Map.Entry)obj).getKey())
+"="+dump(((Map.Entry)obj).getValue());}else if(Stream.class.isInstance(obj)){@SuppressWarnings("unchecked")
final Object s=((Stream)obj).map(v->dump(v)).collect(Collectors.joining(", ","[",
"]"));res+=s.toString();}else{res+=Stream.concat(Arrays.stream(obj.getClass().getFields()).map(v
->{String name=v.getName();String val;try{Object o=v.get(obj);if(o!=null && v.isAnnotationPresent(Dump.class))
{Dump an=v.getAnnotation(Dump.class);Class ocl=o.getClass();val="{";for(String fn:an.fields())
{try{Field f=ocl.getField(fn);val+=fn+"="+dump(f.get(o))+", ";}catch(NoSuchFieldException
nsfex){try{@SuppressWarnings("unchecked")final Method m=ocl.getMethod(fn);val+=fn+"="+dump(m.invoke(o))
+", ";}catch(NoSuchMethodException | IllegalArgumentException | InvocationTargetException
nsmex){}}}if(val.endsWith(", ")){val=val.substring(0,val.length()-2);}val+="}";}
else{val=dump(o);}}catch(IllegalAccessException ex){val="N/A";}return name+"="+val;
}),Arrays.stream(obj.getClass().getMethods()).filter(m->m.isAnnotationPresent(Getter.class)).map(m
->{String name=m.getName();String val;try{Object o=m.invoke(obj);if(o!=null && m.isAnnotationPresent(Dump.class))
{Dump an=m.getAnnotation(Dump.class);Class ocl=o.getClass();val="{";for(String fn:an.fields())
{try{Field f=ocl.getField(fn);val+=fn+"="+dump(f.get(o))+", ";}catch(NoSuchFieldException
nsfex){try{@SuppressWarnings("unchecked")final Method m1=ocl.getMethod(fn);val+=fn+"="+dump(m1.invoke(o))
+", ";}catch(NoSuchMethodException | IllegalArgumentException | InvocationTargetException
nsmex){}}}if(val.endsWith(", ")){val=val.substring(0,val.length()-2);}val+="}";}else
{val=dump(o);}}catch(IllegalAccessException | InvocationTargetException ex){val=
"N/A";}return name+"="+val;})).collect(Collectors.joining(", ","{"+obj.getClass().getName()
+": ","}"));}}if(res.length()==0){res="<null>";}return res;}}@Retention(RetentionPolicy.RUNTIME)
public @interface Dump{String[]fields();}@Retention(RetentionPolicy.RUNTIME)public @interface Getter{}static class
MyScanner{private StringBuilder cache=new StringBuilder();int cache_pos=0;private
int first_linebreak=-1;private int second_linebreak=-1;private StringBuilder sb=
new StringBuilder();private InputStream is=null;public MyScanner(final InputStream
is){this.is=is;}public String charToString(final int c){return String.format("'%s'",
c=='\n'?"\\n":(c=='\r'?"\\r":String.valueOf((char)c)));}public int get(){int res
=-1;if(cache_pos<cache.length()){res=cache.charAt(cache_pos);cache_pos++;if(cache_pos
==cache.length()){cache.delete(0,cache_pos);cache_pos=0;}}else{try{res=is.read();
}catch(IOException ex){throw new RuntimeException(ex);}}return res;}private void
unget(final int c){if(cache_pos==0){cache.insert(0,(char)c);}else{cache_pos--;}}
public String nextLine(){sb.delete(0,sb.length());int c;boolean done=false;boolean
end=false;while((c=get())!=-1){if(check_linebreak(c)){done=true;if(c==first_linebreak)
{if(!end){end=true;}else{cache.append((char)c);break;}}else if(second_linebreak!=
-1 && c==second_linebreak){break;}}if(end && c!=first_linebreak && c!=second_linebreak)
{cache.append((char)c);break;}if(!done){sb.append((char)c);}}return sb.toString();
}private boolean check_linebreak(int c){if(c=='\n'|| c=='\r'){if(first_linebreak
==-1){first_linebreak=c;}else if(c!=first_linebreak && second_linebreak==-1){second_linebreak
=c;}return true;}return false;}public int nextInt(){return Integer.parseInt(next());
}public long nextLong(){return Long.parseLong(next());}public boolean hasNext(){
boolean res=false;int c;while((c=get())!=-1){if(!check_linebreak(c)&& c!=' '&& c
!='\t'){res=true;unget(c);break;}}return res;}public String next(){sb.delete(0,sb.length());
boolean started=false;int c;while((c=get())!=-1){if(check_linebreak(c)|| c==' '||
c=='\t'){if(started){unget(c);break;}}else{started=true;sb.append((char)c);}}return
sb.toString();}public int nextChar(){return get();}public boolean eof(){int c=get();
boolean res=false;if(c!=-1){unget(c);}else{res=true;}return res;}public double nextDouble()
{return Double.parseDouble(next());}}static abstract class Solver{protected String
nameIn=null;protected String nameOut=null;protected boolean singleTest=false;protected
MyScanner sc=null;protected PrintWriter pw=null;private int current_test=0;private
int count_tests=0;protected int currentTest(){return current_test;}protected int
countTests(){return count_tests;}private void process()throws IOException{if(!singleTest)
{count_tests=sc.nextInt();sc.nextLine();for(current_test=1;current_test<=count_tests;
current_test++){solve();pw.flush();}}else{count_tests=1;current_test=1;solve();}
}abstract protected void solve()throws IOException;public void run()throws IOException
{boolean done=false;try{if(nameIn!=null){if(new File(nameIn).exists()){try(FileInputStream
fis=new FileInputStream(nameIn);PrintWriter pw0=select_output();){select_output();
done=true;sc=new MyScanner(fis);pw=pw0;process();}}else{throw new RuntimeException("File "
+new File(nameIn).getAbsolutePath()+" does not exist!");}}}catch(IOException | AccessControlException
ex){}if(!done){try(PrintWriter pw0=select_output();){sc=new MyScanner(System.in);
pw=pw0;process();}}}private PrintWriter select_output()throws FileNotFoundException
{if(nameOut!=null){return new PrintWriter(nameOut);}return new PrintWriter(System.out);
}}static abstract class Tester{static public int getRandomInt(final int min,final
int max){return(min+(int)Math.floor(Math.random()*(max-min+1)));}static public long
getRandomLong(final long min,final long max){return(min+(long)Math.floor(Math.random()
*(max-min+1)));}static public double getRandomDouble(final double min,final double
maxExclusive){return(min+Math.random()*(maxExclusive-min));}abstract protected void
testOutput(final List<String>output_data);abstract protected void generateInput();
abstract protected String inputDataToString();private boolean break_tester=false;
protected void beforeTesting(){}protected void breakTester(){break_tester=true;}
public boolean broken(){return break_tester;}}}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
import java.util.*;
import java.math.*;
public class B {
public static void main(String[] args) throws IOException {
/**/
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(System.in)));
/*/
Scanner sc = new Scanner(new BufferedReader(new InputStreamReader(new FileInputStream("src/b.in"))));
/**/
int t = sc.nextInt();
for (int z = 0; z < t; ++z) {
int n = sc.nextInt();
if (n%2==1) {
System.out.println("NO");
continue;
}
n/=2;
int sqrt = (int)Math.sqrt(n);
if (sqrt*sqrt==n) {
System.out.println("YES");
continue;
}
if (n%2==1) {
System.out.println("NO");
continue;
}
n/=2;
sqrt = (int)Math.sqrt(n);
if (sqrt*sqrt==n) {
System.out.println("YES");
continue;
}
System.out.println("NO");
}
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class CodeChef2 {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
//
// static class Line{
// Point p1;
// Point p2;
//
// Line(Point p1,Point p2){
// this.p1=p1;
// this.p2=p2;
// }
//
// static Boolean intersects(Line l1,Line l2)
// {
// Point A=l1.p1, B=l1.p2,C=l2.p1,D=l2.p2;
//
// long a1 = B.y - A.y;
// long b1 = A.x - B.x;
// long c1 = a1*(A.x) + b1*(A.y);
//
// long a2 = D.y - C.y;
// long b2 = C.x - D.x;
// long c2 = a2*(C.x)+ b2*(C.y);
//
// long determinant = a1*b2 - a2*b1;
//
// if (determinant == 0)
// {
// return false;
// }
// else
// {
// long x = (b2*c1 - b1*c2)/determinant;
// long y = (a1*c2 - a2*c1)/determinant;
//
// if(x>=0 || x<=1) {
// return true;
// }
// return false;
// }
// }
//
// static Point getSlope(Line l) {
// long num=l.p1.y - l.p2.y;
// long den=l.p1.x - l.p2.x;
//
// long gcd=gcd(num,den);
// return new Point(num/gcd,den/gcd);
// }
//
// private static long gcd(long a, long b) {
// return b==0?a:gcd(b,a%b);
// }
//
// }
static class Pair{
int x;
long y;
Pair(int x,long y, Integer integer, int i){
this.y=y;
this.x=x;
}
@Override
public String toString() {
return "(" + x +" "+ y+")";
}
}
static class Edge{
int src;
int dest;
int cost;
int val;
Edge(int src,int dest,int cost,int val){
this.src=src;
this.dest=dest;
this.cost=cost;
this.val=val;
}
public String toString() {
return "(" + src +" "+ dest+": "+ cost +" , "+val+")";
}
}
static class Pair2{
Pair node;
int dist;
Pair2(Pair p,int dist){
node=p;
this.dist=dist;
}
}
static long M=1000000007l;
static HashMap<Character,ArrayList<Character>> dirs;
public static void main(String[] args) throws Exception {
FastReader sc=new FastReader();
BufferedWriter bw=new BufferedWriter(new OutputStreamWriter(System.out));
// ArrayList<Integer> prime=getPrimeSieve();
int t=sc.nextInt();
int po=0;
dirs=new HashMap<>();
dirs.put('U', new ArrayList<>());
dirs.get('U').addAll(Arrays.asList('U','R','D','L'));
dirs.put('L', new ArrayList<>());
dirs.get('L').addAll(Arrays.asList('L','U','R','D'));
dirs.put('D', new ArrayList<>());
dirs.get('D').addAll(Arrays.asList('D','L','U','R'));
dirs.put('R', new ArrayList<>());
dirs.get('R').addAll(Arrays.asList('R','D','L','U'));
//
outer:while(t-- >0) {
po++;
int n=sc.nextInt();
int x=(int) Math.sqrt(n/2);
int y=(int) Math.sqrt(n/4);
if(x*x*2 == n || y*y*4==n)
bw.append("YES\n");
else {
bw.append("NO\n");
}
// int x=sc.nextInt();
// Integer[] arr=new Integer[n];
// long sum=0;
//
// for(int i=0;i<n;i++) {
// arr[i]=sc.nextInt();
// sum+=arr[i];
// }
//
// if(sum==x) {
// bw.append("NO\n");
// continue outer;
// }
//
// Arrays.sort(arr,Collections.reverseOrder());
//
// sum=0;
// for(int i=0;i<n-1;i++) {
// sum+=arr[i];
//
// if(sum==x) {
// int temp=arr[i];
// arr[i+1]=arr[i];
// arr[i]=temp;
// sum=sum-arr[i+1]+arr[i];
// }
// }
// bw.append("YES\n");
// for(int i=0;i<n;i++) {
// bw.append(arr[i]+" ");
// }
// bw.append("\n");
// bw.append("Case #"+po+": "+0+"\n");
}
bw.close();
}
private static int abs(int i) {
if(i<0) {
return -i;
}
return i;
}
private static String getRoaring2(String s) {
String res="";
String max="";
for(int i=1;i<=s.length()/2;i++) {
long prev=Long.parseLong(s.substring(0, i));
res=Long.toString(prev);
long prev1=Long.parseLong(res);
long ans=Long.parseLong(s);
long next=prev+1;
while(prev1 <= ans) {
prev1=Long.parseLong(res+Long.toString(next));
res+=Long.toString(next);
next++;
}
if(max.length() == 0) {
max=res;
res="";
}else {
Long a=Long.parseLong(max);
long m=Math.max(a, prev1);
max=Long.toString(m);
}
}
return max;
}
private static String getRoaring(String s) {
int val=-1;
for(int i=1;i<=s.length()/2;i++) {
long prev=Long.parseLong(s.substring(0, i));
int j=i,update=i;
while(j<s.length()) {
if(numDigit(prev+1) > numDigit(prev)) {
update++;
}
if(j+update > s.length()) {
break;
}
long cur=Long.parseLong(s.substring(j, j+update));
if(cur != prev+1) {
break;
}
prev=cur;
j+=update;
}
if(j>= s.length()) {
val=i;
break;
}
}
if(val==-1) {
return "";
}else {
String res="";
long prev=Long.parseLong(s.substring(0, val));
res=Long.toString(prev+1);
System.out.println(res+ " ");
long prev1=Long.parseLong(res);
long ans=Long.parseLong(s);
long next=prev+1;
while(prev1 <= ans) {
prev1=Long.parseLong(res+Long.toString(next));
next++;
}
return Long.toString(prev1);
}
}
private static boolean isRoaring(String s) {
for(int i=1;i<=s.length()/2;i++) {
long prev=Long.parseLong(s.substring(0, i));
// System.out.println("prev= "+prev+" ");
int j=i,update=i;
while(j<s.length()) {
if(numDigit(prev+1) > numDigit(prev)) {
update++;
}
if(j+update > s.length()) {
break;
}
long cur=Long.parseLong(s.substring(j, j+update));
// System.out.println("cur= "+cur+" ");
if(cur != prev+1) {
break;
}
prev=cur;
j+=update;
}
if(j>= s.length()) {
return true;
}
}
return false;
}
private static long numDigit(long ans) {
long sum=0;
while(ans > 0) {
sum++;
ans/=10;
}
return sum;
}
private static boolean go(int i, int j, long n, long m, Integer k, HashMap<Integer, Boolean>[][] dp) {
if(i==n && j==m && k==0) {
return true;
}
if(i<1 || j<1 || i>n || j>m || k<0) {
return false;
}
if(dp[i][j].containsKey(k)) {
return dp[i][j].get(k);
}
boolean down=go(i+1,j,n,m,k-j,dp);
boolean left=go(i,j+1,n,m,k-i,dp);
dp[i][j].put(k, left||down);
return left||down;
}
private static long getDigitSum(long ans) {
long sum=0;
while(ans > 0) {
sum+=ans%10;
ans/=10;
}
return sum;
}
private static boolean getAns2(int l, long[] prefix, long x) {
for(int i=l;i<prefix.length-1;i++) {
if((x^prefix[i]) == (prefix[prefix.length-1]^prefix[i])) {
return true;
}
}
return false;
}
private static boolean getAns(long[] prefix) {
for(int i=0;i<prefix.length-1;i++) {
if(prefix[i] == (prefix[prefix.length - 1]^prefix[i])) {
return true;
}
}
return false;
}
private static void rotate(ArrayList<Integer> arr, int i) {
reverse(arr,0,i-1);
reverse(arr,i,arr.size()-1);
reverse(arr,0,arr.size()-1);
}
private static void reverse(ArrayList<Integer> arr, int l, int m) {
while(l<m) {
int temp=arr.get(l);
arr.set(l,arr.get(m));
arr.set(m, temp);
l++;
m--;
}
}
static int modInverse(int a, int m)
{
int m0 = m;
int y = 0, x = 1;
if (m == 1)
return 0;
while (a > 1) {
int q = a / m;
int t = m;
m = a % m;
a = t;
t = y;
y = x - q * y;
x = t;
}
if (x < 0)
x += m0;
return x;
}
private static long isPerfectSquare(long num) {
long l=1,h=num;
while(l<=h) {
long mid=l+(h-l)/2;
if(mid*mid == num) {
return mid;
}else if(mid*mid < num) {
l=mid+1;
}else {
h=mid-1;
}
}
return -1;
}
private static void rightmax(long[] arr, long n,int[] res,int[] rightmax) {
Deque<Integer> stack=new ArrayDeque<>();
stack.clear();
for(int i=(int) (n-1);i>=0;i--) {
while(!stack.isEmpty() && arr[stack.peek()] <= arr[i]) {
stack.pop();
}
rightmax[i]=(stack.isEmpty()?Integer.MAX_VALUE:stack.peek());
stack.addFirst(i);
}
}
private static boolean rotatedSorted(long[] arr, int min) {
reverse(arr,0,min-1);
reverse(arr,min,arr.length-1);
reverse(arr,0,arr.length-1);
if(isSorted(arr)) {
return true;
}
return false;
}
private static boolean isSorted(long[] arr) {
for(int i=1;i<arr.length;i++) {
if(arr[i] < arr[i-1]) {
return false;
}
}
return true;
}
private static int countDigit(long x) {
int count=0;
while(x > 0) {
x/=10;
count++;
}
return count;
}
private static boolean isSub(String s, String c) {
int l=0;
for(int i=0;i<s.length();i++) {
if(l < c.length() && c.charAt(l)==s.charAt(i)) {
l++;
}
if(l==c.length()) {
break;
}
}
if(l==c.length()) {
return true;
}
return false;
}
static long power(long a, long d, long n)
{
long res = 1;
a = a % n;
if (a == 0)
return 0;
while (d > 0)
{
if ((d & 1) != 0)
res = (res * a) % n;
d = d >> 1;
a = (a * a) % n;
}
return res;
}
private static void reverse(long[] arr,int l,int m) {
while(l<m) {
long temp=arr[l];
arr[l]=arr[m];
arr[m]=temp;
l++;
m--;
}
}
static int UpperBound(ArrayList<Integer> a, int x) {// x is the key or target value
int l=-1,r=a.size();
while(l+1<r) {
int m=(l+r)>>>1;
if(a.get(m)<=x) l=m;
else r=m;
}
return l+1;
}
private static void printMat(int[][] dp) {
System.out.println("--------------------------------------------------------------------");
for(int i=0;i<dp.length;i++) {
for(int j=0;j<dp[0].length;j++) {
System.out.print(dp[i][j]+" ");
}
System.out.println();
}
System.out.println("--------------------------------------------------------------------");
}
private static int highestOneBit(long n) {
long x=Long.highestOneBit(n);
int c=0;
while(x >0) {
x=x/2;
c++;
}
return c-1;
}
private static int bitcount(long l) {
int count=0;
while(l>0) {
l-=(l&(-l));
count++;
}
return count;
}
private static void bfs(HashMap<Integer, HashSet<Integer>> tree, int start) {
Queue<Integer> q=new LinkedList<>();
q.offer(start);
HashSet<Integer> visited=new HashSet<>();
System.out.print(q.peek()+"\n");
while(!q.isEmpty()) {
int parent=q.poll();
if(visited.contains(parent)) {
continue;
}
visited.add(parent);
int flag=0;
for(int child:tree.get(parent)) {
if(!visited.contains(child)) {
q.offer(child);
System.out.print(child+" ");
flag=1;
}
}
if(flag==0) {
continue;
}
System.out.println();
}
}
static int par;
private static HashMap<Integer, HashSet<Integer>> getTreeInputLevel(StringTokenizer st) {
Queue<Integer> q=new LinkedList<>();
HashMap<Integer, HashSet<Integer>> tree=new HashMap<>();
q.offer(Integer.parseInt(st.nextToken()));
par=q.peek();
while(!q.isEmpty()) {
int parent=q.poll();
if(!tree.containsKey(parent)) {
tree.put(parent, new HashSet<Integer>());
}
int left=-1,right=-1;
if(st.hasMoreElements())
left=Integer.parseInt(st.nextToken());
if(st.hasMoreElements())
right=Integer.parseInt(st.nextToken());
if(left != -1) {
tree.get(parent).add(left);
if(!tree.containsKey(left)) {
tree.put(left, new HashSet<Integer>());
}
tree.get(left).add(parent);
q.offer(left);
}
if(right != -1) {
tree.get(parent).add(right);
if(!tree.containsKey(right)) {
tree.put(right, new HashSet<Integer>());
}
tree.get(right).add(parent);
q.offer(right);
}
}
tree.remove(-1);
return tree;
}
private static int containsString(String s1,String s2) {
String s=s1+"#"+s2;
int[] z=getZfunc(s);
boolean flag=false;
for(int i=0;i<s.length();i++) {
if(z[i]==s1.length()) {
flag=true;
}
}
int count=0;
for(int i=s1.length();i<z.length;i++) {
// System.out.print(z[i]+" ");
if(z[i]==s1.length()) {
count++;
}
}
// System.out.println();
return count;
}
private static int[] getZfunc(String s) {
int[] z=new int[s.length()];
int l=0,r=0;
for(int i=1;i<s.length();i++) {
if(i <= r) {
z[i]=Math.min( z[i-l] , r-i+1);
}
while(i+z[i] < s.length() && s.charAt(z[i])==s.charAt(i+z[i])) {
z[i]++;
}
if(i+z[i] -1 > r) {
l=i;
r=i+z[i]-1;
}
}
return z;
}
private static long ceil(long n,long k) {
long ans;
if(n%k==0) {
ans=n/k;
}else {
ans=n/k+1;
}
return ans;
}
static ArrayList<Integer> getDivisor(int n){
ArrayList<Integer> div=new ArrayList<>();
for (int i=1; i*i <= n; i++)
{
if (n%i==0)
{
if (n/i == i)
div.add(i);
else {
div.add(i);
div.add(n/i);
}
}
}
return div;
}
static long gcd(long x,long y) {
return (y==0?x:gcd(y,x%y));
}
static int MAXN = 1000001;
static int[] spf=new int[MAXN];
static void sieveSmallestFactor()
{
spf[1] = 1;
for (int i=2; i<MAXN; i++)
spf[i] = i;
for (int i=4; i<MAXN; i+=2)
spf[i] = 2;
for (int i=3; i*i<MAXN; i++)
{
if (spf[i] == i)
{
for (int j=i*i; j<MAXN; j+=i)
if (spf[j]==j)
spf[j] = i;
}
}
}
private static HashMap<Integer,Integer> PrimeFactorizationmap(long n) {
int count=0;
HashMap<Integer,Integer> factors=new HashMap<>();
if(n==1) {
factors.put( 1,1);
return factors;
}else {
for(long i=2; i*i <= n ;i++) {
long z=n;
if(z%i==0) {
count=0;
while(z%i==0) {
count++;
z=z/i;
}
factors.put((int) (i+0),count);
}
}
if(n>1) {
factors.put((int) (n+0),1);
}
}
return factors;
}
static HashMap<Integer,Integer> getprimeFactors(int n)
{
HashMap<Integer,Integer> ret = new HashMap<>();
while (n > 1)
{
if(ret.containsKey(spf[n])) {
ret.put(spf[n],ret.get(spf[n])+1);
}else {
ret.put(spf[(int) n],1);
}
n = n / spf[n];
}
return ret;
}
static ArrayList<Integer> getPrimeSieve(){
int primesieve[]=new int[1000005];
Arrays.fill(primesieve,0);
for(int i=2;i*i<primesieve.length;i++) {
if(primesieve[i]==0)
for(int j=i*i;j<primesieve.length;j+=i) {
primesieve[j]=1;
}
}
ArrayList<Integer> prime=new ArrayList<>();
for(int i=2;i<primesieve.length;i++) {
if(primesieve[i]==0) {
prime.add(i);
}
}
return prime;
}
private static boolean checkPrimeRM(long n,int k) {
if(n<=4) {
return n==2||n==3;
}
int s=0;
long d=n-1;
while((d&1) != 1) {
d=d/2;
s++;
}
for(int i=0;i<k;i++) {
long a=2+(int)Math.random()*(n-4);
if(isComposite(a,s,d,n)) {
return false;
}
}
return true;
}
private static boolean isComposite(long a, int s, long d, long n) {
long x=power(a,d,n);
if(x==1 || x==n-1) {
return false;
}
for(int i=0;i<s;i++){
if(x%(n-1)==0) {
return false;
}
x=(x*x)%n;
}
return true;
}
public static HashSet<Long> getPrimeLtoR(int l,int r,List<Integer> prime) {
if(l==1) l++;
int[] arr=new int[r-l+1];
Arrays.fill(arr,0);
for(int i: prime ){
if(i*i<=r) {
int j=(l/i)*i;
if(j<l)
j+=i;
for(;j<=r;j+=i) {
if(j!=i)
arr[j-l]=1;
}
}else {
break;
}
}
HashSet<Long> primeLtoR=new HashSet<>();
for(int i=0;i<arr.length;i++) {
if(arr[i]==0) {
primeLtoR.add((i+l+0l));
}
}
return primeLtoR;
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.*;
import java.io.*;
public class B {
static class Scan {
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
public static void sort(int arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(int arr[],int l1,int r1,int l2,int r2) {
int tmp[]=new int[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void sort(long arr[],int l,int r) { //sort(arr,0,n-1);
if(l==r) {
return;
}
int mid=(l+r)/2;
sort(arr,l,mid);
sort(arr,mid+1,r);
merge(arr,l,mid,mid+1,r);
}
public static void merge(long arr[],int l1,int r1,int l2,int r2) {
long tmp[]=new long[r2-l1+1];
int indx1=l1,indx2=l2;
//sorting the two halves using a tmp array
for(int i=0;i<tmp.length;i++) {
if(indx1>r1) {
tmp[i]=arr[indx2];
indx2++;
continue;
}
if(indx2>r2) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
if(arr[indx1]<arr[indx2]) {
tmp[i]=arr[indx1];
indx1++;
continue;
}
tmp[i]=arr[indx2];
indx2++;
}
//Copying the elements of tmp into the main array
for(int i=0,j=l1;i<tmp.length;i++,j++) {
arr[j]=tmp[i];
}
}
public static void main(String args[]) throws IOException {
Scan input=new Scan();
StringBuilder ans=new StringBuilder("");
int test=input.scanInt();
// boolean sq[]=new boolean[1000000005];
// for(int i=1;i*i<sq.length;i++) {
// sq[i*i]=true;
// }
for(int tt=1;tt<=test;tt++) {
int n=input.scanInt();
if(n%2==1) {
ans.append("NO\n");
continue;
}
n/=2;
double sq=Math.sqrt(n);
if(Math.floor(sq)==Math.ceil(sq)) {
ans.append("YES\n");
continue;
}
if(n%2==1) {
ans.append("NO\n");
continue;
}
n/=2;
sq=Math.sqrt(n);
if(Math.floor(sq)==Math.ceil(sq)) {
ans.append("YES\n");
continue;
}
ans.append("NO\n");
}
System.out.println(ans);
}
}
| linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.util.*;
import java.io.*;
public class _G14 {
public static void main(String[] args) {
MyScanner sc = new MyScanner();
PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
int t = sc.nextInt();
Set<Long> square = new HashSet<>();
for (long i = 1; i <= (long) 1e5; i++) square.add(i * i);
while (t-- > 0) {
long n = sc.nextLong();
if ((n % 2 ==0 && square.contains(n / 2))|| (n % 4 == 0 &&square.contains(n / 4))) {
out.println("YES");
} else {
out.println("NO");
}
}
out.close();
}
static void sort(int[] a) {
ArrayList<Integer> q = new ArrayList<>();
for (int i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
static void sort(long[] a) {
ArrayList<Long> q = new ArrayList<>();
for (long i : a) q.add(i);
Collections.sort(q);
for (int i = 0; i < a.length; i++) a[i] = q.get(i);
}
//-----------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;
}
}
} | linear | 1515_B. Phoenix and Puzzle | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.util.Set;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author prakharjain
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int d = in.nextInt();
int[] a = new int[n];
Set<Integer> set = new HashSet<>();
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int ans = 2;
for (int i = 1; i < n; i++) {
if (a[i] - a[i - 1] == 2 * d) {
ans++;
} else if (a[i] - a[i - 1] > 2 * d) {
ans += 2;
}
}
out.println(ans);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
import java.io.*;
public class A {
FastScanner in;
PrintWriter out;
boolean systemIO = true;
public static void quickSort(int[] a, int from, int to) {
if (to - from <= 1) {
return;
}
int i = from;
int j = to - 1;
int x = a[from + (new Random()).nextInt(to - from)];
while (i <= j) {
while (a[i] < x) {
i++;
}
while (a[j] > x) {
j--;
}
if (i <= j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
i++;
j--;
}
}
quickSort(a, from, j + 1);
quickSort(a, j + 1, to);
}
public void solve() {
int n = in.nextInt();
HashSet<Long> ans = new HashSet<>();
Long d = in.nextLong();
long[] a = new long[n];
for (int i = 0; i < a.length; i++) {
a[i] = in.nextInt();
}
for (int i = 0; i < a.length; i++) {
long x = a[i] - d;
boolean flag = true;
for (int j = 0; j < a.length; j++) {
if (Math.abs(a[j] - x) < d) {
flag = false;
break;
}
}
if (flag) {
ans.add(x);
}
x = a[i] + d;
flag = true;
for (int j = 0; j < a.length; j++) {
if (Math.abs(a[j] - x) < d) {
flag = false;
break;
}
}
if (flag) {
ans.add(x);
}
}
out.println(ans.size());
}
public void run() {
try {
if (systemIO) {
in = new FastScanner(System.in);
out = new PrintWriter(System.out);
} else {
in = new FastScanner(new File("input.txt"));
out = new PrintWriter(new File("output.txt"));
}
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
FastScanner(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
FastScanner(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
String nextLine() {
try {
return br.readLine();
} catch (IOException e) {
return null;
}
}
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());
}
}
// AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
public static void main(String[] args) {
new A().run();
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
/*
Keep solving problems.
*/
import java.util.*;
import java.io.*;
public class CFA {
BufferedReader br;
PrintWriter out;
StringTokenizer st;
boolean eof;
private static final long MOD = 1000L * 1000L * 1000L + 7;
private static final int[] dx = {0, -1, 0, 1};
private static final int[] dy = {1, 0, -1, 0};
private static final String yes = "Yes";
private static final String no = "No";
void solve() throws IOException {
int n = nextInt();
long d = nextInt();
long[] arr = nextLongArr(n);
Set<Long> res = new HashSet<>();
for (long cur : arr) {
if (findMin(cur - d, arr) == d) {
res.add(cur - d);
}
if (findMin(cur + d, arr) == d) {
res.add(cur + d);
}
}
outln(res.size());
}
long findMin(long cur, long[] arr) {
long res = Long.MAX_VALUE;
for (long v : arr) {
res = Math.min(res, Math.abs(v - cur));
}
return res;
}
void shuffle(int[] a) {
int n = a.length;
for(int i = 0; i < n; i++) {
int r = i + (int) (Math.random() * (n - i));
int tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
long gcd(long a, long b) {
while(a != 0 && b != 0) {
long c = b;
b = a % b;
a = c;
}
return a + b;
}
private void outln(Object o) {
out.println(o);
}
private void out(Object o) {
out.print(o);
}
private void formatPrint(double val) {
outln(String.format("%.9f%n", val));
}
public CFA() throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
}
public static void main(String[] args) throws IOException {
new CFA();
}
public long[] nextLongArr(int n) throws IOException{
long[] res = new long[n];
for(int i = 0; i < n; i++)
res[i] = nextLong();
return res;
}
public int[] nextIntArr(int n) throws IOException {
int[] res = new int[n];
for(int i = 0; i < n; i++)
res[i] = nextInt();
return res;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return null;
}
}
return st.nextToken();
}
public String nextString() {
try {
return br.readLine();
} catch (IOException e) {
eof = true;
return null;
}
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt(), d = s.nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; i++){
arr[i] = s.nextInt();
}
Arrays.sort(arr);
int count = 0;
for(int i = 1; i < n; i++){
int dist = arr[i] - arr[i - 1];
if(dist > 2 * d){
count += 2;
}else if(dist == 2 * d){
count++;
}
}
System.out.println(count + 2);
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
public class Main {
private static void solve() {
int n = ni();
int d = ni();
int[] a = na(n);
Arrays.sort(a);
Set<Integer> set = new HashSet<>();
for (int i = 0; i < n; i ++) {
int cand1 = a[i] - d;
int cand2 = a[i] + d;
int d1 = d;
int d2 = d;
for (int j = 0; j < n; j ++) {
d1 = Math.min(d1, Math.abs(a[j] - cand1));
d2 = Math.min(d2, Math.abs(a[j] - cand2));
}
if (d1 == d) {
set.add(cand1);
}
if (d2 == d) {
set.add(cand2);
}
}
System.out.println(set.size());
}
public static void main(String[] args) {
new Thread(null, new Runnable() {
@Override
public void run() {
long start = System.currentTimeMillis();
String debug = args.length > 0 ? args[0] : null;
if (debug != null) {
try {
is = java.nio.file.Files.newInputStream(java.nio.file.Paths.get(debug));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
reader = new java.io.BufferedReader(new java.io.InputStreamReader(is), 32768);
solve();
out.flush();
tr((System.currentTimeMillis() - start) + "ms");
}
}, "", 64000000).start();
}
private static java.io.InputStream is = System.in;
private static java.io.PrintWriter out = new java.io.PrintWriter(System.out);
private static java.util.StringTokenizer tokenizer = null;
private static java.io.BufferedReader reader;
public static String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new java.util.StringTokenizer(reader.readLine());
} catch (Exception e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
private static double nd() {
return Double.parseDouble(next());
}
private static long nl() {
return Long.parseLong(next());
}
private static int[] na(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = ni();
return a;
}
private static char[] ns() {
return next().toCharArray();
}
private static long[] nal(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nl();
return a;
}
private static int[][] ntable(int n, int m) {
int[][] table = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
table[i][j] = ni();
}
}
return table;
}
private static int[][] nlist(int n, int m) {
int[][] table = new int[m][n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
table[j][i] = ni();
}
}
return table;
}
private static int ni() {
return Integer.parseInt(next());
}
private static void tr(Object... o) {
if (is != System.in)
System.out.println(java.util.Arrays.deepToString(o));
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashSet;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt(), d = in.nextInt();
long a[] = in.parseLong1D(n);
int cnt = 0;
HashSet<Long> ans = new HashSet<>();
for (long v : a) {
long c = v - d;
if (isPos(a, c, d)) ans.add(c);
c = v + d;
if (isPos(a, c, d)) ans.add(c);
}
out.println(ans.size());
}
private boolean isPos(long a[], long c, long d) {
for (long v : a) {
if (Math.abs(v - c) < d) return false;
}
return true;
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void println(int i) {
writer.println(i);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long readLong() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int nextInt() {
return readInt();
}
public long[] parseLong1D(int n) {
long r[] = new long[n];
for (int i = 0; i < n; i++) {
r[i] = readLong();
}
return r;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
// package CF;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
import java.util.TreeSet;
public class F {
public static void main(String[] args) throws Exception
{
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt(), d = sc.nextInt();
int [] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
Arrays.sort(a);
TreeSet<Integer> set = new TreeSet<>();
for (int i = 0; i < a.length; i++) {
int tmp = a[i] - d;
if(i == 0 || tmp > a[i-1] && tmp - a[i-1] >= d)
set.add(tmp);
tmp = a[i] + d;
if(i == n-1 || tmp < a[i+1] && a[i+1] - tmp >= d)
set.add(tmp);
}
out.println(set.size());
out.flush();
out.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s)
{
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader fileReader)
{
br = new BufferedReader(fileReader);
}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(next());
}
public long nextLong() throws IOException
{
return Long.parseLong(next());
}
public String nextLine() throws IOException
{
return br.readLine();
}
public boolean ready() throws IOException
{
return br.ready();
}
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
//package contests.CF495;
import java.io.*;
import java.util.HashSet;
import java.util.StringTokenizer;
public class A {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int d = sc.nextInt();
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
HashSet<Integer> set = new HashSet<>();
for (int i = 0; i < arr.length; i++) {
set.add(arr[i]+d);
set.add(arr[i]-d);
}
int cnt = 0;
for (int loc: set) {
int minDist = (int)2e9;
for (int i = 0; i < n; i++) {
minDist = Math.min(minDist, Math.abs(arr[i]-loc));
}
if(minDist == d)
cnt++;
}
pw.println(cnt);
pw.flush();
pw.close();
}
static int[][] packD(int n, int[] from, int[] to) {
int[][] g = new int[n][];
int[] p = new int[n];
for (int f : from) if(f != -1) p[f]++;
for (int i = 0; i < n; i++) g[i] = new int[p[i]];
for (int i = 0; i < from.length; i++) if(from[i] != -1) {g[from[i]][--p[from[i]]] = to[i];}
return g;
}
static void shuffle(int[] a)
{
int n = a.length;
for(int i = 0; i < n; i++)
{
int r = i + (int)(Math.random() * (n - i));
int tmp = a[i];
a[i] = a[r];
a[r] = tmp;
}
}
static class Scanner
{
StringTokenizer st; BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public Scanner(String s) throws FileNotFoundException { br = new BufferedReader(new FileReader(new File(s)));}
public String next() throws IOException {while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());return st.nextToken();}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready();}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
import java.io.*;
public class A6
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int d = in.nextInt();
int ans=2;
int[] a = new int[n];
for(int i=0;i<n;i++)
a[i] = in.nextInt();
for(int i=1;i<n;i++)
{
if(a[i]-a[i-1]>2*d)
{
ans += 2;
}
else if(a[i]-a[i-1]==2*d)
ans += 1;
}
System.out.println(ans);
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
String fileName = "<name>";
public TreeSet<Integer> set = new TreeSet<>();
public int getLowerDist(int x) {
Integer higher = set.higher(x);
Integer lower = set.lower(x);
if (higher == null)
return lower;
if (lower == null)
return higher;
if (Math.abs(x - higher) < Math.abs(x - lower)) {
return higher;
} else {
return lower;
}
}
public void solve() throws IOException {
int n = nextInt();
int d = nextInt();
int[] a = new int[n];
Set<Integer> ans = new HashSet<>((int) 1e6, 1f);
for (int i = 0; i < n; i++) {
a[i] = nextInt();
set.add(a[i]);
}
for (int i = 0; i < n; i++) {
int pos1 = a[i] + d;
int pos2 = a[i] - d;
if (!set.contains(pos1) && Math.abs(pos1 - getLowerDist(pos1)) == d) {
ans.add(pos1);
}
if (!set.contains(pos2) && Math.abs(pos2 - getLowerDist(pos2)) == d) {
ans.add(pos2);
}
}
out.print(ans.size());
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
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 {
Locale.setDefault(Locale.US);
new A().run();
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
public class Hotels {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int[] cities = new int[n];
int d = input.nextInt();
for (int i = 0; i<n; i++) {
cities[i] = input.nextInt();
}
int possibilities = 0;
ArrayList<Integer> newHotels = new ArrayList<Integer>();
for (int i = 0; i<n; i++) {
int plusD = cities[i]+d;
if (newHotels.indexOf(cities[i]+d)==-1 && minDist(plusD,cities)==d) {
possibilities++;
newHotels.add(cities[i]+d);
}
if (newHotels.indexOf(cities[i]-d)==-1 && minDist(cities[i]-d,cities)==d) {
possibilities++;
newHotels.add(cities[i]-d);
}
}
System.out.println(possibilities);
}
public static int minDist(int a, int[] arr) {
int minDist = Integer.MAX_VALUE;
for (int i=0; i<arr.length; i++) {
minDist = Math.min(Math.abs(arr[i]-a), minDist);
}
return minDist;
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class main {
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter pw = new PrintWriter(System.out);
int n = nextInt();
int d = nextInt();
int ans = 2;
int b[] = new int [n];
Arrays.sort(b);
for (int i = 0; i < n; i++) {
b[i] = nextInt();
}
for (int i = 1; i < n; i++) {
if (b[i] - b[i - 1] >= d * 2) {
ans++;
}
if (b[i] - b[i - 1] > d * 2) {
ans++;
}
}
pw.println(ans);
pw.close();
}
static BufferedReader br;
static StringTokenizer st = new StringTokenizer("");
public static int nextInt() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return Integer.parseInt(st.nextToken());
}
public static String next() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public static double nextDouble() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return Double.parseDouble(st.nextToken());
}
public static long nextLong() throws IOException {
if (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return Long.parseLong(st.nextToken());
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class A {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
// Scanner scan = new Scanner(System.in);
// PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// int n = Integer.parseInt(bf.readLine());
StringTokenizer st = new StringTokenizer(bf.readLine());
// int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken());
int n = Integer.parseInt(st.nextToken());
int d = Integer.parseInt(st.nextToken());
st = new StringTokenizer(bf.readLine());
int[] a = new int[n]; for(int i=0; i<n; i++) a[i] = Integer.parseInt(st.nextToken());
int ans = 2;
for(int i=0; i<n-1; i++) {
int diff = a[i+1]-a[i];
if(diff == 2*d) ans++;
else if(diff > 2*d) ans += 2;
}
System.out.println(ans);
// int n = scan.nextInt();
// out.close(); System.exit(0);
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.util.*;
import java.lang.*;
import java.math.BigInteger;
import java.math.BigDecimal;
public class Main {
static class Task {
void solve(int test, FastScanner in, PrintWriter out) throws IOException {
int n = in.nextInt();
int d = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
Arrays.sort(a);
int ans = 2;
for (int i = 0; i < n - 1; i++) {
if (a[i + 1] - d > a[i] + d) {
ans += 2;
} else if (a[i + 1] - d >= a[i] + d) {
ans++;
}
}
out.println(ans);
}
}
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
// FastScanner in = new FastScanner("input.txt");
// PrintWriter out = new PrintWriter(new FileWriter("output.txt"));
new Task().solve(1, in, out);
out.close();
}
static class FastScanner {
BufferedReader br;
StringTokenizer token;
public FastScanner(InputStream is) {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public String nextToken() {
while (token == null || !token.hasMoreTokens()) {
try {
token = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return token.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class A {
static StringTokenizer st;
static BufferedReader br;
static PrintWriter pw;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
int n = nextInt();
int d = nextInt();
int[]x = new int[n];
for (int i = 0; i < n; i++) {
x[i] = nextInt();
}
int ans = 2;
for (int i = 1; i < n; i++) {
if (x[i]-x[i-1]==2*d)
ans++;
else if (x[i]-x[i-1] > 2*d)
ans += 2;
}
System.out.println(ans);
pw.close();
}
private static int nextInt() throws IOException {
return Integer.parseInt(next());
}
private static long nextLong() throws IOException {
return Long.parseLong(next());
}
private static double nextDouble() throws IOException {
return Double.parseDouble(next());
}
private static String next() throws IOException {
while (st==null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class Main implements Runnable {
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
private BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
}
catch (IOException e) {
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine() {
String str = "";
try {
str = br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return str;
}
public int nextInt() {
int c = read();
while(isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if(c<'0'||c>'9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public long nextLong() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
}
if (c == '.') {
c = read();
double m = 1;
while (!isSpaceChar(c)) {
if (c == 'e' || c == 'E')
return res * Math.pow(10, nextInt());
if (c < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
}
while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return readString();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
public static void main(String args[]) throws Exception {
new Thread(null, new Main(),"Main",1<<26).start();
}
public void run() {
InputReader sc = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
int n = sc.nextInt();
long d = sc.nextLong();
long a[] = new long[n];
for(int i = 0; i < n; ++i)
a[i] = sc.nextLong();
int ans = 0;
for(int i = 1; i < n; ++i) {
if(a[i] - a[i - 1] > 2 * d)
ans += 2;
else if(a[i] - a[i - 1] == 2 * d)
ans++;
}
ans += 2;
w.print(ans);
w.close();
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in Actual solution is at the top
*
* @author MaxHeap
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n = in.nextInt();
int d = in.nextInt();
Set<Integer> pts = new HashSet<>();
int[] x = in.nextIntArray(n);
for (int i = 0; i < n; i++) {
pts.add(x[i] - d);
pts.add(x[i] + d);
}
Set<Integer> ans = new HashSet<>();
for (int pt : pts) {
int min = (int) (1e9 + 10);
for (int i = 0; i < n; i++) {
min = Math.min(Math.abs(x[i] - pt), min);
}
if (min >= d) {
ans.add(pt);
}
}
out.println(ans.size());
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1 << 13];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (this.numChars == -1) {
throw new UnknownError();
} else {
if (this.curChar >= this.numChars) {
this.curChar = 0;
try {
this.numChars = this.stream.read(this.buf);
} catch (IOException ex) {
throw new InputMismatchException();
}
if (this.numChars <= 0) {
return -1;
}
}
return this.buf[this.curChar++];
}
}
public int nextInt() {
int c;
for (c = this.read(); isSpaceChar(c); c = this.read()) {
}
byte sgn = 1;
if (c == 45) {
sgn = -1;
c = this.read();
}
int res = 0;
while (c >= 48 && c <= 57) {
res *= 10;
res += c - 48;
c = this.read();
if (isSpaceChar(c)) {
return res * sgn;
}
}
throw new InputMismatchException();
}
public static boolean isSpaceChar(int c) {
return c == 32 || c == 10 || c == 13 || c == 9 || c == -1;
}
public int[] nextIntArray(int n) {
int[] arr = new int[n];
for (int i = 0; i < n; i++) {
arr[i] = nextInt();
}
return arr;
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskA solver = new TaskA();
solver.solve(1, in, out);
out.close();
}
static class TaskA {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int d = in.nextInt();
int[] x = new int[n + 1];
int ans = 2;
for (int i = 1; i <= n; i++) x[i] = in.nextInt();
for (int i = 1; i < n; i++) {
ans += (x[i + 1] - x[i] >= 2 * d) ? (x[i + 1] - x[i] == 2 * d ? 1 : 2) : 0;
}
out.print(ans);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0) {
writer.print(' ');
}
writer.print(objects[i]);
}
}
public void close() {
writer.close();
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c)) c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
boolean isSpaceChar(int ch);
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import javafx.collections.transformation.SortedList;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
Scan scan = new Scan();
int n = scan.scanInt();
long d = scan.scanLong();
long a[]=new long[n];
for(int i=0;i<n;i++){
a[i]=scan.scanLong();
}
Arrays.sort(a);
int count=0;
for(int i=0;i<n-1;i++){
if((a[i+1]-d)>(a[i]+d)){
count+=2;
}else if((a[i+1]-d)==(a[i]+d)){
count++;
}
}
count+=2;
System.out.println(count);
}
static class Scan
{
private byte[] buf=new byte[1024];
private int index;
private InputStream in;
private int total;
public Scan()
{
in=System.in;
}
public int scan()throws IOException
{
if(total<0)
throw new InputMismatchException();
if(index>=total)
{
index=0;
total=in.read(buf);
if(total<=0)
return -1;
}
return buf[index++];
}
public int scanInt()throws IOException
{
int integer=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
integer*=10;
integer+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
return neg*integer;
}
public char scanchar()throws IOException
{
int n=scan();
while(isWhiteSpace(n))
n=scan();
return (char)n;
// int neg=1;
// while(!isWhiteSpace(n))
// {
// if(n>='0'&&n<='9')
// {
// integer*=10;
// integer+=n-'0';
// n=scan();
// }
// else throw new InputMismatchException();
// }
// return neg*integer;
}
public long scanLong()throws IOException
{
long lng=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n) && n!='.')
{
if(n>='0'&&n<='9')
{
lng*=10;
lng+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
long temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
lng+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return neg*lng;
}
public double scanDouble()throws IOException
{
double doub=0;
int n=scan();
while(isWhiteSpace(n))
n=scan();
int neg=1;
if(n=='-')
{
neg=-1;
n=scan();
}
while(!isWhiteSpace(n)&&n!='.')
{
if(n>='0'&&n<='9')
{
doub*=10;
doub+=n-'0';
n=scan();
}
else throw new InputMismatchException();
}
if(n=='.')
{
n=scan();
double temp=1;
while(!isWhiteSpace(n))
{
if(n>='0'&&n<='9')
{
temp/=10;
doub+=(n-'0')*temp;
n=scan();
}
else throw new InputMismatchException();
}
}
return doub*neg;
}
public String scanString()throws IOException
{
StringBuilder sb=new StringBuilder();
int n=scan();
while(isWhiteSpace(n))
n=scan();
while(!isWhiteSpace(n))
{
sb.append((char)n);
n=scan();
}
return sb.toString();
}
private boolean isWhiteSpace(int n)
{
if(n==' '||n=='\n'||n=='\r'||n=='\t'||n==-1)
return true;
return false;
}
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
//package que_a;
import java.io.*;
import java.util.*;
import java.math.*;
public class utkarsh {
InputStream is;
PrintWriter out;
long mod = (long) (1e9 + 7);
boolean SHOW_TIME;
void solve() {
//Enter code here utkarsh
//SHOW_TIME = true;
int n = ni();
int d = ni();
int a[] = na(n);
Arrays.sort(a);
int ans = 2;
for(int i = 0; i < n-1; i++) {
if(a[i+1] - a[i] == (2 * d)) ans++;
else if(a[i+1] - a[i] > (2 * d)) ans += 2;
}
out.println(ans);
}
//---------- I/O Template ----------
public static void main(String[] args) { new utkarsh().run(); }
void run() {
is = System.in;
out = new PrintWriter(System.out);
long start = System.currentTimeMillis();
solve();
long end = System.currentTimeMillis();
if(SHOW_TIME) out.println("\n" + (end - start) + " ms");
out.flush();
}
byte input[] = new byte[1024];
int len = 0, ptr = 0;
int readByte() {
if(ptr >= len) { ptr = 0;
try { len = is.read(input); }
catch(IOException e) { throw new InputMismatchException(); }
if(len <= 0) { return -1; }
} return input[ptr++];
}
boolean isSpaceChar(int c) { return !( c >= 33 && c <= 126 ); }
int skip() {
int b = readByte();
while(b != -1 && isSpaceChar(b)) { b = readByte(); }
return b;
}
char nc() { return (char)skip(); }
String ns() {
int b = skip();
StringBuilder sb = new StringBuilder();
while(!isSpaceChar(b)) { sb.appendCodePoint(b); b = readByte(); }
return sb.toString();
}
String nLine() {
int b = skip();
StringBuilder sb = new StringBuilder();
while( !(isSpaceChar(b) && b != ' ') ) { sb.appendCodePoint(b); b = readByte(); }
return sb.toString();
}
int ni() {
int n = 0, b = readByte();
boolean minus = false;
while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
if(b == '-') { minus = true; b = readByte(); }
if(b == -1) { return -1; } //no input
while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
return minus ? -n : n;
}
long nl() {
long n = 0L; int b = readByte();
boolean minus = false;
while(b != -1 && !( (b >= '0' && b <= '9') || b == '-')) { b = readByte(); }
if(b == '-') { minus = true; b = readByte(); }
while(b >= '0' && b <= '9') { n = n * 10 + (b - '0'); b = readByte(); }
return minus ? -n : n;
}
double nd() { return Double.parseDouble(ns()); }
float nf() { return Float.parseFloat(ns()); }
int[] na(int n) {
int a[] = new int[n];
for(int i = 0; i < n; i++) { a[i] = ni(); }
return a;
}
char[] ns(int n) {
char c[] = new char[n];
int i, b = skip();
for(i = 0; i < n; i++) {
if(isSpaceChar(b)) { break; }
c[i] = (char)b; b = readByte();
} return i == n ? c : Arrays.copyOf(c,i);
}
} | linear | 1004_A. Sonya and Hotels | CODEFORCES |
//package CodeforcesJava;
import java.io.*;
import java.util.*;
public class Main {
public void solve(InputProvider input, PrintWriter output) throws IOException {
int n = input.nextInt();
int d = input.nextInt();
int count = 1;
int current = input.nextInt();
for (int i = 1; i < n; i++) {
int x = input.nextInt();
if (x - current == d * 2) {
count++;
} else if (x - current > d * 2) {
count += 2;
}
current = x;
}
count++;
output.print(count);
}
public static void main(String[] args) throws Exception {
try (InputProvider input = new InputProvider(System.in);
PrintWriter output = new PrintWriter(System.out)) {
new Main().solve(input, output);
}
}
public static class InputProvider implements AutoCloseable {
private final BufferedReader reader;
private StringTokenizer tokenizer;
public InputProvider(Reader reader) {
this.reader = new BufferedReader(reader);
}
public InputProvider(InputStream input) {
reader = new BufferedReader(new InputStreamReader(input));
}
public String next() throws IOException {
if (Objects.isNull(tokenizer) || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(reader.readLine());
return tokenizer.nextToken();
}
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());
}
public String nextLine() throws IOException {
return reader.readLine();
}
@Override
public void close() throws Exception {
reader.close();
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
public static boolean ok(int []x,int d,int X)
{
for(int i=0;i<x.length;i++)
if(Math.abs(x[i]-X)<d)
return false;
return true;
}
public static void main(String[] args) throws IOException {
Scanner sc=new Scanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
int ans=0;
int n=sc.nextInt(),d=sc.nextInt();
TreeSet<Integer> set=new TreeSet();
int []x=new int [n];
for(int i=0;i<n;i++)
x[i]=sc.nextInt();
for(int i=0;i<n;i++)
{
int x1=x[i]+d;
if (ok(x,d,x1))
set.add(x1);
x1=x[i]-d;
if (ok(x,d,x1))
set.add(x1);
}
pw.println(set.size());
pw.close();
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public Scanner(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(s));
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| linear | 1004_A. Sonya and Hotels | CODEFORCES |