zoukankan      html  css  js  c++  java
  • UVa 10170

    题目:求从s開始的递增序列(每次加1)。求出他们加和不小于D的那个最后的加数。

    分析:数学题。分治。s + s+1 + ... + n = n*(n+1)/2 - s*(s-1)/2 = (n+s)*(n-s+1)/2。

                 直接二分答案就可以(二分范围0~10^8)。

    说明:(⊙_⊙)。

    #include <iostream>
    #include <cstdlib> 
    
    using namespace std;
    
    long long sum(long long s, long long n)
    {
    	return (n-s+1LL)*(n+s)/2LL;
    }
    
    long long bs(int S, long long D)
    {
    	long long mid,l = 1LL,r = 100000000LL;
    	while (l < r) {
    		mid = l+(r-l)/2LL;
    		if (sum(S, mid) >= D)
                r = mid;  
            else l = mid+1LL;  
    	} 
    	return r;
    }
    
    int main()
    {
    	long long s,D;
    	while (cin >> s >> D)
    		cout << bs(s, D) << endl;
    	
    	return 0;
    }
    

  • 相关阅读:
    2、变量
    1、基本的数据类型
    jenkins入门
    我的Python基础笔记
    jmeter测试手机app
    Python:字典
    Python:元组
    Python:列表
    Python:函数
    H3C-L2TP
  • 原文地址:https://www.cnblogs.com/zsychanpin/p/6760971.html
Copyright © 2011-2022 走看看