classSolution{public:typedeflonglong ll;// greatest common divisor 最大公约数
ll gcd(ll a, ll b){return b ==0? a :gcd(b, a % b);}// least common multiple 最小公倍数
ll lcm(ll a, ll b){return a * b /gcd(a, b);}intnthMagicalNumber(int n,int a,int b){
ll ab =lcm(a, b);//本题结果在 [1, 2 * 10^9] 的范围内,优化为 [min({a,b,c}), 2 * 10^9]
ll l =min(a, b)-1, r =4e13+1;while(l+1!= r){
ll mid =(l + r)>>1;// 容斥原理:计算 cnt 为[1,mid]中的丑数个数
ll cnt = mid / a + mid / b - mid / ab;if(cnt < n){
l = mid;}else{
r = mid;}}return r %1000000007;}};
911. 在线选举
代码实现(自解)
classTopVotedCandidate{private:
vector<int> times;
map<int,int> query;public:TopVotedCandidate(vector<int>& persons, vector<int>& times):times(times){
map<int,int> _map;int maxVotes =0, selectedPerson =0;for(int i =0; i < times.size(); i++){
_map[persons[i]]++;for(auto it = _map.begin(); it != _map.end(); it++){if(it->second > maxVotes){
maxVotes = it->second;
selectedPerson = it->first;}}if(maxVotes == _map[persons[i]]){
selectedPerson = persons[i];}
query[times[i]]= selectedPerson;}}intq(int t){int l =0, r = times.size()-1, m =0;while(l < r){
m =(l + r +1)>>1;if(times[m]<= t) l = m;else r = m -1;}return query[times[l]];}};/**
* Your TopVotedCandidate object will be instantiated and called as such:
* TopVotedCandidate* obj = new TopVotedCandidate(persons, times);
* int param_1 = obj->q(t);
*/