[Educational Codeforces Round 16]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions
试题描述
You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such that L ≤ x ≤ R andx = a1k' + b1 = a2l' + b2, for some integers k', l' ≥ 0.
输入
The first line contains integer n (1 ≤ n ≤ 3·105) — the number of points on the line.
The second line contains n integers xi ( - 109 ≤ xi ≤ 109) — the coordinates of the given n points.
输出
输入示例
输出示例
数据规模及约定
解一下不定方程 a1k + b1 = a2l + b2,设 k mod lcm(a1, a2) / a1 的值是 t,设 lcm(a1, a2) / a1 = A,那么 k 可以写成 q·A + t 这个样子,那么显然 A 是有上下界的,我们二分到这个上下界,做个差就是答案了。
一上午就调它了。。。woc cf 数据太强了
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <stack>
#include <vector>
#include <queue>
#include <cstring>
#include <string>
#include <map>
#include <set>
using namespace std; #define MAXN 1000+10 #define oo 4000000000ll
#define LL long long
#define LD long double
LL a1, b1, a2, b2, L, R; LL gcd(LL a, LL b, LL& x, LL& y) {
if(b == 0){ x = 1, y = 0; return a; }
LL d = gcd(b, a % b, y, x); y -= (a / b) * x;
return d;
} LL gcd(LL a, LL b) { return b == 0 ? a : gcd(b, a % b); } int main() {
cin >> a1 >> b1 >> a2 >> b2 >> L >> R; LL k, t;
LL d = gcd(a1, a2, k, t);
if((b2 - b1) % d != 0) return puts("0"), 0;
k *= (b2 - b1) / d; t *= (b2 - b1) / d;
LL A2 = a2 / gcd(a1, a2);
LL mod = (k % A2 + A2) % A2, al, ar;
// printf("%lld %lld\n", k, mod);
LL l, r; l = -oo - 1; r = oo + 1;
// printf("%lld %lld\n", l, r);
while(l < r) {
LL mid = l + (r - l) / 2;
LL lsid = (L - b1) % a1 != 0 ? (L - b1) / a1 + (L - b1 > 0 ? 1 : 0) : (L - b1) / a1,
rsid = (R - b1) % a1 != 0 ? (R - b1) / a1 + (R - b1 > 0 ? 0 : -1) : (R - b1) / a1,
x = mid * A2 + mod;
LL l2 = (L - b2) % a2 != 0 ? (L - b2) / a2 + (L - b2 > 0 ? 1 : 0) : (L - b2) / a2,
r2 = (R - b2) % a2 != 0 ? (R - b2) / a2 + (R - b2 > 0 ? 0 : -1) : (R - b2) / a2,
y = ((LD)a1 * x - b2 + b1) / a2;
// printf("%lld %lld %lld %lld %lld [%lld, %lld]\n", lsid, l2, mid, y, x, l, r);
if(lsid <= x && l2 <= y && x >= 0 && y >= 0) r = mid;
else l = mid + 1;
}
al = l;
l = -oo - 1; r = oo + 1;
// printf("%lld %lld\n", l, r);1 -2000000000 2 2000000000 -2000000000 2000000000
while(l < r - 1) {
LL mid = l + (r - l) / 2;
LL lsid = (L - b1) % a1 != 0 ? (L - b1) / a1 + (L - b1 > 0 ? 1 : 0) : (L - b1) / a1,
rsid = (R - b1) % a1 != 0 ? (R - b1) / a1 + (R - b1 > 0 ? 0 : -1) : (R - b1) / a1,
x = mid * A2 + mod;
LL l2 = (L - b2) % a2 != 0 ? (L - b2) / a2 + (L - b2 > 0 ? 1 : 0) : (L - b2) / a2,
r2 = (R - b2) % a2 != 0 ? (R - b2) / a2 + (R - b2 > 0 ? 0 : -1) : (R - b2) / a2,
y = ((LD)a1 * x - b2 + b1) / a2;
// printf("%lld %lld %lld %lld %lld [%lld, %lld]\n", mid, x, y, rsid, r2, l, r);
if(x <= rsid && y <= r2) l = mid;
else r = mid;
}
ar = l;
// printf("%lld %lld\n", al, ar); LL mid = l + (r - l) / 2;
LL lsid = (L - b1) % a1 != 0 ? (L - b1) / a1 + (L - b1 > 0 ? 1 : 0) : (L - b1) / a1,
rsid = (R - b1) % a1 != 0 ? (R - b1) / a1 + (R - b1 > 0 ? 0 : -1) : (R - b1) / a1,
x = mid * A2 + mod;
LL l2 = (L - b2) % a2 != 0 ? (L - b2) / a2 + (L - b2 > 0 ? 1 : 0) : (L - b2) / a2,
r2 = (R - b2) % a2 != 0 ? (R - b2) / a2 + (R - b2 > 0 ? 0 : -1) : (R - b2) / a2,
y = ((LD)a1 * x - b2 + b1) / a2;
if(lsid <= x && x <= rsid && l2 <= y && y <= r2 && x >= 0 && y >= 0 && al <= ar)
cout << ar - al + 1 << endl;
else puts("0"); return 0;
}
[Educational Codeforces Round 16]D. Two Arithmetic Progressions的更多相关文章
- Educational Codeforces Round 16 D. Two Arithmetic Progressions (不互质中国剩余定理)
Two Arithmetic Progressions 题目链接: http://codeforces.com/contest/710/problem/D Description You are gi ...
- [Educational Codeforces Round 16]E. Generate a String
[Educational Codeforces Round 16]E. Generate a String 试题描述 zscoder wants to generate an input file f ...
- [Educational Codeforces Round 16]C. Magic Odd Square
[Educational Codeforces Round 16]C. Magic Odd Square 试题描述 Find an n × n matrix with different number ...
- [Educational Codeforces Round 16]B. Optimal Point on a Line
[Educational Codeforces Round 16]B. Optimal Point on a Line 试题描述 You are given n points on a line wi ...
- [Educational Codeforces Round 16]A. King Moves
[Educational Codeforces Round 16]A. King Moves 试题描述 The only king stands on the standard chess board ...
- Educational Codeforces Round 16 E. Generate a String dp
题目链接: http://codeforces.com/problemset/problem/710/E E. Generate a String time limit per test 2 seco ...
- Educational Codeforces Round 16 E. Generate a String (DP)
Generate a String 题目链接: http://codeforces.com/contest/710/problem/E Description zscoder wants to gen ...
- Educational Codeforces Round 16
A. King Moves water.= =. #include <cstdio> ,,,,,-,-,-}; ,-,,,-,,,-,}; #define judge(x,y) x > ...
- Educational Codeforces Round 16 A B C E
做题太久也有点累了..难题不愿做 水题不愿敲..床上一躺一下午..离下一场div2还有点时间 正好有edu的不计分场 就做了一下玩玩了 D是个数学题 F是个AC自动机 都没看明白 留待以后补 A 给出 ...
随机推荐
- 【niubi-job——一个分布式的任务调度框架】----niubi-job这下更牛逼了!
niubi-job迎来第一次重大优化 niubi-job是一款专门针对定时任务所设计的分布式任务调度框架,它可以进行动态发布任务,并且有超高的可用性保证. 有多少人半夜被叫起来查BUG,结果差到最后发 ...
- 18.C#扩展方法(十章10.1-10.2)
今天的话题,我们来聊下扩展方法,自己也真心感叹自己的文笔,那叫一个惨啊,回顾写的文章,看着看着也忘记当时是怀着什么心态写的,哈哈,现代人真心是太随性了,可能也是太冷漠了,接着写的吧,总是会有帮助,也会 ...
- HTML5——单次定位请求
单次定位请求及点击一次只发出一次请求 下面是个获取经纬度的简单Demo 简要截图如下: 简要代码如下: <!DOCTYPE html> <html> <head> ...
- 'UserInfoBLL' node cannot be resolved for the specified context [MVC展示数据.Controllers.LoginController]问题解决
我们在配置Spring.Net时,往往会提示找不到,然而看了看都对着呢?那么问题出在了哪? 问题呈现: 进行如下修改,将名字随便取个,不为BLL方法名字即可,我这里添加了一个1,注意这里改了,控制器里 ...
- java通过地址获取主机名
关键代码: try { String str=Chat.getJt().getText().toString();//获取输入内容 String[] ipstr=str.split("[.] ...
- MySQL性能分析
第一步 检查系统的状态 通过操作系统的一些工具检查系统的状态,比如CPU.内存.交换.磁盘的利用率,根据经验或与系统正常时的状态相比对,有时系统表面上看起来看空闲,这也可能不是一个正常的状态,因为cp ...
- 关于opacity透明度子元素继承现象的若干研究以及hack方法
[感想]信息时代的信息是有时效性的,今天是确确实实感受到了.互联网资料虽然丰富,但是质量不一,还有大量的跟风雷同,很多人都是随手拷贝过来,根本没有实践.以前端为例,这两年浏览器的迅猛发展,造成很多原有 ...
- Java基础-关键字-String
1.String的本质 线程安全 打开String的源码,类注释中有这么一段话“Strings are constant; their values cannot be changed after t ...
- yii2URL美化
yii2的url 域名/index.php?r=site%2Findex 实际为 域名/index.php?r=site/index 可以美化下 可以在main.php中配置 'components' ...
- Hibernate中一级缓存和二级缓存使用详解
一.一级缓存二级缓存的概念解释 (1)一级缓存就是Session级别的缓存,一个Session做了一个查询操作,它会把这个操作的结果放在一级缓存中,如果短时间内这个 session(一定要同一个ses ...