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;
}

  

时间: 2024-10-02 00:08:02

Codeforces 710 D. Two Arithmetic Progressions的相关文章

poj 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions

Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theore

(素数求解)I - Dirichlet&#39;s Theorem on Arithmetic Progressions(1.5.5)

Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a 

POJ 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions 素数 难度:0

http://poj.org/problem?id=3006 #include <cstdio> using namespace std; bool pm[1000002]; bool usd[1000002]; bool judge(int x) { if(usd[x])return pm[x]; usd[x] = true; if(x == 2) return pm[x] = true; if(((x & 1) == 0) || (x < 2))return pm[x] =

洛谷P1214 [USACO1.4]等差数列 Arithmetic Progressions

P1214 [USACO1.4]等差数列 Arithmetic Progressions• o 156通过o 463提交• 题目提供者该用户不存在• 标签USACO• 难度普及+/提高 提交 讨论 题解 最新讨论• 这道题有问题• 怎么进一步优化时间效率啊 …题目描述一个等差数列是一个能表示成a, a+b, a+2b,..., a+nb (n=0,1,2,3,...)的数列.在这个问题中a是一个非负的整数,b是正整数.写一个程序来找出在双平方数集合(双平方数集合是所有能表示成p的平方 + q的平

USACO 1.4 Arithmetic Progressions

Arithmetic Progressions An arithmetic progression is a sequence of the form a, a+b, a+2b, ..., a+nb where n=0,1,2,3,... . For this problem, a is a non-negative integer and b is a positive integer. Write a program that finds all arithmetic progression

poj3006 Dirichlet&#39;s Theorem on Arithmetic Progressions

Dirichlet's Theorem on Arithmetic Progressions Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 16333   Accepted: 8205 Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing

【POJ3006】Dirichlet&#39;s Theorem on Arithmetic Progressions(素数筛法)

简单的暴力筛法就可. 1 #include <iostream> 2 #include <cstring> 3 #include <cmath> 4 #include <cctype> 5 #include <cstdio> 6 #include <cmath> 7 #include <algorithm> 8 #include <numeric> 9 using namespace std; 10 11 co

POJ 3006 Dirichlet&#39;s Theorem on Arithmetic Progressions 快筛质数

题目大意:给出一个等差数列,问这个等差数列的第n个素数是什么. 思路:这题主要考如何筛素数,线性筛.详见代码. CODE: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 1000010 using namespace std; int prime[MAX],primes; bool notp[MAX]; int a,d,n;

判断素数——Dirichlet&#39;s Theorem on Arithmetic Progressions

Description If a and d are relatively prime positive integers, the arithmetic sequence beginning with a and increasing by d, i.e., a, a + d, a + 2d, a + 3d, a + 4d, ..., contains infinitely many prime numbers. This fact is known as Dirichlet's Theore