CodeForces 785C Anton and Fairy Tale

Posted Fighting Heart

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 785C Anton and Fairy Tale相关的知识,希望对你有一定的参考价值。

二分。

如果$n≤m$,显然只能$n$天。

如果$n>m$,至少可以$m$天,剩余还可以支撑多少天,可以二分计算得到,也可以推公式。二分计算的话可能爆$long$ $long$,上了个$Java$。

import java.math.BigInteger;
import java.util.Scanner;

public class Main 
{
	static Scanner cin = new Scanner(System.in);
	
	static BigInteger sum(BigInteger L,BigInteger R)
	{
		if(L.compareTo(R)>0) return BigInteger.ZERO;
		BigInteger A = L.add(R);
		BigInteger B = R.subtract(L); B = B.add(BigInteger.ONE);
		BigInteger c = A.multiply(B);
		return c.divide(BigInteger.valueOf(2));
	}
	
	public static void main(String []args)
	{
		BigInteger m,n;
		
		n = cin.nextBigInteger();
		m = cin.nextBigInteger();
		
		if(n.compareTo(m)<=0)
		{
			System.out.println(n);
		}
		
		else 
		{
			BigInteger ans = m;
			BigInteger sy = n.subtract(m);
			
			BigInteger hai = BigInteger.ZERO;
			
			BigInteger L = BigInteger.ZERO, R = sy;
			
			boolean pp=false;
			while(L.compareTo(R)<=0)
			{
				BigInteger mid = (L.add(R).divide(BigInteger.valueOf(2)));
				if(sum(ans.add(BigInteger.ONE),ans.add(mid)).compareTo(sy.add(m.multiply(mid)))<0) 
				{
					pp=true;
					hai = mid; 
					L = mid.add(BigInteger.ONE); 
				}
				
				else if(sum(ans.add(BigInteger.ONE),ans.add(mid)).compareTo(sy.add(m.multiply(mid)))==0)
				{
					pp=false;
					hai = mid; 
					L = mid.add(BigInteger.ONE); 
				}
				
				else R = mid.subtract(BigInteger.ONE); 
			}
			
			ans = ans.add(hai);
			
			if(pp == true) ans = ans.add(BigInteger.ONE);
			System.out.println(ans);
			
		}
		
	}
}

以上是关于CodeForces 785C Anton and Fairy Tale的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 734B Anton and Digits

CodeForces 785A Anton and Polyhedrons

codeforces785E Anton and Permutation

CodeForces 734F Anton and School

CodeForces 734E Anton and Tree

Codeforces Round #379 (Div. 2) D. Anton and Chess(模拟)