P2371 [国家集训队]墨墨的等式 同余最短路

传送门

题意:
在这里插入图片描述
思路:
一个同于最短路的板子题,初始的时候值为0,所以设 d i s [ 0 ] = 0 dis[0]=0 dis[0]=0,让后选择一个最小的 a [ i ] a[i] a[i]作为 b a s e base base,跑一遍同余最短路就好啦。跑完 d i s [ i ] dis[i] dis[i]表示在模 b a s e base base的意义下能到达 i i i的最小数。让后问的是区间里的,转化为 [ 0 , r ] − [ 0 , l − 1 ] [0,r]-[0,l-1] [0,r][0,l1]就好啦。

//#pragma GCC optimize(2)
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std;

//void rd_cre() { freopen("d://dp//data.txt","w",stdout); srand(time(NULL)); }
//void rd_ac() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//AC.txt","w",stdout); }
//void rd_wa() { freopen("d://dp//data.txt","r",stdin); freopen("d://dp//WA.txt","w",stdout); }

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int N=1000010,mod=1e9+7,INF=0x3f3f3f3f;
const double eps=1e-6;

int n; LL l,r;
int a[N];
LL dis[N];
bool st[N];

void dijkstra()
{
    
    
    memset(dis,INF,sizeof(dis));
    queue<int>q; q.push(0);
    dis[0]=0;
    while(q.size())
    {
    
    
        int u=q.front(); q.pop(); st[u]=false;
        for(int i=1;i<=n;i++)
        {
    
    
            int now=(a[i]+u)%a[1];
            if(dis[now]>dis[u]+a[i])
            {
    
    
                dis[now]=dis[u]+a[i];
                if(!st[now]) q.push(now),st[now]=true;
            }
        }
    }
}

LL get(LL x)
{
    
    
    LL sum=0;
    for(int i=0;i<a[1];i++) if(dis[i]<=x) sum+=(x-dis[i])/a[1]+1;
    return sum;
}

int main()
{
    
    
//	ios::sync_with_stdio(false);
//	cin.tie(0);

    scanf("%d%lld%lld",&n,&l,&r);
    for(int i=1;i<=n;i++) scanf("%d",&a[i]);
    dijkstra();
    printf("%lld\n",get(r)-get(l-1));




	return 0;
}
/*

*/


猜你喜欢

转载自blog.csdn.net/m0_51068403/article/details/114549954