src
stringlengths 95
64.6k
| complexity
stringclasses 7
values | problem
stringlengths 6
50
| from
stringclasses 1
value |
---|---|---|---|
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B {
public static void main(String[] args) throws IOException {
InputReader in = new InputReader();
int n = in.nextInt();
int k = in.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = in.nextInt();
if (k > n) {
System.out.println(-1 + " " + -1);
return;
}
int[] v = new int[100010];
int cnt = 0;
for (int i = 0; i < k; i++) {
if (v[a[i]] == 0) {
cnt++;
}
v[a[i]]++;
}
int i = k;
while (cnt < k && i < n) {
if (v[a[i]] == 0) {
cnt++;
}
v[a[i]]++;
i++;
}
if (cnt != k) {
System.out.println(-1 + " " + -1);
} else {
int st = 0;
while (st < n && st < i && v[a[st]] > 1) {
v[a[st]]--;
st++;
}
System.out.println((st+1) + " " + (i));
}
}
static class InputReader {
BufferedReader in;
StringTokenizer st;
public InputReader() throws IOException {
in = new BufferedReader(new InputStreamReader(System.in));
st = new StringTokenizer(in.readLine());
}
public String next() throws IOException {
while (!st.hasMoreElements())
st = new StringTokenizer(in.readLine());
return st.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(next());
}
public long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
}
} | linear | 224_B. Array | CODEFORCES |
import java.util.Scanner;
public class BB {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int k=sc.nextInt();
int a[]=new int[n+1];
boolean used[]=new boolean[100009];
for (int i = 1; i <=n; i++) {
a[i]=sc.nextInt();
}
int cnt=0;
int id=0;
for (int i = 1; i <=n; i++) {
if(!used[a[i]]){
cnt++;
used[a[i]]=true;
}
if(cnt==k){
id=i;
break;
}
}
boolean x[]=new boolean[100005];
int y=0;
int id1=0;
if(id==0){
System.out.println(-1+" "+-1);
return;
}
for (int i =id; i >=1; i--) {
if(!x[a[i]]){
y++;
x[a[i]]=true;
}
if(y==k){
id1=i;
break;
}
}
System.out.println(id1+" "+id);
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
public class B {
/**
* @param args
*/
public static void main(String[] args) throws Exception {
// System.setIn(new FileInputStream("b.in"));
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String[] parts = bf.readLine().trim().split("[ ]+");
int N = Integer.parseInt(parts[0]);
int K = Integer.parseInt(parts[1]);
int[] nums = new int[N];
int idx = 0;
String line = bf.readLine();
for(int i = 0; i < line.length(); i++) {
char c = line.charAt(i);
if(c == ' ') idx++;
else {
int d = c - '0';
nums[idx] = 10 * nums[idx] + d;
}
}
int from = -1, to = -1;
HashMap<Integer, Integer> count = new HashMap<Integer, Integer>();
for(int i = 0; i < N; i++) {
Integer q = count.get(nums[i]);
if(q == null) count.put(nums[i], 1);
else count.put(nums[i], q + 1);
if(count.size() == K) {
to = i;
break;
}
}
if(count.size() < K) {
System.out.println("-1 -1");
return;
}
for(from = 0; from <= to; from++) {
Integer q = count.get(nums[from]);
if(q == 1) count.remove(nums[from]);
else count.put(nums[from], q - 1);
if(count.size() < K) break;
}
System.out.println((from + 1) + " " + (to + 1));
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.HashSet;
import java.util.StringTokenizer;
public class BT {
static BufferedReader in = new BufferedReader(new InputStreamReader(
System.in));
static StringTokenizer str;
static String SK;
static String next() throws IOException {
while ((str == null) || (!str.hasMoreTokens())) {
SK = in.readLine();
if (SK == null)
return null;
str = new StringTokenizer(SK);
}
return str.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static void main(String[] args) throws IOException {
int n, k;
n = nextInt();
k = nextInt();
HashSet<Integer> hs = new HashSet<Integer>();
HashMap<Integer, Integer> hm = new HashMap<Integer, Integer>();
int[] ar = new int[n];
int ii = 0, jj = -1;
for (int i = 0; i < n; i++) {
ar[i] = nextInt();
Integer iii = hm.get(ar[i]);
if(iii!=null)
hm.put(ar[i], ++iii); else hm.put(ar[i], 1);
hs.add(ar[i]);
if (hs.size() == k) {
jj = i;
break;
}
}
if (jj == -1) {
System.out.println(-1 + " " + (-1));
System.exit(0);
}
for (int i = 0; i < ar.length; i++) {
Integer iii = hm.get(ar[i]);
if (iii != null && iii - 1 > 0) {
hm.put(ar[i], --iii);
ii++;
} else {
break;
}
}
System.out.println((ii+1) + " " + (jj+1));
}
} | linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;
public class B {
static Vector<Integer> primes;
public static void main(String[] args) throws IOException {
InputReader myScanner = new InputReader();
int n = myScanner.nextInt(), k = myScanner.nextInt();
myScanner.hasNext();
int all[] = new int[n];
boolean numbers[] = new boolean[100100];
int diff[] = new int[n];
all[0] = myScanner.nextInt();
diff[0] = 1;
numbers[all[0]] = true;
int r = -1;
if (k == 1)
r = 1;
for (int i = 1; i < all.length; i++) {
all[i] = myScanner.nextInt();
diff[i] = diff[i - 1];
if (!numbers[all[i]]) {
if (r == -1 && diff[i] + 1 == k)
r = i + 1;
numbers[all[i]] = true;
diff[i]++;
}
}
if (r == -1)
System.out.println(-1 + " " + -1);
else {
numbers = new boolean[100010];
int l = 0, cnt = 1;
numbers[all[r - 1]] = true;
if (k == 1)
System.out.println(1 + " " + 1);
else {
for (int i = r - 2; i >= 0; i--) {
if (!numbers[all[i]]) {
numbers[all[i]] = true;
cnt++;
}
if (cnt == k) {
l = i + 1;
break;
}
}
System.out.println(l + " " + r);
}
}
}
static class InputReader {
BufferedReader buff;
StringTokenizer tok;
String cur;
public InputReader() throws IOException {
buff = new BufferedReader(new InputStreamReader(System.in));
tok = new StringTokenizer(cur = buff.readLine());
}
public boolean hasNext() throws IOException {
if (!tok.hasMoreElements()) {
cur = buff.readLine();
if (cur == null)
return false;
tok = new StringTokenizer(cur);
}
return true;
}
public String next() throws IOException {
while (!tok.hasMoreElements())
tok = new StringTokenizer(cur = buff.readLine());
return tok.nextToken();
}
public int nextInt() throws NumberFormatException, IOException {
return Integer.parseInt(next());
}
public long nextLong() throws NumberFormatException, IOException {
while (!tok.hasMoreElements())
tok = new StringTokenizer(cur = buff.readLine());
return Long.parseLong(next());
}
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.DataInputStream;
import java.io.InputStream;
import java.util.Scanner;
import java.util.TreeMap;
import java.util.TreeSet;
public class B {
public static void main(String[] args) throws Exception {
Parserdoubt2333 s = new Parserdoubt2333(System.in);
int n = s.nextInt();
int k = s.nextInt();
int a[] = new int[n];
for (int i = 0; i < a.length; i++) {
a[i] = s.nextInt();
}
TreeMap<Integer, Integer> tree = new TreeMap<Integer,Integer>();
int left = 0;
int right = 0;
for (right = 0; right < a.length; right++) {
if(tree.containsKey(a[right]))
tree.put(a[right], tree.get(a[right]) + 1);
else
tree.put(a[right],1);
if(tree.size() == k)
break;
}
if(tree.size() < k){
System.out.println("-1 -1");
return ;
}
// System.out.println(right);
for (left = 0; left < a.length; left++) {
int val = tree.get(a[left]);
val--;
if(val > 0)
tree.put(a[left],val);
if(val == 0)
break;
}
left++;
right++;
System.out.println(left + " "+right);
}
}
class Parserdoubt2333
{
final private int BUFFER_SIZE = 1 << 18;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Parserdoubt2333(InputStream in)
{
din = new DataInputStream(in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String nextString() throws Exception
{
StringBuffer sb=new StringBuffer("");
byte c = read();
while (c <= ' ') c = read();
do
{
sb.append((char)c);
c=read();
}while(c>' ');
return sb.toString();
}
public char nextChar() throws Exception
{
byte c=read();
while(c<=' ') c= read();
return (char)c;
}
public int nextInt() throws Exception
{
int ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = c == '-';
if (neg) c = read();
do
{
ret = ret * 10 + c - '0';
c = read();
} while (c > ' ');
if (neg) return -ret;
return ret;
}
public long nextLong() throws Exception
{
long ret = 0;
byte c = read();
while (c <= ' ') c = read();
boolean neg = c == '-';
if (neg) c = read();
do
{
ret = ret * 10 + c - '0';
c = read();
} while (c > ' ');
if (neg) return -ret;
return ret;
}
private void fillBuffer() throws Exception
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1) buffer[0] = -1;
}
private byte read() throws Exception
{
if (bufferPointer == bytesRead) fillBuffer();
return buffer[bufferPointer++];
}
} | linear | 224_B. Array | CODEFORCES |
import java.util.*;
public class TaskB {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
int n = s.nextInt();
int k = s.nextInt();
int[] nums = new int[100000 + 10];
int first = -1, last = -1;
Set<Integer> dif = new TreeSet<Integer>();
s.nextLine();
for (int i = 0; i < n; i++) {
nums[i] = s.nextInt();
dif.add(nums[i]);
if (dif.size() == k) {
last = i;
break;
}
}
dif.clear();
for (int i = last; i >= 0; i--) {
dif.add(nums[i]);
if (dif.size() == k) {
first = i;
break;
}
}
if (last == -1)
System.out.print("-1 -1");
else
System.out.print(Integer.toString(first + 1) + " " + Integer.toString(last + 1));
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class B {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
// Scanner scan = new Scanner(System.in);
StringTokenizer st = new StringTokenizer(br.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
st = new StringTokenizer(br.readLine());
int[] a = new int[n];
for(int i = 0 ; i <n;i++)
a[i] = Integer.parseInt(st.nextToken());
int l = 0, r = 0;
int[] t = new int[100001];
int kk = 0;
int min = 1 << 25 , ll =-1 , rr = -1;
while(r < n)
{
int x = a[r++];
t[x]++;
if(t[x] == 1)
kk++;
while(r < n && kk < k)
{
x = a[r++];
t[x]++;
if(t[x] == 1)
kk++;
}
while(kk == k && l < r)
{
x = a[l];
if(t[x] == 1)
break;
t[x]--;
l++;
}
if(kk == k)
{
int m = r-l+1;
if(m < min)
{
ll = l+1;
rr = r;
min = m;
}
}
}
System.out.println(ll +" "+rr);
}
}
| linear | 224_B. Array | CODEFORCES |
import java.util.*;
public class b {
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n = input.nextInt(), k = input.nextInt();
int[] data = new int[n];
for(int i = 0; i<n; i++)
data[i] = input.nextInt();
int[] freq = new int[100001];
int count = 0;
for(int i = 0; i<n; i++)
{
if(freq[data[i]] == 0)
count++;
freq[data[i]]++;
}
if(count<k)
System.out.println("-1 -1");
else
{
int start = 0;
for(int i = 0; i<n; i++)
{
//System.out.println(i + " " + count + " " + freq[data[i]]);
if(count > k)
{
freq[data[i]]--;
if(freq[data[i]] == 0)
count--;
}
else
{
if(freq[data[i]] > 1)
{
freq[data[i]]--;
}
else
{
start = i;
break;
}
}
}
int end = n-1;
for(int i = n-1; i>=0; i--)
{
if(freq[data[i]] == 1)
{
end = i;
break;
}
else
freq[data[i]]--;
}
start++;
end++;
if(start<= end)
System.out.println(start + " " + end);
else
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.PrintWriter;
import java.io.StreamTokenizer;
import java.util.HashMap;
import java.util.Map;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Tibor
*/
public class test{
// static java.io.InputStreamReader converter = new java.io.InputStreamReader(System.in);
// static java.io.BufferedReader in = new java.io.BufferedReader(converter);
//
// public static String readLine() {
// String s = "";
// try {
//
// s = in.readLine();
// } catch (Exception e) {
// System.out.println("Error! Exception: " + e);
// }
// return s;
// }
static StreamTokenizer in = new StreamTokenizer(new BufferedReader(
new InputStreamReader(System.in)));
static PrintWriter out = new PrintWriter(System.out);
// static {
// in.ordinaryChars('-', '-');
// in.ordinaryChars('+', '+');
// in.wordChars('-', '-');
// in.wordChars('+', '+');
// }
static int nextInt() {
try {
in.nextToken();
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
return (int) in.nval;
}
static String nextString() {
try {
in.nextToken();
} catch (IOException ex) {
Logger.getLogger(test.class.getName()).log(Level.SEVERE, null, ex);
}
return in.sval;
}
public static void main(String args[]) throws Exception {
int n = nextInt();
long k = nextInt();
long a[] = new long[n + 1];
Map<Long, Long> drb = new HashMap<Long, Long>();
int elso = 1;
long sk = 0;
long sm = 0;
long minjo = Long.MAX_VALUE;
long minjoh = Long.MAX_VALUE;
Vector<long[]> ret = new Vector<long[]>();
for (int i = 1; i <= n; i++) {
a[i] = nextInt();
if (/*a[i - 1] <= a[i]*/true) {
sm += a[i];
if (drb.containsKey(a[i])) {
drb.put(a[i], drb.get(a[i]) + 1);
} else {
drb.put(a[i], (long) 1);
sk++;
}
while (sk > k || drb.get(a[elso]) > 1) {
long s = drb.get(a[elso]);
if (s == 1) {
drb.remove(a[elso]);
sk--;
} else {
drb.put(a[elso], s - 1);
}
sm -= a[elso];
elso++;
}
if (sk == k) {
if (minjo > sm) {
minjo = sm;
ret.clear();
minjoh = i - elso;
}
if (minjo == sm) {
if (minjoh > i - elso) {
ret.clear();
minjoh = i - elso;
}
ret.add(new long[]{elso, i});
}
}
} else {
elso = i;
drb.clear();
drb.put(a[i], (long) 1);
sk = 1;
sm = a[i];
if (k == 1) {
if (minjo > sm) {
minjo = sm;
ret.clear();
}
if (minjo == sm) {
ret.add(new long[]{elso, i});
}
}
}
}
for (long[] r : ret) {
System.out.print(r[0] + " ");
System.out.print(r[1] + " ");
break;
}
if (ret.size() == 0) {
System.out.print(-1 + " ");
System.out.print(-1 + " ");
}
}
}
| linear | 224_B. Array | CODEFORCES |
import java.util.*;
import java.math.*;
import java.io.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
public class test{
// ArrayList<Integer> lis = new ArrayList<Integer>();
// ArrayList<String> lis = new ArrayList<String>();
//
// static long sum=0;
// int a,b,c;
// 1000000007 (10^9+7)
//static int mod = 1000000007;
//static int dx[]={1,-1,0,0};
//static int dy[]={0,0,1,-1};
//static long H,L;
public static void main(String[] args) throws Exception, IOException{
//String line=""; throws Exception, IOException
//(line=br.readLine())!=null
//Scanner sc =new Scanner(System.in);
Reader sc = new Reader(System.in);
// while( ){
int n=sc.nextInt(),m=sc.nextInt(),a[]=new int[n];
int b[]=new int[100000+1], r=n+1;
for(int i=0;i<n;i++)a[i]=sc.nextInt();
// db(a);
int s=0,t=-1, sum=0;
int as=0,at=0;
for(;;){
while(t<n-1 && sum<m){
t++;
if( b[ a[t] ]<1 ){sum++; }
b[a[t]]++;
}
db(s,t,sum);
if( sum<m )break;
as=s;at=t;
r=min(r,t-s+1);
if( b[ a[s] ]==1 ){sum--; }
// if(t==n-1)break;
b[a[s]]--;
s++;
}
if( n<r )System.out.println("-1 -1");
else System.out.println(as+1+" "+(at+1));
}
static void db(Object... os){
System.err.println(Arrays.deepToString(os));
}
}
/*
class P implements Comparable<P>{
// implements Comparable<P>
int x;
boolean b;
P(int x,boolean b){
this.x=x;
this.b=b;
}
public int compareTo(P y){
return x-y.x;
}
}
//*/
class Reader
{
private BufferedReader x;
private StringTokenizer st;
public Reader(InputStream in)
{
x = new BufferedReader(new InputStreamReader(in));
st = null;
}
public String nextString() throws IOException
{
while( st==null || !st.hasMoreTokens() )
st = new StringTokenizer(x.readLine());
return st.nextToken();
}
public int nextInt() throws IOException
{
return Integer.parseInt(nextString());
}
public long nextLong() throws IOException
{
return Long.parseLong(nextString());
}
public double nextDouble() throws IOException
{
return Double.parseDouble(nextString());
}
}
| linear | 224_B. Array | CODEFORCES |
import java.io.*;
import java.util.*;
public class Array224B {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(f.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
int[] array = new int[n];
int[] visited = new int[100002];
st = new StringTokenizer(f.readLine());
for(int i=0;i<n;i++){
array[i] = Integer.parseInt(st.nextToken());
}
int count = 0;
int begin = array[0];
while(count<n && array[count] == begin){
count++;
}
count--;
int kcount = 1;
visited[array[count]]++;
int bindex = count;
boolean good=true;
count++;
while(kcount<k){
if(count==n){
System.out.println("-1 -1");
good=false;
break;
}
if(visited[array[count]]==0){
kcount++;
}
visited[array[count]]++;
count++;
}
if(good&&k!=1){
for(int i=bindex;i<count;i++){
if(visited[array[i]]==1){
break;
}
bindex++;
visited[array[i]]--;
}
for(int i=count-1;i>bindex;i--){
if(visited[array[i]]==1){
break;
}
count--;
visited[array[i]]--;
}
}
if(k==1){
System.out.println("1 1");
}
else if(good){
System.out.println(bindex+1+" "+count);
}
}
} | linear | 224_B. Array | CODEFORCES |
import java.io.*;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.StringTokenizer;
public class Array implements Runnable {
void solve() throws IOException {
int n = readInt();
int k = readInt();
int a[] = new int[n];
int startIdx = 0;
int endIdx = -1;
Map<Integer,Integer> map = new HashMap<Integer,Integer>();
for(int i = 0; i < n; i ++) {
a[i] = readInt();
if(map.containsKey(a[i]))
map.put(a[i], map.get(a[i]) + 1);
else
map.put(a[i], 1);
if(map.size() == k && endIdx == -1) {
endIdx = i;
break;
}
}
if(endIdx != -1) {
while(startIdx < n && map.get(a[startIdx])>1) {
map.put(a[startIdx], map.get(a[startIdx]) - 1);
startIdx ++;
}
startIdx ++;
endIdx ++;
} else
startIdx = -1;
out.println((startIdx)+" "+(endIdx));
}
BufferedReader in;
PrintWriter out;
StringTokenizer tok = new StringTokenizer("");
public static void main(String[] args) {
new Array().run();
}
public void run() {
try {
long t1 = System.currentTimeMillis();
if (System.getProperty("ONLINE_JUDGE") != null) {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
} else {
in = new BufferedReader(new FileReader("/Users/shenchen/input.txt"));
out = new PrintWriter("/Users/shenchen/output.txt");
}
Locale.setDefault(Locale.US);
solve();
in.close();
out.close();
long t2 = System.currentTimeMillis();
System.err.println("Time = " + (t2 - t1));
} catch (Throwable t) {
t.printStackTrace(System.err);
System.exit(-1);
}
}
String readString() throws IOException {
while (!tok.hasMoreTokens()) {
tok = new StringTokenizer(in.readLine());
}
return tok.nextToken();
}
int readInt() throws IOException {
return Integer.parseInt(readString());
}
long readLong() throws IOException {
return Long.parseLong(readString());
}
double readDouble() throws IOException {
return Double.parseDouble(readString());
}
} | linear | 224_B. Array | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
import java.util.StringTokenizer;
public class CF_1029E_Tree_with_Small_Distances {
static ArrayList<Integer> adj[];
static int dist[];
static boolean visitParent[];
static int ans=0;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n =sc.nextInt();
adj=new ArrayList[n+1];
dist = new int[n+1];
visitParent = new boolean[n+1];
for(int i=0;i<=n;i++) adj[i]=new ArrayList<Integer>();
int max=0;
//first contribution
for(int i=1;i<n;i++){
int u = sc.nextInt(),v=sc.nextInt();
adj[u].add(v);
adj[v].add(u);
}
dist[1]=0;
dfs(1,1);
System.out.println(ans);
}
private static void dfs(int i , int j) {
// TODO Auto-generated method stub
boolean f = false;
for(int k=0;k<adj[i].size();k++){
int x = adj[i].get(k);
if(x!=j){
dist[x]=dist[i]+1;
dfs(x,i);
if(visitParent[x])
f=true;
}
}
//System.out.println(Arrays.toString(dist));
if(dist[i]>2&&!visitParent[j]&&!f&&!visitParent[i]){
visitParent[j]=true;
ans++;
for(int v=0;v<adj[i].size();v++){
}
}
}
static class Scanner
{
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s){ br = new BufferedReader(new InputStreamReader(s));}
public String next() throws IOException
{
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public double nextDouble() throws IOException
{
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if(x.charAt(0) == '-')
{
neg = true;
start++;
}
for(int i = start; i < x.length(); i++)
if(x.charAt(i) == '.')
{
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
}
else
{
sb.append(x.charAt(i));
if(dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg?-1:1);
}
public int nextInt() throws IOException {return Integer.parseInt(next());}
public long nextLong() throws IOException {return Long.parseLong(next());}
public String nextLine() throws IOException {return br.readLine();}
public boolean ready() throws IOException {return br.ready(); }
}
}
| linear | 1029_E. Tree with Small Distances | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class CF1197B {
public static void main(String[] args) {
FastReader input = new FastReader();
int n = input.nextInt();
int[] arr = new int[n];
int max = 0;
int maxIndex = 0;
for(int i = 0;i < n;i++){
arr[i] = input.nextInt();
if(arr[i] > max){
max = arr[i];
maxIndex = i;
}
}
int j = maxIndex - 1;
int k = maxIndex + 1;
while (j >= 0 && k < n){
if(arr[j] > arr[k]){
if(arr[j] < max){
max = arr[j];
j--;
}
else {
System.out.println("NO");
return;
}
}
else{
if(arr[k] < max){
max = arr[k];
k++;
}
else{
System.out.println("NO");
return;
}
}
}
if(j >= 0){
while (j >= 0){
if(arr[j] < max){
max = arr[j];
j--;
}
else{
System.out.println("NO");
return;
}
}
}
if(k < n){
while (k < n){
if(arr[k] < max){
max = arr[k];
k++;
}
else{
System.out.println("NO");
return;
}
}
}
if(j == -1 && k == n){
System.out.println("YES");
}
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | linear | 1197_B. Pillars | CODEFORCES |
import java.util.Scanner;
public class pillar {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
int a[]=new int[200005];
for (int i=1;i<=n;i++)
a[i]=sc.nextInt();
for (int i=2;i<n;i++)
if (a[i-1]>a[i]&&a[i]<a[i+1]) {
System.out.println("NO");
return;
}
System.out.println("YES");
}
}
| linear | 1197_B. Pillars | CODEFORCES |
import java.util.*;
import java.io.*;
import java.math.*;
public class Solution{
public static void main(String[] args)throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
int[] a = new int[n];
for(int i=0;i<n;i++) a[i] = Integer.parseInt(st.nextToken());
int ind = 0;
for(int i=0;i<n;i++){
if(a[i]==n){
ind = i;
break;
}
}
boolean ok = true;
for(int i=ind+1;i<n;i++) if(a[i]>a[i-1]) ok = false;
for(int i=ind-1;i>=0;i--) if(a[i]>a[i+1]) ok = false;
if(ok) System.out.println("YES");
else System.out.println("NO");
}
}
| linear | 1197_B. Pillars | CODEFORCES |
import java.io.BufferedReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.io.ObjectInputStream.GetField;
import java.security.KeyStore.Entry;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.PriorityQueue;
import java.util.Queue;
import java.util.Stack;
import java.util.StringTokenizer;
import java.util.TreeMap;
import java.util.TreeSet;
import javax.swing.JLabel;
public class codeforcesreturn {
static class edge {
int u;
int v;
public edge(int u, int v) {
this.u = u;
this.v = v;
}
}
static ArrayList<Integer>[] adjlist;
static int[][] adjmatrix;
static int[][] adjmatrix2;
static boolean[] vis;
static boolean[] intialvis;
static boolean[] vis2;
static int[] counter;
static int V, E;
static Stack<Integer> st;
static ArrayList<Integer> arrylist;
static boolean flag;
static int[] dx = new int[] { 1, -1, 0, 0 };
static int[] dy = new int[] { 0, 0, 1, -1 };
static int[] Arr;
static PrintWriter pw;
static boolean ans = true;;
public static long gcd(long u, long v) {
if (u == 0)
return v;
return gcd(v % u, u);
}
public static void bib(int u) {
vis[u] = true;
for (int v : adjlist[u]) {
if (!vis[v]) {
counter[v] = 1 ^ counter[u];
bib(v);
} else if (counter[v] != (1 ^ counter[u]))
ans = false;
}
}
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
// FileWriter f = new FileWriter("C:\\Users\\Hp\\Desktop\\out.txt");
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int k = sc.nextInt();
int sum = n;
for (long i = 0; i < 1e5; i++) {
if (i * (i + 1) / 2 - (n - i) == k) {
System.out.println(n - i);
break;
}
}
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = sc.nextInt(), k = sc.nextInt();
long rhs = 2l * (n + k);
for (int x = 1; ; x++) {
long lhs = 1l * x * x + 3l * x;
if (rhs == lhs) {
out.println(n - x);
break;
}
}
out.flush();
out.close();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public int[] nextIntArray(int n) throws IOException {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public long[] nextLongArray(int n) throws IOException {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
public Integer[] nextIntegerArray(int n) throws IOException {
Integer[] a = new Integer[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
public double[] nextDoubleArray(int n) throws IOException {
double[] ans = new double[n];
for (int i = 0; i < n; i++)
ans[i] = nextDouble();
return ans;
}
public short nextShort() throws IOException {
return Short.parseShort(next());
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
InputReader sc = new InputReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
Task solver = new Task();
solver.solve(1, sc, out);
out.close();
}
static class Task {
public void solve(int testNumber, InputReader sc, PrintWriter out) {
double n=sc.nextInt();
double k=sc.nextInt();
double ans=n-(-1.5+Math.sqrt(9.0/4+2*(n+k)));
out.printf("%.0f\n",ans);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class B {
static long n, k;
public static void main(String[] args) throws Exception {
Scanner in = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
n = in.nextLong();
k = in.nextLong();
long ans = n - TernarySearch(0, n);
pw.println(ans);
pw.close();
}
static long cal(long m) {
long x = (m * (m + 1)) / 2;
long y = x - (n - m);
return abs(k - y);
}
static long TernarySearch(long l, long r) {
long m1, m2;
while (r - l > 5) {
m1 = (2 * l + r) / 3;
m2 = (l + 2 * r) / 3;
if (cal(m1) > cal(m2)) l = m1;
else r = m2;
}
long min = cal(l), i = l;
for (; l <= r; l++) {
long t = cal(l);
if (t < min) {
min = t;
i = l;
}
}
return i;
}
static void debug(Object... obj) {
System.err.println(Arrays.deepToString(obj));
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
//never leave a uncompleted question in this file, always remove the code after submitting it.
import java.io.*;
import java.util.*;
public class run{
public static void main(String args[]){
Scanner in = new Scanner(System.in);
long n = in.nextInt();
long k = in.nextInt();
long ans = (-3 + (long)Math.sqrt(9+8*(n+k)))/2;
System.out.println(n-ans);
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Vaibhav Pulastya
*/
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);
BSportMafia solver = new BSportMafia();
solver.solve(1, in, out);
out.close();
}
static class BSportMafia {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long n = in.nextInt();
long k = in.nextInt();
long d = 9 + 4 * (2 * n + 2 * k);
double smh = Math.sqrt(d);
double ans = (-3 + smh) / 2;
out.println(n - (int) ans);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
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 - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
private boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.StringTokenizer;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Jaynil
*/
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);
BSportMafia solver = new BSportMafia();
solver.solve(1, in, out);
out.close();
}
static class BSportMafia {
public void solve(int testNumber, InputReader in, PrintWriter out) {
long n = in.nextLong();
long k = in.nextLong();
long b = 2 * n + 3;
long c = n * n - 2 * k + n;
long d = b * b - 4 * c;
long val = (b - (long) Math.sqrt(d)) / 2;
out.println(val);
}
}
static class InputReader {
public BufferedReader reader;
public StringTokenizer tokenizer;
public InputReader(InputStream stream) {
reader = new BufferedReader(new InputStreamReader(stream), 32768);
tokenizer = null;
}
public String next() {
while (tokenizer == null || !tokenizer.hasMoreTokens()) {
try {
tokenizer = new StringTokenizer(reader.readLine());
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return tokenizer.nextToken();
}
public long nextLong() {
return Long.parseLong(next());
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.StringTokenizer;
public class CF1195B {
private static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
private static BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
private static StringTokenizer st;
public static void main(String[] args) throws Exception {
st = new StringTokenizer(reader.readLine());
long n = Long.parseLong(st.nextToken());
long k = Long.parseLong(st.nextToken());
long put = (-3 + (long)Math.sqrt((long)9 + 8 * k + 8 * n)) / 2;
long eat = n - put;
writer.write(Long.toString(eat));
writer.newLine();
writer.flush();
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
public class cf5722{
public static void main(String[] args) throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st=new StringTokenizer(br.readLine());
long n=Long.parseLong(st.nextToken());
long k=Long.parseLong(st.nextToken());
long ans=((-3-(long)Math.sqrt(9+4*(1*2*(n+k))))/2);
long ans1=((-3+(long)Math.sqrt(9+4*(1*2*(n+k))))/2);
if(ans>0)
System.out.println(n-ans);
else{
System.out.println(n-ans1);
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.lang.Math;
public class Main{
public static void main(String[] args){
Scanner ak=new Scanner(System.in);
long n,k,x;
n=ak.nextLong();
k=ak.nextLong();
x=(long)((-3+Math.sqrt(9+8*(n+k)))/2);
System.out.println(n-x);
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
public class candies {
public void run() throws Exception {
Scanner file = new Scanner(System.in);
int actions = file.nextInt();
int left = file.nextInt();
int start = 0;
int c = 1;
while (true) {
start += c;
if (c + (start - left) == actions) break;
c++;
}
System.out.println(start - left);
}
public static void main(String[] args) throws Exception {
new candies().run();
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.util.*;
import java.lang.*;
import java.util.HashMap;
import java.util.PriorityQueue;
public class templ implements Runnable{
static class pair implements Comparable
{
int f;
int s;
pair(int fi,int se)
{
f=fi;
s=se;
}
public int compareTo(Object o)//ascending order
{
pair pr=(pair)o;
if(s>pr.s)
return 1;
if(s==pr.s)
{
if(f>pr.f)
return 1;
else
return -1;
}
else
return -1;
}
public boolean equals(Object o)
{
pair ob=(pair)o;
int ff;
int ss;
if(o!=null)
{
ff=ob.f;
ss=ob.s;
if((ff==this.f)&&(ss==this.s))
return true;
}
return false;
}
public int hashCode()
{
return (this.f+" "+this.s).hashCode();
}
}
public class triplet implements Comparable
{
int f,t;
int s;
triplet(int f,int s,int t)
{
this.f=f;
this.s=s;
this.t=t;
}
public boolean equals(Object o)
{
triplet ob=(triplet)o;
int ff;
int ss;
int tt;
if(o!=null)
{
ff=ob.f;
ss=ob.s;
tt=ob.t;
if((ff==this.f)&&(ss==this.s)&&(tt==this.t))
return true;
}
return false;
}
public int hashCode()
{
return (this.f+" "+this.s+" "+this.t).hashCode();
}
public int compareTo(Object o)//ascending order
{
triplet tr=(triplet)o;
if(t>tr.t)
return 1;
else
return -1;
}
}
void merge1(int arr[], int l, int m, int r)
{
int n1 = m - l + 1;
int n2 = r - m;
int L[] = new int [n1];
int R[] = new int [n2];
for (int i=0; i<n1; ++i)
L[i] = arr[l + i];
for (int j=0; j<n2; ++j)
R[j] = arr[m + 1+ j];
int i = 0, j = 0;
int k = l;
while (i < n1 && j < n2)
{
if (L[i]<=R[j])
{
arr[k] = L[i];
i++;
}
else
{
arr[k] = R[j];
j++;
}
k++;
}
while (i < n1)
{
arr[k] = L[i];
i++;
k++;
}
while (j < n2)
{
arr[k] = R[j];
j++;
k++;
}
}
void sort1(int arr[], int l, int r)
{
if (l < r)
{
int m = (l+r)/2;
sort1(arr, l, m);
sort1(arr , m+1, r);
merge1(arr, l, m, r);
}
}
public static void main(String args[])throws Exception
{
new Thread(null,new templ(),"templ",1<<27).start();
}
public void run()
{
try
{
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n=in.ni();
int x=in.ni();
long l=1,r=n;
while(l<=r)
{
long mid=(l+r)/2;
long k=(mid*(mid+1))/2-(n-mid);
if(k==x)
{
out.println((n-mid));
break;
}
else if(k<x)
l=mid+1;
else
r=mid-1;
}
out.close();
}
catch(Exception e){
return;
}
}
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
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 String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
// CodeForces Round #914 B train done
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class SportMafia {
int n,k;
int nCand;
private void readData(BufferedReader bin) throws IOException {
String s = bin.readLine();
String []ss = s.split(" ");
n = Integer.parseInt(ss[0]);
k = Integer.parseInt(ss[1]);
}
void printRes() {
System.out.println(nCand);
}
private void calculate() {
// count napitki
double p;
p = -1.5 + Math.sqrt(2.25 + 2.0*(n+k));
nCand = (int)Math.round(n-p);
}
public static void main(String[] args) throws IOException {
// BufferedReader bin = new BufferedReader(new FileReader("cactus.in"));
BufferedReader bin = new BufferedReader(
new InputStreamReader(System.in));
SportMafia l = new SportMafia();
l.readData(bin);
l.calculate();
l.printRes();
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
public class Main {
static int inf = (int) 1e9;
public static void main(String[] args) throws IOException {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(System.out);
int n = nextInt();
int k = nextInt();
long l = -1;
long r = 100000;
while(l != r - 1) {
long mid = (l + r) / 2;
if (mid * (mid + 1) / 2 - (n - mid) > k) r = mid;
else l = mid;
}
pw.println(n - l);
pw.close();
}
static BufferedReader br;
static StringTokenizer st = new StringTokenizer("");
static PrintWriter pw;
static String next() throws IOException {
while (!st.hasMoreTokens()) st = new StringTokenizer(br.readLine());
return st.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt(next());
}
static long nextLong() throws IOException {
return Long.parseLong(next());
}
static Double nextDouble() throws IOException {
return Double.parseDouble(next());
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long k = in.nextLong();
long res = solve(n, k);
System.out.println(res);
}
private static long solve(long n, long k) {
return solveEq(1, -3 - 2 * n, n * n + n - 2 * k);
}
private static long solveEq(long a, long b, long c) {
long delta = b * b - 4 * a * c;
return (-b - (long)Math.sqrt(delta)) / (2 * a);
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
public class b{
static class FastReader{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args)
{
FastReader sc = new FastReader();
//PrintWriter out = new PrintWriter(System.out);
double n = (double)sc.nextLong();
double k = (double)sc.nextLong();
double div = 9+8*n+8*k;
double ss = Math.sqrt(div);
//System.out.println(ss);
ss = (ss-3)/2;
System.out.println( (int)(n-ss) );
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
import static java.lang.Math.*;
public class Main {
static int n, k;
public static void main(String[] args) throws IOException {
FastScanner sc = new FastScanner();
PrintWriter pw = new PrintWriter(System.out);
n = sc.nextInt();
k = sc.nextInt();
long l = 0;
long r = n + 1;
while (l + 1 != r) {
long m = (r + l) / 2;
if (check(m))
l = m;
else
r = m;
}
pw.print(l * (l + 1L) / 2L - k);
pw.close();
}
public static boolean check(long m) {
return m * (m + 1) / 2 - (n - m) <= k;
}
}
class FastScanner {
static BufferedReader br;
static StringTokenizer st = new StringTokenizer("");
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public String next() throws IOException {
while (!st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
import java.text.*;
public class B1195 {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
PrintWriter pw = new PrintWriter(System.out);
long x =sc.nextInt();
long y =sc.nextInt();
long m = (-3+Math.round(Math.sqrt(9+8*(x+y))))/2;
long e = x-m;
pw.println(e);
pw.flush();
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Rustam Musin (t.me/musin_acm)
*/
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);
BSportivnayaMafiya solver = new BSportivnayaMafiya();
solver.solve(1, in, out);
out.close();
}
static class BSportivnayaMafiya {
public void solve(int testNumber, InputReader in, OutputWriter out) {
int n = in.readInt();
int k = in.readInt();
int have = 0;
for (int x = 1; ; x++) {
have += x;
if (have < k) {
continue;
}
if (have - (n - x) == k) {
out.print(n - x);
return;
}
}
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private InputReader.SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public int readInt() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public boolean isSpaceChar(int c) {
if (filter != null) {
return filter.isSpaceChar(c);
}
return isWhitespace(c);
}
public static boolean isWhitespace(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void close() {
writer.close();
}
public void print(int i) {
writer.print(i);
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayDeque;
import java.util.Arrays;
import java.util.StringTokenizer;
public class B {
static FastReader scan;
static PrintWriter out;
public static void main(String[] args) throws FileNotFoundException {
Solver solver = new Solver();
scan = new FastReader();
out = new PrintWriter(System.out);
int testCases = 1;
for(int i = 1; i <= testCases; i++) {
// out.print("Case #" + i + ": ");
solver.solve();
}
out.close();
}
static class Solver {
void solve() {
long n = scan.nextLong(), k = scan.nextLong();
long low = 0, high = n;
while(true) {
long mid = (low+high)/2;
long s = sum(n-mid);
if(s - mid == k) {
out.println(mid);
return;
}
else if(s - mid < k) {
high = mid-1;
}
else low = mid+1;
//out.println(s-mid + " " + mid);
}
}
static long sum(long a) {
if(a == 0) return 0;
return (a+1)*a/2;
}
}
// Sathvik's Template Stuff BELOW!!!!!!!!!!!!!!!!!!!!!!
static class DSU {
int[] root, size;
int n;
DSU(int n) {
this.n = n;
root = new int[n];
size = new int[n];
for (int i = 0; i < n; i++) {
root[i] = i;
size[i] = 1;
}
}
int findParent(int idx) {
while (root[idx] != idx) {
root[idx] = root[root[idx]];
idx = root[idx];
}
return idx;
}
boolean union(int x, int y) {
int parX = findParent(x);
int parY = findParent(y);
if (parX == parY)
return false;
if (size[parX] < size[parY]) {
root[parY] = parX;
size[parX] += size[parY];
} else {
root[parX] = parY;
size[parY] += size[parX];
}
return true;
}
}
static class Extra {
static void sort(int[] a) {
Integer[] aa = new Integer[a.length];
for (int i = 0; i < aa.length; i++)
aa[i] = a[i];
Arrays.sort(aa);
for (int i = 0; i < aa.length; i++)
a[i] = aa[i];
}
static void sort(long[] a) {
Long[] aa = new Long[a.length];
for (int i = 0; i < aa.length; i++)
aa[i] = a[i];
Arrays.sort(aa);
for (int i = 0; i < aa.length; i++)
a[i] = aa[i];
}
static void sort(double[] a) {
Double[] aa = new Double[a.length];
for (int i = 0; i < aa.length; i++)
aa[i] = a[i];
Arrays.sort(aa);
for (int i = 0; i < aa.length; i++)
a[i] = aa[i];
}
static void sort(char[] a) {
Character[] aa = new Character[a.length];
for (int i = 0; i < aa.length; i++)
aa[i] = a[i];
Arrays.sort(aa);
for (int i = 0; i < aa.length; i++)
a[i] = aa[i];
}
static long gcd(long a, long b) {
while (b > 0) {
long temp = b;
b = a % b;
a = temp;
}
return a;
}
static long lcm(long a, long b) {
return a * (b / gcd(a, b));
}
static boolean isPrime(long n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (long i = 5; i * i <= n; i = i + 6) {
if (n % i == 0 || n % (i + 2) == 0)
return false;
}
return true;
}
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
int[] nextIntArray(int n) {
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = nextInt();
return a;
}
long nextLong() {
return Long.parseLong(next());
}
long[] nextLongArray(int n) {
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = nextLong();
return a;
}
double nextDouble() {
return Double.parseDouble(next());
}
double[] nextDoubleArray(int n) {
double[] a = new double[n];
for (int i = 0; i < n; i++)
a[i] = nextDouble();
return a;
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.lang.*;
import java.io.*;
public class Main
{
PrintWriter out = new PrintWriter(System.out);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tok = new StringTokenizer("");
String next() throws IOException {
if (!tok.hasMoreTokens()) { tok = new StringTokenizer(in.readLine()); }
return tok.nextToken();
}
int ni() throws IOException { return Integer.parseInt(next()); }
long nl() throws IOException { return Long.parseLong(next()); }
void solve() throws IOException {
int n=ni(),k=ni();
int puts=(int)Math.sqrt(2*k);
int t=(puts*(puts+1))/2;
puts++;
while (t<k) { t+=puts; puts++; }
int turns=puts-1;
while (t-k!=n-turns) {
t+=puts;
puts++;
turns++;
}
System.out.println(t-k);
}
public static void main(String[] args) throws IOException {
new Main().solve();
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
public class R574B
{
public static void main(String[] args)
{
JS scan = new JS();
long n = scan.nextInt();
long put = 1;
long k = scan.nextInt();
long have = 0;
long moves = 0;
while(have < k) {
have += put;
put++;
moves++;
}
long ans = 0;
moves += have-k;
ans += have-k;
long lo = 0;
long hi = n-moves;
long bs = 0;
while(lo <= hi) {
//could she have eaten mid candies?
long mid = (lo+hi)/2;
long left = (n-moves)-mid+put-1;
long rr = tri(left)-tri(put);
if(rr <= mid) {
bs = mid;
hi = mid-1;
}
else {
lo = mid+1;
}
}
System.out.println(ans+bs);
}
static long tri(long n) {
return n*(n-1)/2;
}
static class JS{
public int BS = 1<<16;
public char NC = (char)0;
byte[] buf = new byte[BS];
int bId = 0, size = 0;
char c = NC;
double num = 1;
BufferedInputStream in;
public JS() {
in = new BufferedInputStream(System.in, BS);
}
public JS(String s) throws FileNotFoundException {
in = new BufferedInputStream(new FileInputStream(new File(s)), BS);
}
public char nextChar(){
while(bId==size) {
try {
size = in.read(buf);
}catch(Exception e) {
return NC;
}
if(size==-1)return NC;
bId=0;
}
return (char)buf[bId++];
}
public int nextInt() {
return (int)nextLong();
}
public long nextLong() {
num=1;
boolean neg = false;
if(c==NC)c=nextChar();
for(;(c<'0' || c>'9'); c = nextChar()) {
if(c=='-')neg=true;
}
long res = 0;
for(; c>='0' && c <='9'; c=nextChar()) {
res = (res<<3)+(res<<1)+c-'0';
num*=10;
}
return neg?-res:res;
}
public double nextDouble() {
double cur = nextLong();
return c!='.' ? cur:cur+nextLong()/num;
}
public String next() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c>32) {
res.append(c);
c=nextChar();
}
return res.toString();
}
public String nextLine() {
StringBuilder res = new StringBuilder();
while(c<=32)c=nextChar();
while(c!='\n') {
res.append(c);
c=nextChar();
}
return res.toString();
}
public boolean hasNext() {
if(c>32)return true;
while(true) {
c=nextChar();
if(c==NC)return false;
else if(c>32)return true;
}
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
public class submitting {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
StringTokenizer st = new StringTokenizer(sc.nextLine());
long n = Integer.parseInt(st.nextToken());
long k = Integer.parseInt(st.nextToken());
long put = n / 2;
long lower = 0;
long upper = n;
while (put * (put + 1) / 2 - (n - put) != k) {
if (put * (put + 1) / 2 - (n - put) > k) {
upper = put - 1;
put = (lower + upper) / 2;
} else {
lower = put + 1;
put = (lower + upper) / 2;
}
}
System.out.println(n - put);
sc.close();
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
public class Temppp {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextLong();
long k = sc.nextLong();
long ans = (long) ((java.lang.Math.sqrt((9+(8*(n+k))))-3)/2);
System.out.println(n-ans);
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.lang.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.*;
public class Main {
public static void main(String[] args) throws Exception{
FastReader sc=new FastReader();
OutputStream outputStream = System.out;
PrintWriter out = new PrintWriter(outputStream);
Main mm=new Main();
long n=sc.nextLong();
long k=sc.nextLong();
long l=0;
long r=1000000000;
long ans=-1;
while(l<=r) {
long mid=(l+r)/2;
if(n-mid<=0) {
r=mid-1;
}
else {
long temp=(n-mid)*(n-mid+1)-(2*mid);
if(temp==2*k) {
ans=mid;
break;
}
else if(temp<2*k) {
r=mid-1;
}
else if(temp>2*k) {
l=mid+1;
}
}
}
System.out.println(ans);
}
}
class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.List;
import java.util.StringTokenizer;
public class B {
final int INF = 1_000_000_000;
void solve() {
int n = readInt();
int k = readInt();
long l = 0;
long r = INF;
while(r - l > 1){
long m = (r + l) >> 1;
if(m * (m + 1) / 2 + m >= k + n) r = m;
else l = m;
}
out.print(n - r);
}
public static void main(String[] args) {
new B().run();
}
private void run() {
try {
init();
solve();
out.close();
} catch (Exception e) {
e.printStackTrace();
System.exit(-1);
}
}
private BufferedReader in;
private StringTokenizer tok = new StringTokenizer("");
private PrintWriter out;
private void init() {
in = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
// try {
// in = new BufferedReader(new FileReader("absum.in"));
// out = new PrintWriter(new File("absum.out"));
// } catch (FileNotFoundException e) {
// e.printStackTrace();
// }
}
private String readLine() {
try {
return in.readLine();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private String readString() {
while (!tok.hasMoreTokens()) {
String nextLine = readLine();
if (nextLine == null) return null;
tok = new StringTokenizer(nextLine);
}
return tok.nextToken();
}
private int readInt() {
return Integer.parseInt(readString());
}
private long readLong() {
return Long.parseLong(readString());
}
int[] readIntArray(int n){
int[] res = new int[n];
for(int i = 0;i<n;i++){
res[i] = readInt();
}
return res;
}
long[] readLongArray(int n){
long[] res = new long[n];
for(int i = 0;i<n;i++){
res[i] = readLong();
}
return res;
}
int[] castInt(List<Integer> list){
int[] res = new int[list.size()];
for(int i = 0;i<list.size();i++){
res[i] = list.get(i);
}
return res;
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
long n = scan.nextLong();
long k = scan.nextLong();
long D = 9 + 4 * (2 * k + 2 * n);
long y = (- 3 + (long)Math.sqrt(D)) / 2;
System.out.println(n - y);
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
/**
* @Author Tran Quang Loc (darkkcyan)
* BEFORE READING CODE:
* Java is really lengthy (at least for Java 8). So I used the advantage of initialization block.
* Basically, every initialization block run before the constructor and they run in the declaration order.
* And as I understand, every properties (or global variables) is also counted as initialization block.
*/
import java.io.*;
import java.lang.*;
import java.util.*;
import static java.lang.Integer.parseInt;
import static java.lang.Long.parseLong;
import static java.lang.Math.*;
import static java.lang.System.*;
import static java.util.Arrays.*;
import static java.util.Collections.*;
public class Main {
PrintWriter out = new PrintWriter(System.out, false);
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer stok = null;
String next() {
while (stok == null || !stok.hasMoreTokens())
try {
stok = new StringTokenizer(in.readLine());
} catch (IOException e) { throw new RuntimeException(e); }
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
}
///// Actual solution below /////
long n = parseLong(next()), k = parseLong(next());
long delta = 9 + 8 * (n + k);
long a = (-3 + (long)sqrt(delta)) / 2;
long b = n - a;
{
out.println(b);
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
public class Alpha_Round {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
String[] in = reader.readLine().split(" ");
long n = Long.parseLong(in[0]);
long k = Long.parseLong(in[1]);
long D = 9 + 8*k + 8*n;
long m = (long) ((-3 + Math.sqrt(D))/2);
writer.write((n - m) + "");
writer.close();
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
public class B {
public static void main(String[] args) {
InputReader in = new InputReader();
int n = in.nextInt();
int k = in.nextInt();
long numCandies = 1;
int turns = 1, add = 2;
while (numCandies < k) {
++turns;
numCandies += add++;
}
int res = 0;
if (numCandies > k) {
turns += (numCandies-k);
res += (numCandies-k);
numCandies = k;
}
if (turns == n) {
System.out.println(res);
}
else {
while (turns != n) {
res += add;
turns += add++ + 1;
}
System.out.println(res);
}
}
static class InputReader {
public BufferedReader br;
public StringTokenizer st;
public InputReader() {
br = new BufferedReader(new InputStreamReader(System.in));
st = null;
}
public String next() {
while (st == null || !st.hasMoreTokens()) {
try {
st = new StringTokenizer(br.readLine());
}
catch (IOException e) {
throw new RuntimeException(e);
}
}
return st.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
public class icpc
{
public static void main(String[] args) throws IOException
{
Reader in = new Reader();
// BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
long n = in.nextLong();
long k = in.nextLong();
long val = 2 * n + 2 * k;
long D = 9 + 4 * val;
long sqrtD = (long)Math.sqrt((double)D);
double r1 = (-3 + sqrtD) / 2;
long r1DAsh = (long)r1;
System.out.println(n - r1DAsh);
}
}
class NumberTheory
{
public boolean isPrime(long n)
{
if(n < 2)
return false;
for(long x = 2;x * x <= n;x++)
{
if(n % x == 0)
return false;
}
return true;
}
public ArrayList<Integer> primeFactorisation(int n)
{
ArrayList<Integer> f = new ArrayList<>();
for(int x=2;x * x <= n;x++)
{
while(n % x == 0)
{
f.add(x);
n /= x;
}
}
if(n > 1)
f.add(n);
return f;
}
public int[] sieveOfEratosthenes(int n)
{
int[] sieve = new int[n + 1];
for(int x=2;x<=n;x++)
{
if(sieve[x] != 0)
continue;
sieve[x] = x;
for(int u=2*x;u<=n;u+=x)
if(sieve[u] == 0)
sieve[u] = x;
}
return sieve;
}
public long gcd(long a, long b)
{
if(b == 0)
return a;
return gcd(b, a % b);
}
public long phi(long n)
{
double result = n;
for(long p=2;p*p<=n;p++)
{
if(n % p == 0)
{
while (n % p == 0)
n /= p;
result *= (1.0 - (1.0 / (double)p));
}
}
if(n > 1)
result *= (1.0 - (1.0 / (double)n));
return (long)result;
}
public Name extendedEuclid(long a, long b)
{
if(b == 0)
return new Name(a, 1, 0);
Name n1 = extendedEuclid(b, a % b);
Name n2 = new Name(n1.d, n1.y, n1.x - (long)Math.floor((double)a / b) * n1.y);
return n2;
}
public long modularExponentiation(long a, long b, long n)
{
long d = 1L;
String bString = Long.toBinaryString(b);
for(int i=0;i<bString.length();i++)
{
d = (d * d) % n;
if(bString.charAt(i) == '1')
d = (d * a) % n;
}
return d;
}
}
class Name
{
long d;
long x;
long y;
public Name(long d, long x, long y)
{
this.d = d;
this.x = x;
this.y = y;
}
}
class SuffixArray
{
int ALPHABET_SZ = 256, N;
int[] T, lcp, sa, sa2, rank, tmp, c;
public SuffixArray(String str)
{
this(toIntArray(str));
}
private static int[] toIntArray(String s)
{
int[] text = new int[s.length()];
for (int i = 0; i < s.length(); i++) text[i] = s.charAt(i);
return text;
}
public SuffixArray(int[] text)
{
T = text;
N = text.length;
sa = new int[N];
sa2 = new int[N];
rank = new int[N];
c = new int[Math.max(ALPHABET_SZ, N)];
construct();
kasai();
}
private void construct()
{
int i, p, r;
for (i = 0; i < N; ++i) c[rank[i] = T[i]]++;
for (i = 1; i < ALPHABET_SZ; ++i) c[i] += c[i - 1];
for (i = N - 1; i >= 0; --i) sa[--c[T[i]]] = i;
for (p = 1; p < N; p <<= 1)
{
for (r = 0, i = N - p; i < N; ++i) sa2[r++] = i;
for (i = 0; i < N; ++i) if (sa[i] >= p) sa2[r++] = sa[i] - p;
Arrays.fill(c, 0, ALPHABET_SZ, 0);
for (i = 0; i < N; ++i) c[rank[i]]++;
for (i = 1; i < ALPHABET_SZ; ++i) c[i] += c[i - 1];
for (i = N - 1; i >= 0; --i) sa[--c[rank[sa2[i]]]] = sa2[i];
for (sa2[sa[0]] = r = 0, i = 1; i < N; ++i)
{
if (!(rank[sa[i - 1]] == rank[sa[i]]
&& sa[i - 1] + p < N
&& sa[i] + p < N
&& rank[sa[i - 1] + p] == rank[sa[i] + p])) r++;
sa2[sa[i]] = r;
}
tmp = rank;
rank = sa2;
sa2 = tmp;
if (r == N - 1) break;
ALPHABET_SZ = r + 1;
}
}
private void kasai()
{
lcp = new int[N];
int[] inv = new int[N];
for (int i = 0; i < N; i++) inv[sa[i]] = i;
for (int i = 0, len = 0; i < N; i++)
{
if (inv[i] > 0)
{
int k = sa[inv[i] - 1];
while ((i + len < N) && (k + len < N) && T[i + len] == T[k + len]) len++;
lcp[inv[i] - 1] = len;
if (len > 0) len--;
}
}
}
}
class ZAlgorithm
{
public int[] calculateZ(char input[])
{
int Z[] = new int[input.length];
int left = 0;
int right = 0;
for(int k = 1; k < input.length; k++) {
if(k > right) {
left = right = k;
while(right < input.length && input[right] == input[right - left]) {
right++;
}
Z[k] = right - left;
right--;
} else {
//we are operating inside box
int k1 = k - left;
//if value does not stretches till right bound then just copy it.
if(Z[k1] < right - k + 1) {
Z[k] = Z[k1];
} else { //otherwise try to see if there are more matches.
left = k;
while(right < input.length && input[right] == input[right - left]) {
right++;
}
Z[k] = right - left;
right--;
}
}
}
return Z;
}
public ArrayList<Integer> matchPattern(char text[], char pattern[])
{
char newString[] = new char[text.length + pattern.length + 1];
int i = 0;
for(char ch : pattern) {
newString[i] = ch;
i++;
}
newString[i] = '$';
i++;
for(char ch : text) {
newString[i] = ch;
i++;
}
ArrayList<Integer> result = new ArrayList<>();
int Z[] = calculateZ(newString);
for(i = 0; i < Z.length ; i++) {
if(Z[i] == pattern.length) {
result.add(i - pattern.length - 1);
}
}
return result;
}
}
class KMPAlgorithm
{
public int[] computeTemporalArray(char[] pattern)
{
int[] lps = new int[pattern.length];
int index = 0;
for(int i=1;i<pattern.length;)
{
if(pattern[i] == pattern[index])
{
lps[i] = index + 1;
index++;
i++;
}
else
{
if(index != 0)
{
index = lps[index - 1];
}
else
{
lps[i] = 0;
i++;
}
}
}
return lps;
}
public ArrayList<Integer> KMPMatcher(char[] text, char[] pattern)
{
int[] lps = computeTemporalArray(pattern);
int j = 0;
int i = 0;
int n = text.length;
int m = pattern.length;
ArrayList<Integer> indices = new ArrayList<>();
while(i < n)
{
if(pattern[j] == text[i])
{
i++;
j++;
}
if(j == m)
{
indices.add(i - j);
j = lps[j - 1];
}
else if(i < n && pattern[j] != text[i])
{
if(j != 0)
j = lps[j - 1];
else
i = i + 1;
}
}
return indices;
}
}
class Hashing
{
public long[] computePowers(long p, int n, long m)
{
long[] powers = new long[n];
powers[0] = 1;
for(int i=1;i<n;i++)
{
powers[i] = (powers[i - 1] * p) % m;
}
return powers;
}
public long computeHash(String s)
{
long p = 31;
long m = 1_000_000_009;
long hashValue = 0L;
long[] powers = computePowers(p, s.length(), m);
for(int i=0;i<s.length();i++)
{
char ch = s.charAt(i);
hashValue = (hashValue + (ch - 'a' + 1) * powers[i]) % m;
}
return hashValue;
}
}
class BasicFunctions
{
public long min(long[] A)
{
long min = Long.MAX_VALUE;
for(int i=0;i<A.length;i++)
{
min = Math.min(min, A[i]);
}
return min;
}
public long max(long[] A)
{
long max = Long.MAX_VALUE;
for(int i=0;i<A.length;i++)
{
max = Math.max(max, A[i]);
}
return max;
}
}
class Matrix
{
long a;
long b;
long c;
long d;
public Matrix(long a, long b, long c, long d)
{
this.a = a;
this.b = b;
this.c = c;
this.d = d;
}
}
class MergeSortInt
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(int arr[], int l, int m, int r) {
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
int L[] = new int[n1];
int R[] = new int[n2];
/*Copy data to temp arrays*/
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(int arr[], int l, int r) {
if (l < r) {
// Find the middle point
int m = (l + r) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
}
class MergeSortLong
{
// Merges two subarrays of arr[].
// First subarray is arr[l..m]
// Second subarray is arr[m+1..r]
void merge(long arr[], int l, int m, int r) {
// Find sizes of two subarrays to be merged
int n1 = m - l + 1;
int n2 = r - m;
/* Create temp arrays */
long L[] = new long[n1];
long R[] = new long[n2];
/*Copy data to temp arrays*/
for (int i = 0; i < n1; ++i)
L[i] = arr[l + i];
for (int j = 0; j < n2; ++j)
R[j] = arr[m + 1 + j];
/* Merge the temp arrays */
// Initial indexes of first and second subarrays
int i = 0, j = 0;
// Initial index of merged subarry array
int k = l;
while (i < n1 && j < n2) {
if (L[i] <= R[j]) {
arr[k] = L[i];
i++;
} else {
arr[k] = R[j];
j++;
}
k++;
}
/* Copy remaining elements of L[] if any */
while (i < n1) {
arr[k] = L[i];
i++;
k++;
}
/* Copy remaining elements of R[] if any */
while (j < n2) {
arr[k] = R[j];
j++;
k++;
}
}
// Main function that sorts arr[l..r] using
// merge()
void sort(long arr[], int l, int r) {
if (l < r) {
// Find the middle point
int m = (l + r) / 2;
// Sort first and second halves
sort(arr, l, m);
sort(arr, m + 1, r);
// Merge the sorted halves
merge(arr, l, m, r);
}
}
}
class Node
{
String a;
String b;
Node(String s1,String s2)
{
this.a = s1;
this.b = s2;
}
@Override
public boolean equals(Object ob)
{
if(ob == null)
return false;
if(!(ob instanceof Node))
return false;
if(ob == this)
return true;
Node obj = (Node)ob;
if(this.a.equals(obj.a) && this.b.equals(obj.b))
return true;
return false;
}
@Override
public int hashCode()
{
return (int)this.a.length();
}
}
class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
class FenwickTree
{
public void update(long[] fenwickTree,long delta,int index)
{
index += 1;
while(index < fenwickTree.length)
{
fenwickTree[index] += delta;
index = index + (index & (-index));
}
}
public long prefixSum(long[] fenwickTree,int index)
{
long sum = 0L;
index += 1;
while(index > 0)
{
sum += fenwickTree[index];
index -= (index & (-index));
}
return sum;
}
}
class SegmentTree
{
public int nextPowerOfTwo(int num)
{
if(num == 0)
return 1;
if(num > 0 && (num & (num - 1)) == 0)
return num;
while((num &(num - 1)) > 0)
{
num = num & (num - 1);
}
return num << 1;
}
public int[] createSegmentTree(int[] input)
{
int np2 = nextPowerOfTwo(input.length);
int[] segmentTree = new int[np2 * 2 - 1];
for(int i=0;i<segmentTree.length;i++)
segmentTree[i] = Integer.MIN_VALUE;
constructSegmentTree(segmentTree,input,0,input.length-1,0);
return segmentTree;
}
private void constructSegmentTree(int[] segmentTree,int[] input,int low,int high,int pos)
{
if(low == high)
{
segmentTree[pos] = input[low];
return;
}
int mid = (low + high)/ 2;
constructSegmentTree(segmentTree,input,low,mid,2*pos + 1);
constructSegmentTree(segmentTree,input,mid+1,high,2*pos + 2);
segmentTree[pos] = Math.max(segmentTree[2*pos + 1],segmentTree[2*pos + 2]);
}
public int rangeMinimumQuery(int []segmentTree,int qlow,int qhigh,int len)
{
return rangeMinimumQuery(segmentTree,0,len-1,qlow,qhigh,0);
}
private int rangeMinimumQuery(int segmentTree[],int low,int high,int qlow,int qhigh,int pos)
{
if(qlow <= low && qhigh >= high){
return segmentTree[pos];
}
if(qlow > high || qhigh < low){
return Integer.MIN_VALUE;
}
int mid = (low+high)/2;
return Math.max(rangeMinimumQuery(segmentTree, low, mid, qlow, qhigh, 2 * pos + 1),
rangeMinimumQuery(segmentTree, mid + 1, high, qlow, qhigh, 2 * pos + 2));
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
import java.text.*;
import java.math.*;
import static java.lang.Integer.*;
import static java.lang.Double.*;
import java.lang.Math.*;
public class B {
public static void main(String[] args) throws Exception {
new B().run();
}
public void run() throws Exception {
FastIO file = new FastIO();
long n = file.nextLong();
long k = file.nextLong();
long lo = 1;
long hi = n;
long ans = 0;
while (lo <= hi) {
long mi = lo + (hi - lo) / 2;
long q = mi * (mi + 1) / 2 - (n - mi);
if (q == k) {
ans = (n - mi);
break;
}
else if (q < k) {
lo = mi + 1;
}
else {
hi = mi - 1;
}
}
System.out.println(ans);
}
public static class FastIO {
BufferedReader br;
StringTokenizer st;
public FastIO() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
public static long pow(long n, long p, long mod) {
if (p == 0)
return 1;
if (p == 1)
return n % mod;
if (p % 2 == 0) {
long temp = pow(n, p / 2, mod);
return (temp * temp) % mod;
} else {
long temp = pow(n, p / 2, mod);
temp = (temp * temp) % mod;
return (temp * n) % mod;
}
}
public static long pow(long n, long p) {
if (p == 0)
return 1;
if (p == 1)
return n;
if (p % 2 == 0) {
long temp = pow(n, p / 2);
return (temp * temp);
} else {
long temp = pow(n, p / 2);
temp = (temp * temp);
return (temp * n);
}
}
public static long gcd(long x, long y) {
if (x == 0)
return y;
else
return gcd(y % x, x);
}
public static boolean isPrime(int n) {
if (n <= 1)
return false;
if (n <= 3)
return true;
if (n % 2 == 0 || n % 3 == 0)
return false;
for (int i = 5; i * i <= n; i = i + 6)
if (n % i == 0 || n % (i + 2) == 0)
return false;
return true;
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
long n = sc.nextLong(), k = sc.nextLong();
long ans = 0, sum = 0;
while(ans < n) {
if(sum - (n-ans) == k) break;
ans++;
sum += ans;
}
sc.close();
System.out.println(n-ans);
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main
{
public static void main(String[] args)
{
FastReader fr =new FastReader();
PrintWriter op =new PrintWriter(System.out);
long n =fr.nextLong() ,k =fr.nextLong() ,d =(long)Math.sqrt(9l+8l*(n+k)) ;
d -= 3l ; d /=2l ;op.println(n-d) ;
op.flush(); op.close();
}
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br =new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st==null || (!st.hasMoreElements()))
{
try
{
st =new StringTokenizer(br.readLine());
}
catch(IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
String str ="";
try
{
str =br.readLine();
}
catch(IOException e)
{
e.printStackTrace();
}
return str;
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next()) ;
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
public class A {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner();
PrintWriter out = new PrintWriter(System.out);
int n=sc.nextInt(),k=sc.nextInt();
for(int x=0;;x++) {
if(2*1L*x+x*1L*(x+1)==2L*(k+n)) {
out.println(n-x);
break;
}
}
out.close();
}
static class Scanner {
BufferedReader br;
StringTokenizer st;
Scanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
Scanner(String fileName) throws FileNotFoundException {
br = new BufferedReader(new FileReader(fileName));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
String nextLine() throws IOException {
return br.readLine();
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws NumberFormatException, IOException {
return Long.parseLong(next());
}
double nextDouble() throws NumberFormatException, IOException {
return Double.parseDouble(next());
}
boolean ready() throws IOException {
return br.ready();
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Aman Kumar Singh
*/
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);
BSportMafia solver = new BSportMafia();
solver.solve(1, in, out);
out.close();
}
static class BSportMafia {
int MAXN = 200005;
PrintWriter out;
InputReader in;
public void solve(int testNumber, InputReader in, PrintWriter out) {
this.out = out;
this.in = in;
long n = nl();
long k = nl();
long i = 0;
k += n;
for (i = 0; i < MAXN; i++) {
long x = (i * (i + 3)) / 2;
if (k == x) {
pn(n - i);
return;
}
}
}
long nl() {
return in.nextLong();
}
void pn(long zx) {
out.println(zx);
}
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars == -1) {
throw new UnknownError();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new UnknownError();
}
if (numChars <= 0) {
return -1;
}
}
return buf[curChar++];
}
public long nextLong() {
return Long.parseLong(next());
}
public String next() {
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;
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.StringTokenizer;
public class TaskB {
void run() {
FastReader in = new FastReader(System.in);
// FastReader in = new FastReader(new FileInputStream("input.txt"));
PrintWriter out = new PrintWriter(System.out);
// PrintWriter out = new PrintWriter(new FileOutputStream("output.txt"));
long n = in.nextLong();
long k = in.nextLong();
long a = 1;
long b = -(2 * n + 3);
long c = n * n + n - 2 * k;
long d = b * b - 4 * a * c;
long ans1 = (-b + (long) Math.sqrt(d)) / 2;
long ans2 = (-b - (long) Math.sqrt(d)) / 2;
if (ans1 >= 0 && ans1 <= n) {
out.println(ans1);
} else {
out.println(ans2);
}
out.close();
}
class FastReader {
BufferedReader br;
StringTokenizer st;
FastReader(InputStream is) {
br = new BufferedReader(new InputStreamReader(is));
}
Integer nextInt() {
return Integer.parseInt(next());
}
Long nextLong() {
return Long.parseLong(next());
}
Double nextDouble() {
return Double.parseDouble(next());
}
String next() {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(nextLine());
return st.nextToken();
}
String nextLine() {
String s = "";
try {
s = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return s;
}
}
public static void main(String[] args) {
new TaskB().run();
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
//package learning;
import java.util.*;
import java.io.*;
import java.lang.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class NitsLocal {
static ArrayList<String> s1;
static boolean[] prime;
static int n = (int)1e7;
static void sieve() {
Arrays.fill(prime , true);
prime[0] = prime[1] = false;
for(int i = 2 ; i * i <= n ; ++i) {
if(prime[i]) {
for(int k = i * i; k<= n ; k+=i) {
prime[k] = false;
}
}
}
}
public static void main(String[] args) {
InputReader sc = new InputReader(System.in);
prime = new boolean[n + 1];
sieve();
prime[1] = false;
long n = sc.nl();
long k = sc.nl();
long b = 2*n + 3;
long c = n*n - 2*k + n;
long q1 = (b + (long)Math.sqrt(b*b - 4*c))/2;
long q2 = (b - (long)Math.sqrt(b*b - 4*c))/2;
if(q1 >= q2 && q1 <= n)
w.println(q1);
else
w.println(q2);
w.close();
}
static int nextPowerOf2(int n)
{
int count = 0;
// First n in the below
// condition is for the
// case where n is 0
if (n > 0 && (n & (n - 1)) == 0)
return n;
while(n != 0)
{
n >>= 1;
count += 1;
}
return 1 << count;
}
static long sum1(int t1,int t2,int x,int []t)
{
int mid = (t2-t1+1)/2;
if(t1==t2)
return 0;
else
return sum1(t1,mid-1,x,t) + sum1(mid,t2,x,t);
}
static String replace(String s,int a,int n)
{
char []c = s.toCharArray();
for(int i=1;i<n;i+=2)
{
int num = (int) (c[i] - 48);
num += a;
num%=10;
c[i] = (char) (num+48);
}
return new String(c);
}
static String move(String s,int h,int n)
{
h%=n;
char []c = s.toCharArray();
char []temp = new char[n];
for(int i=0;i<n;i++)
{
temp[(i+h)%n] = c[i];
}
return new String(temp);
}
public static int ip(String s){
return Integer.parseInt(s);
}
static class multipliers implements Comparator<Long>{
public int compare(Long a,Long b) {
if(a<b)
return 1;
else if(b<a)
return -1;
else
return 0;
}
}
static class multipliers1 implements Comparator<Student>{
public int compare(Student a,Student b) {
if(a.y<b.y)
return 1;
else if(b.y<a.y)
return -1;
else
{
if(a.id < b.id)
return 1;
else if(b.id<a.id)
return -1;
else
return 0;
//return 0;
}
}
}
// Java program to generate power set in
// lexicographic order.
static class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 0)
return -1;
}
return buf[curChar++];
}
public int ni() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public long nl() {
int c = read();
while (isSpaceChar(c)) {
c = read();
}
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
long res = 0;
do {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nia(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = ni();
}
return a;
}
public String rs() {
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 String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
static PrintWriter w = new PrintWriter(System.out);
static class Student
{
int id;
//int x;
int y;
//long z;
Student(int id,int y)
{
this.id = id;
//this.x = x;
//this.s = s;
this.y = y;
// this.z = z;
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.math.*;
// **** B. Sport Mafia ****
public class B {
static char [] in = new char [1000000];
public static void main (String [] arg) throws Throwable {
int n = nextInt();
int k = nextInt();
long ate = 0;
long ans = -1;
for (long i = 1; ans < 0; ++i) {
long test = (i * (i+1)) / 2;
if (test < k) continue;
long adding_moves = i;
long eating_moves = n-i;
if (test - eating_moves == k) ans = eating_moves;
}
System.out.println(ans);
}
/************** HELPER CLASSES ***************/
//static class HS extends HashSet<Integer>{public HS(){super();}public HS(int a){super(a);}};
//static class AL extends ArrayList<Integer>{public AL(){super();}public AL(int a){super (a);}};
static class Pair implements Comparable<Pair> {
int i,j;long L; public Pair(int xx, int yy, long LL){i=xx;j=yy;L=LL;}
public int compareTo(Pair p) { return (this.L < p.L) ? -1 : ((this.L == p.L && this.i < p.i) ? -1 : 1);}
}
/************** FAST IO CODE FOLLOWS *****************/
public static long nextLong() throws Throwable {
long i = System.in.read();boolean neg = false;while (i < 33) i = System.in.read();if (i == 45) {neg=true;i=48;}i = i - 48;
int j = System.in.read();while (j > 32) {i*=10;i+=j-48;j = System.in.read();}return (neg) ? -i : i;
}
public static int nextInt() throws Throwable {return (int)nextLong();}
public static String next() throws Throwable {
int i = 0; while (i < 33 && i != -1) i = System.in.read(); int cptr = 0; while (i >= 33) { in[cptr++] = (char)i; i = System.in.read();}
return new String(in, 0,cptr);
}
/**** LIBRARIES ****/
public static long gcdL(long a, long b) {while (b != 0) {long tmp = b;b = (a % b);a = tmp;}return a;}
public static int gcd(int a, int b) {while (b != 0) {int tmp = b;b = (a % b);a = tmp;}return a;}
public static int[] sieve(int LIM) {
int i,count = 0;
boolean [] b = new boolean [LIM];
for (i = 2;i<LIM; ++i) if (!b[i]) {count++; for (int j = i<<1; j<LIM; j+=i) b[j] = true;}
int [] primes = new int[count];
for (i = 2,count=0;i<LIM;++i) if (!b[i]) primes[count++] = i;
return primes;
}
public static int[] numPrimeFactors(int LIM) {
int i,count = 0;
int [] b = new int [LIM];
for (i = 2;i<LIM; ++i) if (b[i] == 0) {count++; for (int j = i; j<LIM; j+=i) b[j]++;}
return b;
}
public static StringBuilder stringFromArray(int [] a) {
StringBuilder b = new StringBuilder(9*a.length);
for (int i = 0; i<a.length; ++i) {
if (i != 0) b = b.append(' ');
b = b.append(a[i]);
}
return b;
}
public static long modPow (long a, long n, long MOD) { long S = 1; for (;n > 0; n>>=1, a=(a*a)%MOD) if ((n & 1) != 0) S = (a*S) % MOD; return S;}
}
/* Full Problem Text:
Each evening after the dinner the SIS's students gather together to play the game of Sport Mafia.
For the tournament, Alya puts candies into the box, which will serve as a prize for a winner.
To do that, she performs n actions.
The first action performed is to put a single candy into the box.
For each of the remaining moves she can choose from two options:
the first option, in case the box contains at least one candy, is to take exactly one candy out and eat it.
This way the number of candies in the box decreased by 1;
the second option is to put candies in the box.
In this case, Alya will 1 more candy, than she put in the previous time.
Thus, if the bank is empty, then it can only use the second option.
For example, one possible sequence of Alya's actions look as follows:
put one candy into the box;
put two candies into the box;
eat one candy from the box;
eat one candy from the box;
put three candies into the box;
eat one candy from the box;
put four candies into the box;
eat one candy from the box;
put five candies into the box;
This way she will perform 9 actions, the of candies at the end will be 11, while Alya will eat 4 candies in total.
You know the total number of actions n and the number of candies at the end k.
You need to find the total number of sweets Alya ate.
That is the number of moves of the second option.
It's guaranteed, that for the given n and k the answer always exists.
Please note, that during an action of the first option, Alya takes out and eats exactly one candy.
*/ | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
public class Main {
static class FastScanner {
private BufferedReader bufferedReader;
private StringTokenizer stringTokenizer;
public FastScanner(InputStream inputStream) {
bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
}
public String next() {
while (stringTokenizer == null || !stringTokenizer.hasMoreTokens()) {
try {
stringTokenizer = new StringTokenizer(bufferedReader.readLine());
} catch (IOException ignored) {
}
}
return stringTokenizer.nextToken();
}
public int nextInt() {
return Integer.parseInt(next());
}
public long nextLong() {
return Long.parseLong(next());
}
public double nextDouble() {
return Double.parseDouble(next());
}
}
static class Node implements Comparable {
Node left;
Node right;
private int value;
Node(Node left, Node right, int value) {
this.left = left;
this.right = right;
this.value = value;
}
@Override
public int compareTo(Object o) {
return Integer.compare(this.value, ((Node) o).value);
}
}
private static int fib(int n, int m) {
if (n < 2) return n;
int a = 0;
int b = 1;
for (int i = 0; i < n - 2; i++) {
int c = (a + b) % m;
a = b;
b = c;
}
return (a + b) % m;
}
static long gcd(long a, long b) {
if (b == 0) return a;
return gcd(b, a % b);
}
static long lcm(long a, long b) {
return Math.abs(a * b) / gcd(a, b);
}
static class DSU {
private int[] p;
private int[] r;
DSU(int n) {
p = new int[n];
r = new int[n];
Arrays.fill(p, -1);
Arrays.fill(r, 0);
}
int find(int x) {
if (p[x] < 0) {
return x;
}
return p[x] = find(p[x]);
}
void union(int a, int b) {
a = find(a);
b = find(b);
if (a == b) return;
if (r[a] < r[b]) {
p[a] = b;
} else {
p[b] = a;
}
if (r[a] == r[b]) {
r[a]++;
}
}
}
private static boolean isPrime(long n) {
for (int i = 2; i < n; i++) {
if (n % i == 0) return false;
}
return true;
}
private static double med(Integer[] a) {
Arrays.sort(a);
if (a.length % 2 == 0) {
int r = a.length / 2;
int l = r - 1;
double s = a[l] + a[r];
return s / 2.0;
}
int m = a.length / 2;
return a[m];
}
static Map<Integer, ArrayList<Integer>> g;
static Map<Integer, Integer> color;
static void dfs(int v, int c) {
color.put(v, c);
for (int i = 0; i < g.get(v).size(); i++) {
int u = g.get(v).get(i);
if (!color.containsKey(u)) {
dfs(u, c);
}
}
}
static void reverse(Integer[] a) {
Collections.reverse(Arrays.asList(a));
}
static boolean next(Integer[] a) {
int i = a.length - 1;
while (a[i] == 0) i--;
int c = 0;
while (i >= 0 && a[i] == 1) {
c++;
i--;
}
if (i < 0) return false;
a[i] = 1;
for (int j = i + 1; j < a.length; j++) {
a[j] = 0;
}
c--;
for (int j = 0; j < c; j++) {
a[a.length - 1 - j] = 1;
}
return true;
}
private static int bin(Integer[] a, int l, int r, int x) {
if (l >= r) return l;
int m = (l + r) / 2;
if (a[m] > x) {
return bin(a, l, m, x);
} else if (a[m] < x || (m < a.length - 1 && a[m + 1] == x)) {
return bin(a, m + 1, r, x);
}
return m + 1;
}
private static class SegmentTree {
private long[] d;
private long[] a;
SegmentTree(int n) {
a = new long[n];
d = new long[5 * n];
}
void update(int v, int l, int r, int pos, long val) {
if (l == r) {
d[v] += val;
a[l] += val;
} else {
int mid = (l + r) / 2;
if (pos <= mid) {
update(v * 2, l, mid, pos, val);
} else {
update(v * 2 + 1, mid + 1, r, pos, val);
}
d[v] = d[v * 2] + d[v * 2 + 1];
}
}
int find(int v, int l, int r, long w) {
if (v >= d.length) return -1;
int mid = (l + r) / 2;
if (d[v] <= w) return r;
long o = w - d[v * 2];
if (mid + 1 < a.length && o >= a[mid + 1]) {
return find(v * 2 + 1, mid + 1, r, o);
}
if (w >= a[l])
return find(v * 2, l, mid, w);
return -1;
}
int iterFind(long w) {
if (a[0] > w) return -1;
int l = 0, r = a.length - 1;
int v = 1;
while (d[v] > w) {
int mid = (l + r) / 2;
long o = w - d[v * 2];
if (mid + 1 < a.length && o >= a[mid + 1]) {
l = mid + 1;
v = v * 2 + 1;
w = o;
} else {
v = v * 2;
r = mid;
}
}
return r;
}
int get(int v, int vl, int vr, long w) {
// cout << t[v] << " "<< v << " " << vl << " " << vr<<" " << w << endl;
if (d[v] < w) return -1;
if (vl > vr) return -1;
if (vl == vr) {
if (d[v] > w) return vl - 1;
else return -1;
}
if (d[v * 2] > w) return get(v * 2, vl, (vl + vr) / 2, w);
else return get(v * 2 + 1, (vl + vr + 2) / 2, vr, w - d[v * 2]);
}
}
private static class FenwickTree {
long[] t;
FenwickTree(int n) {
t = new long[n];
}
long sum(int r) {
long result = 0;
for (; r >= 0; r = (r & (r + 1)) - 1)
result += t[r];
return result;
}
void inc(int i, long delta) {
int n = t.length;
for (; i < n; i = (i | (i + 1)))
t[i] += delta;
}
}
void insert(List<Long> list, Long element) {
int index = Collections.binarySearch(list, element);
if (index < 0) {
index = -index - 1;
}
list.add(index, element);
}
public static void main(String[] args) {
FastScanner scanner = new FastScanner(System.in);
PrintWriter printer = new PrintWriter(System.out);
long n = scanner.nextLong();
long k = scanner.nextLong();
long l = 1;
long r = n;
while(true){
long m = (l + r) / 2;
long x = (m * (m + 1)) / 2;
x -= n - m;
if (x == k) {
printer.println(n - m);
break;
} else if (x < k) {
l = m + 1;
} else {
r = m - 1;
}
}
printer.flush();
printer.close();
}
}
/*
4 2
1 1 4
0 2 3
5 3
1 2 4
0 4 5
0 3 5
4 3
1 2 3
1 1 2
0 1 3
NO
*/ | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.lang.reflect.Array;
import java.math.BigInteger;
import java.util.*;
public class q5 {
public static void main(String[] args) throws IOException {
Reader.init(System.in);
PrintWriter out=new PrintWriter(System.out);
long n=Reader.nextInt();
long k=Reader.nextLong();
long v=8*n+8*k+4;
long v2=(long) Math.sqrt(v);
long v3=2*n+2;
//long v4=(v3+v2)/2;
long v5=(v3-v2)/2;
out.println(v5);
out.flush();
}
}
class Reader {
static BufferedReader reader;
static StringTokenizer tokenizer;
/** call this method to initialize reader for InputStream */
static void init() throws IOException {
reader = new BufferedReader(
new FileReader("detect.in"));
tokenizer = new StringTokenizer("");
}
static void init(InputStream input) {
reader = new BufferedReader(
new InputStreamReader(input) );
tokenizer = new StringTokenizer("");
}
/** get next word */
static String nextLine() throws IOException{
return reader.readLine();
}
static String next() throws IOException {
while ( ! tokenizer.hasMoreTokens() ) {
//TODO add check for eof if necessary
tokenizer = new StringTokenizer(
reader.readLine() );
}
return tokenizer.nextToken();
}
static int nextInt() throws IOException {
return Integer.parseInt( next() );
}
static long nextLong() throws IOException {
return Long.parseLong( next() );
}
static double nextDouble() throws IOException {
return Double.parseDouble( next() );
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
public class F {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
PrintWriter writer = new PrintWriter(System.out);
StringTokenizer st = new StringTokenizer(reader.readLine());
long n = Integer.parseInt(st.nextToken());
long k = Integer.parseInt(st.nextToken());
long l = 0;
long r = n;
while(l <= r){
long min = (l + r) / 2;
if((min * (min + 1) / 2 - (n - min) == k)){
System.out.println(n - min);
return;
}
else if((min * (min + 1) / 2 - (n - min) > k)){
r = min - 1;
}
else{
l = min + 1;
}
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
public class zz{
static int mod=(int)1e9+7;
public static void main(String[] args) throws Exception{
//MScanner sc = new MScanner("chess.in");
MScanner sc = new MScanner(System.in);
PrintWriter pw=new PrintWriter(System.out);
int n=sc.nextInt();
//int[]in=new int[n];for(int i=0;i<n;i++)in[i]=sc.nextInt();
int k=sc.nextInt();
int x=(-3+(int)Math.sqrt(9+4*1.0*(2*k*1.0+2*n*1.0)))/2;
pw.println(n-x);
pw.flush();
}
static class pair implements Comparable<pair>{
String t;int d;int idx;
pair(String x,int y,int i){
t=x;d=y;idx=i;
}
@Override
public int compareTo(pair o) {
if(t.compareTo(o.t)!=0) {
return t.compareTo(o.t);
}
return o.d-d;
}
public boolean equals(pair o) {
if(this.compareTo(o)==0)return true;
return false;
}
public String toString() {
return "("+t+" "+d+")";
}
}
static class MScanner {
StringTokenizer st;
BufferedReader br;
public MScanner(InputStream system) {
br = new BufferedReader(new InputStreamReader(system));
}
public MScanner(String file) throws Exception {
br = new BufferedReader(new FileReader(file));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public String nextLine() throws IOException {
return br.readLine();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public double nextDouble() throws IOException {
return Double.parseDouble(next());
}
public char nextChar() throws IOException {
return next().charAt(0);
}
public Long nextLong() throws IOException {
return Long.parseLong(next());
}
public boolean ready() throws IOException {
return br.ready();
}
public void waitForInput() throws InterruptedException {
Thread.sleep(3000);
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StreamTokenizer;
import java.util.Scanner;
public class Main {
static StreamTokenizer st = new StreamTokenizer(new BufferedInputStream(System.in));
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pr = new PrintWriter(new BufferedOutputStream(System.out));
static Scanner sc = new Scanner(System.in);
public static void main(String[] args) throws NumberFormatException, IOException {
int a = sc.nextInt();
int b = sc.nextInt();
int i = 0;
int cont = 0;
while(cont<b) {
i++;
cont+=i;
}
if(i+cont-b==a) {
System.out.println(cont-b);
}else {
while(i+cont-b!=a) {
i++;
cont+=i;
}
System.out.println(cont-b);
}
}
private static int nextInt() {
try {
st.nextToken();
} catch (IOException e) {
e.printStackTrace();
}
return (int) st.nval;
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
// Working program using Reader Class
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main2
{
static class Reader
{
final private int BUFFER_SIZE = 1 << 16;
private DataInputStream din;
private byte[] buffer;
private int bufferPointer, bytesRead;
public Reader()
{
din = new DataInputStream(System.in);
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public Reader(String file_name) throws IOException
{
din = new DataInputStream(new FileInputStream(file_name));
buffer = new byte[BUFFER_SIZE];
bufferPointer = bytesRead = 0;
}
public String readLine() throws IOException
{
byte[] buf = new byte[64]; // line length
int cnt = 0, c;
while ((c = read()) != -1)
{
if (c == '\n')
break;
buf[cnt++] = (byte) c;
}
return new String(buf, 0, cnt);
}
public int nextInt() throws IOException
{
int ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do
{
ret = ret * 10 + c - '0';
} while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public long nextLong() throws IOException
{
long ret = 0;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (neg)
return -ret;
return ret;
}
public double nextDouble() throws IOException
{
double ret = 0, div = 1;
byte c = read();
while (c <= ' ')
c = read();
boolean neg = (c == '-');
if (neg)
c = read();
do {
ret = ret * 10 + c - '0';
}
while ((c = read()) >= '0' && c <= '9');
if (c == '.')
{
while ((c = read()) >= '0' && c <= '9')
{
ret += (c - '0') / (div *= 10);
}
}
if (neg)
return -ret;
return ret;
}
private void fillBuffer() throws IOException
{
bytesRead = din.read(buffer, bufferPointer = 0, BUFFER_SIZE);
if (bytesRead == -1)
buffer[0] = -1;
}
private byte read() throws IOException
{
if (bufferPointer == bytesRead)
fillBuffer();
return buffer[bufferPointer++];
}
public void close() throws IOException
{
if (din == null)
return;
din.close();
}
}
public static void main(String[] args) throws IOException
{
Reader z = new Reader();
long n=z.nextLong(), k=z.nextLong(), x;
x=9L+8L*(k+n);
x=(long) Math.sqrt(x);
x=(x-3)/2;
System.out.println(n-x);
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
public class CFEdu66 {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long k = in.nextLong();
double tmp = Math.sqrt(9 + 8*(n+k));
if(Math.ceil(tmp)-tmp<0.001)
tmp = Math.ceil(tmp);
long root = (long)tmp;
long x = (-3+root)/2;
System.out.println(n-x);
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
import java.math.*;
public class Main{
public static void main(String[] args) {
long n,k;
Scanner s= new Scanner(System.in);
n=s.nextInt();
k=s.nextInt();
System.out.println((int)(n-((-3+Math.sqrt(9+8*(n+k)))/2)));
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.Scanner;
public class Main {
public static void main(String[] args)
{
Scanner s=new Scanner(System.in);
double n=s.nextLong();
double k=s.nextLong();
double num=(-3+Math.sqrt(9+8*(n+k)))/2;
System.out.println((long)(n-num));
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
public class B {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
public static void main(String[] args) throws Exception {
String[] split = br.readLine().split(" ");
long n = Long.parseLong(split[0]);
long k = Long.parseLong(split[1]);
long left = -1;
long right = n + 1;
while(right - left >= 2) {
long mid = (left + right) / 2; // 10
// if(mid > n) {
// right = mid;
// continue;
// }
long newN = n - mid; //-5
long temp = newN * (newN + 1) / 2; //10
long eh = temp - k - mid;
if(eh == 0) {
pw.println(mid);
break;
}
else if(eh < 0)
right = mid;
else
left = mid;
}
pw.close();
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
public class Solution{
public static Integer INT(String s){
return Integer.parseInt(s);
}
public static Long LONG(String s){
return Long.parseLong(s);
}
//====================================================================================================================
public static void main(String args[])throws IOException{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); Scanner in=new Scanner(System.in); StringBuilder out=new StringBuilder();
long n=in.nextLong(),
k=in.nextLong();
long a=1,
b=3,
c=-2*(n+k);
long r1=(-b+(long)Math.sqrt(b*b-4*a*c))/(2*a);
long r2=(-b-(long)Math.sqrt(b*b-4*a*c))/(2*a);
System.out.println(n-Math.max(r1, r2));
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.lang.*;
import java.io.*;
/* Name of the class has to be "Main" only if the class is public. */
public class Main
{
public static void main (String[] args) throws java.lang.Exception
{
// your code goes here
FastReader sc=new FastReader();
long n=sc.L();
long k=sc.L();
long x=8*(n+k);
x+=9;
x=(long)Math.sqrt(x)-3;
x/=2;
System.out.println(n-x);
}
static int binarysearch(int x,int[] b,int n)
{
int l=0,r=n-1,m=(l+r)/2;
if(x<b[0]||x>b[r])
return -1;
while(l<=r)
{
m=(l+r)/2;
if(b[m]==x)
return m;
if(b[m]>x)
r=m-1;
else
l=m+1;
}
return -1;
}
static int lower(int x,int b[],int n)
{
if(x<b[0])
return -1;
else if(x==b[0])
return 0;
if(x>=b[n-1])
return n-1;
int l=0,r=n-1,m=(l+r)/2;
while(l<=r)
{
m=(l+r)/2;
if(b[m]<=x&&b[m+1]>x)
return m;
else if(b[m]>x&&b[m-1]<=x)
return m-1;
if(b[m]>x)
r=m-1;
else if(b[m]<x)
l=m+1;
}
return -1;
}
static int upper(int x,int b[],int n)
{
if(x<=b[0])
return 0;
else if(x==b[n-1])
return n-1;
if(x>b[n-1])
return -1;
int l=0,r=n-1,m=(l+r)/2;
while(l<=r)
{
m=(l+r)/2;
if(b[m]<x&&b[m+1]>=x)
return m+1;
else if(b[m]>=x&&b[m-1]<x)
return m;
if(b[m]>x)
r=m-1;
else if(b[m]<x)
l=m+1;
}
return -1;
}
static long power(long x, long y, long p)
{
// Initialize result
long res = 1;
// Update x if it is more
// than or equal to p
x = x % p;
while (y > 0)
{
// If y is odd, multiply x
// with result
if((y & 1)==1)
res = (res * x) % p;
// y must be even now
// y = y / 2
y = y >> 1;
x = (x * x) % p;
}
return res;
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int I()
{
return Integer.parseInt(next());
}
long L()
{
return Long.parseLong(next());
}
double D()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
static int gcd(int a,int b)
{
if(a%b==0)
return b;
return gcd(b,a%b);
}
static float power(float x, int y)
{
float temp;
if( y == 0)
return 1;
temp = power(x, y/2);
if (y%2 == 0)
return temp*temp;
else
{
if(y > 0)
return x * temp * temp;
else
return (temp * temp) / x;
}
}
static long pow(int a,int b)
{
long result=1;
if(b==0)
return 1;
long x=a;
while(b>0)
{
if(b%2!=0)
result*=x;
x=x*x;
b=b/2;
}
return result;
}
static ArrayList<Integer> sieveOfEratosthenes(int n)
{
ArrayList<Integer> arr=new ArrayList<Integer>();
boolean prime[] = new boolean[n+1];
for(int i=2;i<n;i++)
prime[i] = true;
for(int p = 2; p*p <=n; p++)
{
if(prime[p] == true)
{
arr.add(p);
for(int i = p*p; i <= n; i += p)
prime[i] = false;
}
}
return arr;
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.io.FilterInputStream;
import java.io.BufferedInputStream;
import java.io.InputStream;
/**
* @author khokharnikunj8
*/
public class Main {
public static void main(String[] args) {
new Thread(null, new Runnable() {
public void run() {
new Main().solve();
}
}, "1", 1 << 26).start();
}
void solve() {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
ScanReader in = new ScanReader(inputStream);
PrintWriter out = new PrintWriter(outputStream);
BSportMafia solver = new BSportMafia();
solver.solve(1, in, out);
out.close();
}
static class BSportMafia {
public long findsqrt(long a) {
long b = (long) Math.sqrt(a);
for (long tt = Math.max(0, b - 10); tt <= b + 10; tt++) if (tt * tt == a) return tt;
return -1;
}
public void solve(int testNumber, ScanReader in, PrintWriter out) {
long n = in.scanInt();
long k = in.scanInt();
out.println(n - (-3 + findsqrt(9 + 8 * (k + n))) / 2);
}
}
static class ScanReader {
private byte[] buf = new byte[4 * 1024];
private int index;
private BufferedInputStream in;
private int total;
public ScanReader(InputStream inputStream) {
in = new BufferedInputStream(inputStream);
}
private int scan() {
if (index >= total) {
index = 0;
try {
total = in.read(buf);
} catch (Exception e) {
e.printStackTrace();
}
if (total <= 0) return -1;
}
return buf[index++];
}
public int scanInt() {
int integer = 0;
int n = scan();
while (isWhiteSpace(n)) n = scan();
int neg = 1;
if (n == '-') {
neg = -1;
n = scan();
}
while (!isWhiteSpace(n)) {
if (n >= '0' && n <= '9') {
integer *= 10;
integer += n - '0';
n = scan();
}
}
return neg * integer;
}
private boolean isWhiteSpace(int n) {
if (n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1) return true;
else return false;
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.StringTokenizer;
public class Main {
static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
static StringTokenizer st = new StringTokenizer("");
static PrintWriter pw = new PrintWriter(System.out);
public static String next() throws IOException {
while (!st.hasMoreTokens()) {
st = new StringTokenizer(br.readLine());
}
return st.nextToken();
}
public static int nextInt() throws IOException {
return Integer.parseInt(next());
}
public static long nextLong() throws IOException {
return Long.parseLong(next());
}
public static void main(String[] args) throws IOException {
new Main().run();
}
void run() throws IOException {
long n = nextInt();
long k = nextInt();
long d = 9 + 8 * (n + k);
pw.print(n - (-3 + (int)Math.sqrt(d)) / 2);
pw.close();
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
public class B {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
long n = sc.nextInt(), k = sc.nextInt();
long start = 1, end = n;
while(start <= end) {
long mid = (start + end) >> 1;
if(calc(mid) - (n - mid) == k) {
System.out.println(n - mid);
return;
} else if (calc(mid) - (n - mid) > k) {
end = mid - 1;
} else {
start = mid + 1;
}
}
}
public static long calc(long n) {
return (n * n + n) / 2;
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.nio.CharBuffer;
import java.util.NoSuchElementException;
public class P1195B {
public static void main(String[] args) {
SimpleScanner scanner = new SimpleScanner(System.in);
PrintWriter writer = new PrintWriter(System.out);
int n = scanner.nextInt();
int k = scanner.nextInt();
int l = 0;
int r = n;
int ans = 0;
while (l <= r) {
int eat = (l + r) >> 1;
int lastPut = n - eat;
long totalPut = (long) (lastPut + 1) * lastPut / 2;
long remain = totalPut - eat;
if (remain == k) {
ans = eat;
break;
} else if (remain > k)
l = eat + 1;
else
r = eat - 1;
}
writer.println(ans);
writer.close();
}
private static class SimpleScanner {
private static final int BUFFER_SIZE = 10240;
private Readable in;
private CharBuffer buffer;
private boolean eof;
SimpleScanner(InputStream in) {
this.in = new BufferedReader(new InputStreamReader(in));
buffer = CharBuffer.allocate(BUFFER_SIZE);
buffer.limit(0);
eof = false;
}
private char read() {
if (!buffer.hasRemaining()) {
buffer.clear();
int n;
try {
n = in.read(buffer);
} catch (IOException e) {
n = -1;
}
if (n <= 0) {
eof = true;
return '\0';
}
buffer.flip();
}
return buffer.get();
}
void checkEof() {
if (eof)
throw new NoSuchElementException();
}
char nextChar() {
checkEof();
char b = read();
checkEof();
return b;
}
String next() {
char b;
do {
b = read();
checkEof();
} while (Character.isWhitespace(b));
StringBuilder sb = new StringBuilder();
do {
sb.append(b);
b = read();
} while (!eof && !Character.isWhitespace(b));
return sb.toString();
}
int nextInt() {
return Integer.valueOf(next());
}
long nextLong() {
return Long.valueOf(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.Scanner;
public class CodeForces {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int ans = 0;
long x = n;
x = x*(x+1)/2;
while (x!=k) {
x-=n;
n--;
ans++;
k++;
}
System.out.println(ans);
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;
import java.util.*;
import java.util.concurrent.ThreadLocalRandom;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
public class Main2 {
static int mod = 1000000007;
static FastScanner scanner;
public static void main(String[] args) {
scanner = new FastScanner();
long n = scanner.nextInt();
long k = scanner.nextInt();
if (sum(n) == k) {
System.out.println(0);
return;
}
long s = 0;
long e = n + 1;
while (s < e - 1) {
long m = (s + e) / 2;
long put = sum(n - m);
long candiesLeft = put - m;
if (candiesLeft == k) {
System.out.println(m);
return;
}
if (candiesLeft > k) {
s = m;
} else {
e = m;
}
}
}
static long sum(long n) {
long last = 1 + n - 1;
return ((1 + last) * n) / 2;
}
static class WithIdx {
int val, idx;
public WithIdx(int val, int idx) {
this.val = val;
this.idx = idx;
}
}
public static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
String nextLine() {
try {
return br.readLine();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
int[] nextIntArray(int n) {
int[] res = new int[n];
for (int i = 0; i < n; i++) res[i] = nextInt();
return res;
}
long[] nextLongArray(int n) {
long[] res = new long[n];
for (int i = 0; i < n; i++) res[i] = nextLong();
return res;
}
String[] nextStringArray(int n) {
String[] res = new String[n];
for (int i = 0; i < n; i++) res[i] = nextToken();
return res;
}
}
static class PrefixSums {
long[] sums;
public PrefixSums(long[] sums) {
this.sums = sums;
}
public long sum(int fromInclusive, int toExclusive) {
if (fromInclusive > toExclusive) throw new IllegalArgumentException("Wrong value");
return sums[toExclusive] - sums[fromInclusive];
}
public static PrefixSums of(int[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
public static PrefixSums of(long[] ar) {
long[] sums = new long[ar.length + 1];
for (int i = 1; i <= ar.length; i++) {
sums[i] = sums[i - 1] + ar[i - 1];
}
return new PrefixSums(sums);
}
}
static class ADUtils {
static void sort(int[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
int a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
static void reverse(int[] arr) {
int last = arr.length / 2;
for (int i = 0; i < last; i++) {
int tmp = arr[i];
arr[i] = arr[arr.length - 1 - i];
arr[arr.length - 1 - i] = tmp;
}
}
static void sort(long[] ar) {
Random rnd = ThreadLocalRandom.current();
for (int i = ar.length - 1; i > 0; i--)
{
int index = rnd.nextInt(i + 1);
// Simple swap
long a = ar[index];
ar[index] = ar[i];
ar[i] = a;
}
Arrays.sort(ar);
}
}
static class MathUtils {
static long[] FIRST_PRIMES = {
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, 1009, 1013,
1019, 1021, 1031, 1033, 1039, 1049, 1051};
static long[] primes(int to) {
long[] all = new long[to + 1];
long[] primes = new long[to + 1];
all[1] = 1;
int primesLength = 0;
for (int i = 2; i <= to; i ++) {
if (all[i] == 0) {
primes[primesLength++] = i;
all[i] = i;
}
for (int j = 0; j < primesLength && i * primes[j] <= to && all[i] >= primes[j]; j++) {
all[(int) (i * primes[j])] = primes[j];
}
}
return Arrays.copyOf(primes, primesLength);
}
static long modpow(long b, long e, long m) {
long result = 1;
while (e > 0) {
if ((e & 1) == 1) {
/* multiply in this bit's contribution while using modulus to keep
* result small */
result = (result * b) % m;
}
b = (b * b) % m;
e >>= 1;
}
return result;
}
static long submod(long x, long y, long m) {
return (x - y + m) % m;
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
/*
Roses are red
Memes are neat
All my test cases time out
Lmao yeet
*/
import java.util.*;
import java.io.*;
public class B
{
public static void main(String args[]) throws Exception
{
BufferedReader infile = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st = new StringTokenizer(infile.readLine());
int N = Integer.parseInt(st.nextToken());
int K = Integer.parseInt(st.nextToken());
//bin search
//ugh I'm shafting
long x = (long)N;
long low = 0L;
long high = N;
while(low != high)
{
x = (low+high+1)/2;
long add = (x*(x+1))/2;
long y = N-x;
if(add-y > K)
high = x;
else if(add-y == K)
{
System.out.println(y);
break;
}
else
low = x;
}
//run time?
}
public static void sort(int[] arr)
{
PriorityQueue<Integer> pq = new PriorityQueue<Integer>();
for(int a: arr)
pq.add(a);
for(int i=0; i < arr.length; i++)
arr[i] = pq.poll();
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
import java.lang.*;
import java.math.*;
public class D {
public static void main(String[] args) throws Exception {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
// int n = Integer.parseInt(bf.readLine());
StringTokenizer st = new StringTokenizer(bf.readLine());
int n = Integer.parseInt(st.nextToken());
int k = Integer.parseInt(st.nextToken());
for(int i=0; i<100000; i++) {
long mult = 1L*i*(i+1)/2;
long b = 1L*mult - k;
if(i+b == n*1L) {
out.println(b);
out.close(); System.exit(0);
}
}
//out.println(count);
out.close(); System.exit(0);
}
}
// a(a+1)/2 - b = k; a+b = n | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.Charset;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
public class CFContest {
public static void main(String[] args) throws Exception {
boolean local = System.getProperty("ONLINE_JUDGE") == null;
boolean async = false;
Charset charset = Charset.forName("ascii");
FastIO io = local ? new FastIO(new FileInputStream("D:\\DATABASE\\TESTCASE\\Code.in"), System.out, charset) : new FastIO(System.in, System.out, charset);
Task task = new Task(io, new Debug(local));
if (async) {
Thread t = new Thread(null, task, "dalt", 1 << 27);
t.setPriority(Thread.MAX_PRIORITY);
t.start();
t.join();
} else {
task.run();
}
if (local) {
io.cache.append("\n\n--memory -- \n" + ((Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) >> 20) + "M");
}
io.flush();
}
public static class Task implements Runnable {
final FastIO io;
final Debug debug;
int inf = (int) 1e8;
public Task(FastIO io, Debug debug) {
this.io = io;
this.debug = debug;
}
@Override
public void run() {
solve();
}
public void solve() {
int n = io.readInt();
int k = io.readInt();
int l = 0;
int r = n;
while (l < r) {
int m = (l + r + 1) >> 1;
if (when(n, m) < k) {
r = m - 1;
} else {
l = m;
}
}
io.cache.append(l);
}
public long when(int n, int t) {
long put = n - t;
return (put + 1) * put / 2 - t;
}
}
public static class FastIO {
public final StringBuilder cache = new StringBuilder();
private final InputStream is;
private final OutputStream os;
private final Charset charset;
private StringBuilder defaultStringBuf = new StringBuilder(1 << 8);
private byte[] buf = new byte[1 << 13];
private int bufLen;
private int bufOffset;
private int next;
public FastIO(InputStream is, OutputStream os, Charset charset) {
this.is = is;
this.os = os;
this.charset = charset;
}
public FastIO(InputStream is, OutputStream os) {
this(is, os, Charset.forName("ascii"));
}
private int read() {
while (bufLen == bufOffset) {
bufOffset = 0;
try {
bufLen = is.read(buf);
} catch (IOException e) {
throw new RuntimeException(e);
}
if (bufLen == -1) {
return -1;
}
}
return buf[bufOffset++];
}
public void skipBlank() {
while (next >= 0 && next <= 32) {
next = read();
}
}
public int readInt() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
int val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public long readLong() {
int sign = 1;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+' ? 1 : -1;
next = read();
}
long val = 0;
if (sign == 1) {
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
} else {
while (next >= '0' && next <= '9') {
val = val * 10 - next + '0';
next = read();
}
}
return val;
}
public double readDouble() {
boolean sign = true;
skipBlank();
if (next == '+' || next == '-') {
sign = next == '+';
next = read();
}
long val = 0;
while (next >= '0' && next <= '9') {
val = val * 10 + next - '0';
next = read();
}
if (next != '.') {
return sign ? val : -val;
}
next = read();
long radix = 1;
long point = 0;
while (next >= '0' && next <= '9') {
point = point * 10 + next - '0';
radix = radix * 10;
next = read();
}
double result = val + (double) point / radix;
return sign ? result : -result;
}
public String readString(StringBuilder builder) {
skipBlank();
while (next > 32) {
builder.append((char) next);
next = read();
}
return builder.toString();
}
public String readString() {
defaultStringBuf.setLength(0);
return readString(defaultStringBuf);
}
public int readLine(char[] data, int offset) {
int originalOffset = offset;
while (next != -1 && next != '\n') {
data[offset++] = (char) next;
next = read();
}
return offset - originalOffset;
}
public int readString(char[] data, int offset) {
skipBlank();
int originalOffset = offset;
while (next > 32) {
data[offset++] = (char) next;
next = read();
}
return offset - originalOffset;
}
public int readString(byte[] data, int offset) {
skipBlank();
int originalOffset = offset;
while (next > 32) {
data[offset++] = (byte) next;
next = read();
}
return offset - originalOffset;
}
public char readChar() {
skipBlank();
char c = (char) next;
next = read();
return c;
}
public void flush() {
try {
os.write(cache.toString().getBytes(charset));
os.flush();
cache.setLength(0);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
public boolean hasMore() {
skipBlank();
return next != -1;
}
}
public static class Debug {
private boolean allowDebug;
public Debug(boolean allowDebug) {
this.allowDebug = allowDebug;
}
public void assertTrue(boolean flag) {
if (!allowDebug) {
return;
}
if (!flag) {
fail();
}
}
public void fail() {
throw new RuntimeException();
}
public void assertFalse(boolean flag) {
if (!allowDebug) {
return;
}
if (flag) {
fail();
}
}
private void outputName(String name) {
System.out.print(name + " = ");
}
public void debug(String name, int x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, long x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, double x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, int[] x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.toString(x));
}
public void debug(String name, long[] x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.toString(x));
}
public void debug(String name, double[] x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.toString(x));
}
public void debug(String name, Object x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println("" + x);
}
public void debug(String name, Object... x) {
if (!allowDebug) {
return;
}
outputName(name);
System.out.println(Arrays.deepToString(x));
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.lang.*;
import static java.lang.Math.*;
public class Main implements Runnable {
public static void main(String[] args) {
new Thread(null, new Main(), "Check2", 1 << 28).start();// to increse stack size in java
}
static long mod = (long) (1e9 + 7);
public void run() {
InputReader in = new InputReader(System.in);
PrintWriter w = new PrintWriter(System.out);
long n = in.nextLong();
long k = in.nextLong();
long ans= 0;
for (long i=1;i<=Math.min(100000,n);i++){
long left = i;
long right = n-i;
long sum = left * (left + 1);
sum /= 2;
if (sum - right ==k){
ans = right;
}
}
w.println(ans);
w.close();
}
void debug(Object...args) {
System.out.println(Arrays.deepToString(args));
}
static class pair implements Comparable<pair>{
int a;
int b;
pair(int a,int b){
this.a = a;
this.b = b;
}
public int compareTo(pair o){
if(this.b != o.b)return this.b - o.b;
return this.a - o.a;
}
}
long modinv(long a,long b) {
long p=power(b,mod-2,mod);
p=a%mod*p%mod;
p%=mod;
return p;
}
long power(long x,long y,long mod){
if(y==0)return 1%mod;
if(y==1)return x%mod;
long res=1;
x=x%mod;
while(y>0)
{
if((y%2)!=0){
res=(res*x)%mod;
}
y=y/2;
x=(x*x)%mod;
}
return res;
}
long gcd(long a,long b){
if(b==0)return a;
return gcd(b,a%b);
}
void sev(int a[],int n){
for(int i=2;i<=n;i++)a[i]=i;
for(int i=2;i<=n;i++){
if(a[i]!=0){
for(int j=2*i;j<=n;){
a[j]=0;
j=j+i;
}
}
}
}
static class InputReader
{
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream)
{
this.stream = stream;
}
public int read()
{
if (numChars==-1)
throw new InputMismatchException();
if (curChar >= numChars){
curChar = 0;
try
{
numChars = stream.read(buf);
}
catch (IOException e)
{
throw new InputMismatchException();
}
if(numChars <= 0)
return -1;
}
return buf[curChar++];
}
public String nextLine()
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String stock = "";
try
{
stock = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return stock;
}
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);
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Cf2 {
static boolean ok(long n, long k, long eatten) {
long moves = n-eatten;
long ans = moves*(moves+1)/2;
ans -= eatten;
return ans <= k;
}
public static void main(String[] args) {
FastReader in = new FastReader();
long n = in.nextInt();
long k = in.nextInt();
long left = 0, right = n;
while (left <= right) {
long middle = (left+right)/2;
if (ok(n, k, middle)) right = middle-1;
else left=middle+1;
}
System.out.println(left);
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.Scanner;
public class SportMafia {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
int next = 1;
int current = 0;
int result = 0;
for(int i = 0; i < n; i++) {
if(current < k) {
current += next++;
} else {
current--;
result++;
}
}
System.out.println(result);
sc.close();
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
public class algo_1802
{
public static void main(String args[])
{
Scanner ex=new Scanner(System.in);
int n=ex.nextInt();
int k=ex.nextInt();
int x=(int)((Math.sqrt(9.0+8.0*((double)n+(double)k))-3.0)/2.0);
System.out.println(n-x);
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
public class Main
{
public static void main(String[] args)
{
FastReader reader = new FastReader();
PrintWriter writer = new PrintWriter(System.out);
long n = reader.nextLong();
long k = reader.nextLong();
long s=0;
long e=n;
long ans = -1;
while (s<=e) {
long m = (s+e)/2;
long temp = ((n-m)*(n-m+1))/2 - m;
if (temp < k)
e = m-1;
else if (temp > k)
s = m+1;
else {
ans = m;
break;
}
}
writer.println(ans);
writer.close();
}
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
public class B {
public void solve() throws IOException {
long n = nextInt(), k = nextInt();
long c = 2 * (n + k);
long l = -1, r = 200000;
while (r - l > 1) {
long m = l + (r - l) / 2;
if (m * m + 3 * m >= c) {
r = m;
} else {
l = m;
}
}
out.print(n - r);
}
public void run() {
try {
br = new BufferedReader(new InputStreamReader(System.in));
out = new PrintWriter(System.out);
solve();
out.close();
} catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
}
BufferedReader br;
StringTokenizer in;
PrintWriter out;
public String nextToken() throws IOException {
while (in == null || !in.hasMoreTokens()) {
in = new StringTokenizer(br.readLine());
}
return in.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(nextToken());
}
public double nextDouble() throws IOException {
return Double.parseDouble(nextToken());
}
public long nextLong() throws IOException {
return Long.parseLong(nextToken());
}
public int[] nextArr(int n) throws IOException {
int[] res = new int[n];
for (int i = 0; i < n; i++) {
res[i] = nextInt();
}
return res;
}
public static void main(String[] args) throws IOException {
Locale.setDefault(Locale.US);
new B().run();
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
// Created by Whiplash99
import java.io.*;
import java.util.*;
public class A
{
public static void main(String[] args) throws IOException
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
long N,K,tmp,ans=0;
String s[]=br.readLine().trim().split(" ");
N=Long.parseLong(s[0]);
K=Long.parseLong(s[1]);
long l=1,r=N,mid;
while(l<=r)
{
mid=(l+r)/2;
tmp=mid*(mid+1)/2;
tmp-=N;
tmp+=mid;
if(tmp==K)
{
ans=N-mid;
break;
}
else if(tmp>K)
r=mid-1;
else
l=mid+1;
}
System.out.println(ans);
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
long n = in.nextLong();
long k = in.nextLong();
long disc = (long)(Math.sqrt(9 - 4 * (-2 * n - 2 * k)));
long x = (-3 + disc) / 2;
System.out.println(n - x);
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
int n = Integer.parseInt(tokenizer.nextToken());
int k = Integer.parseInt(tokenizer.nextToken());
System.out.println((int)(n-(-3.0+Math.sqrt(9.0+8.0*(n+k)))/2.0));
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.Writer;
import java.io.OutputStreamWriter;
import java.util.InputMismatchException;
import java.io.IOException;
import java.io.InputStream;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author cunbidun
*/
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);
BSportMafia solver = new BSportMafia();
solver.solve(1, in, out);
out.close();
}
static class BSportMafia {
private InputReader in;
private OutputWriter out;
public void solve(int testNumber, InputReader in, OutputWriter out) {
this.in = in;
this.out = out;
long n = in.nextInt();
long k = in.nextInt();
for (long i = 1; i * (i + 1) / 2 + i <= n + k; i++) {
if (i * (i + 1) / 2 + i == n + k) {
out.println(n - i);
return;
}
}
}
}
static class InputReader extends InputStream {
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 - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
private static boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
static class OutputWriter {
private final PrintWriter out;
public OutputWriter(OutputStream outputStream) {
out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.out = new PrintWriter(writer);
}
public void close() {
out.close();
}
public void println(long i) {
out.println(i);
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Codeforces {
static class FastReader
{
BufferedReader br;
StringTokenizer st;
public FastReader()
{
br = new BufferedReader(new
InputStreamReader(System.in));
}
String next()
{
while (st == null || !st.hasMoreElements())
{
try
{
st = new StringTokenizer(br.readLine());
}
catch (IOException e)
{
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt()
{
return Integer.parseInt(next());
}
long nextLong()
{
return Long.parseLong(next());
}
double nextDouble()
{
return Double.parseDouble(next());
}
String nextLine()
{
String str = "";
try
{
str = br.readLine();
}
catch (IOException e)
{
e.printStackTrace();
}
return str;
}
}
public static void main(String[] args) {
FastReader input = new FastReader();
long n = input.nextLong();
long K = input.nextLong();
long root = (long) Math.sqrt(8 * (K+n) + 9);
if (root * root != 8 * (K+n) + 9){
root++;
if (root * root != 8 * (K+n) + 9) root -= 2;
}
System.out.println(n - (root - 3) / 2);
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;
import static java.lang.Math.*;
import static java.util.Arrays.*;
import static java.util.Comparator.*;
public class Main {
FastScanner in;
PrintWriter out;
private void solve() throws IOException {
solveB();
}
private void solveA() throws IOException {
int n = in.nextInt(), k = in.nextInt();
int[] cnt = new int[k];
for (int i = 0; i < n; i++)
cnt[in.nextInt() - 1] ^= 1;
int ans = 0;
for (int i = 0; i < k; i++)
ans += cnt[i];
out.println(n - ans + (ans + 1) / 2);
}
private void solveB() throws IOException {
long n = in.nextLong();
long c = (n + in.nextLong()) * 2;
long l = 0, r = (long) 1e9;
while (l + 1 < r) {
long m = (l + r) / 2;
if (m * m + 3 * m >= c)
r = m;
else
l = m;
}
out.println(n-r);
}
private void solveC() throws IOException {
}
private void solveD() throws IOException {
}
private void solveE() throws IOException {
}
class FastScanner {
StringTokenizer st;
BufferedReader br;
FastScanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
boolean hasNext() throws IOException {
return br.ready() || (st != null && st.hasMoreTokens());
}
int nextInt() throws IOException {
return Integer.parseInt(next());
}
long nextLong() throws IOException {
return Long.parseLong(next());
}
double nextDouble() throws IOException {
return Double.parseDouble(next());
}
String nextLine() throws IOException {
return br.readLine();
}
boolean hasNextLine() throws IOException {
return br.ready();
}
}
private void run() throws IOException {
in = new FastScanner(System.in); // new FastScanner(new FileInputStream(".in"));
out = new PrintWriter(System.out); // new PrintWriter(new FileOutputStream(".out"));
for (int t = 1; t-- > 0; )
solve();
out.flush();
out.close();
}
public static void main(String[] args) throws IOException {
new Main().run();
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;
public class Main {
public static void main(String[] args) throws IOException {
InputStreamReader r = new InputStreamReader(System.in);
BufferedReader f = new BufferedReader(r);
Scanner sc = new Scanner(System.in);
long n=sc.nextLong();
long m=sc.nextLong();
long sum=0;
if(n==1){
}else {
for (long i = 1; i <= n; i++) {
sum += i;
if (sum - (n - i) == m) {
sum = n - i;
break;
}
}
}
System.out.println(sum);
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.*;
public class l {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////// /////////
//////// /////////
//////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE /////////
//////// HHHH HHHH EEEEEEEEEEEEE MMMMMM MMMMMM OOO OOO SSSS SSS EEEEEEEEEEEEE /////////
//////// HHHH HHHH EEEEE MMMM MMM MMM MMMM OOO OOO SSSS SSS EEEEE /////////
//////// HHHH HHHH EEEEE MMMM MMMMMM MMMM OOO OOO SSSS EEEEE /////////
//////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSSSSS EEEEE /////////
//////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSS EEEEEEEEEEE /////////
//////// HHHHHHHHHHHHHHHH EEEEEEEEEEE MMMM MMMM OOO OOO SSSSSSS EEEEEEEEEEE /////////
//////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSSS EEEEE /////////
//////// HHHH HHHH EEEEE MMMM MMMM OOO OOO SSS SSSS EEEEE /////////
//////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOO OOO SSS SSSS EEEEEEEEEEEEE /////////
//////// HHHH HHHH EEEEEEEEEEEEE MMMM MMMM OOOOOO SSSSSSS EEEEEEEEEEEEE /////////
//////// /////////
//////// /////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
static int mod = (int) (1e9 + 7);
// static int n;
static StringBuilder sol;
static class pair implements Comparable<pair> {
int left, type;
double w;
public pair(int x, double y) {
left = x;
w = y;
type= 0;
}
public int compareTo(pair o) {
return Double.compare(w,o.w);
}
public String toString() {
return left + " " + w;
}
}
static class tri implements Comparable<tri> {
int st, end,len, side;
tri(int a, int b, int c,int d) {
st = a;
end = b;
len = c;
side=d;
}
public int compareTo(tri o) {
if (st == o.st) return end - o.end;
return st - o.st;
}
public String toString() {
return st + " " + end ;
}
}
static ArrayList<pair>[]adj;
static int n;
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
//FileWriter f = new FileWriter("C:\\Users\\Ibrahim\\out.txt");
PrintWriter pw = new PrintWriter(System.out);
int n = sc.nextInt();
int k = sc.nextInt();
int lo=0;
int hi=n;
int ans=0;
while (lo<=hi){
int mid=lo+hi>>1;
long rem= n-mid;
rem*=(rem+1);
rem/=2;
rem-=mid;
//pw.println(rem+" "+mid+" "+k);
if (rem==k){
ans=mid;
break;
}
else if (rem>k){
lo=mid+1;
}
else hi=mid-1;
}
pw.println(ans);
pw.flush();
}
static long gcd(long a ,long b){
if (a==0)return b;
return gcd(b%a,a);
}
static class Scanner {
StringTokenizer st;
BufferedReader br;
public Scanner(FileReader r) {
br = new BufferedReader(r);
}
public Scanner(InputStream s) {
br = new BufferedReader(new InputStreamReader(s));
}
public String next() throws IOException {
while (st == null || !st.hasMoreTokens())
st = new StringTokenizer(br.readLine());
return st.nextToken();
}
public int nextInt() throws IOException {
return Integer.parseInt(next());
}
public long nextLong() throws IOException {
return Long.parseLong(next());
}
public String nextLine() throws IOException {
return br.readLine();
}
public double nextDouble() throws IOException {
String x = next();
StringBuilder sb = new StringBuilder("0");
double res = 0, f = 1;
boolean dec = false, neg = false;
int start = 0;
if (x.charAt(0) == '-') {
neg = true;
start++;
}
for (int i = start; i < x.length(); i++)
if (x.charAt(i) == '.') {
res = Long.parseLong(sb.toString());
sb = new StringBuilder("0");
dec = true;
} else {
sb.append(x.charAt(i));
if (dec)
f *= 10;
}
res += Long.parseLong(sb.toString()) / f;
return res * (neg ? -1 : 1);
}
public boolean ready() throws IOException {
return br.ready();
}
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.OutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
/**
* Built using CHelper plug-in
* Actual solution is at the top
*
* @author Xinyi Tao
*/
public class Main {
public static void main(String[] args) {
InputStream inputStream = System.in;
OutputStream outputStream = System.out;
Scanner in = new Scanner(inputStream);
PrintWriter out = new PrintWriter(outputStream);
TaskBR574D2 solver = new TaskBR574D2();
solver.solve(1, in, out);
out.close();
}
static class TaskBR574D2 {
public void solve(int testNumber, Scanner in, PrintWriter out) {
long n = in.nextLong();
long k = in.nextLong();
long r = (long) (Math.sqrt(9 + 8 * (n + k)) - 3) / 2;
out.println(n - r);
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.io.Writer;
import java.util.Arrays;
import java.util.InputMismatchException;
import java.util.*;
import java.io.*;
import java.math.*;
public class Main7{
static class Pair
{
int x;
int y;
public Pair(int x,int y)
{
this.x= x;
this.y= y;
}
@Override
public int hashCode()
{
final int temp = 14;
int ans = 1;
ans =x*31+y*13;
return ans;
}
// Equal objects must produce the same
// hash code as long as they are equal
@Override
public boolean equals(Object o)
{
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (this.getClass() != o.getClass()) {
return false;
}
Pair other = (Pair)o;
if (this.x != other.x || this.y!=other.y) {
return false;
}
return true;
}
}
static class Pair1
{
String x;
int y;
int z;
}
static class Compare
{
/*static void compare(Pair arr[], int n)
{
// Comparator to sort the pair according to second element
Arrays.sort(arr, new Comparator<Pair>() {
@Override public int compare(Pair p1, Pair p2)
{
if(p1.start>p2.start)
{
return 1;
}
else if(p1.start==p2.start)
{
return 0;
}
else
{
return -1;
}
}
});
}
*/
}
public static long pow(long a, long b)
{
long result=1;
while(b>0)
{
if (b % 2 != 0)
{
result=(result*a)%mod;
b--;
}
a=(a*a)%mod;
b /= 2;
}
return result;
}
public static long fact(long num)
{
long value=1;
int i=0;
for(i=2;i<num;i++)
{
value=((value%mod)*i%mod)%mod;
}
return value;
}
public static int gcd(int a, int b)
{
if (a == 0)
return b;
return gcd(b%a, a);
}
/* public static long lcm(long a,long b)
{
return a * (b / gcd(a, b));
}
*/ public static long sum(int h)
{
return (h*(h+1)/2);
}
/*public static void dfs(int parent,boolean[] visited)
{
TreeSet<Integer> arr=new TreeSet<Integer>();
arr=graph.get(parent);
visited[parent]=true;
if(a[parent]==1)
{
flag=1;
}
if(a[parent]==2)
{
flag1=1;
}
if(flag==1 && flag1==1)
{
return;
}
Iterator itr=arr.iterator();
while(itr.hasNext())
{
int num=(int)itr.next();
if(visited[num]==false)
{
dfs(num,visited);
}
}
x1x`
}*/
// static int flag1=0;
static int[] dis;
static int mod=1000000007;
static ArrayList<ArrayList<Integer>> graph;
public static void bfs(int num,int size)
{
boolean[] visited=new boolean[size+1];
Queue<Integer> q=new LinkedList<>();
q.add(num);
ans[num]=1;
visited[num]=true;
while(!q.isEmpty())
{
int x=q.poll();
ArrayList<Integer> al=graph.get(x);
for(int i=0;i<al.size();i++)
{
int y=al.get(i);
if(visited[y]==false)
{
q.add(y);
ans[y]=ans[x]+1;
visited[y]=true;
}
}
}
}
static int[] ans;
// static int[] a;
public static int[] sort(int[] a)
{
int n=a.length;
ArrayList<Integer> ar=new ArrayList<>();
for(int i=0;i<a.length;i++)
{
ar.add(a[i]);
}
Collections.sort(ar);
for(int i=0;i<n;i++)
{
a[i]=ar.get(i);
}
return a;
}
// static int flag=1;
static public void main(String args[])throws IOException
{
// BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
/* boolean[] prime=new boolean[1000001];
for(int i=2;i*i<=1000000;i++)
{
if(prime[i]==false)
{
for(int j=2*i;j<=1000000;j+=i)
{
prime[j]=true;
}
}
}
*/
int n=i();
int k=i();
long low=0;
long high=k;
long fin=0;
long ans=0;
for(int i=1;i<=n;i++)
{
ans+=i;
if(Math.abs(ans-k)+i==n && ans>=k)
{
fin=Math.abs(ans-k);
break;
}
}
pln(fin+"");
}
/**/
static InputReader in=new InputReader(System.in);
static OutputWriter out=new OutputWriter(System.out);
public static long l()
{
String s=in.String();
return Long.parseLong(s);
}
public static void pln(String value)
{
System.out.println(value);
}
public static int i()
{
return in.Int();
}
public static String s()
{
return in.String();
}
}
class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
private SpaceCharFilter filter;
public InputReader(InputStream stream) {
this.stream = stream;
}
public int read() {
if (numChars== -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public int Int() {
int c = read();
while (isSpaceChar(c))
c = read();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = read();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public String String() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isSpaceChar(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
if (filter != null)
return filter.isSpaceChar(c);
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public String next() {
return String();
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
class OutputWriter {
private final PrintWriter writer;
public OutputWriter(OutputStream outputStream) {
writer = new PrintWriter(new BufferedWriter(new OutputStreamWriter(outputStream)));
}
public OutputWriter(Writer writer) {
this.writer = new PrintWriter(writer);
}
public void print(Object...objects) {
for (int i = 0; i < objects.length; i++) {
if (i != 0)
writer.print(' ');
writer.print(objects[i]);
}
}
public void printLine(Object...objects) {
print(objects);
writer.println();
}
public void close() {
writer.close();
}
public void flush() {
writer.flush();
}
}
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.Int();
return array;
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class B_574 {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String[] input = br.readLine().split(" ");
int N = Integer.valueOf(input[0]);
int K = Integer.valueOf(input[1]);
long sum = 0;
for(int i = 0; i < N; i++){
if(sum - (N - i) == K){
System.out.println(Integer.valueOf(N-i));
return;
}
sum += (i+1);
}
System.out.println("0");
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
import java.io.*;
public class template {
public static void main(String[] args) {
FastScanner sc = new FastScanner();
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
int n = sc.nextInt();
int k = sc.nextInt();
long left = 0;
long right = n;
long mid = left+right/2;
while(left<=right) {
mid = (left+(right))/2;
if(((mid+1)*mid)/2-(n-mid)==k) {
pw.println(n-mid);
pw.close();
break;
}
else if(((mid+1)*mid)/2-(n-mid)>k) {
right = mid-1;
}
else left = mid+1;
}
}
}
@SuppressWarnings("all")
class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(String s) {
try {
br = new BufferedReader(new FileReader(s));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public FastScanner() {
br = new BufferedReader(new InputStreamReader(System.in));
}
String nextToken() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(nextToken());
}
long nextLong() {
return Long.parseLong(nextToken());
}
double nextDouble() {
return Double.parseDouble(nextToken());
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.math.*;
import java.util.*;
import java.io.*;
public class Main{
static StreamTokenizer in=new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
static BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
static Scanner sc=new Scanner(System.in);
static PrintWriter out=new PrintWriter(System.out);
//static InputReader sc=new InputReader(System.in);
static final int inf=10000000;
public static void main(String args[])throws Exception {
double n,k;
n=sc.nextDouble();
k=sc.nextDouble();
double ans=0;
ans=Math.sqrt(2.25+2*(n+k))-1.5;
System.out.printf("%.0f\n",n-ans);
}
static void ssort(int arr[]){
int len=arr.length;
for(int i=0;i<len;i++){
int t=(int)(len*Math.random());
int temp=arr[t];
arr[t]=arr[i];
arr[i]=temp;
}
Arrays.sort(arr);
}
static int getInt()throws Exception{
in.nextToken();
return (int)in.nval;
}
}
class InputReader {
private final InputStream stream;
private final byte[] buf = new byte[8192];
private int curChar, snumChars;
public InputReader(InputStream st) {
this.stream = st;
}
public int read() {
if (snumChars == -1)
throw new InputMismatchException();
if (curChar >= snumChars) {
curChar = 0;
try {
snumChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (snumChars <= 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 {
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 {
res *= 10;
res += c - '0';
c = read();
} while (!isSpaceChar(c));
return res * sgn;
}
public int[] nextIntArray(int n) {
int a[] = new int[n];
for (int i = 0; i < n; i++) {
a[i] = nextInt();
}
return a;
}
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 String nextLine() {
int c = read();
while (isSpaceChar(c))
c = read();
StringBuilder res = new StringBuilder();
do {
res.appendCodePoint(c);
c = read();
} while (!isEndOfLine(c));
return res.toString();
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
private boolean isEndOfLine(int c) {
return c == '\n' || c == '\r' || c == -1;
}
}
/*
*/ | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;
public class B {
static PrintWriter out = new PrintWriter(new BufferedOutputStream(System.out));
static MyScanner sc;
static {
try {
sc = new MyScanner();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
doTask();
out.flush();
}
public static void doTask(){
long n = sc.nextInt();
long k = sc.nextInt();
long c = -2*(n+k);
long d = 9 - 4*c;
double result = n - (-3 + Math.sqrt(1.0*d))/2;
out.println(Math.round(result));
}
public static class MyScanner {
BufferedReader br;
StringTokenizer st;
public MyScanner() throws FileNotFoundException {
br = new BufferedReader(new InputStreamReader(System.in));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine(){
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
| logn | 1195_B. Sport Mafia | CODEFORCES |
import java.util.*;
public class B
{
static long sumN(long n)
{
return n * (n + 1) / 2;
}
static int binSearchPuts(int n, int k)
{
int L = 1;
int U = n;
int M = (L + U) / 2;
while(L <= U)
{
long left = sumN(M) - (n - M);
if(left < k)
{
L = M + 1;
}
else if(left > k)
{
U = M;
}
else
{
break;
}
M = (L + U) / 2;
}
return M;
}
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
int n = input.nextInt();
int k = input.nextInt();
long ate = n - binSearchPuts(n, k);
System.out.println(ate);
}
} | logn | 1195_B. Sport Mafia | CODEFORCES |
import java.io.*;
import java.math.*;
import java.util.*;
import java.util.stream.*;
import java.lang.management.*;
import static java.lang.Math.abs;
import static java.lang.Math.min;
import static java.lang.Math.max;
import static java.lang.Math.sqrt;
import static java.lang.Integer.signum;
@SuppressWarnings("unchecked")
public class P1177B {
public void run() throws Exception {
for (long k = nextLong() - 1, d = 1, dc = 9, sv = 1; true; k -= dc, d++, sv *= 10, dc = sv * d * 9) {
if (k <= dc) {
println(Long.toString(sv + k / d).charAt((int)(k % d)));
break;
}
}
}
public static void main(String... args) throws Exception {
br = new BufferedReader(new InputStreamReader(System.in));
pw = new PrintWriter(new BufferedOutputStream(System.out));
new P1177B().run();
br.close();
pw.close();
System.err.println("\n[Time : " + (System.currentTimeMillis() - startTime) + " ms]");
long gct = 0, gcc = 0;
for (GarbageCollectorMXBean garbageCollectorMXBean : ManagementFactory.getGarbageCollectorMXBeans()) {
gct += garbageCollectorMXBean.getCollectionTime();
gcc += garbageCollectorMXBean.getCollectionCount();
}
System.err.println("[GC time : " + gct + " ms, count = " + gcc + "]");
}
static long startTime = System.currentTimeMillis();
static BufferedReader br;
static PrintWriter pw;
StringTokenizer stok;
String nextToken() throws IOException {
while (stok == null || !stok.hasMoreTokens()) {
String s = br.readLine();
if (s == null) { return null; }
stok = new StringTokenizer(s);
}
return stok.nextToken();
}
void print(byte b) { print("" + b); }
void print(int i) { print("" + i); }
void print(long l) { print("" + l); }
void print(double d) { print("" + d); }
void print(char c) { print("" + c); }
void print(Object o) {
if (o instanceof int[]) { print(Arrays.toString((int [])o));
} else if (o instanceof long[]) { print(Arrays.toString((long [])o));
} else if (o instanceof char[]) { print(Arrays.toString((char [])o));
} else if (o instanceof byte[]) { print(Arrays.toString((byte [])o));
} else if (o instanceof short[]) { print(Arrays.toString((short [])o));
} else if (o instanceof boolean[]) { print(Arrays.toString((boolean [])o));
} else if (o instanceof float[]) { print(Arrays.toString((float [])o));
} else if (o instanceof double[]) { print(Arrays.toString((double [])o));
} else if (o instanceof Object[]) { print(Arrays.toString((Object [])o));
} else { print("" + o); }
}
void printsp(int [] a) { for (int i = 0, n = a.length; i < n; print(a[i] + " "), i++); }
void print(String s) { pw.print(s); }
void println() { println(""); }
void println(byte b) { println("" + b); }
void println(int i) { println("" + i); }
void println(long l) { println("" + l); }
void println(double d) { println("" + d); }
void println(char c) { println("" + c); }
void println(Object o) { print(o); println(); }
void println(String s) { pw.println(s); }
int nextInt() throws IOException { return Integer.parseInt(nextToken()); }
long nextLong() throws IOException { return Long.parseLong(nextToken()); }
double nextDouble() throws IOException { return Double.parseDouble(nextToken()); }
char nextChar() throws IOException { return (char) (br.read()); }
String next() throws IOException { return nextToken(); }
String nextLine() throws IOException { return br.readLine(); }
int [] readInt(int size) throws IOException {
int [] array = new int [size];
for (int i = 0; i < size; i++) { array[i] = nextInt(); }
return array;
}
long [] readLong(int size) throws IOException {
long [] array = new long [size];
for (int i = 0; i < size; i++) { array[i] = nextLong(); }
return array;
}
double [] readDouble(int size) throws IOException {
double [] array = new double [size];
for (int i = 0; i < size; i++) { array[i] = nextDouble(); }
return array;
}
String [] readLines(int size) throws IOException {
String [] array = new String [size];
for (int i = 0; i < size; i++) { array[i] = nextLine(); }
return array;
}
int gcd(int a, int b) {
if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a);
a = Math.abs(a); b = Math.abs(b);
int az = Integer.numberOfTrailingZeros(a), bz = Integer.numberOfTrailingZeros(b);
a >>>= az; b >>>= bz;
while (a != b) {
if (a > b) { a -= b; a >>>= Integer.numberOfTrailingZeros(a); }
else { b -= a; b >>>= Integer.numberOfTrailingZeros(b); }
}
return (a << Math.min(az, bz));
}
long gcd(long a, long b) {
if (a == 0) return Math.abs(b); if (b == 0) return Math.abs(a);
a = Math.abs(a); b = Math.abs(b);
int az = Long.numberOfTrailingZeros(a), bz = Long.numberOfTrailingZeros(b);
a >>>= az; b >>>= bz;
while (a != b) {
if (a > b) { a -= b; a >>>= Long.numberOfTrailingZeros(a); }
else { b -= a; b >>>= Long.numberOfTrailingZeros(b); }
}
return (a << Math.min(az, bz));
}
void shuffle(int [] a) { // RANDOM shuffle
Random r = new Random();
for (int i = a.length - 1, j, t; i >= 0; j = r.nextInt(a.length), t = a[i], a[i] = a[j], a[j] = t, i--);
}
void shuffle(int [] a, int m) { // QUICK shuffle
for (int i = 0, n = a.length, j = m % n, t; i < n; t = a[i], a[i] = a[j], a[j] = t, i++, j = (i * m) % n);
}
void shuffle(long [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
long t = a[i]; a[i] = a[j]; a[j] = t;
}
}
void shuffle(Object [] a) {
Random r = new Random();
for (int i = a.length - 1; i >= 0; i--) {
int j = r.nextInt(a.length);
Object t = a[i]; a[i] = a[j]; a[j] = t;
}
}
int [] sort(int [] a) {
final int SHIFT = 16, MASK = (1 << SHIFT) - 1, SIZE = (1 << SHIFT) + 1;
int n = a.length, ta [] = new int [n], ai [] = new int [SIZE];
for (int i = 0; i < n; ai[(a[i] & MASK) + 1]++, i++);
for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++);
for (int i = 0; i < n; ta[ai[a[i] & MASK]++] = a[i], i++);
int [] t = a; a = ta; ta = t;
ai = new int [SIZE];
for (int i = 0; i < n; ai[(a[i] >> SHIFT) + 1]++, i++);
for (int i = 1; i < SIZE; ai[i] += ai[i - 1], i++);
for (int i = 0; i < n; ta[ai[a[i] >> SHIFT]++] = a[i], i++);
return ta;
}
void flush() {
pw.flush();
}
void pause() {
flush(); System.console().readLine();
}
} | logn | 1177_B. Digits Sequence (Hard Edition) | CODEFORCES |
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
FastScanner in = new FastScanner(System.in);
PrintWriter out = new PrintWriter(System.out);
new Main().run(in, out);
out.close();
}
public static long mod = 17352642619633L;
void run(FastScanner in, PrintWriter out) {
// kth digit
long K = in.nextLong();
// which number encompasses the Kth digit
long lo = 1;
long hi = (long)1e12+1;
while (lo < hi) {
long m = (lo+hi)>>1;
long d = numDigitsLte(m);
if (d <= K) {
lo = m+1;
} else {
hi = m;
}
}
// 123[1]1391 = m digits
long numDigits = numDigitsLte(lo);
if (numDigitsLte(lo-1) == K) {
out.println((((lo-1)%10)+10)%10);
} else {
int offset = (int)(numDigits-K);
// out.print(lo + " ");
List<Long> digits = new ArrayList<>();
while (lo > 0) {
digits.add(lo%10);
lo /= 10;
}
// backwards
// before : 1[2]3456
// in list: 6543[2]1
// offset = 4
out.println(digits.get(offset));
}
}
static long[] dig = new long[15];
static {
for (int i = 1; i < dig.length; i++) {
dig[i] = 9 * (long)Math.pow(10, i-1) * i;
}
for (int i = 1; i < dig.length; i++) {
dig[i] += dig[i-1];
}
}
long numDigitsLte(long m) {
if (m <= 9) return m;
int numDigits = 0;
long M = m;
while (M > 0) {
numDigits++;
M /= 10;
}
long ret = dig[numDigits-1];
ret += (m-(long)Math.pow(10, numDigits-1)+1)*numDigits;
return ret;
// digits below
// 9 + 90 + 900
// [1-9], [10-99], [100-999]
// 9, 90*2, 900*3, ...
// 9999..
// 9138
// [1-1000), [1000,9138]
}
static class FastScanner {
BufferedReader br;
StringTokenizer st;
public FastScanner(InputStream in) {
br = new BufferedReader(new InputStreamReader(in));
st = null;
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
}
}
| logn | 1177_B. Digits Sequence (Hard Edition) | CODEFORCES |
Subsets and Splits