题意:\(f(x) = \text{abs}(\text{sin}(\frac{p}{q} \pi x))\),给定\(a,b,p,q\),求\(x\in[a,b]\)最大的\(f(x)\)。

题解:div2都这么仙了吗。。。

根据高中数学知识可以推出要求的就是使得\(\frac{px \mod q}{q}\)最接近\(\frac12\)的\(x\),也就是\(px \mod q\)最接近\(\frac q2\)。

有一个结论:\([px \mod q \in [l,r]] = \lfloor\frac{px-l}{q}\rfloor - \lfloor\frac{px-r-1}{q}\rfloor\)。考虑二分\(px \mod q\)与\(\frac q2\)的距离,对于一个\(mid\),相当于要求是否存在\(x\)使得\(px \mod q \in [l=\frac q2-mid,r=\frac q2+mid]\)。根据上述结论,这等价于\(\sum_{x=a}^b\lfloor\frac{px-l}{q}\rfloor - \lfloor\frac{px-r-1}{q}\rfloor\)是否\(>0\)。这个式子是可以类欧求的。

求出最小距离以后,用\(\text{exgcd}\)还原\(x\)即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll Inf = 1e18; int gi() {
int x = 0, o = 1;
char ch = getchar();
while((ch < '0' || ch > '9') && ch != '-') {
ch = getchar();
}
if(ch == '-') {
o = -1, ch = getchar();
}
while(ch >= '0' && ch <= '9') {
x = x * 10 + ch - '0', ch = getchar();
}
return x * o;
} ll solve(ll n, ll a, ll b, ll c) {
if(n < 0) {
return 0;
}
if(!n) {
return b / c;
}
if(a >= c || b >= c) {
return solve(n, a % c, b % c, c) + n * (n + 1) / 2 * (a / c) + (n + 1) * (b / c);
}
ll m = (a * n + b) / c;
return m * n - solve(m - 1, c, c - b - 1, a);
} ll solve(ll l, ll r, ll a, ll b, ll c) {
return solve(r, a, b, c) - solve(l - 1, a, b, c);
} void exgcd(ll a, ll b, ll &x, ll &y) {
if(!b) {
x = 1, y = 0;
return;
}
exgcd(b, a % b, y, x), y -= a / b * x;
} ll a, b, p, q; ll solve(ll p, ll q, ll t) {
ll gg = __gcd(p, q);
if(t % gg != 0) {
return Inf;
}
p /= gg, q /= gg, t /= gg;
ll x, y;
exgcd(p, q, x, y);
x *= t, y *= t;
ll k = (a - x) / q;
x += k * q;
while(x >= a) {
x -= q;
}
while(x < a) {
x += q;
}
return x;
} int main() {
#ifndef ONLINE_JUDGE
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
int T = gi();
while(T--) {
a = gi(), b = gi(), p = gi() << 1, q = gi() << 1;
ll l = 0, w = q / 2, r = w;
while(l < r) {
ll mid = (l + r) >> 1;
ll L = w - mid, R = w + mid;
if(solve(a, b, p, q - L, q) - solve(a, b, p, q - R - 1, q)) {
r = mid;
} else {
l = mid + 1;
}
}
cout << min(solve(p, q, w - l), solve(p, q, w + l)) << endl;
}
return 0;
}

[CF1182F]Maximum Sine的更多相关文章

  1. CF1182F Maximum Sine【类欧,扩欧】

    题目链接:洛谷 题目描述:求整数$x\in [a,b]$使得$|2px \ mod \ 2q-q|$最小,如果有多个$x$输出最小的. 数据范围:$1\leq a,b,p,q\leq 10^9$ 第一 ...

  2. CF 1182F Maximum Sine——根号算法

    题目:http://codeforces.com/contest/1182/problem/F 注意有绝对值. 那么就是 k*p 对 q 取模,找最接近 \(\frac{q}{2}\) 的结果. 也就 ...

  3. Codeforces Round #566 (Div. 2)

    Codeforces Round #566 (Div. 2) A Filling Shapes 给定一个 \(3\times n\) 的网格,问使用 这样的占三个格子图形填充满整个网格的方案数 如果 ...

  4. Codeforces Round #566 (Div. 2)题解

    时间\(9.05\)好评 A Filling Shapes 宽度为\(3\),不能横向填 考虑纵向填,长度为\(2\)为一块,填法有两种 如果长度为奇数则显然无解,否则\(2^{n/2}\) B Pl ...

  5. POJ3693 Maximum repetition substring [后缀数组 ST表]

    Maximum repetition substring Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9458   Acc ...

  6. Uncaught RangeError: Maximum call stack size exceeded 调试日记

    异常处理汇总-前端系列 http://www.cnblogs.com/dunitian/p/4523015.html 开发道路上不是解决问题最重要,而是解决问题的过程,这个过程我们称之为~~~调试 记 ...

  7. iOS---The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

  8. MTU(Maximum transmission unit) 最大传输单元

    最大传输单元(Maximum transmission unit),以太网MTU为1500. 不同网络MTU如下: 如果最大报文数据大小(MSS)超过MTU,则会引起分片操作.   路径MTU: 网路 ...

  9. uva 11059 maximum product(水题)——yhx

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAB1QAAAMcCAIAAABo0QCJAAAgAElEQVR4nOydW7msuhKF2wIasIAHJK

随机推荐

  1. delphi备份恢复剪切板

    http://blog.csdn.net/youthon/article/details/7327776 delphi备份恢复剪切板         分类:            Delphi编程20 ...

  2. centos6.5搭建禅道

    linux用一键安装包 简介:本文介绍如何在linux下面使用禅道一键安装包搭建禅道的运行环境. 一.安装 二.如何访问数据库 linux一键安装包内置了XXD.apache, php, mysql这 ...

  3. PL/SQL Developer 报错Dynamic Performance Tables not accessible, Automatic Statistics disabled for this session You can disable statistics in the preference menu, or obtain select priviliges on the V$ses

    可以从以下几个方面考虑: 1)单独给用户授动态性能视图的权限: SQL> grant select on V_session  to user; SQL> grant select on  ...

  4. 剑指offer--day07

    1.1 题目:反转链表:输入一个链表,反转链表后,输出新链表的表头. 1.2 思路:这道题,我们要做到的是反转链表,我们的思路是将前一个节点与后一个节点断开,然后让后一个节点指向前一个节点,这个过程就 ...

  5. 什么是HIS、PACS、LIS、RIS

    什么是HIS?医院信息系统的定义(HIS)医院信息系统(Hospital Information System,HIS)在国际学术界已公认为新兴的医学信息学(Medical Informatics)的 ...

  6. STL 迭代器适配器(iterator adapter)

    iterator adapter graph LR iterator --- reverse_iterator iterator --- Insert_iterator iterator --- io ...

  7. SpringBoot 整合 MybatisPlus (项目的创建及简单的单表查询)

    添加依赖 <!--mybatis-plus的springboot支持--> <dependency> <groupId>com.baomidou</group ...

  8. [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块)

    [BZOJ 2820] YY的gcd(莫比乌斯反演+数论分块) 题面 给定N, M,求\(1\leq x\leq N, 1\leq y\leq M\)且gcd(x, y)为质数的(x, y)有多少对. ...

  9. 洛谷P4391 [BOI2009]Radio Transmission 无线传输

    (https://www.luogu.org/problemnew/show/P4391) 题目描述 给你一个字符串,它是由某个字符串不断自我连接形成的. 但是这个字符串是不确定的,现在只想知道它的最 ...

  10. LeetCode107. 二叉树的层次遍历 II

    107. 二叉树的层次遍历 II 描述 给定一个二叉树,返回其节点值自底向上的层次遍历. (即按从叶子节点所在层到根节点所在的层,逐层从左向右遍历) 示例 例如,给定二叉树: [3,9,20,null ...