CodeForces 670D2 Magic Powder - 2

E - Magic Powder - 2
Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  CodeForces 670D2

Description

The term of this problem is the same as the previous one, the only exception — increased restrictions.

Input

The first line contains two positive integers n and k (1 ≤ n ≤ 100 000, 1 ≤ k ≤ 109) — the number of ingredients and the number of grams of the magic powder.

The second line contains the sequence a1, a2, ..., an (1 ≤ ai ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, needed to bake one cookie.

The third line contains the sequence b1, b2, ..., bn (1 ≤ bi ≤ 109), where the i-th number is equal to the number of grams of the i-th ingredient, which Apollinaria has.

Output

Print the maximum number of cookies, which Apollinaria will be able to bake using the ingredients that she has and the magic powder.

Sample Input

Input
1 1000000000
1
1000000000
Output
2000000000
Input
10 1
1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000 1000000000
1 1 1 1 1 1 1 1 1 1
Output
0
Input
3 1
2 1 4
11 3 16
Output
4
Input
4 3
4 3 5 6
11 12 14 20
Output
3
FAQ | About Virtual Judge |  Forum |  Discuss |  Open Source Project


题意:有n中材料,每种材料有b克,他想做饼干,做1个饼干需要每种材料ai克,现在有k克魔法粉,这k克魔法粉可以变成任意一种材料,求最终最多做多少个饼干;

另一个所有数据范围变成10^9时我们可以想到,他最多做不超过2*(10^9)个饼干,所以我们可以二分搜索答案,一直到找到符合题意de饼干个数为止;

注意中间过程会爆int的所以用long long


#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<cctype>
#include <map>
#define max(a,b)(a>b?a:b)
#define min(a,b)(a<b?a:b)
#define INF 0x3f3f3f3f
typedef long long ll;

using namespace std;
#define N 110000
int n;
ll k,Max;
ll a[N],b[N];

ll Search(ll l,ll r)
{
    while(l<=r)
    {
        ll mid=(l+r)/2;
        ll sum=0;
        for(int i=1;i<=n;i++)
        {
            if(b[i]<a[i]*mid)
                sum=sum+(a[i]*mid)-b[i];
            if(sum>k)
                break;
        }
        if(sum==k)
            return mid;
        else if(sum<k)
            l=mid+1;
        else
            r=mid-1;
    }
    return l-1;
}

int main()
{
    while(scanf("%d%I64d",&n,&k)!=EOF)
    {
        for(int i=1;i<=n;i++)
            scanf("%I64d",&a[i]);
        for(int i=1;i<=n;i++)
            scanf("%I64d",&b[i]);
        Max=2*1e9+10;
        ll ans=Search(0,Max);
        printf("%I64d\n",ans);
    }
    return 0;
}


你可能感兴趣的:(CodeForces 670D2 Magic Powder - 2)