codeforces contest 785 c题

C. Anton and Fairy Tale
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard 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粒,如果加满n粒则不加,然后第i天要减少i粒,问你多少天之后仓库为空。

解题思路:首先当m >= n时,一定是n天,因为在n天之前,每天都可以通过加m粒而加满仓库,只有当第n天时,加满后,然后减少n粒刚好为空。当m < n时,在第m天之前仓库一定是满的,每天减少的小于m,所以加入m后一定能补满,当第m天结束后,仓库一定剩下n - m,从第m + 1天开始,以后每天仓库以此减少1,2,3,,,,,,,,,,直到为空为止,但是n,m的范围的都long long,所以有可能爆long long,在这里有两种处理办法,第一种是大数弄一下,第二种是用double,都行,但是最后都要用二分。
double

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e3;
ll n,m;
bool judge(ll i)
{
    double x = (double)(i)*(i + 1)/2;
    double y = (double)(n - m);
    if(x >= y) return true;
    else return false;
}
int main()
{
    while(~scanf("%lld%lld",&n,&m))
    {
        ll result;
        if(m >= n) result = n;
        else
        {

                result = m;
                ll l = 1;
                ll r = n;
                ll x = l;
                while(l <= r)
                {
                    ll mid = (l + r)>>1;
                    if(judge(mid))
                    {
                        x = mid;
                        r = mid - 1;
                    }
                    else l = mid + 1;
                }
                result += x;

        }
        printf("%lld\n",result);
    }
    return 0;
}

大数方法

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1e4;
ll n,m;
class bignum{
public:
    int d[maxn];//每一位
    int len;//数的长度
    bignum()
    {
        len = 0;
        memset(d,0,sizeof(d));
    }
    bignum(ll x)
    {
        int l = 0;
        memset(d,0,sizeof(d));
        while(x)
        {
            d[l++] = x%10;
            x /= 10;
        }
        len = l;
    }
    bignum(const bignum &res){
        len = res.len;
        for(int i = 0; i <= len - 1; i++)
        {
            d[i] = res.d[i];
        }
    }
    bignum operator * (const bignum &res) const
    {
        bignum ans;
        for(int i = 0; i < this->len; i++)
        {
            for(int j = 0; j < res.len; j++)
            {
                ans.d[i + j] += d[i]*res.d[j];
            }
        }
        for(int i = 0; i < this->len + res.len; i++)
        {
            ans.d[i + 1] += ans.d[i]/10;
            ans.d[i] %= 10;
        }
        int Len  = this->len + res.len;
        while(Len > 1&&ans.d[Len - 1] == 0)
        {
            Len--;
        }
        ans.len = Len;
        return ans;
    }
    bignum operator ^ (const int x) const
    {
        bignum ans(1);
        bignum res;
        res.len = this->len;
        int term = x;
        for(int i = 0; i < this->len; i++)
        {
            res.d[i] = this->d[i];
        }
        while(term)
        {
            if(term&1) ans = ans*res;
            term >>= 1;
            res = res*res;
        }
        return ans;

    }
    void print()
    {
        for(int i = len - 1; i >= 0; i--)
        {
            printf("%d",d[i]);
        }
        printf("\n");
    }
};
bool cmp(bignum n1,bignum n2)
{
    if(n1.len > n2.len) return true;
    else if(n1.len < n2.len) return false;
    else
    {
        int len = n1.len;
        for(int i = len - 1; i >= 0; i--)
        {
            if(n1.d[i] > n2.d[i]) return true;
            else if(n1.d[i] < n2.d[i]) return false;
        }
    }
    return true;
}
bool judge(ll i)
{
    bignum ans1(i);
    bignum ans2(i + 1);
    bignum ans3(n - m);
    bignum ans4(2);
    bignum ans5 = ans4*ans3;
    bignum ans6 = ans1*ans2;
    if(cmp(ans6,ans5)) return true;
    else return false;
}
int main()
{
    while(~scanf("%lld%lld",&n,&m))
    {
        ll result;
        if(m >= n) result = n;
        else
        {

                result = m;
                ll l = 1;
                ll r = n;
                ll x = l;
                while(l <= r)
                {
                    ll mid = (l + r)>>1;
                    if(judge(mid))
                    {
                        x = mid;
                        r = mid - 1;
                    }
                    else l = mid + 1;
                }
                result += x;

        }
        printf("%lld\n",result);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值