题解:CF177E2 Space Voyage
freedom_wang
·
·
题解
其实是简单题,为什么会是蓝呢qwq
思路:
考虑二分。
二分旅行总时长为 c 的最大值和旅行总时长为 c-1 的最大值,作差即可。
注意 check 函数会爆 `long long`,需要开 `__int128`。
### AC code:
```cpp
#include<bits/stdc++.h>
#define int __int128
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define rop(i,a,b) for(int i=a;i<b;i++)
#define per(i,a,b) for(int i=a;i>=b;i--)
#define por(i,a,b) for(int i=a;i>b;i--)
//#define endl '\n'
#define pii pair<int,int>
#define fi first
#define se second
typedef long long ll;
typedef unsigned long long ull;
typedef double lf;
typedef long double ld;
using namespace std;
namespace input{const int len=1<<21;char buf[len],*S,*T;inline char getchar(){if(S==T){T=(S=buf)+fread(buf,1,len,stdin);if(S==T)return'\n';}return*S++;}}namespace output{const int len=1<<21;char buf[len],*S=buf,*T=buf+len;inline void flush(){fwrite(buf,1,S-buf,stdout);S=buf;}inline void putchar(char c){*S++=c;if(S==T)flush();}struct NTR{~NTR(){flush();}}ztr;}
#ifdef ONLINE_JUDGE
#define getchar input::getchar
#define putchar output::putchar
#endif
namespace FastIO{struct Reader{template<typename T>Reader&operator>>(T&x){char c=getchar();T f=1;for(;c<'0'||c>'9';){if(c=='-')f=-1;c=getchar();}x=0;for(;c>='0'&&c<='9';){x=x*10+c-48;c=getchar();}x*=f;return*this;}Reader&operator>>(char&c){c=getchar();for(;c==' '||c=='\n';)c=getchar();return*this;}Reader&operator>>(char*str){int len=0;char c=getchar();for(;c==' '||c=='\n';)c=getchar();for(;c!=' '&&c!='\n'&&c!='\r';){str[len++]=c;c=getchar();}str[len]='\0';return*this;}Reader(){}}cin;const char endl='\n';struct Writer{template<typename T>Writer&operator<<(T x){if(!x){putchar('0');return*this;}if(x<0){putchar('-');x=-x;}static int sta[45];int top=0;for(;x;){sta[++top]=x%10;x/=10;}for(;top;){putchar(sta[top]+48);top--;}return*this;}Writer&operator<<(char c){putchar(c);return*this;}Writer&operator<<(char*str){int cur=0;for(;str[cur];)putchar(str[cur++]);return*this;}Writer&operator<<(const char*str){int cur=0;for(;str[cur];)putchar(str[cur++]);return*this;}Writer(){}}cout;}
#define cin FastIO::cin
#define cout FastIO::cout
#define endl FastIO::endl
int n,c,a[10005],b[10005],l,r=1e20,res;
bool flag;
bool check(int x,int c){
int res=0;
rep(i,1,n)
res+=a[i]*x/b[i]+1;
return res<=c;
}
signed main(){
// ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n>>c;
rep(i,1,n){
cin>>a[i]>>b[i];
if(a[i])
flag=1;
}
if(flag)
goto loop;
if(n==c){
cout<<-1;
return 0;
}
loop:;
while(l+1<r){
int mid=l+r>>1;
if(check(mid,c))
l=mid;
else
r=mid;
}
res=r;
l=0;
r=1e20;
while(l+1<r){
int mid=l+r>>1;
if(check(mid,c-1))
l=mid;
else
r=mid;
}
cout<<res-r;
return 0;
}
```