题目链接:点击这里
题目大意:
给定
n
n
n 个整数
a
i
a_i
ai ,和
l
,
r
l,r
l,r 求
∑
i
=
1
n
a
i
x
i
=
n
(
n
∈
[
l
,
r
]
)
\sum_{i=1}^na_ix_i=n(n\in [l,r])
∑i=1naixi=n(n∈[l,r]) 有多少组非负整数解
题目分析:
同样是同余最短路的模型,还是套路的取
a
1
a_1
a1 作为
b
a
s
e
base
base ,然后建出
0
0
0 到
a
1
−
1
a_1-1
a1−1 这
a
1
a_1
a1 个点,然后对每个点连出
n
−
1
n-1
n−1 条边:连接
i
i
i 与
(
i
+
v
a
l
)
m
o
d
a
1
(i+val)\mod a_1
(i+val)moda1 边权为
v
a
l
val
val ,然后因为是求
[
l
,
r
]
[l,r]
[l,r] 范围的解数量,不难想到转换为求出
[
1
,
r
]
[1,r]
[1,r] 的解的数量再减去
[
1
,
l
−
1
]
[1,l-1]
[1,l−1] 的解的数量
具体细节见代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<set>
#include<map>
#define ll long long
#define inf 0x3f3f3f3f
#define Inf 0x3f3f3f3f3f3f3f3f
#define int ll
using namespace std;
int read()
{
int res = 0,flag = 1;
char ch = getchar();
while(ch<'0' || ch>'9')
{
if(ch == '-') flag = -1;
ch = getchar();
}
while(ch>='0' && ch<='9')
{
res = (res<<3)+(res<<1)+(ch^48);//res*10+ch-'0';
ch = getchar();
}
return res*flag;
}
const int maxn = 5e5+5;
const int maxm = 5005;
const int mod = 998244353;
const double pi = acos(-1);
const double eps = 1e-8;
struct Edge{
int nxt,to,val;
}edge[maxn*13];
int n,m,l,r,a[maxn],cnt,head[maxn],dis[maxn];
bool vis[maxn];
void addedge(int from,int to,int val)
{
edge[++cnt].nxt = head[from];
edge[cnt].to = to;
edge[cnt].val = val;
head[from] = cnt;
}
struct node{
int id,dis;
bool operator < (const node &b) const {
return dis > b.dis;
}
};
void dijkstra(int s)
{
memset(dis,0x3f,sizeof(dis));
dis[s] = 0;
priority_queue<node>qu;
qu.push({s,0});
while(!qu.empty())
{
int h = qu.top().id;qu.pop();
if(vis[h]) continue;
vis[h] = true;
for(int i = head[h];i;i = edge[i].nxt)
{
int to = edge[i].to,val = edge[i].val;
if(dis[to] > dis[h]+val)
{
dis[to] = dis[h]+val;
qu.push({to,dis[to]});
}
}
}
}
int solve(int x)
{
int res = 0;
for(int i = 0;i < a[1];i++)
if(dis[i] <= x) res += (x-dis[i])/a[1]+1;
return res;
}
signed main()
{
n = read(),l = read(),r = read();
for(int i = 1;i <= n;i++) a[i] = read();
for(int i = 0;i < a[1];i++)
for(int j = 2;j <= n;j++) addedge(i,(i+a[j])%a[1],a[j]);
dijkstra(0);
printf("%lld\n",solve(r)-solve(l-1));
return 0;
}