[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 给出 ...
随机推荐
- 打造自己的MyLifeOrganized 2(MLO2)云同步
0x01 官方云同步,付费也很卡 MyLifeOrganized(MLO)是Windows平台下强大的GTD软件,PC版本和Android版本需要分别购买授权,云同步还要再买包月或包年服务真不便宜,关 ...
- 怎样将SQL Azure数据库备份到本地或者Storage
怎样备份SQL Azure数据库到本地或者云存储Storage,可以使用SQL Database Import Export 的功能. 具体操作如下: 用SSMS链接SQL Azure数据库 注意:服 ...
- setTimeout 0秒
我们通常知道常用setTimeout 0秒来解决动画或者一些效果的延迟问题:众所周知js是单线程,用0秒能把要执行的任务从队列中提出来.其实我也不太懂 有这个问题alert(1);setTimeout ...
- js字符串函数(split、join、indexOf、substring)
1,函数:split()功能:使用一个指定的分隔符把一个字符串分割存储到数组示例: str="jpg|bmp|gif|ico|png";arr= str .split(" ...
- 一头扎进EasyUI2
惯例广告一发,对于初学真,真的很有用www.java1234.com,去试试吧! 一头扎进EasyUI第6讲 .日历组件 <div class="easyui-calendar&quo ...
- eclipse&android的环境搭建
这次我选择使用Android来完成这次软件工程实践,不过配置eclipse和android环境真是个麻烦事. 因为之前有用过eclipse,对其比较熟悉,于是就放弃了android studio这个工 ...
- java定时器的使用(Timer)
1.在应用开发中,经常需要一些周期性的操作,比如每5分钟执行某一操作等. 对于这样的操作最方便.高效的实现方式就是使用java.util.Timer工具类. private java.util.Tim ...
- iOS边练边学--文件压缩和解压缩的第三方框架SSZipArchive的简单使用
一.非cocoaPods方法,需要注意的是:直接将SSZipArchive拖入项目编译会报错. Undefined symbols for architecture x86_64: "_cr ...
- .net架构设计读书笔记--第二章 第7节 神化般的业务层
一.编排业务逻辑的模式1. 事务脚本模式TS(The Transaction Script pattern ) TS模式概述 TS 鼓励你跳过任何的面向对象的设计,你直接到所需的用户操作的业务 ...
- Java-人民币转成大写
/** * 人民币转成大写 hangeToBig * * @param value * @return String */ public static String 人民币转成大写(double va ...