Description
墨墨突然对等式很感兴趣,他正在研究a1x1+a2y2+…+anxn=B存在非负整数解的条件,他要求你编写一个程序,给定N、{an}、以及B的取值范围,求出有多少B可以使等式存在非负整数解。
对于100%的数据,N≤12,0≤ai≤5*105,1≤BMin≤BMax≤1012。
Solution
考虑简化剩余系,若能够组成一个x,则x+mn也可以
我们最短路求出dis[x]表示组成%mn余x的最小值,考虑算出[1,R]的答案到[1,L-1]的答案
a
n
s
(
x
)
=
∑
i
=
0
m
n
−
1
⌊
x
−
d
i
s
[
i
]
m
n
⌋
+
1
ans(x)=\sum_{i=0}^{mn-1} \lfloor\frac{x-dis[i]}{mn}\rfloor+1
ans(x)=∑i=0mn−1⌊mnx−dis[i]⌋+1
Code
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <queue>
#define rep(i,st,ed) for (int i=st;i<=ed;++i)
#define fill(x,t) memset(x,t,sizeof(x))
typedef long long LL;
const LL INF=1e15;
const int N=500005;
const int E=5200005;
struct edge {int y; LL w; int next;} e[E];
LL dis[N],a[N];
int ls[N],edCnt;
bool vis[N];
int read() {
int x=0,v=1; char ch=getchar();
for (;ch<'0'||ch>'9';v=(ch=='-')?(-1):(v),ch=getchar());
for (;ch<='9'&&ch>='0';x=x*10+ch-'0',ch=getchar());
return x*v;
}
void add_edge(int x,LL y,LL w) {
e[++edCnt]=(edge) {(int)y,w,ls[x]}; ls[x]=edCnt;
}
void spfa(int st) {
std:: queue <int> que;
que.push(st); vis[st]=true;
rep(i,0,N-1) dis[i]=INF; dis[st]=0;
for (;!que.empty();) {
int now=que.front(); que.pop();
for (int i=ls[now];i;i=e[i].next) {
if (dis[now]+e[i].w<dis[e[i].y]) {
dis[e[i].y]=dis[now]+e[i].w;
if (!vis[e[i].y]) {
vis[e[i].y]=true;
que.push(e[i].y);
}
}
} vis[now]=false;
}
}
LL calc(LL x,int mn) {
LL res=0;
rep(i,0,mn-1) if (dis[i]<=x) {
res+=((x-dis[i])/mn)+1;
}
return res;
}
int main(void) {
freopen("data.in","r",stdin);
freopen("myp.out","w",stdout);
int n; LL L,R; scanf("%d%lld%lld",&n,&L,&R);
LL mn=INF;
rep(i,1,n) {
scanf("%lld",&a[i]);
mn=std:: min(mn,a[i]);
}
rep(i,0,mn-1) rep(j,1,n) {
add_edge(i,(1LL*i+a[j])%mn,a[j]);
}
spfa(0); dis[0]=mn;
printf("%lld\n", calc(R,mn)-calc(L-1,mn));
return 0;
}