loj # 3144. "APIO 2019" strange device
It is clear that we need to find \ ((x, y) \ ) the length of the loop section
When the \ (t = 0 \) when, \ (X = 0, Y = 0 \)
When the \ (t \ neq 0 \) , the still make the \ (x = 0, y = 0 \) , then there must be
\ [\ begin {cases} t + \ lfloor \ frac {t} {B} \ rfloor \ equiv0 (mod \ A) \\ t \ equiv0 (mod \ B) \ end {cases} \]
Note \ (T = t'B \) , there \ (A | T '(B +. 1) \) , so \ (t' \) minimum \ (\ frac {A} { gcd (A, B + 1)}\)
So \ (T_ {} min = \ {AB} {FRAC GCD (A, B +. 1)} \) , which is the length of the loop section
To take a given mold section, and the problem becomes interval
Special attention to special circumstances and sentenced to die after taking each interval whether the spin-off and other issues need to solve this problem
#include<iostream>
#include<string.h>
#include<string>
#include<stdio.h>
#include<algorithm>
#include<vector>
#include<math.h>
#include<queue>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
typedef long double db;
const int N=10000;
const db pi=acos(-1.0);
#define lowbit(x) (x)&(-x)
#define sqr(x) (x)*(x)
#define rep(i,a,b) for (register int i=a;i<=b;i++)
#define per(i,a,b) for (register int i=a;i>=b;i--)
#define fir first
#define sec second
#define mp(a,b) make_pair(a,b)
#define pb(a) push_back(a)
#define maxd 998244353
#define eps 1e-8
struct node{
ll l,r;
}seg[1001000],x[2002000];
int n,tot=0;
ll a,b,tim;
ll read()
{
ll x=0;int f=1;char ch=getchar();
while ((ch<'0') || (ch>'9')) {if (ch=='-') f=-1;ch=getchar();}
while ((ch>='0') && (ch<='9')) {x=x*10+(ch-'0');ch=getchar();}
return x*f;
}
void insert(ll l,ll r)
{
ll tmp1=l/tim,tmp2=r/tim;
if (tmp1==tmp2) x[++tot]=(node){l%tim,r%tim};
else
{
x[++tot]=(node){l%tim,tim-1};
x[++tot]=(node){0,r%tim};
}
}
bool cmp(node p,node q) {return p.l<q.l;}
int main()
{
n=read();a=read();b=read();
ll maxr=0;
rep(i,1,n) {seg[i].l=read();seg[i].r=read();maxr=max(maxr,seg[i].r);}
ll d=__gcd(a,b+1);
if ((db)1.0*a/d*b>maxr)
{
ll ans=0;
rep(i,1,n) ans+=(seg[i].r-seg[i].l+1);
printf("%lld",ans);
return 0;
}
tim=a/d*b;
rep(i,1,n)
{
ll len=seg[i].r-seg[i].l+1;
if (len>=tim) {printf("%lld\n",tim);return 0;}
insert(seg[i].l,seg[i].r);
}
sort(x+1,x+1+tot,cmp);
ll l=x[1].l,r=x[1].r,ans=0;
rep(i,2,tot)
{
if (x[i].l>r) {ans+=(r-l+1);l=x[i].l;r=x[i].r;}
else
{
l=min(x[i].l,l);r=max(x[i].r,r);
}
}
ans+=(r-l+1);
printf("%lld",ans);
return 0;
}