Bzoj2118: 墨墨的等式
题面
Sol
和这道题很像Luogu跳楼机
找到最小的那个\(a[i]\)设为\(mn\)
\(SPFA\)算出得到每个值为\(i\)的是否能凑出来,设为\(f[i]\)
实际上\(i\)变成\(i\%mn\),所有的这一类都可以通过\(mn\)这个桥梁得到
那么直接求到\(i\%mn\)就好了
最后统计\([0, B_{max}]\)时就是所有的
\[\lfloor\frac{B_{max}-f[i]}{mn}\rfloor+1
\]
减去\(B_{min}\)的就好了
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
const int _(5e5 + 5);
typedef long long ll;
IL ll Input(){
RG ll x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int a[13], first[_], cnt, vis[_], mn = 2e9, n;
ll L, R, f[_];
struct Edge{
int to, next, w;
} edge[_ * 15];
queue <int> Q;
IL void Add(RG int u, RG int v, RG int w){
edge[cnt] = (Edge){v, first[u], w}, first[u] = cnt++;
}
IL ll Calc(RG ll x){
RG ll ret = 0;
for(RG int i = 0; i < mn; ++i) if(f[i] <= x) ret += (x - f[i]) / mn + 1;
return ret;
}
int main(RG int argc, RG char* argv[]){
n = Input(), L = Input(), R = Input();
for(RG int i = 1; i <= n; ++i) a[i] = Input(), mn = min(mn, a[i]);
for(RG int i = 0; i < mn; ++i) first[i] = -1;
for(RG int i = 0; i < mn; ++i)
for(RG int j = 1; j <= n; ++j) Add(i, (i + a[j]) % mn, a[j]);
Fill(f, 127), f[0] = 0, vis[0] = 1, Q.push(0);
while(!Q.empty()){
RG int u = Q.front(); Q.pop();
for(RG int e = first[u]; e != -1; e = edge[e].next){
RG int v = edge[e].to, w = edge[e].w;
if(f[u] + w < f[v]){
f[v] = f[u] + w;
if(!vis[v]) vis[v] = 1, Q.push(v);
}
}
vis[u] = 0;
}
return printf("%lld\n", Calc(R) - Calc(L - 1)), 0;
}