loj #3144. 「APIO 2019」奇怪装置
很明显的是我们需要找到((x,y))的循环节的长度
当(t=0)时,(x=0,y=0)
当(t eq 0)时,仍然要使的(x=0,y=0)的话,必有
[egin{cases}
t+lfloor frac{t}{B}
floor equiv0(mod A)\
tequiv0(mod B)
end{cases}
]
记(t=t'B),则有(A|t'(B+1)),故(t')最小为(frac{A}{gcd(A,B+1)})
所以(t_{min}=frac{AB}{gcd(A,B+1)}),这就是循环节长度
把给出的区间取个模,变成区间并问题
注意一些特殊情况的特判以及取模之后每个区间是否需要分拆等问题即可解决此题
#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
",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;
}