luoguP5444 [APIO2019]奇怪装置 数论+贪心

本题的关键在于求循环节.  

如果能想到从循环节入手的话这道题还是比较友好的.    

code: 

#include <cstdio> 
#include <cstring> 
#include <algorithm>  
#include <cmath>   
#define ll long long  
#define N 1000009 
#define setIO(s) freopen(s".in","r",stdin) 
using namespace std;    
int n;   
ll A,B,G,L[N],R[N];  
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a; } 
struct line 
{
    ll l,r;      
    line(ll l=0,ll r=0):l(l),r(r){}  
    bool operator<(const line &b) const  { return l<b.l; }
}s[N<<1];   
int main() 
{ 
    // setIO("input");  
    int i,j,tot=0; 
    scanf("%d%lld%lld",&n,&A,&B);    
    G=A/gcd(A,B+1)*B;        
    for(i=1;i<=n;++i) 
    { 
        scanf("%lld%lld",&L[i],&R[i]);                                
        if(L[i]-R[i]+1>=G) { printf("%lld\n",G); return 0; }   
        else 
        {
            L[i]%=G,R[i]%=G;    
            if(L[i]<=R[i]) s[++tot]=line(L[i],R[i]);      
            else s[++tot]=line(0,R[i]),s[++tot]=line(L[i],G-1);     
        }
    }     
    sort(s+1,s+1+tot);   
    ll ans=0;  
    for(i=1;i<=tot;i=j) 
    {                             
        ll nr=s[i].r;             
        for(j=i;j<=tot&&s[j].l<=nr;++j) nr=max(nr,s[j].r);     
        ans+=nr-s[i].l+1;     
    }
    printf("%lld\n",ans);  
    return 0;
}

  

猜你喜欢

转载自www.cnblogs.com/guangheli/p/12310773.html
今日推荐