Let's first introduce several definitions and notations from the Linear Algebra.
The letter R will always denote the field of real numbers. A vector space over R is a set X, whose elements are called vectors, and in which
two operations, addition and scalar multiplication, are defined, with the following familiar algebraic properties:
(I) To every pair of vectors x and y corresponds a vector x+y, in such a way that
x + y = y + x and x + (y + z) = (x + y) + z;
X contains a unique vector 0 such that x + 0 = x for every x(x is a element of X); and to each x(x is a element of X) corresponds
a unique vector -x such that
x + (-x) = 0.
(II) To every pair (a,x) with a,x(a is a element of R and x is a element of X) corresponds a vector ax, in such a way that
1x = x, a(bx) = (ab)x,
and such that the two distributive laws
a(x + y) = ax + ay, (a+b)x = ax + bx
hold.
In a vector space V over the field R, the span of subspace by vectors v1 and v2 (v1 and v2 is elements of V) is defined as
Span (v1, v2) = { av1 +bv2 : a,b is elements of R} Note: v1,v2 is subscript of a,b
and every element of V representable in the form a_v1+b_v2 is called the linear combination of v1 and v2.
Fortunately, the specific case you are going to tackle is not as perplexing as what is mentioned above. Consider two positive integers a
and b which have no common factor other than 1 or -1. That is to say, a and b are relatively prime. Define the natural span of a and b as
NSpan (a, b) = {sa + tb : s and t range over non-negative integers}.
Now given two arbitrary positive integers m and n where m is less or equal than n, your job is to calculate the number of integers
between m and n inclusively that do NOT belong to NSpan (a, b).
输入格式
Several lines, each containing four positive integers a, b, m, n while 0 < a, b <=1000 and 0 < m, n < =2^31-1.
Input will be terminated by EOF.
输出格式
You should print the number of integers between m and n inclusively that do NOT belong to NSpan (a, b) on separate lines and
each line of output corresponds to each line of input.
样例输入
3 5 8 12 4 7 8 12 9 4 13 20
样例输出
0 2 3
/*
大于(a-1)*(b-1)的数一定可以用a,b的线性组合来表示。
证明:因为a和b互斥,对于任何大于(a-1)*(b-1)的数c,
一定存在一个k(0<=k<b),使a*k和c同余(mod b)所以c就可以表示为a*k+((c-a*k)/b)*b
*/
#include<stdio.h>
#include<stdbool.h>
int main(){
int a,b,m,n,p,i,j,sum;
bool flag;
while(scanf("%d %d %d %d",&a, &b, &m, &n)!=EOF)
{
sum=0;
if (n>(a-1)*(b-1))
p=(a-1)*(b-1);
else
p=n+1;
for(i=m;i<p;i++)
{
flag=false;
for(j=0;j<=i/b;j++)
if((i-j*b)%a==0)
{
flag=true;
break;
}
if(!flag)
sum++;
}
printf("%d\n",sum);
}
return 0;
}