Codeforces 710 D. Two Arithmetic Progressions
Description
\(x=a_1k+b_1=a_2l+b_2,L\leqslant x \leqslant R\) 求满足这样条件的 \(x\) 的个数.
Sol
扩展欧几里得+中国剩余定理.
发现这个相当于一个线性方程组.
\(x \equiv b_1(mod a_1)\)
\(x \equiv b_2(mod a_2)\)
将原来两式相减得到 \(a_1k-a_2l=b_2-b_1\)
这个用扩展欧几里得求一下,如果 \((a_1,a_2)\nmid (b_2-b_1)\) 显然无解.
用扩展欧几里得求的方程是 \(a_1k-a_2l=(a_1,a_2)\) ,将这个等式再乘上 \(\frac{b_2-b_1}{(a_1,a_2)}\)
现在我们得到了一组合法解,通解就是 \(k=k_0+\frac {a_2}{(a_1,a_2)},l=l_0-\frac {a_1}{(a_1,a_2)}\)
求得最小正数解可以对 \(\frac {a_2}{(a_1,a_2)}\) 取模.
然后原方程的解个数就是 \(k+n[a_1,a_2]\) ,不要忘记计算端点的这个值.
Code
#include<cstdio>
#include<algorithm>
#include<iostream>
using namespace std; typedef long long LL;
#define debug(a) cout<<#a<<"="<<a<<" " LL a1,b1,a2,b2,k,l,x,lcm,gcd,L,R,ans; void Exgcd(LL a,LL b,LL &x,LL &y){
if(!b){ x=1,y=0;return; }
Exgcd(b,a%b,x,y);
LL t=x;x=y,y=t-(a/b)*y;
}
int main(){
ios::sync_with_stdio(false);
cin>>a1>>b1>>a2>>b2>>L>>R;
Exgcd(a1,a2,k,l);
gcd=__gcd(a1,a2),lcm=a1/gcd*a2;
L=max(L,max(b1,b2));
if((b2-b1)%gcd || L>R) return puts("0"),0;
k*=(b2-b1)/gcd,k=(k%(a2/gcd)+a2/gcd)%(a2/gcd);
x=a1*k+b1;
// debug(x),debug(lcm),debug(L),debug(R);
if(R>=x) ans+=(R-x)/lcm+1;
if(L-1>=x) ans-=(L-1-x)/lcm+1;
cout<<ans<<endl;
return 0;
}
Codeforces 710 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]D. Two Arithmetic Progressions
[Educational Codeforces Round 16]D. Two Arithmetic Progressions 试题描述 You are given two arithmetic pr ...
- E - Two Arithmetic Progressions(CodeForces - 710D)(拓展中国剩余定理)
You are given two arithmetic progressions: a1k + b1 and a2l + b2. Find the number of integers x such ...
- Dirichlet's Theorem on Arithmetic Progressions 分类: POJ 2015-06-12 21:07 7人阅读 评论(0) 收藏
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- 洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions
P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题 ...
- POJ 3006 Dirichlet's Theorem on Arithmetic Progressions (素数)
Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS Memory Limit: 65536K Total Submi ...
- poj 3006 Dirichlet's Theorem on Arithmetic Progressions【素数问题】
题目地址:http://poj.org/problem?id=3006 刷了好多水题,来找回状态...... Dirichlet's Theorem on Arithmetic Progression ...
- (素数求解)I - Dirichlet's Theorem on Arithmetic Progressions(1.5.5)
Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%I64d & %I64u Submit cid=1006#sta ...
- USACO 1.4 Arithmetic Progressions
Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb ...
随机推荐
- centos设置编码
CentOS6.5解决中文乱码与设置字符集 Windows的默认编码为GBK,Linux的默认编码为UTF-8.在Windows下编辑的中文,在Linux下显示为乱码.为了解决此问题,修改Linux的 ...
- 《深入理解bootstrap》读书笔记:第4章 CSS组件(上)
bootstrap三大核心之二. 包括glyphicon图标,下拉菜单(dropdown),按钮组(button group)....等等. 一. 图标字体 bootstrap3提供了200多个免费图 ...
- 网页js,DIV全屏布局
<script type="text/javascript"> $(document).ready(function(){ findDimensions(); }); ...
- Array subscript is not an integer
字典的字母写成大写了,也不能查成出来,没有报没有这个字典,反而报这个错……找了好久
- Robot Framework--07 变量的声明、赋值及其使用
转自:http://blog.csdn.net/tulituqi/article/details/7984642 一.变量的声明 1.变量标识符 每个变量都可以用 变量标识符{变量名} 来进行 ...
- redis+Keepalived实现Redis主从复制
redis+Keepalived实现Redis主从复制: 环境:CentOs6.5Master: 10.10.10.203Slave: 10.10.10.204Virtural IP Addres ...
- sqlmap的一些小技巧
前言 很多人都使用sqlmap来进行SQL注入测试,但是很多人只是简简单单的current-user,current-db,-D,-T,--dump这样子来做,其实sqlmap还有很多很强大的功能,这 ...
- MVC缓存OutPutCache学习笔记 (二) 缓存及时化VaryByCustom
<MVC缓存OutPutCache学习笔记 (一) 参数配置> 本篇来介绍如何使用 VaryByCustom参数来实现缓存的及时化.. 根据数据改变来及时使客户端缓存过期并更新.. 首先更 ...
- tinyfox for linux 独立版 fox.sh
#!/bin/sh export LANG="zh_CN.UTF-8" # export MONO_IOMAP="all" # export MONO_EXTE ...
- JavaScript基础整理(2)
接下来的重点是函数.我们知道函数是特殊的对象. 函数作用域和声明提前.JavaScript中没有块级作用域,只有函数作用域:变量在声明它们的函数体以及这个函数体嵌套的任意 函数体内都要定义. func ...