src
stringlengths 95
64.6k
| complexity
stringclasses 7
values | problem
stringlengths 6
50
| from
stringclasses 1
value |
---|---|---|---|
import java.io.*;
import java.util.*;
public class Main
{
class node{
int data;
node next;
public node(int val){
data=val;
next=null;
}
}
class linkedlist{
node start;
node end;
int size;
int turn;
public linkedlist(){
start=null;
end=null;
size=0;
}
void add(int val){
if(size==0){
node t=new node(val);
start=t;
end=t;
size++;
}
else{
node t=new node(val);
end.next=t;
end=end.next;
size++;
}
}
void myfunc(){
if(start.data>start.next.data){
// System.out.println("me ni hu");
node t=new node(start.next.data);
start.next=start.next.next;
end.next=t;
end=end.next;
}
else{
// System.out.println("me hu");
int t=start.data;
start=start.next;
add(t);
size--;
}
}
int findmax(){
int m=0;
node temp=start;
for(int i=0;i<size;i++){
if(temp.data>m){
m=temp.data;
}
temp=temp.next;
}
return m;
}
void display(){
node temp=start;
for(int i=0;i<size;i++){
System.out.print(temp.data+" ");
temp=temp.next;
}
System.out.println("");
}
}
linkedlist l;
public Main(){
l=new linkedlist();
}
public static void main(String [] argv) throws IOException
{
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
Main ma=new Main();
String[] l1=in.readLine().split(" ");
int n=Integer.parseInt(l1[0]);
int q=Integer.parseInt(l1[1]);
String[] ar=in.readLine().split(" ");
int a1=Integer.parseInt(ar[0]);
int b1=Integer.parseInt(ar[1]);
for(int i=0;i<n;i++){
ma.l.add(Integer.parseInt(ar[i]));
}
int m=ma.l.findmax();
int[][] pair=new int[n][2];
int t=0;
for(int i=0;i<n;i++){
if(ma.l.start.data==m)
break;
ma.l.myfunc();
pair[t][0]=ma.l.start.data;
pair[t][1]=ma.l.start.next.data;
t++;
}
int rl[]=new int[n];
node temp=ma.l.start;
for(int i=0;i<n;i++){
rl[i]=temp.data;
temp=temp.next;
}
for(int i=0;i<q;i++){
long a=Long.parseLong(in.readLine());
if(a==1){
System.out.println(a1 + " " + b1);
}
else{
if(a<=t+1){
System.out.println(pair[(int)(a-2)][0]+" "+pair[(int)(a-2)][1]);
}
else{
if((a-t)%(n-1)==0){
System.out.println(rl[0]+" "+rl[n-1]);
}
else{
System.out.println(rl[0]+" "+rl[(int)((a-t)%(n-1))]);
}
}
}
}
}
} | linear | 1179_A. Valeriy and Deque | CODEFORCES |
import java.io.*;
import java.util.*;
public class D999 {
public static void main(String args[])throws IOException
{
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int m=sc.nextInt();
int req=n/m;
int arr[]=new int[n+1];
int size[]=new int[m];
List<Integer> list[]=new ArrayList[m];
for(int i=0;i<m;i++)
{
list[i]=new ArrayList<>();
}
for(int i=1;i<=n;i++)
{
arr[i]=sc.nextInt();
size[arr[i]%m]++;
list[arr[i]%m].add(i);
}
long tot=0;int x=0,y=0;
List<Integer> idx=new ArrayList<>();
for(int i=0;i < 2*m;i++)
{
//System.out.println(i+" "+size[i%m]);
if(size[i%m]>req)
{
for(int j=0;j<size[i%m]-req;j++)
{
idx.add(list[i%m].get(j));
y++;
}
size[i%m]=req;
//System.out.println(i+" "+x+" "+y);
}
else if(size[i%m]<req)
{
//System.out.println(idx+" "+i);
while(x!=y && size[i%m]<req)
{
int num=arr[idx.get(x)];
int gg=i-num%m;
tot+=gg;
arr[idx.get(x)]+=gg;
x++;
size[i%m]++;
}
}
}
System.out.println(tot);
for(int i=1;i<=n;i++)
{
System.out.print(arr[i]+" ");
}
}
} | linear | 999_D. Equalize the Remainders | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* @author Don Li
*/
public class EhabAndAComponentChoosingProblem {
long INF = (long) 1e18;
int n;
int[] a;
int[][] G;
void solve() {
n = in.nextInt();
a = new int[n];
for (int i = 0; i < n; i++) a[i] = in.nextInt();
int[] fr = new int[n - 1], to = new int[n - 1];
for (int i = 0; i < n - 1; i++) {
fr[i] = in.nextInt() - 1;
to[i] = in.nextInt() - 1;
}
G = build_graph(n, fr, to);
int[][] ret = bfs(G, 0);
int[] par = ret[0], ord = ret[2];
long best = -INF;
long[] dp = new long[n];
for (int i = n - 1; i >= 0; i--) {
int u = ord[i];
dp[u] = a[u];
for (int v : G[u]) {
if (v != par[u]) {
if (dp[v] > 0) dp[u] += dp[v];
}
}
best = Math.max(best, dp[u]);
}
int k = 0;
for (int i = n - 1; i >= 0; i--) {
int u = ord[i];
dp[u] = a[u];
for (int v : G[u]) {
if (v != par[u]) {
if (dp[v] > 0) dp[u] += dp[v];
}
}
if (dp[u] == best) {
dp[u] = -INF;
k++;
}
}
out.printf("%d %d%n", best * k, k);
}
int[][] bfs(int[][] G, int root) {
int n = G.length;
int[] par = new int[n];
Arrays.fill(par, -1);
int[] dep = new int[n];
dep[root] = 0;
int[] qu = new int[n];
qu[0] = root;
for (int l = 0, r = 1; l < r; l++) {
int u = qu[l];
for (int v : G[u]) {
if (v != par[u]) {
qu[r++] = v;
par[v] = u;
dep[v] = dep[u] + 1;
}
}
}
return new int[][]{par, dep, qu};
}
int[][] build_graph(int n, int[] from, int[] to) {
int[][] G = new int[n][];
int[] cnt = new int[n];
for (int i = 0; i < from.length; i++) {
cnt[from[i]]++;
cnt[to[i]]++;
}
for (int i = 0; i < n; i++) G[i] = new int[cnt[i]];
for (int i = 0; i < from.length; i++) {
G[from[i]][--cnt[from[i]]] = to[i];
G[to[i]][--cnt[to[i]]] = from[i];
}
return G;
}
public static void main(String[] args) {
in = new FastScanner(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
new EhabAndAComponentChoosingProblem().solve();
out.close();
}
static FastScanner in;
static PrintWriter out;
static class FastScanner {
BufferedReader in;
StringTokenizer st;
public FastScanner(BufferedReader in) {
this.in = in;
}
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(in.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
public long nextLong() {
return Long.parseLong(nextToken());
}
public double nextDouble() {
return Double.parseDouble(nextToken());
}
}
}
| linear | 1088_E. Ehab and a component choosing problem | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main implements Runnable {
public void _main() throws IOException {
int n = nextInt();
int k = nextInt();
boolean[] p = new boolean[n + 1];
Arrays.fill(p, true);
List<Integer> primes = new ArrayList<Integer>();
for (int i = 2; i <= n; i++)
if (p[i]) {
primes.add(i);
for (int j = i + i; j <= n; j += i)
p[j] = false;
}
boolean[] ok = new boolean[n + 1];
for (int i = 0; i < primes.size() - 1; i++) {
int x = primes.get(i);
int y = primes.get(i + 1);
if (x + y + 1 <= n)
ok[x + y + 1] = true;
}
for (int i = 2; i <= n; i++)
if (p[i] && ok[i]) {
--k;
}
out.println(k <= 0 ? "YES" : "NO");
}
private BufferedReader in;
private PrintWriter out;
private StringTokenizer st;
private String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
String rl = in.readLine();
if (rl == null)
return null;
st = new StringTokenizer(rl);
}
return st.nextToken();
}
private int nextInt() throws IOException {
return Integer.parseInt(next());
}
private long nextLong() throws IOException {
return Long.parseLong(next());
}
private double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) {
new Thread(new Main()).start();
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
_main();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(202);
}
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main {
BufferedReader in;
PrintWriter out;
StringTokenizer st;
void solve() throws IOException {
int n=ni();
int k=ni();
boolean[] t = new boolean[n+1];
for(int i=2;i<=n;i++){
t[i]=false;
}
int p=2;
while(true){
int pointer=2;
while(pointer*p<=n){
t[pointer*p]=true;
pointer++;
}
boolean flag=false;
for(int i=p+1;i<=n;i++){
if(!t[i]){p=i;flag=true;break;}
}
if(!flag)break;
}
List<Integer> lst=new ArrayList<Integer>();
int countN=0;
for(int i=1;i<=n;i++){
if(!t[i]){lst.add(i);countN++; }
}
int count=0;
String resulPO="NO";
for(int i=2;i<countN;i++){
boolean result=false;
for(int j=0;j<i;j++){
if(lst.get(j)+lst.get(j+1)+1==lst.get(i)){
result=true;
//out.println(lst.get(j)+"+"+lst.get(j+1)+"+"+1+"=="+lst.get(i));
break;
}
}
if(result)count++;
}
if(count>=k)resulPO="YES";
else resulPO="NO";
out.print(resulPO);
}
public Main() throws IOException {
Locale.setDefault(Locale.US);
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
String ns() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int ni() throws IOException {
return Integer.valueOf(ns());
}
long nl() throws IOException {
return Long.valueOf(ns());
}
double nd() throws IOException {
return Double.valueOf(ns());
}
public static void main(String[] args) throws IOException {
new Main();
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.*;
import java.lang.*;
import java.math.*;
import java.io.*;
public class a {
boolean[] isp;
ArrayList<Integer> primes;
private void solve() throws Exception {
int n = nextInt();
int k = nextInt();
int cnt = 0;
primes = new ArrayList<Integer>();
isp = new boolean[n + 1];
Arrays.fill(isp, true);
for (int i = 2; i <= n; ++i) {
for (int j = 2; j * j <= i; ++j)
if (i % j == 0)
isp[i] = false;
if (isp[i])
primes.add(i);
}
for (int i = 2; i <= n; ++i)
if (isp[i]) {
boolean can = false;
for (int j = 0; j < primes.size() - 1; ++j) {
int sum = primes.get(j) + primes.get(j + 1) + 1;
if (i == sum)
can = true;
}
if (can)
++cnt;
}
if (cnt >= k)
out.print("YES");
else
out.print("NO");
}
public void run() {
try {
solve();
} catch (Exception e) {
NOO(e);
} finally {
out.close();
}
}
PrintWriter out;
BufferedReader in;
StringTokenizer St;
void NOO(Exception e) {
e.printStackTrace();
System.exit(1);
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
String nextToken() {
while (!St.hasMoreTokens()) {
try {
String line = in.readLine();
St = new StringTokenizer(line);
} catch (Exception e) {
NOO(e);
}
}
return St.nextToken();
}
private a(String name) {
try {
in = new BufferedReader(new FileReader(name + ".in"));
St = new StringTokenizer("");
out = new PrintWriter(new FileWriter(name + ".out"));
} catch (Exception e) {
NOO(e);
}
}
private a() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
St = new StringTokenizer("");
out = new PrintWriter(System.out);
} catch (Exception e) {
NOO(e);
}
}
public static void main(String[] args) {
Locale.setDefault(Locale.US);
new a().run();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
//package round17;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
public class A {
static StreamTokenizer in =
new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static int nextInt() throws IOException{
in.nextToken();
return (int)in.nval;
}
static PrintWriter out = new PrintWriter(System.out);
static boolean prime(int n){
int j = 2;
while (j*j <= n)
if (n%j == 0) return false;
else j++;
return true;
}
public static void main(String[] args) throws IOException{
int n = nextInt(),
k = nextInt(),
a[] = new int[n];
int s = 0;
for (int i=2; i<=n; i++)
if (prime(i))
a[s++] = i;
int m = 0;
for (int i=2; i<s; i++)
for (int j=i-1; j>0; j--)
if (a[i] == a[j]+a[j-1]+1){
m++;
break;
}
if (m >= k) out.println("YES");
else out.println("NO");
out.flush();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.BitSet;
import java.util.Scanner;
public class A {
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws Exception {
BitSet primes = primes(1001);
int N = sc.nextInt();
int K = sc.nextInt();
int count = 0;
for (int i = 2; i <= N; ++i) {
if (!primes.get(i)) continue;
int res = i - 1;
boolean found = false;
for (int j = 2; j <= i / 2; ++j) {
if (primes.get(j) && primes.nextSetBit(j + 1) == res - j) {
found = true;
break;
}
}
if (found) {
++count;
}
}
System.out.println(count >= K ? "YES" : "NO");
}
public static BitSet primes(int max) {
BitSet primeSet = new BitSet(max + 1);
if (max < 2) {
return primeSet;
}
int limit = (int) Math.sqrt(max + 1);
primeSet.set(2);
for (int i = 3; i < max + 1; i += 2) {
primeSet.set(i);
}
for (int i = 3; i <= limit; i += 2) {
if (!primeSet.get(i)) {
continue;
}
for (int j = i * i; j < max; j += i) {
primeSet.clear(j);
}
}
return primeSet;
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.awt.*;
import java.math.*;
import java.util.regex.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
import static java.lang.Integer.parseInt;
import static java.util.AbstractMap.*;
import static java.lang.System.*;
import static java.lang.Math.*;
import java.awt.geom.*;
import java.util.*;
import java.text.*;
import java.io.*;
public class A {
public static void main(String[] args) throws Exception {
boolean submit = true;
Scanner sc = submit ? new Scanner(System.in) : new Scanner(new File("A.in"));
while(sc.hasNext()) {
int n = sc.nextInt(), k = sc.nextInt();
boolean p[] = sieveOfEratosthenes(1001);
ArrayList<Integer> nolds = new ArrayList<Integer>();
for(int i = 0, prev = 0; i < p.length; i++) {
if(p[i]) {
nolds.add(prev+i + 1);
prev = i;
}
}
//System.out.println(nolds);
int c = 0;
for(int i : nolds)
if(i >= 2 && i <= n && p[i])
c++;
System.out.println(c >= k ? "YES" : "NO");
}
}
//prime[i] = true iff i is prime, prime[0] = prime[1] = false and i can be from 0 to n (both inclusive)
static boolean[] sieveOfEratosthenes(int n) {
boolean prime[] = new boolean[n+1];
fill(prime, 2, n, true);
for(int i = 2; i <= n; i++)
if(prime[i])
for(int j = i*i; j <= n; j+=i) //check for i*i overflow
prime[j] = false;
return prime;
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
StreamTokenizer in;
PrintWriter out;
public static void main(String[] args) throws IOException
{
new Main().run();
}
public void run() throws IOException
{
// in =new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
// out = new PrintWriter(new FileWriter("output.txt"));
in =new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(new OutputStreamWriter(System.out));
solve();
out.flush();
}
public int nextInt() throws IOException
{
in.nextToken();
return (int) in.nval;
}
boolean pr(int i)
{
if(i<4) return true;
for(int j=2;j<Math.sqrt(i)+1;j++)
if(i%j==0)
return false;
return true;
}
public void solve() throws IOException
{
int n=nextInt(),k=nextInt();
int prost[]=new int[1000];
boolean now[]=new boolean[10000];
int a=0;
for(int i=2;i!=1000;i++)
if(pr(i))
prost[a++]=i;
for(int i=0;i!=a-1;i++)
{
if(pr(prost[i]+prost[i+1]+1))
now[prost[i]+prost[i+1]+1]=true;
}
int answ=0;
for(int i=0;i!=n+1;i++)
if(now[i])
answ++;
if(answ>=k)
out.print("YES");
else
out.print("NO");
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.*;
public class A
{
public static void main(String args[])
{
boolean[] b = new boolean[11000];
Arrays.fill(b, true);
b[0] = b[1] = false;
for(int i=2;i < b.length;i++)
{
if(!b[i])
continue;
for(int j=2;i*j<b.length;j++)
b[i*j] = false;
}
int[] p = new int[11000];
int pn = 0;
for(int i=0;i < b.length;i++)
{
if(b[i])
p[pn++] = i;
}
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
int rtn = 0;
for(int j=0;p[j] <= n;j++)
{
//Try to make sum
for(int h=0;h <= j;h++)
{
if(p[h] + p[h+1] + 1 == p[j])
{
rtn++;
break;
}
}
}
System.out.println(rtn >= k ? "YES" : "NO");
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
public class A {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int N = s.nextInt();
int K = s.nextInt();
int[] primes = getPrimesFast(N);
Set<Integer> ints = new HashSet<Integer>();
for(int i=0;i<primes.length;i++) {
ints.add(primes[i]);
}
for(int i=1;i<primes.length;i++) {
ints.remove(primes[i] + primes[i-1]+1);
}
boolean res = primes.length - ints.size() >= K;
System.out.print(res?"YES":"NO");
}
public static int[] getPrimesFast(int n) {
if (n <= 1) {
return new int[0];
}
boolean[] b = new boolean[n + 1];
int m = n - 1;
for (int i = 2; i * i <= n; i++) {
if (!b[i]) {
for (int j = i + i; j <= n; j += i) {
if (!b[j]) {
m--;
b[j] = true;
}
}
}
}
int[] primes = new int[m];
int j = 0;
for (int i = 2; i <= n; i++) {
if (!b[i]) {
primes[j++] = i;
}
}
return primes;
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.ArrayList;
import java.util.Scanner;
public class primes {
public static void main(String [] args){
ArrayList<Integer> numb=new ArrayList<Integer>();
Scanner br1 = new Scanner(System.in);
int n=br1.nextInt();
int steps=br1.nextInt();
//if(n>2)numb.add(2);
if(n>=3)numb.add(3);
for(int j=4;j<=n;j++){
if(chekprime(j)==0){
numb.add(j);
//System.out.println(j);
}
}
int counter =0;
for(int give=0;give<numb.size();give++)
{if("YES".equals(sumup(numb, 2, numb.get(give)))){
counter++;
// System.out.println(numb.get(give)+"ksjdfskldfgaskldfgasklfgaskldfgaklsfgasdklfgaskldfgaskldfgasdklfg");
}
}
//System.out.println(counter);
if(counter>=steps)System.out.println("YES");
else System.out.println("NO");
}
public static String sumup(ArrayList<Integer> list,int number,int NUM){
String ret="NO";
ArrayList<Integer> result=new ArrayList<Integer>();
ArrayList<Integer>[] arList=new ArrayList[number];
for(int i=0;i<number;i++){
arList[i]=new ArrayList<Integer>();
arList[i]=(ArrayList<Integer>)list.clone();
for(int k=0;k<i;k++){
arList[i].add(0,arList[i].remove(arList[i].size()-1));
}
}
int [] temp=new int[list.size()];
for(int z=0;z<list.size();z++){
for(int count=0;count<number;count++){
temp[z]+=arList[count].get(z);
//System.out.println(arList[count].get(z));
}
result.add(temp[z]);
}
if(result.contains(NUM-1))
{ //System.out.println(NUM-1);
ret="YES";
}
return ret;
}
public static int chekprime(int n){
int flag=0;
for(int i=2;i<=Math.sqrt(n)+1;i++)
{
if(n%i==0){
flag=1;
break;
}
}
return flag;
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws Exception {
//System.setIn(new FileInputStream("1"));
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int n = nextInt(), k = nextInt();
int[] primes = new int[n + 1];
for (int i = 2; i <= n; i++) {
if (primes[i] == 0) {
primes[i] = 1;
for (int j = i * 2; j <= n; j += i)
primes[j] = 2;
}
}
ArrayList<Integer> res = new ArrayList<Integer>();
HashSet<Integer> p = new HashSet<Integer>(), v = new HashSet<Integer>();
for (int i = 2; i <= n; i++) {
if (primes[i] == 1) {
res.add(i);
p.add(i);
}
}
int c = 0;
if (res.size() >= 3) {
for (int i = 2; i < res.size(); i++) {
int zz = res.get(i - 2) + res.get(i - 1) + 1;
if (p.contains(zz))
v.add(zz);
}
c = v.size();
}
if (c >= k) {
out.println("YES");
} else {
out.println("NO");
}
in.close();
out.close();
}
static BufferedReader in;
static PrintWriter out;
static StringTokenizer st;
static String nextString() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
static int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextString());
}
static double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextString());
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
public class Solution {
Scanner in;
PrintWriter out;
boolean isPrime(int x) {
for (int i = 2; i * i <= x; ++i) {
if (x % i == 0) {
return false;
}
}
return true;
}
void solve() throws IOException {
in = new Scanner(System.in);
//in = new Scanner(new FileReader("input.txt"));
out = new PrintWriter(System.out);
int N = in.nextInt();
int K = in.nextInt();
List<Integer> primes = new ArrayList<Integer>();
for (int i = 2; i <= N; ++i) {
if (isPrime(i)) {
primes.add(i);
}
}
int c = 0;
for (int i = 2; i <= N; ++i) {
if (!isPrime(i)) continue;
for (int j = 0; j + 1 < primes.size(); ++j) {
int p1 = primes.get(j);
int p2 = primes.get(j + 1);
if (p1 + p2 + 1 == i) {
++c;
//out.println(i);
break;
}
}
}
if (c >= K) out.println("YES");
else out.println("NO");
out.close();
}
public static void main(String args[]) throws IOException {
new Solution().solve();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
public class Noldbach
{
public Scanner in = new Scanner(System.in);
public PrintStream out = System.out;
public boolean[] yes;
public int n, k;
public void main()
{
n = in.nextInt();
k = in.nextInt();
genPrime();
int i;
yes = new boolean[n+1];
int x;
for(i=0;i+1<prime.length;++i)
{
x = prime[i]+prime[i+1]+1;
if(x <= n && fac[x] == x) yes[x] = true;
}
int count = 0;
for(i=0;i<yes.length;++i) if(yes[i]) ++count;
out.println((count>=k?"YES":"NO"));
}//end public void main()
//Generating Primes
public int N = 100000+100;
public int[] fac, rest;
public int[] prime;
public void genPrime()
{
ArrayList<Integer> ap = new ArrayList<Integer>();
fac = new int[N];
rest = new int[N];
int x,y;
for(x=0;x<N;++x)
{
fac[x] = x;
rest[x] = 1;
}
for(x=2;x<N;++x)
if(fac[x]==x)
{
ap.add(x);
for(y=x+x;y<N;y+=x)
if(fac[y]==y)
{
fac[y] = x;
rest[y] = y/x;
}
}
prime = new int[ap.size()];
for(int i=0;i<prime.length;++i) prime[i] = ap.get(i);
}
public static void main(String[] args)
{
(new Noldbach()).main();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import static java.util.Arrays.*;
import static java.lang.Math.*;
import java.util.*;
import java.io.*;
public class A implements Runnable
{
public static void main(String [] args) throws IOException
{
new Thread(null, new A(), "", 1 << 20).start();
}
String file = "input";
BufferedReader input;
PrintWriter out;
public void run()
{
try
{
//input = new BufferedReader(new FileReader(file + ".in"));
input = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new BufferedOutputStream(System.out));
solve();
input.close();
out.close();
}
catch(Exception e)
{
e.printStackTrace();
System.exit(1);
}
}
void solve() throws IOException
{
ArrayList<Integer> p = new ArrayList<Integer>();
StringTokenizer st = tokens();
int n = nextInt(st), k = nextInt(st);
for(int i = 2; i <= n; i++)
if(prime(i)) p.add(i);
int count = 0;
for(int x = 2; x <= n; x++)
{
if(!prime(x)) continue;
for(int i = 0; i + 1 < p.size(); i++)
{
int p1 = p.get(i);
int p2 = p.get(i + 1);
int P = p1 + p2 + 1;
if(P == x)
{
count++;
break;
}
if(P > x) break;
}
}
System.out.println(count >= k ? "YES" : "NO");
}
boolean prime(int n)
{
for(int i = 2; i * i <= n; i++)
if(n % i == 0) return false;
return true;
}
StringTokenizer tokens() throws IOException
{
return new StringTokenizer(input.readLine());
}
String next(StringTokenizer st)
{
return st.nextToken();
}
int nextInt() throws IOException
{
return Integer.parseInt(input.readLine());
}
int nextInt(StringTokenizer st)
{
return Integer.parseInt(st.nextToken());
}
double nextDouble() throws IOException
{
return Double.parseDouble(input.readLine());
}
double nextDouble(StringTokenizer st)
{
return Double.parseDouble(st.nextToken());
}
void print(Object... o)
{
out.println(deepToString(o));
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.*;
import java.io.*;
import java.math.*;
import javax.script.*;
public class Noldbach {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
boolean[] sieve=new boolean[1001];
sieve[2]=false;
ArrayList<Integer> primes=new ArrayList<Integer>();
for(int x=2;x<1001;x++)
if(!sieve[x])
{
primes.add(x);
for(int y=x;y<1001;y+=x)
sieve[y]=true;
}
int sum=0;
for(int x=2;x<=n;x++)
{
if(primes.contains(x))
{
int need=x-1;
for(int y=0;y<primes.size()-1;y++)
{
if(primes.get(y)+primes.get(y+1)==need)
{
sum++;
break;
}
}
}
if(sum==k)break;
}
if(sum==k)System.out.println("YES");
else System.out.println("NO");
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Locale;
import java.util.StringTokenizer;
public class Solution implements Runnable {
public static void main(String[] args) {
(new Thread(new Solution())).start();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st;
String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
void solve() throws Exception {
boolean[] r = new boolean[1010];
Arrays.fill(r, true);
r[0] = r[1] = false;
for (int i = 2; i < 1010; i++) {
if (r[i]) {
for (int j = i + i; j < 1010; j += i) {
r[j] = false;
}
}
}
int[] pr = new int[1010];
int l = 0;
for (int i = 2; i < 1010; i++) if (r[i]) pr[l++] = i;
int n = nextInt();
int k = nextInt();
int ans = 0;
int j = 0;
for (int i = 2; i <= n; i++) {
if (r[i]) {
for (; j < l - 1; j++) {
if (i == pr[j] + pr[j + 1] + 1) {
ans++;
break;
}
if (i < pr[j] + pr[j + 1] + 1) break;
}
}
}
if (ans >= k) out.println("YES"); else out.println("NO");
}
public void run() {
try {
Locale.setDefault(Locale.US);
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
} catch (Exception e) {
e.printStackTrace();
}
out.flush();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.awt.geom.Point2D;
import java.text.*;
import java.math.*;
import java.util.*;
public class Main implements Runnable {
final String filename="";
public void solve() throws Exception {
int n = iread(), k = iread();
boolean[] f = new boolean[10000];
int prev = -1;
cycle:for (int i=2; i<=n; i++)
{
for (int j=2; j*j<=i; j++)
if (i%j==0)
continue cycle;
if (prev!=-1)
f[i+prev+1] = true;
if (f[i])
k--;
prev = i;
}
if (k<=0)
out.write("YES\n");
else out.write("NO\n");
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new BufferedWriter(new OutputStreamWriter(System.out));
// in = new BufferedReader(new FileReader(filename+".in"));
// out = new BufferedWriter(new FileWriter(filename+".out"));
solve();
out.flush();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public int iread() throws Exception {
return Integer.parseInt(readword());
}
public double dread() throws Exception {
return Double.parseDouble(readword());
}
public long lread() throws Exception {
return Long.parseLong(readword());
}
BufferedReader in;
BufferedWriter out;
public String readword() throws IOException {
StringBuilder b = new StringBuilder();
int c;
c = in.read();
while (c >= 0 && c <= ' ')
c = in.read();
if (c < 0)
return "";
while (c > ' ') {
b.append((char) c);
c = in.read();
}
return b.toString();
}
public static void main(String[] args) {
try{
Locale.setDefault(Locale.US);
} catch (Exception e)
{
}
new Thread(new Main()).start();
//new Thread(null, new Main(), "1", 1<<25).start();
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.*;
public class Main
{
static int MAX = 1000;
static BitSet P = new BitSet(MAX + 1);
public static boolean Noldbach(int n)
{
n--;
int j;
for(int i=2; i<=n; i++)
{
if(!P.get(i))
{
j = i + 1;
while(P.get(j))
j++;
if(i+j == n)
{
if(!P.get(i+j+1))
{
//System.out.println((n+1)+" = "+i+" + "+(n-i)+" + 1");
return true;
}
}
}
}
return false;
}
public static void main(String[] args)
{
Scanner lee = new Scanner(System.in);
for(int i=2; i*i<=MAX; i++)
{
if(!P.get(i))
{
for(int j=i+i; j<=MAX; j+=i)
P.set(j);
}
}
int n, k, c;
n = lee.nextInt();
k = lee.nextInt();
c = 0;
for(int i=2; i<=n; i++)
{
if(Noldbach(i))
c++;
if(c == k)
break;
}
if(c == k)
System.out.println("YES");
else
System.out.println("NO");
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.InputMismatchException;
/**
* @author Egor Kulikov ([email protected])
* Created on 14.03.2010
*/
public class TaskA implements Runnable {
private InputReader in;
private PrintWriter out;
public static void main(String[] args) {
new Thread(new TaskA()).start();
// new Template().run();
}
public TaskA() {
// String id = getClass().getName().toLowerCase();
// try {
// System.setIn(new FileInputStream(id + ".in"));
// System.setOut(new PrintStream(new FileOutputStream(id + ".out")));
// System.setIn(new FileInputStream("input.txt"));
// System.setOut(new PrintStream(new FileOutputStream("output.txt")));
// } catch (FileNotFoundException e) {
// throw new RuntimeException();
// }
in = new InputReader(System.in);
out = new PrintWriter(System.out);
}
public void run() {
// int numTests = in.readInt();
// for (int testNumber = 0; testNumber < numTests; testNumber++) {
// }
int n = in.readInt();
int k = in.readInt();
int last = 2;
for (int i = 3; i + last < n; i++) {
boolean good = true;
for (int j = 2; j * j <= i; j++) {
if (i % j == 0) {
good = false;
break;
}
}
if (good) {
int p = i + last + 1;
for (int j = 2; j * j <= p; j++) {
if (p % j == 0) {
good = false;
break;
}
}
if (good)
k--;
last = i;
}
}
if (k <= 0)
out.println("YES");
else
out.println("NO");
out.close();
}
private static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1000];
private int curChar, numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private 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 String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private String readLine0() {
StringBuffer buf = new StringBuffer();
int c = read();
while (c != '\n' && c != -1) {
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(readString());
} catch (NumberFormatException e) {
throw new InputMismatchException();
}
}
public char readCharacter() {
int c = read();
while (isSpaceChar(c))
c = read();
return (char) c;
}
public double readDouble() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
double res = 0;
while (!isSpaceChar(c) && c != '.') {
if (c < '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 < '0' || c > '9')
throw new InputMismatchException();
m /= 10;
res += (c - '0') * m;
c = read();
}
}
return res * sgn;
}
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import static java.lang.Math.*;
import static java.lang.System.currentTimeMillis;
import static java.lang.System.exit;
import static java.lang.System.arraycopy;
import static java.util.Arrays.sort;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.fill;
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
try {
if (new File("input.txt").exists())
System.setIn(new FileInputStream("input.txt"));
} catch (SecurityException e) {
}
new Thread() {
public void run() {
try {
new Main().run();
} catch (IOException e) {
e.printStackTrace();
}
}
}.start();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
private void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
boolean[] pr = new boolean[1001];
for (int i = 2; i <= 1000; i++)
if (!pr[i]) {
int l = 2 * i;
while (l <= 1000) {
pr[l] = true;
l += i;
}
}
Set<Integer> set = new HashSet<Integer>();
for (int i = 2; i < 1000; i++) {
for (int j = i + 1; j <= 1000; j++) {
if (!pr[j]) {
set.add(j + i + 1);
i = j - 1;
break;
}
}
}
int n = nextInt();
int k = nextInt();
int res = 0;
for (int i = 2; i <= n; i++) {
if (set.contains(i) && !pr[i])
res++;
}
if (res >= k)
out.println("YES");
else
out.println("NO");
in.close();
out.close();
}
void chk(boolean b) {
if (b)
return;
System.out.println(new Error().getStackTrace()[1]);
exit(999);
}
void deb(String fmt, Object... args) {
System.out.printf(Locale.US, fmt + "%n", args);
}
String nextToken() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.*;
import static java.lang.Math.*;
public class Main {
StreamTokenizer in;
//BufferedReader in;
PrintWriter out;
public static void main(String[] args) throws IOException {
new Main().run();
}
int ni() throws IOException {
in.nextToken(); return (int)in.nval;
}
void run() throws IOException {
//in = new StreamTokenizer(new BufferedReader(new FileReader("input.txt")));
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(new OutputStreamWriter(System.out));
//out = new PrintWriter(new FileWriter("output.txt"));
int cprime = 0;
int[] prime = new int[1000];
for(int i = 2; i < 1001; i++) {
boolean f = false;
for(int j = 2; j*j <= i; j++)
if(i % j == 0) {
f = true; break;
}
if(!f) prime[cprime++] = i;
}
int n = ni(), k = ni();
int last = 0;
int count = 0;
for(int i = 0; i < cprime && prime[i] <= n; i++) {
for(int j = 0; j < cprime - 1; j++)
if(prime[j] + prime[j + 1] + 1 == prime[i]) {
count++; break;
}
else if(prime[j] + prime[j + 1] + 1 > prime[i]) break;
}
if(count >= k) out.print("YES");
else out.print("NO");
out.flush();
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class Main {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner entrada = new Scanner (System.in);
int Primos []= {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,
71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,
151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,
233,239,
241,
251,
257,
263,
269,
271,
277,
281,
283,
293,
307,
311,
313,
317,
331,
337,
347,
349,
353,
359,
367,
373,
379,
383,
389,
397,
401,
409,
419,
421,
431,
433,
439,
443,
449,
457,
461,
463,
467,
479,
487,
491,
499,
503,
509,
521,
523,
541,
547,
557,
563,
569,
571,
577,
587,
593,
599,
601,
607,
613,
617,
619,
631,
641,
643,
647,
653,
659,
661,
673,
677,
683,
691,
701,
709,
719,
727,
733,
739,
743,
751,
757,
761,
769,
773,
787,
797,
809,
811,
821,
823,
827,
829,
839,
853,
857,
859,
863,
877,
881,
883,
887,
907,
911,
919,
929,
937,
941,
947,
953,
967,
971,
977,
983,
991,
997
};
boolean sw=true;
int Indices [] = new int [Primos.length];
int cantidad = 0;
for(int i=0;i<Primos.length-1 && sw;i++)
{
int suma=Primos[i]+Primos[i+1]+1;
int posicion = Arrays.binarySearch(Primos,suma);
if(posicion>-1)
Indices[posicion]=1;
}
while(entrada.hasNextInt())
{
int N = entrada.nextInt();
int K = entrada.nextInt();
int contador=0;
for(int i=0;Primos[i]<=N && i<Primos.length-1;i++)
contador+=Indices[i];
if(contador>=K)
System.out.println("YES");
else
System.out.println("NO");
}
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class A {
public static void main(String[] args) {
new A().run();
}
private void run() {
Scanner sc =new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
sc.close();
boolean[] isp = new boolean[n + 1];
Arrays.fill(isp, true);
isp[1] = false;
int[] primes = new int[n];
int pc = 0;
for (int i = 2; i <= n; i++) {
if (isp[i]) {
primes[pc++] = i;
for (int j = i * i; j <= n; j+= i) {
isp[j] = false;
}
}
}
int res = 0;
for (int i = 0; i < pc; i++) {
for (int j = 1; j < i; j++)
if (primes[i] == primes[j] + primes[j - 1] + 1)
res++;
}
System.out.println(res >= k ? "YES" : "NO");
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.*;
import java.io.*;
/**
* Created by HREN_VAM.
*/
public class A implements Runnable{
BufferedReader in;
PrintWriter out;
StringTokenizer st;
public static final String filename = "";
public void solve() throws IOException{
int n = nextInt();
int[] a = new int[n + 1];
ArrayList<Integer> pr = new ArrayList<Integer>();
for(int i = 2;i < n + 1;i ++){
if(a[i] != 0)continue;
pr.add(i);
for(int j = 2 * i;j < n + 1;j += i){
a[j] = 1;
}
}
int k = nextInt();
for(int i = 2;i < n + 1;i ++){
if(a[i] != 0)continue;
for(int j = 1;j < pr.size();j ++){
if(i == pr.get(j) + pr.get(j - 1) + 1){
k --;
break;
}
}
}
if(k > 0){
out.println("NO");
return;
}
out.println("YES");
}
public void run(){
try{
Locale.setDefault(Locale.US);
in = new BufferedReader(new InputStreamReader(System.in));
//in = new BufferedReader(new FileReader(filename + ".in"));
out = new PrintWriter(System.out);
//out = new PrintWriter(new FileWriter(filename + ".out"));
st = new StringTokenizer("");
solve();
out.close();
} catch(IOException e){
throw new RuntimeException(e);
}
}
public static void main(String[] args){
new Thread(new A()).start();
}
public String nextToken() throws IOException{
while(!st.hasMoreTokens()){
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException{
return Integer.parseInt(nextToken());
}
public double nextDouble() throws IOException{
return Double.parseDouble(nextToken());
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main {
static Scanner in;
static PrintWriter out;
// static final String PROBLEM = "";
public static void main(String[] args) throws Exception {
in = new Scanner(System.in);
out = new PrintWriter(System.out);
int n = in.nextInt();
int k = in.nextInt();
boolean[] p = new boolean[n + 5];
int[] pp = new int[n + 5];
int ind = 0;
Arrays.fill(p, true);
p[0] = false;
p[1] = false;
for (int i = 2; i < n + 5; i++)
if (p[i]) {
pp[ind++] = i;
for (int j = 2*i; j < n + 5; j += i) p[j] = false;
}
// for (int i = 0; i < 30; i++) if (p[i]) out.println(i);
boolean[] b = new boolean[n + 1];
for (int i = 0; i < ind - 1; i++)
if (pp[i] + pp[i + 1] + 1 <= n && p[pp[i] + pp[i + 1] + 1]) b[pp[i] + pp[i + 1] + 1] = true;
int kol = 0;
for (int i = 2; i <= n; i++)
if (b[i]) kol++;
if (kol >= k) out.println("YES");
else out.println("NO");
out.close();
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
public class Answer17A{
public static void main(String[] args){
try{
BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
String[] tmp=reader.readLine().split(" ");
int n=Integer.parseInt(tmp[0]);
int k=Integer.parseInt(tmp[1]);
boolean[] m=getPrime(n);
ArrayList<Integer> prime=new ArrayList<Integer>();
for(int i=0;i<m.length;i++){
if(m[i])prime.add(i);
}
int sum=0;
for(int i=2;i<=n;i++){
if(m[i]){
for(int j=0;j<prime.size()-1;j++){
if(i==prime.get(j)+prime.get(j+1)+1){
sum++;
break;
}
}
}
}
if(sum>=k){
System.out.println("YES");
}else{
System.out.println("NO");
}
}catch(IOException e){
e.printStackTrace();
}
}
public static boolean[] getPrime(int N){
boolean[] memo=new boolean[N+1];
Arrays.fill(memo,true);
memo[0]=false;
memo[1]=false;
for(int i=2;i*i<=N;i++){
if(memo[i]){
for(int j=i*i;j<=N;j+=i){
memo[j]=false;
}
}
}
return memo;
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.LinkedList;
import java.util.Locale;
import java.util.Scanner;
public class A{
void exe(){
LinkedList<Integer> list=new LinkedList<Integer>();
for(int i=2;i<=1000;i++)
if(isPrime(i))
list.add(i);
Object[] primes=list.toArray();
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int cnt=0;
for(int c=2;c<=n;c++){
if(!isPrime(c))
continue;
for(int i=0;i<primes.length-1;i++){
int p1=(Integer)primes[i];
int p2=(Integer)primes[i+1];
if(c==1+p1+p2){
// System.out.println("c="+c+", i="+p1+", j="+p2);
cnt++;
}
}
}
if(cnt>=k){
System.out.println("YES");
}else{
System.out.println("NO");
}
}
boolean isPrime(int n){
if(n<=1)return false;
if(n==2)return true;
if(n%2==0)return false;
int m=(int)Math.sqrt(n)+1;
for(int i=3;i<=m;i+=2)
if(n%i==0)
return false;
return true;
}
public static void main(String[] args){
Locale.setDefault(Locale.US);
new A().exe();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.StringTokenizer;
/**
* Problem solution template.
* @author Andrew Porokhin, [email protected]
*/
public class Problem17A implements Runnable {
void solve() throws NumberFormatException, IOException {
int n = nextInt();
int k = nextInt();
ArrayList<Integer> primes = new ArrayList<Integer>(n + 1);
boolean[] simplePrime = new boolean[n + 1];
Arrays.fill(simplePrime, 0, simplePrime.length, true);
simplePrime[0] = simplePrime[1] = false;
for (int i = 2; i <= n; i++) {
if (simplePrime[i]) {
for (int j = i + i; j <= n; j += i) {
simplePrime[j] = false;
}
primes.add(i);
}
}
int actualK = 0;
for (int i = 1; i < primes.size(); i++) {
int val = primes.get(i - 1) + primes.get(i) + 1;
if (val <= n) {
if (simplePrime[val]) {
// System.out.printf("%d + %d + 1 = %d%n", primes.get(i - 1), primes.get(i), val);
actualK++;
}
} else {
break;
}
}
// System.out.printf("req: %d actual: %d%n", k, actualK);
System.out.println((k <= actualK ? "YES" : "NO"));
}
StringTokenizer st;
BufferedReader in;
PrintWriter out;
public static void main(String[] args) {
new Problem17A().run();
}
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
} catch (Exception e) {
e.printStackTrace();
} finally {
out.flush();
out.close();
}
}
String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Solution {
BufferedReader in;
PrintWriter out;
StringTokenizer st;
int n, k;
boolean[] prime;
int[] primes;
void sieve() {
prime = new boolean[n + 1];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
int cnt = 0;
for (int i = 2; i <= n; ++i)
if (prime[i]) {
++cnt;
for (int j = i + i; j <= n; j += i)
prime[j] = false;
}
primes = new int[cnt];
cnt = 0;
for (int i = 0; i <= n; ++i)
if (prime[i])
primes[cnt++] = i;
}
void solve() throws IOException {
n = ni();
k = ni();
sieve();
int cnt = 0;
for (int i = 2; i <= n; ++i) {
if (!prime[i])
continue;
boolean ok = false;
for (int j = 0; j < primes.length - 1; ++j)
if (primes[j] + primes[j + 1] + 1 == i) {
ok = true;
break;
}
if (ok)
++cnt;
}
if (cnt >= k)
out.println("YES");
else
out.println("NO");
}
public Solution() throws IOException {
Locale.setDefault(Locale.US);
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
in.close();
out.close();
}
String ns() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int ni() throws IOException {
return Integer.valueOf(ns());
}
long nl() throws IOException {
return Long.valueOf(ns());
}
double nd() throws IOException {
return Double.valueOf(ns());
}
public static void main(String[] args) throws IOException {
new Solution();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
public class ProblemaNoldbaha implements Runnable{
public static void main(String[] args) throws IOException {
new Thread(new ProblemaNoldbaha()).start();
}
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 void solve() throws IOException{
int n = nextInt();
int k = nextInt();
int[] prime = new int[1000];
int l = 0;
for (int i = 2; i <= n; i++) {
boolean f = false;
for (int j = 2; j < i; j++) {
if (i % j == 0){
f = true;
break;
}
}
if (!f){
prime[l] = i;
l++;
}
}
int count = 0;
for (int i = 2; i < l; i++) {
boolean f = false;
for (int j = 0; j < l - 1; j++) {
if (prime[j] + prime[j + 1] + 1 == prime[i]){
f = true;
break;
}
}
if (f) count++;
}
if (count >= k){
out.println("YES");
}
else{
out.println("NO");
}
}
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);
}
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
private static Scanner sc = new Scanner(new InputStreamReader(System.in));
public static void main (String[] args) throws IOException {
BitSet b = new BitSet(1001);
BitSet p = primes(1001);
for (int i = 0; i < ps.length - 1; i++) {
b.set(ps[i] + ps[i+1] + 1);
}
int n = sc.nextInt(), k = sc.nextInt();
for (int x = 0; x <= n; x++) {
if (b.get(x) && p.get(x)) k--;
}
System.out.println(k > 0 ? "NO" : "YES");
}
private static BitSet primes (int n) {
BitSet b = new BitSet(n+1);
b.set(2, n);
for (int p = 2; p <= n; p++) {
if (b.get(p)) {
for (int x = p * 2; x <= n; x += p) {
b.clear(x);
}
}
}
return b;
}
private static int [] ps = new int[] {2,
3,
5,
7,
11,
13,
17,
19,
23,
29,
31,
37,
41,
43,
47,
53,
59,
61,
67,
71,
73,
79,
83,
89,
97,
101,
103,
107,
109,
113,
127,
131,
137,
139,
149,
151,
157,
163,
167,
173,
179,
181,
191,
193,
197,
199,
211,
223,
227,
229,
233,
239,
241,
251,
257,
263,
269,
271,
277,
281,
283,
293,
307,
311,
313,
317,
331,
337,
347,
349,
353,
359,
367,
373,
379,
383,
389,
397,
401,
409,
419,
421,
431,
433,
439,
443,
449,
457,
461,
463,
467,
479,
487,
491,
499};
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.fill;
import static java.util.Arrays.binarySearch;
import static java.util.Arrays.sort;
public class Main {
public static void main(String[] args) throws IOException {
try {
if (new File("input.txt").exists()) {
System.setIn(new FileInputStream("input.txt"));
}
} catch (SecurityException e) {
}
new Main().run();
}
BufferedReader in;
PrintWriter out;
StringTokenizer st = new StringTokenizer("");
boolean[] erat = new boolean [1000 + 1];
int[] primes = new int [1000 + 1];
int pNum = 0;
void run() throws IOException {
for (int i = 2; i <= 1000; i++) {
if (!erat[i]) {
primes[pNum++] = i;
for (int j = i * i; j <= 1000; j += i)
erat[j] = true;
}
}
int[] cnt = new int [1000 + 1];
cnt[2] = 0;
for (int i = 3; i <= 1000; i++) {
cnt[i] = cnt[i - 1];
if (!erat[i]) {
int r = i - 1;
for (int j = 1; j < pNum; j++) {
if (r == primes[j - 1] + primes[j]) {
cnt[i]++;
break;
}
}
}
}
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
int n = nextInt();
int k = nextInt();
out.println(k <= cnt[n] ? "YES" : "NO");
out.close();
}
String nextToken() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextLine() throws IOException {
st = new StringTokenizer("");
return in.readLine();
}
boolean EOF() throws IOException {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null)
return true;
st = new StringTokenizer(s);
}
return false;
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Naldbah implements Runnable {
boolean isLocalMode = false;
public static void main(String[] args) {
new Naldbah().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
reader = new BufferedReader(getReader());
tokenizer = null;
writer = new PrintWriter(System.out);
//do job
doJob();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
private void doJob() throws IOException {
int n = nextInt();
int k = nextInt();
boolean[] primes = sieve(n + 1);
for(int i=n;i>=2;i--){
if(primes[i]){
int solve = i-1;
int sn=getNextD(primes,solve);
int en = getNextD(primes,n);
while(en!=-1&&sn+en>=solve){
if((sn+en)==solve)k--;
sn=en;
en=getNextD(primes,en);
}
}
}
writer.write(k<=0?"YES":"NO");
}
private int getNextD(boolean[] primes, int i) {
for(int p = i-1;p>=2;p--){
if(primes[p])return p;
}
return -1;
}
public boolean[] sieve(int n)
{
boolean[] prime=new boolean[n+1];
Arrays.fill(prime,true);
prime[0]=false;
prime[1]=false;
int m= (int) Math.sqrt(n);
for (int i=2; i<=m; i++)
if (prime[i])
for (int k=i*i; k<=n; k+=i)
prime[k]=false;
return prime;
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
public Reader getReader() throws FileNotFoundException {
if (isLocalMode) {
return new FileReader("input.txt");
} else {
return new InputStreamReader(System.in);
}
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
public class A implements Runnable {
public static void main(String[] args) {
new A().run();
}
class FastScanner {
BufferedReader br;
StringTokenizer st;
boolean eof;
String buf;
public FastScanner(String fileName) throws FileNotFoundException {
br = new BufferedReader(new FileReader(fileName));
nextToken();
}
public FastScanner(InputStream stream) {
br = new BufferedReader(new InputStreamReader(stream));
nextToken();
}
String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
break;
}
}
String ret = buf;
buf = eof ? "-1" : st.nextToken();
return ret;
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
void close() {
try {
br.close();
} catch (Exception e) {
}
}
boolean isEOF() {
return eof;
}
}
FastScanner sc;
PrintWriter out;
public void run() {
Locale.setDefault(Locale.US);
try {
sc = new FastScanner(System.in);
out = new PrintWriter(System.out);
solve();
sc.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() {
return sc.nextInt();
}
String nextToken() {
return sc.nextToken();
}
long nextLong() {
return sc.nextLong();
}
double nextDouble() {
return sc.nextDouble();
}
boolean isPrime(int x) {
for (int i = 2; i * i <= x; i++) {
if (x % i == 0) {
return false;
}
}
return true;
}
void solve() {
int n = nextInt();
int k = nextInt();
ArrayList<Integer> primes = new ArrayList<Integer>();
for (int i = 2; i <= n; i++) {
if (isPrime(i)) {
primes.add(i);
}
}
int ans = 0;
for (int i = 0; i < primes.size(); i++) {
for (int j = 0; j < i - 1; j++) {
if (primes.get(j) + primes.get(j + 1) + 1 == primes.get(i)
.intValue()) {
ans++;
break;
}
}
}
if (ans >= k) {
out.println("YES");
} else {
out.println("NO");
}
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String args[]) throws IOException
{
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), k = sc.nextInt(), kol = 0, prev;
boolean ok;
ArrayList<Integer> al = new ArrayList<Integer>();
al.add(2);
prev = 2;
for(int i=3;i<=n;i+=2)
{
ok = true;
for(Integer x: al)
if (i%x == 0)
{
ok = false;
break;
}
if (ok)
{
for(Integer x: al)
if (ok)
{
prev = x;
ok = false;
} else
{
if (x + prev + 1 == i)
{
kol++;
break;
}
if (x + prev + 1 > i) break;
prev = x;
}
al.add(i);
}
}
if (kol >= k) System.out.print("YES"); else System.out.print("NO");
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class A {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt()-1;
PrimeGen p = new PrimeGen(n);
List<Integer> prims = new ArrayList<Integer>();
for(int i = 2; i <= n; i++){
if(p.isPrime(i)>0){
prims.add(i);
}
}
int sum = 0;
for(int i = 0; i < prims.size() - 1; i++){
int c = prims.get(i) + prims.get(i+1) + 1;
if(c <= n && p.isPrime(c)>0){
sum ++;
}
}
System.out.println(sum>=k?"YES":"NO");
}
static int sum(List<Integer> is) {
int c = 0;
for (int i : is)
c += i;
return c;
}
static class PrimeGen {
public PrimeGen(int m) {
m = (int) Math.sqrt(m);
double max = 0;
int r = 1;
for (int i = 0; i < 4;) {
max += r * m / Math.pow(Math.log1p(m), ++i);
r *= i;
}
p = new int[(int) max];
for (int i = 0, e = 2; i < p.length; i++) {
for (; isPrime(e) < 1; e++)
;
p[i] = e++;
}
this.m = p[p.length - 1];
this.m = this.m * this.m;
}
int isPrime(int n) {
for (int e : p)
if (e < 1)
break;
else if (n != e && n % e < 1)
return 0;
return 1;
}
int max() {
return m;
}
int[] p;
int m;
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
//package codeforces.br17;
import java.io.*;
import java.text.DecimalFormat;
public class ProblemA {
public void solve() {
boolean oj = true;
try {
Reader reader = oj ? new InputStreamReader(System.in) : new FileReader("A.in");
Writer writer = oj ? new OutputStreamWriter(System.out) : new FileWriter("A.out");
BufferedReader br = new BufferedReader(reader);
PrintWriter out = new PrintWriter(writer);
MyTokenizer tok = new MyTokenizer(br.readLine());
int n = (int)tok.getNum();
int k = (int)tok.getNum();
boolean[] isPrime = new boolean[n + 1];
for(int i=1;i<=n;i++)
isPrime[i] = true;
isPrime[1] = false;
isPrime[2] = true;
for(int i=2;i*i<=n;i++)
for(int j=2*i;j<=n;j+=i)
isPrime[j] = false;
int[] primes = new int[n];
int cur = 0;
for(int i=2;i<=n;i++)
if (isPrime[i]) {
primes[cur] = i;
cur++;
}
int count = 0;
for(int i=0;i<cur-1;i++) {
if (primes[i] + primes[i+1] + 1 <= n && isPrime[primes[i] + primes[i+1] + 1])
count++;
}
if (count >= k)
out.printf("YES");
else
out.printf("NO");
br.close();
out.close();
reader.close();
writer.close();
}
catch (Exception ex) {
ex.printStackTrace();
}
finally {
}
}
public static void main(String[] args) {
ProblemA f = new ProblemA();
f.solve();
}
private class MyTokenizer {
private String s;
private int cur;
public MyTokenizer(String s) {
this.s = s;
cur = 0;
}
public void skip() {
while (cur < s.length() && (s.charAt(cur) == ' ' || s.charAt(cur) == '\n')) {
cur++;
}
}
public double getNum() {
skip();
String snum = "";
while (cur < s.length() && (s.charAt(cur) >= '0' && s.charAt(cur) <= '9' || s.charAt(cur) == '.')) {
snum += s.charAt(cur);
cur++;
}
return Double.valueOf(snum);
}
public String getString() {
skip();
String s2 = "";
while (cur < s.length() && (s.charAt(cur) >= 'a' && s.charAt(cur) <= 'z')) {
s2 += s.charAt(cur);
cur++;
}
return s2;
}
public char getCurrentChar() throws Exception {
if (cur < s.length())
return s.charAt(cur);
else
throw new Exception("Current character out of string length");
}
public void moveNextChar() {
if (cur < s.length())
cur++;
}
public boolean isFinished() {
return cur >= s.length();
}
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.*;
import java.io.*;
import static java.lang.Math.*;
import java.math.*;
public class Main implements Runnable {
BufferedReader in;
StringTokenizer st = new StringTokenizer("");
public static void main(String [] args) throws Exception {
new Thread(new Main()).start();
}
void printExit(String s) {
System.out.println(s);
System.exit(0);
}
public void run() {
try {
Locale.setDefault(Locale.US);
in = new BufferedReader(new InputStreamReader(System.in));
int n = nextInt();
int k = nextInt();
int max = 5000;
ArrayList <Integer> primes = new ArrayList <Integer> ();
boolean [] p = new boolean [max];
Arrays.fill(p, true);
for (int i = 2; i < max; i++) {
if (p[i]) {
primes.add(i);
for (int j = i * i; j < max; j += i)
p[j] = false;
}
}
HashSet <Integer> good = new HashSet <Integer> ();
for (int i = 0; i < primes.size() - 1; i++) {
good.add(primes.get(i) + primes.get(i + 1) + 1);
}
int have = 0, pos = 0;
while (true) {
int i = primes.get(pos);
if (i > n) break;
if (good.contains(i)) have++;
pos++;
}
System.out.println(have >= k ? "YES" : "NO");
}
catch (Exception e) {
e.printStackTrace();
}
}
boolean seekForToken() {
try {
while (!st.hasMoreTokens()) {
String s = in.readLine();
if (s == null) {
return false;
}
st = new StringTokenizer(s);
}
return true;
}
catch (IOException e) {
e.printStackTrace();
return false;
}
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
BigInteger nextBigInteger() {
return new BigInteger(nextToken());
}
String nextToken() {
seekForToken();
return st.nextToken();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.*;
import static java.util.Arrays.deepToString;
public class A {
private static int[] prime = new int[] {
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43,
47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107,
109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181,
191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263,
269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349,
353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433,
439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521,
523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613,
617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701,
709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809,
811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887,
907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997
};
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
for (int i=0;i<prime.length-1;i++) {
if ((prime[i]+prime[i+1]+1) > n || k == 0)
break;
if (isPrime(prime[i]+prime[i+1]+1))
k--;
}
if (k == 0)
outnl("YES");
else
outnl("NO");
}
public static boolean isPrime(int x) {
int i=0;
while (i<prime.length)
if (prime[i++] == x)
return true;
return false;
}
private static void debug(Object... os) { System.out.println(deepToString(os)); }
private static void outnl(String out) { System.out.println(out); }
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.*;
import java.io.*;
import java.math.*;
import javax.script.*;
public class Noldbach {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
boolean[] sieve=new boolean[1001];
sieve[2]=false;
ArrayList<Integer> primes=new ArrayList<Integer>();
for(int x=2;x<1001;x++)
if(!sieve[x])
{
primes.add(x);
for(int y=x;y<1001;y+=x)
sieve[y]=true;
}
int sum=0;
for(int x=2;x<=n;x++)
{
if(primes.contains(x))
{
int need=x-1;
for(int y=0;y<primes.size()-1;y++)
{
if(primes.get(y)+primes.get(y+1)==need)
{
sum++;
break;
}
}
}
if(sum==k)break;
}
if(sum==k)System.out.println("YES");
else System.out.println("NO");
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
import java.math.*;
public class Main implements Runnable {
//public static final String FileName = "test";
StreamTokenizer ST;
PrintWriter out;
BufferedReader br;
Scanner in;
static final int inf = 1000000000;
int nextInt() throws IOException{
ST.nextToken();
return (int)ST.nval;
}
long nextLong() throws IOException{
ST.nextToken();
return (long)ST.nval;
}
String next() throws IOException{
ST.nextToken();
return ST.sval;
}
double nextD() throws IOException{
ST.nextToken();
return ST.nval;
}
public static void main(String[] args) throws IOException {
new Thread(new Main()).start();
// new Main().run();
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
//br = new BufferedReader(new FileReader(new File(FileName+".in")));
//out = new PrintWriter(new BufferedWriter(new FileWriter(FileName+".out")));
in = new Scanner(br);
ST = new StreamTokenizer(br);
solve();
out.close();
//in.close();
br.close();
}
catch (IOException e) {
e.printStackTrace();
throw new IllegalStateException(e);
}
}
public void solve() throws IOException {
int n = nextInt();
int K = nextInt();
boolean[] f = new boolean[n+1];
Arrays.fill(f, true);
Vector<Integer> P = new Vector<Integer>();
for (int i=2; i<=n; i++)
if (f[i]) {
for (int j=2*i; j<=n; j+=i)
f[j] = false;
P.add(i);
}
for (int i=0; i<P.size()-1; i++) {
int x = P.elementAt(i)+P.elementAt(i+1)+1;
if (x<=n && f[x]) K--;
}
if (K<=0) out.println("YES"); else out.println("NO");
//for (int x:P) out.print(x+" ");
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class A {
public static void main(String[] args) throws IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String s = new String(in.readLine());
String[] t=s.split(" ");
int n = Integer.parseInt(t[0]);
int k = Integer.parseInt(t[1]);
boolean[] prime=new boolean[n+1];
for (int i=2;i<Math.sqrt(n);i++) {
for (int j=i+i;j<=n;j=j+i) {
prime[j]=true;
}
}
int size=0;
for (int i=2;i<=n;i++) {
if (!prime[i]) {
size++;
}
}
int[] pn=new int[size];
int index=0;
for (int i=2;i<=n;i++) {
if (!prime[i]) {
pn[index]=i;
index++;
}
}
for (int i=2;i<size;i++) {
for (int j=0;j<i;j++) {
if (pn[i]==pn[j]+pn[j+1]+1) {
// System.out.println(pn[i]+"="+pn[j]+"+"+pn[j+1]+"+1");
k--;
}
}
}
if (k<=0) {
System.out.println("YES");
} else {
System.out.println("NO");
}
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Arrays;
import java.util.HashSet;
public class E17 {
static StreamTokenizer in;
static PrintWriter out;
static int nextInt() throws IOException {
in.nextToken();
return (int)in.nval;
}
static String nextString() throws IOException {
in.nextToken();
return in.sval;
}
public static void main(String[] args) throws IOException {
in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
out = new PrintWriter(System.out);
int n = nextInt(), k = nextInt();
int MAX = n, nprimes = 0;
int[] primes = new int[MAX];
boolean[] isPrime = new boolean[MAX+1];
Arrays.fill(isPrime, true);
isPrime[0] = false;
isPrime[1] = false;
for (int i = 2; i <= MAX; i++) if (isPrime[i]) {
primes[nprimes++] = i;
for (int j = i + i; j <= MAX; j += i) isPrime[j] = false;
}
primes[nprimes] = Integer.MAX_VALUE;
HashSet<Integer> h = new HashSet<Integer>();
for (int i = 1; i < nprimes; i++) {
int x = primes[i-1] + primes[i] + 1;
if (x > n) break;
if (isPrime[x]) h.add(x);
}
out.println(h.size() >= k ? "YES" : "NO");
out.flush();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class A {
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt()-1;
PrimeGen p = new PrimeGen(n);
List<Integer> prims = new ArrayList<Integer>();
for(int i = 2; i <= n; i++){
if(p.isPrime(i)>0){
prims.add(i);
}
}
int sum = 0;
for(int i = 0; i < prims.size() - 1; i++){
int c = prims.get(i) + prims.get(i+1) + 1;
if(c <= n && p.isPrime(c)>0){
sum ++;
}
}
System.out.println(sum>=k?"YES":"NO");
}
static int sum(List<Integer> is) {
int c = 0;
for (int i : is)
c += i;
return c;
}
static class PrimeGen {
public PrimeGen(int m) {
m = (int) Math.sqrt(m);
double max = 0;
int r = 1;
for (int i = 0; i < 4;) {
max += r * m / Math.pow(Math.log1p(m), ++i);
r *= i;
}
p = new int[(int) max];
for (int i = 0, e = 2; i < p.length; i++) {
for (; isPrime(e) < 1; e++)
;
p[i] = e++;
}
this.m = p[p.length - 1];
this.m = this.m * this.m;
}
int isPrime(int n) {
for (int e : p)
if (e < 1)
break;
else if (n != e && n % e < 1)
return 0;
return 1;
}
int max() {
return m;
}
int[] p;
int m;
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.util.*;
public class Solution {
private BufferedReader in;
private PrintWriter out;
private StringTokenizer st;
void solve() throws IOException {
int n = nextInt();
int k = nextInt();
ArrayList<Integer> ps = new ArrayList<Integer>();
boolean[] prime = new boolean[n + 1];
Arrays.fill(prime, true);
prime[0] = prime[1] = false;
for (int i = 2; i <= n; ++i) {
if (prime[i]) {
ps.add(i);
for (int j = 2 * i; j <= n; j += i) {
prime[j] = false;
}
}
}
for (int i = 0; i < ps.size() - 1; ++i) {
int t = ps.get(i) + ps.get(i + 1) + 1;
if (t <= n && prime[t]) {
--k;
}
}
// System.out.println(k);
out.println(k <= 0 ? "YES" : "NO");
}
Solution() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
eat("");
solve();
in.close();
out.close();
}
private void eat(String str) {
st = new StringTokenizer(str);
}
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
eat(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) throws IOException {
new Solution();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.*;
import java.util.regex.*;
import java.text.*;
import java.math.*;
public class Main
{
Scanner cin;
int []prime;
int top;
void work()
{
cin=new Scanner(System.in);
int n=cin.nextInt();
int k=cin.nextInt();
top=0;
prime=new int[2000];
for(int i=2;i<=n;i++)
{
if(isprime(i))
prime[top++]=i;
}
int cnt=0;
for(int i=0;i<top;i++)
{
if(cando(prime[i]))
cnt++;
}
if(cnt>=k) System.out.println("YES");
else System.out.println("NO");
}
boolean cando(int n)
{
for(int i=0;i<top-1;i++)
{
if(prime[i]+prime[i+1]+1==n) return true;
}
return false;
}
boolean isprime(int n)
{
for(int i=2;i*i<=n;i++)
if(n%i==0)return false;
return true;
}
public static void main(String args[]) throws Exception
{
new Main().work();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.util.*;
public class Beta17PA {
boolean[] isPrime = new boolean[1001];
int[] prime = new int[200];
public static void main(String[] args) {
// TODO Auto-generated method stub
new Beta17PA().solve();
}
public void solve() {
Scanner scan = new Scanner(System.in);
int n, k;
n = scan.nextInt();
k = scan.nextInt();
init();
int m = 0;
for(int i=2; i<=n; i++) {
if(check(i)) m ++;
}
if(m>=k) System.out.println("YES");
else System.out.println("NO");
}
private boolean check(int n) {
if(n<6||!isPrime[n]) return false;
int d = n-1;
for(int i=0; i<199&&prime[i]+prime[i+1]<=d; i++) {
if(prime[i]+prime[i+1]==d) return true;
}
return false;
}
private void init() {
Arrays.fill(isPrime, true);
isPrime[0] = isPrime[1] = false;
for(int i=2; i<1001; i++) {
if(!isPrime[i]) continue;
for(int j=i+i; j<1001; j+=i) {
isPrime[j] = false;
}
}
int count = 0;
for(int i=2; i<1001; i++) {
if(isPrime[i]) prime[count++] = i;
}
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
public class A {
BufferedReader in;
StringTokenizer st;
PrintWriter out;
void solve() throws IOException {
int n = nextInt();
int k = nextInt();
boolean[] sieve = new boolean[n + 1];
List<Integer> primes = new ArrayList<Integer>();
for (int i = 2; i <= n; ++i) {
if (!sieve[i]) {
primes.add(i);
for (int j = 2 * i; j <= n; j += i) {
sieve[j] = true;
}
}
}
int count = 0;
for (int i = 0; i + 1 < primes.size(); ++i) {
int v = primes.get(i) + primes.get(i + 1) + 1;
if (v <= n && !sieve[v]) {
++count;
}
}
// System.err.println(count);
out.println(count >= k ? "YES" : "NO");
}
public void run() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
eat("");
solve();
out.close();
in.close();
}
void eat(String s) {
st = new StringTokenizer(s);
}
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
eat(line);
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public static void main(String[] args) throws IOException {
new A().run();
}
} | linear | 17_A. Noldbach problem | CODEFORCES |
import java.lang.*;
import java.io.*;
import java.util.*;
import java.math.*;
public class Solution implements Runnable{
private static BufferedReader br = null;
private static PrintWriter out = null;
private static StringTokenizer stk = null;
public static void main(String[] args) {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
(new Thread(new Solution())).start();
}
private void loadLine() {
try {
stk = new StringTokenizer(br.readLine());
}
catch (IOException e) {
e.printStackTrace();
}
}
private String nextLine() {
try {
return br.readLine();
}
catch (IOException e) {
e.printStackTrace();
}
return null;
}
private Integer nextInt() {
while (stk==null||!stk.hasMoreTokens()) loadLine();
return Integer.parseInt(stk.nextToken());
}
private Long nextLong() {
while (stk==null||!stk.hasMoreTokens()) loadLine();
return Long.parseLong(stk.nextToken());
}
private String nextWord() {
while (stk==null||!stk.hasMoreTokens()) loadLine();
return (stk.nextToken());
}
private Double nextDouble() {
while (stk==null||!stk.hasMoreTokens()) loadLine();
return Double.parseDouble(stk.nextToken());
}
public void run() {
int n = nextInt();
int k = nextInt();
boolean[] isP = new boolean[2*n];
Arrays.fill(isP, true);
isP[0] = isP[1] = false;
for (int i = 0; i <= n; ++i) {
if (isP[i]) {
for (int j = i+i; j <= n; j+=i) {
isP[j] = false;
}
}
}
ArrayList<Integer> p = new ArrayList<Integer>();
for (int i = 0; i <= n; ++i) {
if (isP[i]) {
p.add(i);
}
}
int cnt = 0;
for (int i = 0; i < p.size(); ++i) {
int num = p.get(i) - 1;
for (int j = 0; j < p.size() - 1; ++j) {
if (p.get(j) + p.get(j+1) == num) {
++cnt;
break;
}
}
}
if (cnt >= k) {
out.println("YES");
}
else {
out.println("NO");
}
out.flush();
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.*;
import java.math.*;
import java.util.*;
public class Primes
{
static Scanner in = new Scanner( new BufferedReader( new InputStreamReader( System.in ) ) );
public static void main( String[] args )
{
int n = in.nextInt(), k = in.nextInt(), count = 0;
boolean[] isP = new boolean[n+1];
for( int i = 2; i <= n; i++ ) isP[i] = true;
ArrayList<Integer> primes = new ArrayList<Integer>();
for( int i = 2; i <= n; i++ ) if( isP[i] )
{
primes.add(i);
if( i <= Math.sqrt(n) ) for( int j = 2*i; j <= n; j += i ) isP[j] = false;
}
for( int i = 0; i < primes.size()-1; i++ )
{
int sum = primes.get(i)+primes.get(i+1)+1;
if( sum<=n && isP[sum] ) count++;
}
if( count>=k ) System.out.println( "YES" );
else System.out.println( "NO" );
}
}
| linear | 17_A. Noldbach problem | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.StringTokenizer;
public class B {
static ArrayList<Integer> [] adj;
static int [] num;
static int dfs(int u, int p){
int cnt = 0;
for(int v:adj[u]){
if(v != p)
cnt += dfs(v, u);
}
if(adj[u].size() == 1 && u != 0 || u == 0 && adj[0].size() == 0)
cnt++;
num[cnt]++;
return cnt;
}
public static void main(String[] args) throws NumberFormatException, IOException {
Scanner sc = new Scanner();
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt();
adj = new ArrayList[n];
for (int i = 0; i < adj.length; ++i) {
adj[i] = new ArrayList<>();
}
for (int i = 1; i < n; ++i) {
int p = sc.nextInt()-1;
adj[p].add(i);
adj[i].add(p);
}
num = new int[n+1];
dfs(0, -1);
for (int i = 1; i < num.length; ++i) {
num[i] += num[i-1];
}
int cur = 1;
for (int i = 0; i < num.length; ++i) {
while(cur <= num[i]){
out.print(i + " ");
++cur;
}
}
out.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
public Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(next());
}
}
}
| linear | 1056_D. Decorate Apple Tree | CODEFORCES |
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Math.min;
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 F {
static void solve() throws Exception {
int n = scanInt();
long l[] = new long[n];
for (int i = 0; i < n; i++) {
l[i] = scanLong();
}
long e1 = 0, e2 = 0, ans = 0;
boolean water = false;
String types = scanString();
for (int i = 0; i < n; i++) {
long li = l[i], cur;
switch (types.charAt(i)) {
case 'G':
cur = min(e1, li);
e1 -= cur;
li -= cur;
e2 += 2 * cur;
ans += 2 * cur;
e2 += li;
ans += 3 * li;
break;
case 'W':
water = true;
e1 += li;
ans += 2 * li;
break;
case 'L':
cur = min(e1, li);
e1 -= cur;
li -= cur;
ans += 2 * cur;
cur = min(e2, li);
e2 -= cur;
li -= cur;
ans += 3 * cur;
ans += (water ? 4 : 6) * li;
break;
default:
throw new AssertionError();
}
}
out.print(ans);
}
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 | 1091_F. New Year and the Mallard Expedition | CODEFORCES |
import java.util.*;
import java.io.*;
import java.math.BigInteger;
public class Main
{
static final long mod=(int)1e9+7;
public static void main(String[] args) throws Exception
{
FastReader in=new FastReader();
PrintWriter pw=new PrintWriter(System.out);
int n=in.nextInt();
long ans=0;
for(int i=2;2*i<=n;i++)
{
ans+=i*(n/i-1);
}
ans*=4;
pw.print(ans);
pw.flush();
}
}
class pair
{
int f,s;
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br=new BufferedReader(new InputStreamReader(System.in));
}
public String next() throws IOException
{
if(st==null || !st.hasMoreElements())
{
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());
}
} | linear | 1062_D. Fun with Integers | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class C {
public static int mod = 1000000000 + 7;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String n = br.readLine();
int k = Integer.parseInt(br.readLine());
int l = n.length();
if(k == 0) {
System.out.println(1);
}else {
int max = 1000;
if (l <= 10) {
max = Integer.min(1000, Integer.parseInt(n, 2));
}
int[] steps = new int[max + 1];
for (int i = 2; i <= max; i++) {
int ones = numberOfOnes(i);
steps[i] = 1 + steps[ones];
}
if (l <= 10) {
int ans = 0;
for (int i = 1; i <= max; i++) {
if (steps[i] == k) {
ans++;
}
}
System.out.println(ans);
} else {
int[][] C = binomial(max);
int ans = 0;
int count = 0;
for (int i = 0; i < l; i++) {
if (n.charAt(i) == '1') {
for (int j = count; j < max; j++) {
if (steps[j] == k - 1) {
ans = (ans + C[l - i - 1][j - count]) % mod;
if (i == 0 && k == 1) {
ans = (ans + mod - 1) % mod;
}
}
}
count++;
}
}
int ones = 0;
for (int i = 0; i < l; i++) {
if (n.charAt(i) == '1') {
ones++;
}
}
if (steps[ones] == k-1) {
ans = (ans + 1) % mod;
}
System.out.println(ans);
}
}
}
public static int numberOfOnes(int x) {
char[] s = Integer.toBinaryString(x).toCharArray();
int count = 0;
for (char c : s) {
if (c == '1') {
count++;
}
}
return count;
}
public static int[][] binomial(int n) {
int[][] C = new int[n + 1][n + 1];
for (int i = 0; i <= n; i++) {
C[i][0] = 1;
for (int j = 1; j <= i; j++) {
C[i][j] = ((C[i - 1][j - 1] % mod) + (C[i - 1][j] % mod)) % mod;
}
}
return C;
}
}
| linear | 914_C. Travelling Salesman and Special Numbers | CODEFORCES |
import java.util.*;
import java.io.*;
public class A{
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int ans = 0;
for(int i = 1; i <= n; i++){
ans += ((i*2) <= n) ? i : n-i+1;
}
System.out.println(ans);
}
} | linear | 909_B. Segments | CODEFORCES |
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int[][] x = new int [200010][10];
String a = sc.nextLine();
String b = sc.nextLine();
int n = a.length();
int m = b.length();
for (int i = 1; i <= m; i++) {
for (int j = 0; j < 2; j++) {
x[i][j] = x[i - 1][j];
}
++x[i][b.charAt(i - 1) - '0'];
}
long res = 0;
for (int i = 0, c; i < n; i++) {
c = a.charAt(i) - '0';
for (int j = 0; j < 2; j++) {
res += Math.abs(c - j) * (x[m - n + i + 1][j] - x[i][j]);
}
}
System.out.println(res);
}
} | linear | 608_B. Hamming Distance Sum | CODEFORCES |
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
import java.math.*;
import java.lang.*;
import static java.lang.Math.*;
public class TaskB implements Runnable {
boolean prime[] = new boolean[(int)1e6+10];
InputReader c;
PrintWriter w;
public void run() {
c = new InputReader(System.in);
w = new PrintWriter(System.out);
char a[] = c.next().toCharArray(), b[] = c.next().toCharArray();
int n = a.length, m = b.length;
int[][] prefix = new int[m][2];
for(int i=0;i<m;i++){
if(i!=0) {
prefix[i][0] = prefix[i-1][0];
prefix[i][1] = prefix[i-1][1];
}
prefix[i][b[i] - '0']++;
//w.println(prefix[i][0]+" "+prefix[i][1]);
}
long res = 0;
for(int i=0;i<n;i++){
int temp = a[i] - '0';
res += prefix[m - n + i][temp^1];
if(i!=0) res -= prefix[i-1][temp^1];
}
w.println(res);
w.close();
}
void sieveOfEratosthenes(int n) {
for(int i=0;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
}
class pair implements Comparable<pair>{
char ch;
int ind;
@Override
public String toString() {
return "pair{" +
"ch=" + ch +
", ind=" + ind +
'}';
}
public pair(char ch, int ind) {
this.ch = ch;
this.ind = ind;
}
public int compareTo(pair car) {
if(this.ch==car.ch)
return this.ind - car.ind;
return this.ch - car.ch;
}
}
public static void sortbyColumn(int arr[][], int col){
Arrays.sort(arr, new Comparator<int[]>()
{
public int compare(int[] o1, int[] o2){
return(Integer.valueOf(o1[col]).compareTo(o2[col]));
}
});
}
static long gcd(long a, long b){
if (b == 0)
return a;
return gcd(b, a % b);
}
public static class DJSet {
public int[] upper;
public DJSet(int n) {
upper = new int[n];
Arrays.fill(upper, -1);
}
public int root(int x) {
return upper[x] < 0 ? x : (upper[x] = root(upper[x]));
}
public boolean equiv(int x, int y) {
return root(x) == root(y);
}
public boolean union(int x, int y) {
x = root(x);
y = root(y);
if (x != y) {
if (upper[y] < upper[x]) {
int d = x;
x = y;
y = d;
}
upper[x] += upper[y];
upper[y] = x;
}
return x == y;
}
}
public static int[] radixSort(int[] f) {
int[] to = new int[f.length];
{
int[] b = new int[65537];
for(int i = 0;i < f.length;i++)b[1+(f[i]&0xffff)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < f.length;i++)to[b[f[i]&0xffff]++] = f[i];
int[] d = f; f = to;to = d;
}
{
int[] b = new int[65537];
for(int i = 0;i < f.length;i++)b[1+(f[i]>>>16)]++;
for(int i = 1;i <= 65536;i++)b[i]+=b[i-1];
for(int i = 0;i < f.length;i++)to[b[f[i]>>>16]++] = f[i];
int[] d = f; f = to;to = d;
}
return f;
}
public void printArray(int[] a){
for(int i=0;i<a.length;i++)
w.print(a[i]+" ");
w.println();
}
public int[] scanArrayI(int n){
int a[] = new int[n];
for(int i=0;i<n;i++)
a[i] = c.nextInt();
return a;
}
public long[] scanArrayL(int n){
long a[] = new long[n];
for(int i=0;i<n;i++)
a[i] = c.nextLong();
return a;
}
public void printArray(long[] a){
for(int i=0;i<a.length;i++)
w.print(a[i]+" ");
w.println();
}
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 TaskB(),"TaskB",1<<26).start();
}
}
| linear | 608_B. Hamming Distance Sum | CODEFORCES |
import java.util.Scanner;
public class HammingDistancesSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String a = sc.nextLine(), b = sc.nextLine();
long sum = 0;
int frequency[][] = new int[200010][2];
for (int i = 1; i <= b.length(); i++) {
for (int j = 0; j < 2; j++)
frequency[i][j] = frequency[i - 1][j];
frequency[i][Character.getNumericValue((b.charAt(i - 1)))]++;
}
for (int i = 0; i < a.length(); i++) {
int c = Character.getNumericValue(a.charAt(i));
for (int j = 0; j < 2; j++) {
int flippingTerm = Math.abs(c - j);
int endOfWindowValue = frequency[b.length() - a.length() + i + 1][j];
int startOfWindowOffset = frequency[i][j];
sum += flippingTerm * (endOfWindowValue - startOfWindowOffset);
}
}
System.out.println(sum);
sc.close();
}
}
| linear | 608_B. Hamming Distance Sum | CODEFORCES |
/* package whatever; // don't place package name! */
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Ideone
{
static int check(int temp)
{
int count1 = 0;
while (temp>0)
{
if(temp % 2 != 0)
count1++;
temp/= 2;
}
return count1;
}
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
Scanner sc=new Scanner(System.in);
String a=sc.next();
String b=sc.next();
int m=a.length();
int n=b.length();
int[] zero=new int[n];
int[] one=new int[n];
for(int i=0;i<n;i++)
{
if(i==0)
{
if(b.charAt(i)=='0')
zero[i]++;
else
one[i]++;
}
else
{
zero[i]=zero[i-1];
one[i]=one[i-1];
if(b.charAt(i)=='0')
zero[i]++;
else
one[i]++;
}
}
long res=0;
for(int i=0;i<m;i++)
{
int x=n-m+i;
if(a.charAt(i)=='0')
res+=one[x];
else
res+=zero[x];
if(i>0)
{
if(a.charAt(i)=='0')
res-=one[i-1];
else
res-=zero[i-1];
}
}
System.out.println(res);
}
} | linear | 608_B. Hamming Distance Sum | CODEFORCES |
import java.io.*;
import java.lang.*;
import java.util.*;
import static java.lang.Integer.*;
import static java.lang.Long.*;
import static java.lang.Math.*;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
public class Main {
Main() throws IOException {
String a = nextLine();
String b = nextLine();
long ans = 0;
int s = 0;
for (int i = 0; i < b.length() - a.length(); ++i) {
s += b.charAt(i) == '1' ? 1 : 0;
}
for (int i = 0; i < a.length(); ++i) {
s += b.charAt(i + b.length() - a.length()) == '1' ? 1 : 0;
ans += a.charAt(i) == '1' ? b.length() - a.length() + 1 - s : s;
s -= b.charAt(i) == '1' ? 1 : 0;
}
out.println(ans);
}
//////////////////////////////
PrintWriter out = new PrintWriter(System.out, false);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stok = null;
String nextLine() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
stok = new StringTokenizer(in.readLine());
}
return stok.nextToken();
}
public static void main(String args[]) throws IOException {
if (args.length > 0) {
setIn(new FileInputStream(args[0] + ".inp"));
setOut(new PrintStream(args[0] + ".out"));
}
Main solver = new Main();
solver.out.flush(); // could be replace with a method, but nah, this is just competitive programming :p
}
}
| linear | 608_B. Hamming Distance Sum | 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.io.StreamTokenizer;
import java.util.Arrays;
public class B {
// -- DEBUG switch --
static final boolean DBG = false;
static StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
static int[] e = new int[100001];
public static void main(String[] args) throws IOException {
int n = i(), k = i(), cnt = 0;
int[] a = new int[n+1];
for (int i=1; i<=n; i++){
a[i] = i();
if (e[a[i]] == 0)
cnt++;
e[a[i]]++;
}
if (k > cnt){
pw.println("-1 -1");
pw.close();
return;
}
if (cnt == n){
pw.print("1 " + k);
pw.close();
return;
}
if (k == 1){
pw.println("1 1");
pw.close();
return;
}
Arrays.fill(e, 0);
int i = 1, j = 0, unik = 0, start = 0, end = 0, len = n, m = 0;
if (e[a[i]] == 0){
unik++;
}
e[a[i]]++;
while (i+1<=n && a[i+1] == a[i]){
i = i+1;
}
j = i+1;
while (j <= n){
if (e[a[j]] == 0){
unik++;
if (unik == k){
while (e[a[i]] > 1){
e[a[i]]--;
i++;
while (i+1<=n && a[i+1] == a[i]){
i = i+1;
}
}
m = j - i + 1;
if (m < len){
start = i; end = j; len = m;
if (m == k)
break;
}
while (i <=n && unik == k){
e[a[i]]--;
if (e[a[i]] == 0)
unik--;
i++;
while (i+1<=n && a[i+1] == a[i]){
i = i+1;
}
}
}
}
e[a[j]]++;
while (j+1<=n && a[j+1] == a[j]){
j++;
}
j++;
}
pw.println(start + " " + end);
pw.close();
}
static int i() throws IOException{
st.nextToken();
return (int)st.nval;
}
static long l() throws IOException {
st.nextToken();
return (long)st.nval;
}
static double d() throws IOException {
st.nextToken();
return st.nval;
}
static String s() throws IOException{
st.nextToken();
return st.sval;
}
}
| linear | 224_B. Array | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class Array {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
/*
if ( k == 1 ){
System.out.println("1 1"); return;
}
*/
int last[] = new int[100001];
int distinct = 0;
for ( int i = 0 ; i < n ; ++i ) {
int t = in.nextInt();
if ( last[t] == 0 ) ++distinct;
last[t] = i+1;
if ( distinct == k ) {
int min = i+1;
for ( int j = 0 ; j < last.length ; ++j ) {
if ( last[j] != 0 ) min = min>last[j]?last[j]:min;
}
System.out.println(min+" "+(i+1)); return;
}
}
System.out.println("-1 -1");
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class C_138B {
private static BufferedReader in;
private static StringTokenizer st;
private static PrintWriter out;
static String nextToken() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
static int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
public static void main(String[] args) throws NumberFormatException, IOException {
in = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer("");
out = new PrintWriter(new OutputStreamWriter(System.out));
int n = nextInt();
int k = nextInt();
int [] a = new int [n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
Set<Integer> set = new HashSet<Integer>();
for (int i = 0; i < a.length; i++) {
set.add(a[i]);
if(set.size()==k){
Set<Integer> set2 = new HashSet<Integer>();
for (int j = i; j >= 0; j--) {
set2.add(a[j]);
if(set2.size()==k){
out.print((j+1)+" "+(i+1));
out.close();
return;
}
}
}
}
out.print("-1 -1");
out.close();
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.StringTokenizer;
public class Array {
void run() {
try {
BufferedReader bfd = new BufferedReader(new InputStreamReader(
System.in));
StringTokenizer tk = new StringTokenizer(bfd.readLine());
int n = Integer.parseInt(tk.nextToken());
int k = Integer.parseInt(tk.nextToken()), i;
int arr[] = new int[n];
tk = new StringTokenizer(bfd.readLine());
for(i=0;i<n;++i)
arr[i] = Integer.parseInt(tk.nextToken());
int dist=0,l=0,r=0;
HashSet<Integer> hs = new HashSet<Integer>();
for(i=0; i<n; ++i) {
if(!hs.contains(arr[i])){
hs.add(arr[i]);
dist++;
}
if(dist==k) break;
r++;
}
int freq[] = new int[100010];
if(hs.size()<k) System.out.println("-1 -1");
else {
while(l<arr.length-1 && l<r && arr[l]==arr[l+1])
++l;
while(r>=1 && r>l && arr[r]==arr[r-1])
--r;
for(i=l;i<=r;++i)
freq[arr[i]]++;
while(freq[arr[l]]>1){
freq[arr[l]]--;
l++;
}
while(freq[arr[r]]>1){
freq[arr[r]]--;
r--;
}
System.out.println((l+1)+" " +(r+1));
}
} catch (Exception e) {
}
}
public static void main(String[] args) {
new Array().run();
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.*;
import java.util.*;
public class B implements Runnable {
void Solution() throws IOException {
int n = nextInt(), k = nextInt();
int[] mas = new int[n];
for (int i = 0; i < n; i++)
mas[i] = nextInt();
int l = 0, r = 0;
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
map.put(mas[l], 1);
int cur = 1;
while (true) {
if (cur == k) {
print(l + 1, r + 1);
return;
}
r++;
if (r >= n)
break;
int kol = map.containsKey(mas[r]) ? map.remove(mas[r]) : 0;
if (kol == 0) {
cur++;
map.put(mas[r], 1);
} else
map.put(mas[r], kol + 1);
while (true) {
kol = map.remove(mas[l]);
if (kol == 1) {
map.put(mas[l], 1);
break;
} else
map.put(mas[l++], kol - 1);
}
}
print(-1, -1);
}
public static void main(String[] args) {
new B().run();
}
BufferedReader in;
PrintWriter out;
StringTokenizer tokenizer;
public void run() {
try {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
Solution();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(0);
}
}
void print(Object... obj) {
for (int i = 0; i < obj.length; i++) {
if (i != 0)
out.print(" ");
out.print(obj[i]);
}
}
void println(Object... obj) {
print(obj);
out.println();
}
void halt() {
out.close();
System.exit(0);
}
String nextLine() throws IOException {
return in.readLine();
}
String next() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens())
tokenizer = new StringTokenizer(nextLine());
return tokenizer.nextToken();
}
int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(next());
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.*;
import java.util.*;
public class B {
static int i(String s) { return Integer.parseInt(s); }
public static void main(String[] args) throws Exception {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
String[] arr = in.readLine().split(" ");
int n = i(arr[0]);
int k = i(arr[1]);
int[] A = new int[n];
arr = in.readLine().split(" ");
for(int i=0; i<n; i++)
A[i] = i(arr[i]);
int st = 0;
int cnt = 0;
int[] cnts = new int[100*100*10+1];
for(int i=0; i<n; i++) {
cnts[A[i]]++;
if(cnts[A[i]] == 1) cnt++;
else while(cnts[A[st]] > 1) {
cnts[A[st]]--;
st++;
}
if(cnt == k) {
System.out.println((st+1)+" "+(i+1));
return;
}
}
System.out.println(-1+" "+-1);
}
}
| linear | 224_B. Array | CODEFORCES |
import java.util.HashMap;
import java.util.HashSet;
import java.util.Scanner;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int distinct = sc.nextInt();
HashMap<Integer, Integer> set = new HashMap<Integer, Integer>();
int[] ar = new int[n];
for (int i = 0; i < n; i++) {
ar[i] = sc.nextInt();
if (set.containsKey(ar[i])) {
set.put(ar[i], set.get(ar[i])+1);
} else {
set.put(ar[i], 1);
}
if (set.size() == distinct) {
int st = 0;
for (int j = 0; j < i; j++) {
st=j;
if (set.get(ar[j]) > 1) {
set.put(ar[j], set.get(ar[j]) - 1);
} else {
break;
}
}
System.out.println((st + 1) + " " + (i + 1));
return;
}
}
System.out.println("-1 -1");
}
}
| linear | 224_B. Array | CODEFORCES |
import java.util.Scanner;
public class B {
/**
* @param args
*/
public static void main(String[] args) {
Scanner scr = new Scanner(System.in);
int n = scr.nextInt();
int k = scr.nextInt();
int[] a = new int[n+1];
int[] d = new int[100001];
int tk = 0;
int l = 1;
int r = -1;
boolean find = false;
for (int i = 1; i <= n; i++){
a[i] = scr.nextInt();
if (d[a[i]] == 0){
d[a[i]] = 1;
tk++;
if ((!find) && (tk == k)){
find = true;
r = i;
} // if
} // if
} // for
if (r > 0) {
int[] cd = new int[100001];
tk = 0;
find = false;
for (int j = r; j >= l; j--){
if(cd[a[j]] == 0){
cd[a[j]] = 1;
tk++;
if ((!find) && (tk == k)){
find = true;
l = j;
break;
} // if
} // if
} // for
System.out.println(l + " " + r);
} // if
else {
System.out.println("-1 -1");
}
}
}
| linear | 224_B. Array | CODEFORCES |
import java.util.*;
import java.io.*;
public class B {
public void solve() throws IOException {
int n = nextInt();
int k = nextInt();
int[] a = new int[n];
for(int i = 0; i < n; i++){
a[i] = nextInt();
}
int[] num = new int[n];
Set<Integer> set = new HashSet<Integer>();
int s = -1;
int l = -1;
for(int i = 0; i < n; i++){
set.add(a[i]);
num[i] = set.size();
if( num[i] == k ){
l = i+1;
set = new HashSet<Integer>();
for(int j = i; j >= 0; j--){
set.add(a[j]);
if( set.size() == k ){
s = j+1;
break;
}
}
break;
}
}
writer.println(s + " " + l);
}
public static void main(String[] args) throws IOException {
new B().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() throws IOException {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class PrB {
public static long time;
public static void main(String[] args) throws Exception {
time = System.currentTimeMillis();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(br.readLine());
n = Integer.parseInt(st.nextToken());
k = Integer.parseInt(st.nextToken());
a = new int[n];
st = new StringTokenizer(br.readLine());
for(int i = 0; i < n; i++)
a[i] = Integer.parseInt(st.nextToken());
int end = end();
if(end < 0) System.out.println("-1 -1");
else System.out.println((start(end) + 1) + " " + (end + 1));
br.close();
System.exit(0);
}
static int n, k;
static int[] a;
public static int end() throws Exception {
boolean[] reached = new boolean[100002];
int rem = k;
for(int i = 0; i < n; i++) {
if(!reached[a[i]]) {
rem--;
if(rem == 0) return i;
}
reached[a[i]] = true;
}
return -1;
}
public static int start(int end) throws Exception {
boolean[] reached = new boolean[100002];
int rem = k;
for(int i = end; i >= 0; i--) {
if(!reached[a[i]]) {
rem--;
if(rem == 0) return i;
}
reached[a[i]] = true;
}
return 0;
}
public static void checkTime() {
System.out.println(System.currentTimeMillis() - time);
time = System.currentTimeMillis();
}
} | linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class B {
static BufferedReader in;
static StringTokenizer st;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = nextInt();
int k = nextInt();
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = nextInt();
}
Set<Integer> set_1 = new HashSet<Integer>();
for (int i = 1; i <= n; i++) {
set_1.add(a[i]);
if (set_1.size() == k) {
Set<Integer> set_2 = new HashSet<Integer>();
for (int j = i; j >= 1; j--) {
set_2.add(a[j]);
if (set_2.size() == k) {
out.print(j + " " + i);
out.close();
return;
}
}
}
}
out.print("-1 -1");
out.close();
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) throws Exception {
new B().solve();
}
void solve() throws IOException {
BufferedReader in = new BufferedReader(new
InputStreamReader(System.in));
// Scanner sc = new Scanner(System.in);
String[] sp = in.readLine().split(" ");
int n = Integer.parseInt(sp[0]);
int k = Integer.parseInt(sp[1]);
int[] a = new int[n];
sp = in.readLine().split(" ");
for (int i = 0; i < n; i++) {
a[i] = Integer.parseInt(sp[i]);
}
HashMap<Integer, Integer> map = new HashMap<Integer, Integer>();
int r = 0;
map.put(a[r], 1);
while (map.size() < k) {
r++;
if (r == n) {
// ng
System.out.println("-1 -1");
return;
}
if (map.containsKey(a[r])) {
map.put(a[r], map.get(a[r]) + 1);
} else {
map.put(a[r], 1);
}
}
int l = 0;
while (map.get(a[l]) > 1) {
map.put(a[l], map.get(a[l]) - 1);
l++;
}
System.out.println((l + 1) + " " + (r + 1));
}
}
//
| linear | 224_B. Array | CODEFORCES |
import java.io.*;
import java.util.*;
import java.math.*;
public class Main {
static StringTokenizer st;
static PrintWriter out = new PrintWriter(System.out,true);
static BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
public static int nextInt() throws Exception {
if(st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine());
return Integer.parseInt(st.nextToken());
}
public static long nextLong() throws Exception {
if(st == null || !st.hasMoreTokens()) st = new StringTokenizer(in.readLine());
return Long.parseLong(st.nextToken());
}
public static void main(String[] args) throws Exception {
HashSet<Integer> set = new HashSet<>();
int n = nextInt();
int k = nextInt();
int[] m = new int[n];
int[] d = new int[n];
for(int i = 0;i < n;i++) m[i] = nextInt();
int l = -1;
int r = -1;
for(int i = 0;i < n;i++) {
set.add(m[i]);
d[i] = set.size();
if(d[i] == k) {
r = i;
break;
}
}
if(r == -1) {
out.println("-1 -1");
return;
}
for(int i = r;i >= 0;i--) {
set.remove(m[i]);
if(set.size() == 0) {
l = i;
break;
}
}
out.println((l+1)+" "+(r+1));
}
} | linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class B {
static BufferedReader in;
static StringTokenizer st;
static String next() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static void main(String[] args) throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
int n = nextInt();
int k = nextInt();
int[] a = new int[n + 1];
for (int i = 1; i <= n; i++) {
a[i] = nextInt();
}
Set<Integer> set_1 = new HashSet<Integer>();
for (int i = 1; i <= n; i++) {
set_1.add(a[i]);
if (set_1.size() == k) {
Set<Integer> set_2 = new HashSet<Integer>();
for (int j = i; j >= 1; j--) {
set_2.add(a[j]);
if (set_2.size() == k) {
out.print(j + " " + i);
out.close();
return;
}
}
}
}
out.print("-1 -1");
out.close();
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.*;
public class B {
private void solve() throws IOException {
int n = nextInt();
int k = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
int[] f = new int[100000 + 2];
int min = Integer.MAX_VALUE;
int cur = 0;
int start = 0;
int from = -1, to = -1;
for (int i = 0; i < n; i++) {
f[a[i]]++;
if (f[a[i]] == 1) cur++;
if (cur == k) {
while (f[a[start]] > 1) {
f[a[start]]--;
start++;
}
if (i - start + 1 < min) {
min = i - start + 1;
from = start;
to = i;
}
}
}
pl(from == -1 ? "-1 -1" : ((1 + from) + " " + (1 + to)));
}
public static void main(String[] args) {
new B().run();
}
BufferedReader reader;
StringTokenizer tokenizer;
PrintWriter writer;
public void run() {
try {
reader = new BufferedReader(new InputStreamReader(System.in));
tokenizer = null;
writer = new PrintWriter(System.out);
solve();
reader.close();
writer.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(1);
}
}
int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
BigInteger nextBigInteger() throws IOException {
return new BigInteger(nextToken());
}
String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
void p(Object... objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.flush();
writer.print(objects[i]);
writer.flush();
}
}
void pl(Object... objects) {
p(objects);
writer.flush();
writer.println();
writer.flush();
}
int cc;
void pf() {
writer.printf("Case #%d: ", ++cc);
writer.flush();
}
} | linear | 224_B. Array | CODEFORCES |
import java.util.*;
public class B138 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[] = new int[100004];
int b[] = new int[100004];
int n, m, ans = 0, dau, cuoi=-1;
n = sc.nextInt();
m = sc.nextInt();
for(int i=0;i<100004;i++) a[i] = 0;
for(int i=0;i<n;i++){
b[i] = sc.nextInt();
if(a[b[i]]==0){
a[b[i]] = 1;
ans++;
if(ans==m){
cuoi = i+1;
break;
}
}
}
for(int i=cuoi-1;i>=00;i--){
if(a[b[i]]==1){
a[b[i]] = 0;
ans--;
if(ans==0){
System.out.println((i+1)+" "+cuoi);
System.exit(0);
}
}
}
System.out.println("-1 -1");
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static final double eps = 1e-8;
public static void main(String[] args) throws IOException {
try {
int n = nextInt();
int k = nextInt();
int[] a = new int[n];
int[] d = new int[n];
int[] dr = new int[n];
boolean used[] = new boolean[100001];
a[0] = nextInt();
used[ a[0] ] = true;
d[0] = 1;
for(int i = 1; i < n; i++)
{
a[i] = nextInt();
if(!used[ a[i] ])
{
used[ a[i] ] = true;
d[i] = d[i - 1] + 1;
}
else
{
d[i] = d[i - 1];
}
}
Arrays.fill(used, false);
int r = Arrays.binarySearch(d, k);
if(r < 0)
{
pw.println("-1 -1");
return;
}
while( r > 0 && d[r] == d[r - 1] )
r--;
used[ a[r] ] = true;
dr[r] = 1;
for(int i = r - 1; i >= 0; i--)
{
if(!used[ a[i] ])
{
used[ a[i] ] = true;
dr[i] = dr[i + 1] + 1;
}
else
{
dr[i] = dr[i + 1];
}
}
int l = 0;
while(l < n - 1 && dr[l] == dr[l + 1] && r - l >= k)
l++;
pw.println(l + 1 + " " + (r + 1));
}
finally {
pw.close();
}
}
static Scanner sc = new Scanner(System.in);
static PrintWriter pw = new PrintWriter(System.out);
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st ;
static int nextInt() throws IOException {
in.nextToken();
return (int) in.nval;
}
static long nextLong() throws IOException {
in.nextToken();
return (long) in.nval;
}
static double nextDouble() throws IOException {
in.nextToken();
return in.nval;
}
static String next() throws IOException {
in.nextToken();
return in.sval;
}
static void outArray(int[] O) {
for(int i = 0; i < O.length - 1; i++)
pw.print(O[i] + " ");
pw.println(O[O.length - 1]);
}
} | linear | 224_B. Array | CODEFORCES |
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.FileReader;
import java.io.BufferedWriter;
import java.util.Set;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.File;
import java.io.Writer;
import java.util.StringTokenizer;
import java.util.HashSet;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author jarek
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt(), k = in.nextInt();
int[] a = IOUtils.readIntArray(in, n);
Set<Integer> cnt = new HashSet<Integer>();
int i = 0;
while (i < n && cnt.size() < k) {
cnt.add(a[i]);
if (cnt.size() < k)
++i;
}
if (cnt.size() < k)
out.print("-1 -1");
else {
int r = i;
cnt = new HashSet<Integer>();
while (i >= 0 && cnt.size() < k) {
cnt.add(a[i]);
if (cnt.size() < k) --i;
}
out.print(i + 1, r + 1);
}
}
}
class InputReader {
BufferedReader br;
StringTokenizer st;
public InputReader(File f) {
try {
br = new BufferedReader(new FileReader(f));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public InputReader(InputStream f) {
br = new BufferedReader(new InputStreamReader(f));
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
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();
}
}
class IOUtils {
public static int[] readIntArray(InputReader in, int size) {
int[] array = new int[size];
for (int i = 0; i < size; i++)
array[i] = in.nextInt();
return array;
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.util.HashSet;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Lokesh Khandelwal aka (codeKNIGHT | phantom11)
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n=in.nextInt(),k=in.nextInt();
int a[]=new int[n];
int i;
for(i=0;i<n;i++)
a[i]=in.nextInt();
HashSet<Integer> hs=new HashSet<Integer>();
boolean status=false;
int index=-1;
for(i=0;i<n;i++)
{
hs.add(a[i]);
if(hs.size()==k)
{
index=i;
status=true;
break;
}
}
if(!status)
{
out.println(-1+" "+ -1);
return;
}
HashSet<Integer> hash=new HashSet<Integer>();
for(i=index;i>=0;i--)
{
hash.add(a[i]);
if(hash.size()==k)
{
break;
}
}
out.println((i+1)+" "+(index+1));
}
}
class InputReader
{
BufferedReader in;
StringTokenizer tokenizer=null;
public InputReader(InputStream inputStream)
{
in=new BufferedReader(new InputStreamReader(inputStream));
}
public String next()
{
try{
while (tokenizer==null||!tokenizer.hasMoreTokens())
{
tokenizer=new StringTokenizer(in.readLine());
}
return tokenizer.nextToken();
}
catch (IOException e)
{
return null;
}
}
public int nextInt()
{
return Integer.parseInt(next());
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.StringTokenizer;
public class Solver {
StringTokenizer st;
BufferedReader in;
PrintWriter out;
public static void main(String[] args) throws NumberFormatException,
IOException {
Solver solver = new Solver();
solver.open();
long time = System.currentTimeMillis();
solver.solve();
if (!"true".equals(System.getProperty("ONLINE_JUDGE"))) {
System.out.println("Spent time: "
+ (System.currentTimeMillis() - time));
}
solver.close();
}
public void open() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}
public String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(in.readLine());
}
return st.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(nextToken());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(nextToken());
}
public double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(nextToken());
}
public void solve() throws NumberFormatException, IOException {
int n = nextInt();
int k = nextInt();
int[] ar = new int[n];
int[] ex = new int[100005];
int dif = 0;
for(int i=0;i<n;i++){
int tmp = nextInt();
ar[i] = tmp;
if (ex[tmp]++==0){
dif++;
}
}
if (dif<k){
out.println("-1 -1");
return;
}
Arrays.fill(ex, 0);
dif = 0;
int right = 0;
while(dif<k){
int tmp = ar[right];
if(ex[tmp]++==0){
dif++;
}
right++;
}
int left = 0;
while (ex[ar[left]]-- > 1) left++;
out.println((left+1)+" "+right);
}
public void close() {
out.flush();
out.close();
}
} | linear | 224_B. Array | CODEFORCES |
import java.util.*;
import java.math.*;
public class Main {
static final long MOD = 1000000007L;
public static void main(String[] args) throws Exception {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
int k = scan.nextInt();
int res = -1;
int[] arr = new int[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = scan.nextInt();
}
BitSet bits = new BitSet();
int count = 0;
int end = -1;
for (int i = 0; i < arr.length; i++) {
if (!bits.get(arr[i])) {
bits.set(arr[i]);
count++;
if (count == k) {
end = i;
break;
}
}
}
if (end == -1) {
System.out.print("-1 -1");
return;
}
bits = new BitSet();
count = 0;
int start = end;
while (start >= 0) {
if (!bits.get(arr[start])) {
bits.set(arr[start]);
count++;
if (count == k) {
break;
}
}
start--;
}
System.out.println((start + 1) + " " + (end + 1));
}
} | linear | 224_B. Array | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main {
boolean eof;
public String nextToken() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
} catch (Exception e) {
eof = true;
return "-1";
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(nextToken());
}
int NOD(int a, int b) {
while (a * b > 0) {
if (a > b) {
a %= b;
} else {
b %= a;
}
}
return a + b;
}
void solve() {
int n = nextInt(), k= nextInt();
int[] a = new int[n];
for (int i = 0; i < n; ++i){
a[i] = nextInt() - 1;
}
int[] b = new int[100000];
int p = 0;
for (int i = 0; i < n; ++i){
++b[a[i]];
if (b[a[i]] == 1){
++p;
}
if (k == p){
int j;
for (j = 0; j <= i; ++j){
if (b[a[j]] > 1){
--b[a[j]];
} else {
break;
}
}
out.print((j + 1) + " " + (i + 1));
return;
}
}
out.print("-1 -1");
}
BufferedReader br;
StringTokenizer st;
PrintWriter out;
void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(new OutputStreamWriter(System.out));
// br = new BufferedReader(new FileReader("input.txt"));
// out = new PrintWriter(new FileWriter("output.txt"));
solve();
br.close();
out.close();
} catch (Throwable e) {
e.printStackTrace();
System.exit(1);
}
}
public static void main(String[] args) {
new Main().run();
}
}
| linear | 224_B. Array | CODEFORCES |
import java.util.Map;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.math.BigDecimal;
import java.io.BufferedWriter;
import java.util.Locale;
import java.util.InputMismatchException;
import java.util.HashMap;
import java.util.Set;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.Writer;
import java.math.BigInteger;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Jacob Jiang
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
OutputWriter out = new OutputWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.nextInt();
int k = in.nextInt();
int[] a = in.nextIntArray(n);
if (k == 1) {
out.println("1 1");
return;
}
int left = -1, right = -1;
Counter<Integer> counter = new Counter<Integer>();
while (true) {
right++;
if (right == n) {
out.println("-1 -1");
return;
}
counter.add(a[right]);
if (counter.size() >= k)
break;
}
while (true) {
left++;
if (counter.get(a[left]) == 1)
break;
counter.add(a[left], -1);
}
out.printLine(left + 1, right + 1);
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1 << 16];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int nextInt() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c & 15;
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public int[] nextIntArray(int count) {
int[] result = new int[count];
for (int i = 0; i < count; i++) {
result[i] = nextInt();
}
return result;
}
}
class OutputWriter {
private PrintWriter writer;
public OutputWriter(OutputStream stream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(stream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object obj) {
writer.print(obj);
}
public void println() {
writer.println();
}
public void println(String x) {
writer.println(x);
}
public void print(char c) {
writer.print(c);
}
public void close() {
writer.close();
}
public void printItems(Object... items) {
for (int i = 0; i < items.length; i++) {
if (i != 0) {
print(' ');
}
print(items[i]);
}
}
public void printLine(Object... items) {
printItems(items);
println();
}
}
class Counter<T> extends HashMap<T, Long> {
public void add(T obj, long count) {
put(obj, get(obj) + count);
}
public void add(T obj) {
put(obj, get(obj) + 1L);
}
public Long get(Object key) {
return containsKey(key) ? super.get(key) : 0L;
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Reader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;
public class Main {
static class MyReader{
private BufferedReader reader = null;
private StringTokenizer tokenizer = null;
MyReader(Reader r) throws IOException{
reader = new BufferedReader(r);
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public String nextToken() throws IOException {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
tokenizer = new StringTokenizer(reader.readLine());
}
return tokenizer.nextToken();
}
}
public static void main(String []args) throws IOException{
PrintWriter writer = new PrintWriter(new OutputStreamWriter(System.out));
MyReader reader = new MyReader(new InputStreamReader(System.in));
int n = reader.nextInt();
int k = reader.nextInt();
int []a = new int[n];
for (int i = 0; i < n; ++i)
a[i] = reader.nextInt();
int j = 0;
HashMap<Integer,Integer> map = new HashMap<>();
for (int i = 0; i < n; ++i){
if (map.containsKey(a[i]))
map.put(a[i], map.get(a[i])+1);
else{
map.put(a[i], 1);
if (map.size()==k) { j = i+1; break; }
}
}
if (map.size()<k){
System.out.println("-1 -1");
return;
}
for (int i = 0; i < n; ++i){
if (map.get(a[i])==1){
System.out.println(i+1 + " " + j);
return;
}
map.put(a[i], map.get(a[i])-1);
}
}
} | linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.TreeMap;
public class B {
static class Scanner
{
BufferedReader rd;
StringTokenizer tk;
public Scanner() throws IOException
{
rd=new BufferedReader(new InputStreamReader(System.in));
tk=new StringTokenizer(rd.readLine());
}
public String next() throws IOException
{
while(!tk.hasMoreTokens())
tk=new StringTokenizer(rd.readLine());
return tk.nextToken();
}
public int nextInt() throws NumberFormatException, IOException
{
return Integer.valueOf(this.next());
}
}
static int N,K;
static int[] array=new int[100010];
public static void main(String args[]) throws IOException{
Scanner sc=new Scanner();
N=sc.nextInt();
K=sc.nextInt();
for(int i=0;i<N;i++)
array[i]=sc.nextInt();
TreeMap<Integer,Integer> map=new TreeMap<Integer,Integer>();
boolean flag=false;
for(int i=0;i<N;i++){
if (!map.containsKey(array[i])){
map.put(array[i], i);
if (map.size()==K){
flag=true;
break;
}
}
else
map.put(array[i], i);
}
if (!flag)
System.out.println("-1 -1");
else{
Set<Integer> s=map.keySet();
int l=Integer.MAX_VALUE;
int r=Integer.MIN_VALUE;
for(int k: s){
int tmp=map.get(k);
l=Math.min(l, tmp);
r=Math.max(r, tmp);
}
System.out.println((l+1)+" "+(r+1));
}
}
} | linear | 224_B. Array | CODEFORCES |
import java.io.*;
import java.util.*;
public class CodeForces {
static boolean ONLINE_JUDGE = System.getProperty("ONLINE_JUDGE") != null;
void runCase(int caseNum) throws IOException {
int n = nextInt();
int k = nextInt();
int[] nums = new int[n];
int distinct = 0;
int L = -1, R = -1;
int minLen = Integer.MAX_VALUE;
int maxNum = 0;
for (int i = 0; i < n; ++i) {
nums[i] = nextInt();
maxNum = Math.max(maxNum, nums[i]);
}
int[] count = new int[maxNum + 1];
int j = 0;
for (int i = 0; i < n; ++i) {
++count[nums[i]];
if (count[nums[i]] == 1) {
++distinct;
if (distinct >= k) {
for (; j <= i; ++j) {
--count[nums[j]];
if (count[nums[j]] <= 0) {
--distinct;
if (distinct < k) {
if (i - j < minLen) {
minLen = i - j;
L = j + 1;
R = i + 1;
}
break;
}
}
}
}
}
}
out.print(L + " " + R);
}
public static void main(String[] args) throws IOException {
if (ONLINE_JUDGE){
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
}else{
in = new BufferedReader(new FileReader("input.txt"));
out = new PrintWriter("output.txt");
}
new CodeForces().runIt();
out.flush();
out.close();
return;
}
static BufferedReader in;
private StringTokenizer st;
static PrintWriter out;
String next() throws IOException {
while (!st.hasMoreTokens()) {
String line = in.readLine();
if (line == null) {
return null;
}
st = new StringTokenizer(line, " ");
}
return st.nextToken();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
void runIt() throws IOException {
st = new StringTokenizer("");
// int N = nextInt();
// for (int i = 0; i < N; i++) {
// runCase(i + 1);
// }
runCase(0);
out.flush();
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
/**
* Created with IntelliJ IDEA.
* User: Alexey
* Date: 16.09.12
* Time: 19:29
* To change this template use File | Settings | File Templates.
*/
public class stub implements Runnable {
public static void main(String[] args) {
new stub().run();
}
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(System.out);
StringTokenizer st;
public String nextToken() throws IOException {
while (st == null || !st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private void solve() throws IOException {
int[] cnt = new int[(int) 1e6];
int n = nextInt();
int k = nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
int cur = 0;
int left = 0;
int i = left;
while (i < n && cur != k) {
if (cnt[a[i]] == 0) {
cur++;
}
cnt[a[i]]++;
i++;
}
i--;
if (cur != k) {
out.println("-1 -1");
return;
}
int right = i;
while (cnt[a[left]] > 1) {
cnt[a[left]]--;
left++;
}
out.println((left + 1) + " " + (right + 1));
}
public void run() {
try {
solve();
out.close();
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.StringTokenizer;
public class Main{
/**
* @param args
*/
public static void main(String[] args) {
Parser p = new Parser(System.in);
PrintWriter pw= new PrintWriter(System.out);
int n = p.nextInt();
int k = p.nextInt();
int[] a = p.nextIntArray(n);
int [] pos = new int[100001];
Arrays.fill(pos,-1);
int cnt = 0;
for(int i=0; i<n; ++i){
int e = a[i];
if( pos[e] == -1 ){
++cnt;
}
pos[e] = i;
if( cnt == k){
break;
}
}
if( cnt < k){
pw.println("-1 -1");
pw.close();
return;
}
int min = 1000000;
int max = -1;
for(int i=0; i<100001; ++i){
if(pos[i] != -1 && pos[i] < min ){
min = pos[i];
}
if( pos[i] > max){
max = pos[i];
}
}
++min;
++max;
pw.println(min+" "+max);
pw.close();
}
static class Parser{
StringTokenizer st;
BufferedReader br;
public Parser(InputStream is){
this.br = new BufferedReader( new InputStreamReader(is));
}
public int nextInt(){
return Integer.parseInt(nextToken());
}
public double nextDouble(){
return Double.parseDouble(nextToken());
}
public String nextString(){
return nextToken();
}
public int[] nextIntArray(int s){
int[] a = new int[s];
for(int i=0; i<s; ++i){
a[i] = nextInt();
}
return a;
}
public int[][] nextIntTable(int r, int c){
int[][] a = new int[r][c];
for(int i=0; i<r; ++i){
a[i] = nextIntArray(c);
}
return a;
}
private String nextToken() {
if( st == null || ! st.hasMoreTokens() ){
try{
st = new StringTokenizer( br.readLine());
}catch( Exception e){
e.printStackTrace();
}
}
return st.nextToken();
}
}
}
| linear | 224_B. Array | CODEFORCES |
import java.util.Arrays;
import java.util.Scanner;
public class B {
void run(){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), k = sc.nextInt();
int[] a = new int[n+1];
for(int i=1;i<=n;i++)a[i]=sc.nextInt();
int[] c = new int[100001];
int num = 0;
int ri = -1, rj = -1;
int s = 1, t = 0;
while(t<n){
t++;
if(c[a[t]]==0){
num++;
}
c[a[t]]++;
for(;k<=num;s++){
if(ri==-1 || rj-ri+1>t-s+1){
ri = s; rj = t;
}
c[a[s]]--;
if(c[a[s]]==0){
num--;
}
}
}
System.out.println(ri+" "+rj);
}
void debug(Object...o){
System.out.println(Arrays.deepToString(o));
}
public static void main(String[] args) {
new B().run();
}
} | linear | 224_B. Array | CODEFORCES |
import java.io.InputStreamReader;
import java.io.IOException;
import java.util.Arrays;
import java.io.BufferedReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author Sunits789
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader in = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, InputReader in, PrintWriter out) {
int n=in.nextInt();
int k=in.nextInt();
int arr[]=new int[n];
in.getArray(arr);
int ansl=-1;
int ansr=n;
int occ[]=new int[100100];
boolean f[]=new boolean[n];
Arrays.fill(occ,0);
Arrays.fill(f,true);
int pk=0;
for (int l=0,r=0;r<n&&l<n;){
int num=arr[r];
if(f[r]){
f[r]=false;
occ[num]++;
if(occ[num]==1){
pk++;
}
}
//System.out.println(l+" "+r+" "+pk+" "+k);
if(pk<k){
r++;
}
else if (pk==k){
if((r-l)<=(ansr-ansl)){
ansl=l+1;
ansr=r+1;
}
num=arr[l];
occ[num]--;
if(occ[num]==0){
pk--;
}
l++;
}
else {
num=arr[l];
occ[num]--;
if(occ[num]==0){
pk--;
}
l++;
}
}
if(ansl==-1){
ansr=-1;
}
out.println(ansl+" "+ansr);
}
}
class InputReader{
private BufferedReader reader;
private StringTokenizer tokenizer;
public InputReader(InputStream stream){
reader = new BufferedReader(new InputStreamReader(stream));
tokenizer = null;
}
public String next(){
while (tokenizer == null||!tokenizer.hasMoreTokens()){
try{
tokenizer = new StringTokenizer(reader.readLine());
}
catch (IOException e){
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt(){
return Integer.parseInt(next());
}
public void getArray(int arr[]){
for(int i=0;i<arr.length;i++){
arr[i]=nextInt();
}
}
}
| linear | 224_B. Array | CODEFORCES |
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int a[] = new int[100004];
int b[] = new int[100004];
int n, m, ans = 0, dau, cuoi=-1;
n = sc.nextInt();
m = sc.nextInt();
for(int i=0;i<100004;i++) a[i] = 0;
for(int i=0;i<n;i++){
b[i] = sc.nextInt();
if(a[b[i]]==0){
a[b[i]] = 1;
ans++;
if(ans==m){
cuoi = i+1;
break;
}
}
}
for(int i=cuoi-1;i>=00;i--){
if(a[b[i]]==1){
a[b[i]] = 0;
ans--;
if(ans==0){
System.out.println((i+1)+" "+cuoi);
System.exit(0);
}
}
}
System.out.println("-1 -1");
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
* @author George Marcus
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
StreamInputReader in = new StreamInputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskB solver = new TaskB();
solver.solve(1, in, out);
out.close();
}
}
class TaskB {
public void solve(int testNumber, StreamInputReader in, PrintWriter out) {
int N = in.readInt();
int K = in.readInt();
int[] A = new int[N];
for(int i = 0; i < N; i++)
A[i] = in.readInt();
int num = 0;
int left = 0;
int right = 0;
int[] seen = new int[100005];
while(right < N && num < K) {
if(seen[A[right]] == 0)
num++;
seen[A[right]]++;
right++;
}
right--;
if(num == K) {
while(seen[A[left]] > 1) {
seen[A[left]]--;
left++;
}
out.print((left + 1) + " " + (right + 1));
return;
}
out.print("-1 -1");
}
}
class StreamInputReader extends InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar, numChars;
public StreamInputReader(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++];
}
}
abstract class InputReader {
public abstract int read();
public int readInt() {
return Integer.parseInt(readString());
}
public String readString() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuffer res = new StringBuffer();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
| linear | 224_B. Array | CODEFORCES |
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class CF224B {
public static void main(String[] args) throws Exception {
new CF224B().solve();
}
private void solve() throws Exception {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = sc.nextInt();
}
final int MAX_A = 100000;
int[] freq = new int[MAX_A+1];
int numDistinct = 0;
int r = -1;
for (int i = 0; i < n; i++) {
int t = a[i];
freq[t]++;
if (freq[t] == 1) {
numDistinct++;
}
if (numDistinct == k) {
r = i;
break;
}
}
if (r == -1) {
System.out.println("-1 -1");
return;
}
int l;
for (l = 0; l < r; l++) {
int t = a[l];
freq[t]--;
if (freq[t] == 0) {
break;
}
}
System.out.println((l+1) + " " + (r+1));
}
}
| linear | 224_B. Array | CODEFORCES |
import java.util.Scanner;
import java.util.TreeMap;
public class B {
static Scanner in;
static void put(TreeMap<Integer, Integer> m, int key) {
if (m.containsKey(key)) {
m.put(key, m.get(key) + 1);
} else {
m.put(key, 1);
}
}
static void remove(TreeMap<Integer, Integer> m, int key) {
if (!m.containsKey(key))
return;
m.put(key, m.get(key) - 1);
if (m.get(key) == 0) {
m.remove(key);
}
}
public static void main(String[] args) {
in = new Scanner(System.in);
int n = in.nextInt();
int k = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++) {
a[i] = in.nextInt();
}
int i = 0;
while (i + 1 < n && a[i + 1] == a[0]) {
i++;
}
int left = i;
TreeMap<Integer, Integer> used = new TreeMap<Integer, Integer>();
for (; i < n; i++) {
put(used, a[i]);
if (used.size() == k) {
while (used.get(a[left]) > 1) {
remove(used, a[left]);
left++;
}
System.out.println(left + 1 + " " + (i + 1));
return;
}
}
System.out.println("-1 -1");
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
public class Code implements Runnable {
public static void main(String[] args) throws IOException {
new Thread(new Code()).start();
}
private void solve() throws IOException {
taskB();
}
private void taskB() throws IOException {
int n = nextInt(), k = nextInt();
int[] arr = new int[n];
for(int i = 0; i < n; ++i) arr[i] = nextInt();
int i = 0;
while(i < n - 1 && arr[i] == arr[i + 1]) ++i;
if(k == 1)
writer.println(1 + " " + 1);
else if(k == 2 && i == n - 2)
writer.println(n - 1 + " " + n);
else {
if(i == n - 1)
writer.println(-1 + " " + -1);
else {
int l = i;
Map<Integer, Integer> set = new HashMap<Integer, Integer>(n);
while(i < n && set.size() < k) {
if(set.containsKey(arr[i])) set.put(arr[i], set.get(arr[i]) + 1);
else set.put(arr[i], 1);
i += 1;
}
if(set.size() < k) writer.println(-1 + " " + -1);
else {
while(l < i && i - l > k && set.get(arr[l]) > 1) {
set.put(arr[l], set.get(arr[l]) - 1);
l += 1;
}
writer.println((l + 1) + " " + i);
}
}
}
}
private class Pair<E extends Comparable, V extends Comparable> implements Comparable<Pair<E, V>> {
public Pair(E first, V second) {
this.first = first;
this.second = second;
}
@Override
public int compareTo(Pair<E, V> obj) {
if(first.equals(obj.first)) return second.compareTo(obj.second);
return first.compareTo(obj.first);
}
@Override
public boolean equals(Object obj) {
Pair other = (Pair)obj;
return first.equals(other.first) && second.equals(other.second);
}
public E first;
public V second;
}
@Override
public void run() {
try {
if(in.equals("")) reader = new BufferedReader(new InputStreamReader(System.in));
else reader = new BufferedReader(new FileReader(in));
if(out.equals("")) writer = new PrintWriter(new OutputStreamWriter(System.out));
else writer = new PrintWriter(new FileWriter(out));
solve();
} catch(IOException e) {
e.printStackTrace();
} finally {
try {
reader.close();
writer.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
private int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
private long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
private double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
private float nextFloat() throws IOException {
return Float.parseFloat(nextToken());
}
private String nextToken() throws IOException {
while(st == null || !st.hasMoreTokens()) st = new StringTokenizer(reader.readLine());
return st.nextToken();
}
private String in = "", out = "";
private BufferedReader reader;
private PrintWriter writer;
private StringTokenizer st;
} | linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintStream;
import java.util.*;
public class B
{
BufferedReader in;
PrintStream out;
StringTokenizer tok;
public B() throws NumberFormatException, IOException
{
in = new BufferedReader(new InputStreamReader(System.in));
//in = new BufferedReader(new FileReader("in.txt"));
out = System.out;
run();
}
void run() throws NumberFormatException, IOException
{
int n = nextInt();
int k = nextInt();
int[] num = new int[n];
for(int i = 0; i < n; i++)
num[i] = nextInt();
int[] cant = new int[100001];
int cnt = 0;
int r = 0;
for(; r < n; r++)
{
if(cant[num[r]]==0)cnt++;
cant[num[r]]++;
if(cnt==k) break;
}
if(cnt<k)
{
out.println("-1 -1");
return;
}
int l = 0;
for(; l < r; l++)
{
cant[num[l]]--;
if(cant[num[l]]==0)cnt--;
if(cnt<k) break;
}
out.println((l+1)+" "+(r+1));
}
public static void main(String[] args) throws NumberFormatException, IOException
{
new B();
}
String nextToken() throws IOException
{
if(tok ==null || !tok.hasMoreTokens()) tok = new StringTokenizer(in.readLine());
return tok.nextToken();
}
int nextInt() throws NumberFormatException, IOException
{
return Integer.parseInt(nextToken());
}
long nextLong() throws NumberFormatException, IOException
{
return Long.parseLong(nextToken());
}
double nextDouble() throws NumberFormatException, IOException
{
return Double.parseDouble(nextToken());
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.*;
import java.util.*;
public class C138B
{
private static StringTokenizer st;
public static void nextLine(BufferedReader br) throws IOException
{
st = new StringTokenizer(br.readLine());
}
public static int nextInt()
{
return Integer.parseInt(st.nextToken());
}
public static String next()
{
return st.nextToken();
}
public static long nextLong()
{
return Long.parseLong(st.nextToken());
}
/**
* @param args
*/
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
nextLine(br);
int n = nextInt();
int k = nextInt();
int[] a = new int[n];
nextLine(br);
for (int i = 0; i < n; i++) a[i] = nextInt();
int bp = 0, fp = 0, count = 0;
boolean good = false;
int[] mark = new int[100001];
for (fp = 0; fp < n; fp++)
{
if (mark[a[fp]] == 0)
{
count++;
}
mark[a[fp]]++;
if (count == k)
{
good = true;
break;
}
}
if (!good)
{
System.out.println("-1 -1");
return;
}
for (bp = 0; bp < fp; bp++)
{
if (mark[a[bp]] > 1)
{
mark[a[bp]]--;
}
else break;
}
System.out.println((bp+1) + " " + (fp+1));
}
}
| linear | 224_B. Array | CODEFORCES |