# 同余最短路

2021-09-15 08:02:02  阅读：14  来源： 互联网

## 同余最短路

### 墨墨的等式

#include <cstdio>
#include <queue>
#include <vector>
#include <cstring>
#include <algorithm>
#define N  500009
#define ll long long
#define pb push_back
#define setIO(s) freopen(s".in","r",stdin)
using namespace std;
const ll inf=(ll)1e12;
queue<int>q;
int a[12],inq[N];
ll dis[N];
int main() {
// setIO("input");
int n ;
ll L, R;
scanf("%d%lld%lld",&n,&L,&R);
for(int i=1;i<=n;++i) scanf("%d",&a[i]);
sort(a+1,a+1+n);
int mx=a[1];
for(int i=0;i<mx;++i) dis[i]=inf;
dis[0]=0;
inq[0]=1;
q.push(0);
while(!q.empty()) {
int u=q.front(); q.pop();
inq[u]=0;
for(int i=1;i<=n;++i) {
int v = (u + a[i]) % mx;
if(dis[v] > dis[u] + a[i])  {
dis[v] = dis[u] + a[i];
if(!inq[v]) {
q.push(v);
inq[v] = 1;
}
}
}
}
for(int i=0;i<mx;++i) {
if(dis[i] == inf) continue;
dis[i] -= i;
dis[i] /= mx;
}
ll ans=0;
for(int i=0;i<mx;++i) {
ll pl = (L - 1 - i) / mx;
ll pr = (R - i) / mx;
ll t1 = max(0ll, pl - dis[i] + 1);
ll t2 = max(0ll, pr - dis[i] + 1);
ans += t2 - t1;
}
printf("%lld\n", ans);
return 0;
}