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);
    			
    		}
    		
    	}
    }
  • 相关阅读:
    java 运算符优先级(sum operator priority level)
    how to improve your programming ablity?
    window.returnValue = 00000000; window.close();
    list
    select card 双层
    double color ball
    Linq 左连接 内连接
    DDD 详细 介绍 摘自网络
    没有老板的公司,你适应吗?摘自网络
    Using Java Classes in your .NET Application 摘自网络
  • 原文地址:https://www.cnblogs.com/zufezzt/p/6868950.html
Copyright © 2011-2022 走看看