跳到正文

题解

题解:CF177E2 Space Voyage

freedom_wang约 5 分钟
原文
文章信息与版本

文章信息

发布
2025年8月28日
收录
2025年12月12日
更新
2026年11月12日
篇幅
约 5 分钟
题目
CF177E2

版本

  1. v1当前

其实是简单题,为什么会是蓝呢qwq

思路:

考虑二分。

二分旅行总时长为 cc 的最大值和旅行总时长为 c−1c-1 的最大值,作差即可。

n≤1×104n\le1\times10^4,单次 O(n)O(n) 的 check 即可。

注意 check 函数会爆 long long,需要开 __int128。

AC code:

C++
#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;
}
原文
存档评论2 条抓取于