• #404(div 2)C. Anton and Fairy Tale(二)


    C. Anton and Fairy Tale
    time limit per test
    1 second
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Anton likes to listen to fairy tales, especially when Danik, Anton's best friend, tells them. Right now Danik tells Anton a fairy tale:

    "Once upon a time, there lived an emperor. He was very rich and had much grain. One day he ordered to build a huge barn to put there all his grain. Best builders were building that barn for three days and three nights. But they overlooked and there remained a little hole in the barn, from which every day sparrows came through. Here flew a sparrow, took a grain and flew away..."

    More formally, the following takes place in the fairy tale. At the beginning of the first day the barn with the capacity of n grains was full. Then, every day (starting with the first day) the following happens:

    • m grains are brought to the barn. If m grains doesn't fit to the barn, the barn becomes full and the grains that doesn't fit are brought back (in this problem we can assume that the grains that doesn't fit to the barn are not taken into account).
    • Sparrows come and eat grain. In the i-th day i sparrows come, that is on the first day one sparrow come, on the second day two sparrows come and so on. Every sparrow eats one grain. If the barn is empty, a sparrow eats nothing.

    Anton is tired of listening how Danik describes every sparrow that eats grain from the barn. Anton doesn't know when the fairy tale ends, so he asked you to determine, by the end of which day the barn will become empty for the first time. Help Anton and write a program that will determine the number of that day!

    Input

    The only line of the input contains two integers n and m (1 ≤ n, m ≤ 1018) — the capacity of the barn and the number of grains that are brought every day.

    Output

    Output one integer — the number of the day when the barn will become empty for the first time. Days are numbered starting with one.

    Examples
    input
    5 2
    output
    4
    input
    8 1
    output
    5
    Note

    In the first sample the capacity of the barn is five grains and two grains are brought every day. The following happens:

    • At the beginning of the first day grain is brought to the barn. It's full, so nothing happens.
    • At the end of the first day one sparrow comes and eats one grain, so 5 - 1 = 4 grains remain.
    • At the beginning of the second day two grains are brought. The barn becomes full and one grain doesn't fit to it.
    • At the end of the second day two sparrows come. 5 - 2 = 3 grains remain.
    • At the beginning of the third day two grains are brought. The barn becomes full again.
    • At the end of the third day three sparrows come and eat grain. 5 - 3 = 2 grains remain.
    • At the beginning of the fourth day grain is brought again. 2 + 2 = 4 grains remain.
    • At the end of the fourth day four sparrows come and eat grain. 4 - 4 = 0 grains remain. The barn is empty.

    So the answer is 4, because by the end of the fourth day the barn becomes empty.

     题意:给你一个容量为n的满的粮仓,每天往粮仓送m粮食。第一天来一直鸟吃1单位粮食,第二天来两只鸟,以此类推······。问当粮仓为空时是第几天?

    思路:如果m>=n的话则当第n天粮仓空了,m<n时,前m天粮仓为n,可知在m+1天粮仓变成了n-1,m+2天粮仓变成了n-3,依次类推······可知当t*(t+1)/2>=n-m时,即第t+m天粮仓为空。二分时间即可得到最终答案。

     1 #include <iostream>
     2 #include<bits/stdc++.h>
     3 using namespace std;
     4 const long long inf =1888888888;
     5 int main()
     6 {
     7     long long n,m;
     8     while(~scanf("%I64d%I64d",&n,&m))
     9     {
    10         if(m>=n)
    11         {
    12             printf("%I64d
    ",n);
    13             continue;
    14         }
    15         long long l=0,r=inf,ans;
    16         while(l<=r)
    17         {
    18             long long mid=(l+r)/2;
    19             if((mid+1)*mid/2>=n-m)
    20             {
    21                 r=mid-1;
    22                 ans=mid;
    23             }
    24             else
    25                 l=mid+1;
    26         }
    27         printf("%I64d
    ",ans+m);
    28     }
    29     return 0;
    30 }
    View Code
  • 相关阅读:
    论 设计模式及项目架构的作用
    Linux根据进程号查找其程序文件路径 及 lsof 命令使用
    Synchronized 原理
    Seata 中类SPI使用机制分析
    redisson spring boot starter 做分布式锁
    TTFB 时间过长
    ShardingSphere Hint模式 SpringBoot + Mybatis
    Core源码(十三)Stack和ConcurrentStack
    C#源码(十二) HashSet
    Core源码(十一)Queue
  • 原文地址:https://www.cnblogs.com/wang-ya-wei/p/6647208.html
Copyright © 2020-2023  润新知