Codeforces1182F Maximum Sine (类欧几里得)

2023-10-06 13:18

本文主要是介绍Codeforces1182F Maximum Sine (类欧几里得),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

传送门
f ( x ) = abs ( sin ( p q π x ) ) f(x) = \text{abs}(\text{sin}(\frac{p}{q} \pi x)) f(x)=abs(sin(qpπx))
求整数x在[a,b]之间 f x f_x fx最大值
这道题官方给的题解是分块暴力?参考qzh巨佬题解,我也用类欧几里得做的这道题
首先sin非常不友善,我们发现这题可以转化为求 p x q \frac{px}{q} qpx最接近 k + 1 2 k+\frac {1}{2} k+21 x x x
p ⋅ x m o d q p\cdot x \ mod \ q px mod q 最接近 q 2 \frac{q}{2} 2q
同时乘2,得 2 p ⋅ x m o d 2 q 2p\cdot x \ mod \ 2q 2px mod 2q 最接近 q q q
考虑转化为可行性问题,二分mid,则问题转化为是否存在x使得 2 p ⋅ x m o d 2 q 2p\cdot x \ mod \ 2q 2px mod 2q [ q − m i d , q + m i d ] [q-mid,q+mid] [qmid,q+mid]上有取值
问题等价于求 ∑ x = a b ⌊ p x − l q ⌋ − ⌊ p x − r − 1 q ⌋ \sum_{x=a}^b\lfloor\frac{px-l}{q}\rfloor - \lfloor\frac{px-r-1}{q}\rfloor x=abqpxlqpxr1是否大于0其中 l = q − m i d , r = q + m i d l=q-mid,r=q+mid l=qmid,r=q+mid
这个意思就是说看你这段里跨没跨过一个q的整数倍,如果跨过了那么这两个值就会不同
用类欧几里得 f x f_x fx求解即可
最后用exgcd还原x
Code

#include<bits/stdc++.h>
using namespace std;
typedef long long ll; 
int t;
int a,b,p,q;
ll gcd(int n,int a,int b,int c)
{//cout<<n<<" "<<a<<" "<<b<<" "<<c<<endl;if(n==-1)return 0;if(a==0)return 1ll*(b/c)*(n+1);if(a>=c||b>=c){return 1ll*n*(n+1)/2*(a/c)+1ll*(n+1)*(b/c)+gcd(n,a%c,b%c,c);}ll m=(1ll*a*n+b)/c-1;//cout<<m<<" "<<c<<" "<<c-b-1<<" "<<a<<"fdsafsdfsa"<<endl;return 1ll*n*(m+1)-gcd(m,c,c-b-1,a);
}
ll sum(int x,int y,int a,int b,int c)
{return gcd(y,a,b,c)-gcd(x-1,a,b,c);
}
bool check(int l,int r)
{return sum(a,b,p,q-l,q)-sum(a,b,p,q-r-1,q)>0?1:0;
}
void exgcd(int a,int b,ll &x,ll &y)
{if(b==0){x=1;y=0;return ;}exgcd(b,a%b,y,x);y-=a/b*x;
}
ll sol(int p,int q,int a,int b,int t)
{int gc=__gcd(p,q);if(t%gc)return 1e18;p /= gc, q /= gc, t /= gc;ll x, y;exgcd(p, q, x, y);//cout<<x<<" "<<y<<endl;x *= t, y *= t;x += 1ll * (a - x) / q * q;while(x < a) x += q;while(x - q >= a) x -= q;if(x > b) return 1e18;return x;  
}
void solve()
{scanf("%d%d%d%d",&a,&b,&p,&q);p*=2,q*=2;int l=0,r=q/2;while(l<r-1){int mid=l+r>>1;//cout<<mid<<endl;if(check(q/2-mid,q/2+mid))r=mid;else l=mid;}//cout<<"gfdgdsf"<<endl;long long ans;if(check(q/2-l,q/2+l))ans=l;else ans=r;printf("%lld\n",min(sol(p,q,a,b,q/2-ans),sol(p,q,a,b,q/2+ans)));
}
int main()
{scanf("%d",&t);while(t--){solve();}return 0;
}

这篇关于Codeforces1182F Maximum Sine (类欧几里得)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/153155

相关文章

csu 1446 Problem J Modified LCS (扩展欧几里得算法的简单应用)

这是一道扩展欧几里得算法的简单应用题,这题是在湖南多校训练赛中队友ac的一道题,在比赛之后请教了队友,然后自己把它a掉 这也是自己独自做扩展欧几里得算法的题目 题意:把题意转变下就变成了:求d1*x - d2*y = f2 - f1的解,很明显用exgcd来解 下面介绍一下exgcd的一些知识点:求ax + by = c的解 一、首先求ax + by = gcd(a,b)的解 这个

最大公因数:欧几里得算法

简述         求两个数字 m和n 的最大公因数,假设r是m%n的余数,只要n不等于0,就一直执行 m=n,n=r 举例 以18和12为例 m n r18 % 12 = 612 % 6 = 06 0所以最大公因数为:6 代码实现 #include<iostream>using namespace std;/

leetcode#628. Maximum Product of Three Numbers

题目 Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3]Output: 6 Example 2: Input: [1,2,3,4]Output: 24 Note: The lengt

Maximum likelihood function maximizes what thing?

最大似然函数(Maximum Likelihood Function)最大化的是数据在给定参数下出现的概率。具体来说,它最大化的是似然函数(Likelihood Function),即给定参数 ( \theta ) 下观测数据的概率。在统计学中,似然函数 ( L(\theta) ) 通常定义为所有独立观测数据点概率的乘积,对于参数 ( \theta ) 的函数。 对于一组独立同分布的观测数据

ORA-24067: exceeded maximum number of subscribers for queue ADMIN.SMS_MT_QUEUE

临时处理办法: delete from aq$_ss_MT_tab_D;delete from aq$_ss_MT_tab_g;delete from aq$_ss_MT_tab_h;delete from aq$_ss_MT_tab_i;delete from aq$_ss_MT_tab_p;delete from aq$_ss_MT_tab_s;delete from aq$

[LeetCode] 239. Sliding Window Maximum

题:https://leetcode.com/problems/sliding-window-maximum/description/ 题目 Given an array nums, there is a sliding window of size k which is moving from the very left of the array to the very right. You

vue3 el-menu 菜单Maximum recursive updates exceeded 报错

vue3 用el-menu实现管理后台左侧菜单,报Uncaught (in promise) Maximum recursive updates exceeded in component <ElMenu>. This means you have a reactive effect that is mutating its own dependencies and thus recursivel

解决ax+by=c,不定方程(扩展欧几里得)

首先有几个定理我们需要知道,在这里我也会一一证明。 —————————————————————————————————————— 定理1:gcd(a,b)==gcd(b,a%b);这个是欧几里得提出并证明的。 (%是取余的意思,在数学中 可用mod表示); 以下是证明过程 —————————————————————————————————————— 令a = k * b + r; (k

浙大数据结构:01-复杂度2 Maximum Subsequence Sum

数据结构MOOC PTA习题 01-复杂度2 Maximum Subsequence Sum #include <iostream>using namespace std;const int M = 100005;int a[M];int main(){int k;cin >> k;int f = 1;for (int i = 0; i < k; i++){cin >> a[i

Maximum Index

Given an array arr[], find the maximum j – i such that arr[j] > arr[i] Given an array arr[], find the maximum j – i such that arr[j] > arr[i]. Examples: Input: {34, 8, 10, 3, 2, 80, 30, 33, 1}O