ABC 165 D - Floor Function

ABC 165 D - Floor Function

Question

f(x)=floor(Ax/B)A×floor(x/B)f(x)=floor(Ax/B) - A × floor(x/B)
xnx\le n使得f(x)maxf(x)_{max}f(x)maxf(x)_{max}

Solution

  1. 三分法。當時打的時候盲猜這是一個先遞增後遞減的函數,故可以採用三分法。然而實際上這是一個關於B爲週期的函數,並不滿足嚴格三分的條件。問了下大佬,說隨機數據的情況下,剛好被週期卡住的概率很小,所以能過。我感覺這個雖然具有週期性,但是會往同一個週期收斂,雖然在不同週期但是可以看做同一個週期。(我口胡瞎說的,不會嚴格證明,就直覺吧?)
  2. f(x)=f(x+B)f(x)=f(x+B),且在x[0,B1]x\in[0,B-1]f(x)f(x)單調遞增。嚴格證明待補。

Code1.1

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;

ll a,b,n;

ll check(ll x){
	return (a*x)/b-a*(x/b);
}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>a>>b>>n;
	ll L=1,R=n;
	while(R>L){
		ll m1=L+(R-L)/3;
		ll m2=R-(R-L)/3;
		if(check(m1)>check(m2))
			R=m2-1;
		else
			L=m1+1;
	}
	cout<<check(L)<<'\n';
	return 0;
}

Code1.2

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;

ll a,b,n;

ll check(ll x){return (a*x)/b-a*(x/b);}

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cin>>a>>b>>n;
	ll L=1,R=n;
	while(R-L>10){
		ll m1=L+(R-L)/3;
		ll m2=R-(R-L)/3;
		if(check(m1)>check(m2))
			R=m2-1;
		else
			L=m1+1;
	}
	ll mx=0;
	for(ll i=L;i<=min(n,R);i++){
		if(check(i)>mx){
			mx=check(i);
		}
	}
	cout<<mx<<'\n';
	return 0;
}

Code2

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const double eps = 1e-8;
const int NINF = 0xc0c0c0c0;
const int INF  = 0x3f3f3f3f;
const ll  mod  = 1e9 + 7;
const ll  maxn = 1e6 + 5;

int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	ll a, b, n;
	cin>>a>>b>>n;
	n=min(n, b-1);
	cout<<a*n/b<<endl;
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章