「雜題亂刷」CF1985F

wangmarui發表於2024-06-13

程式碼恢復訓練 2024.6.13.

題目連結

CF1985F (CF)

CF1985F (luogu)

解題思路

由於一個回合可以用所有無冷卻的技能,因此我們對於技能肯定是能用就用的。

進而推出答案具有單調性

直接二分答案即可,注意二分邊界問題,這裡我開了 __int128 來避免這個問題。

參考程式碼

點選檢視程式碼
#include<bits/stdc++.h>
using namespace std;
//#define map unordered_map
#define forl(i,a,b) for(register long long i=a;i<=b;i++)
#define forr(i,a,b) for(register long long i=a;i>=b;i--)
#define forll(i,a,b,c) for(register long long i=a;i<=b;i+=c)
#define forrr(i,a,b,c) for(register long long i=a;i>=b;i-=c)
#define lc(x) x<<1
#define rc(x) x<<1|1
#define mid ((l+r)>>1)
#define cin(x) scanf("%lld",&x)
#define cout(x) printf("%lld",x)
#define lowbit(x) (x&-x)
#define pb push_back
#define pf push_front
#define IOS ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define endl '\n'
#define QwQ return 0;
#define ll long long
#define ull unsigned long long
#define lcm(x,y) x/__gcd(x,y)*y
#define Sum(x,y) 1ll*(x+y)*(y-x+1)/2
#define aty cout<<"Yes\n";
#define atn cout<<"No\n";
#define cfy cout<<"YES\n";
#define cfn cout<<"NO\n";
#define xxy cout<<"yes\n";
#define xxn cout<<"no\n";
#define printcf(x) x?cout<<"YES\n":cout<<"NO\n";
#define printat(x) x?cout<<"Yes\n":cout<<"No\n";
#define printxx(x) x?cout<<"yes\n":cout<<"no\n";
ll t;
ll n,m;
ll a[200010],b[200010];
ll L,R;
bool check(ll Mid)
{
	__int128 sum=0;
	forl(i,1,m)
	{
		sum+=(Mid/b[i]+1)*a[i];
		if(sum>=n)
			return 1;
	}
	return sum>=n;
}
void solve()
{
	cin>>n>>m;
	forl(i,1,m)
		cin>>a[i];
	forl(i,1,m)
		cin>>b[i];
	L=0,R=3e13;
	while(L<R)
	{
	//	cout<<L<<R<<endl;
		ll Mid=(L+R)/2;
		if(check(Mid))
			R=Mid;
		else
			L=Mid+1;
	}
	cout<<L+1<<endl;
}
int main()
{
	IOS;
	t=1;
 	cin>>t;
	while(t--)
		solve();
	QwQ;
}