zoukankan      html  css  js  c++  java
  • 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);
    			
    		}
    		
    	}
    }
  • 相关阅读:
    Greedy Gift Givers 贪婪的送礼者
    USACO 1.1.3 Friday the Thirteenth 黑色星期五
    Prime Matrix(暴力出奇迹)
    博弈论
    好车牌
    C语言中动态内存的分配(malloc,realloc)
    Saruman's Army(贪心)
    Python练习——循环2
    Fox and Number Game
    Repair the Wall (贪心)
  • 原文地址:https://www.cnblogs.com/zufezzt/p/6868950.html
Copyright © 2011-2022 走看看