SCU - 4532
a1∗b1+a2∗b2...+an∗bn=B
给定 a1...an , Bmax 、 Bmin
问有多少个B, Bmin<=B<=Bmax ,
使得 b1...bn 存在非负整数解
首先问题可以转化为在区间 [0,Bmax] 有多少个 B满足条件
如果能解决这个,原问题的用 [0,Bmax] 的答案减去 [0,Bmin−1] 的即可
接下来有一个很关键的步骤,也是这题的神来之笔
之所以说神,是因为目前我还没深刻理解这么做的道理 (2016.7.14)
将等式两边 moda1 (a序列中的任意一个数)
这样使得 [0,Bmax] 中的数按 moda1 的余数划分成若干个等价类
于是题目就转化为了求每个等价类中最小的那个数
因为知道了最小的数 x ,那么这个等价类的大小就可以用 Bmax−xa1+1来计算
然后把每个等价类的答案累加起来即为结果
设 dp[m]=b ,即 B%a1=m 这个等价类中,最小的那个数是 b
初始条件 dp[0]=0,在当前数 u 的基础上不断增加 ai,去更新 dp[(u+ai)%a1] 的状态
转移方程为 for i=2..n:dp[(u+ai)%a1]=min(dp[u]+ai)
由于这个 dp过程可能有环,所以采用 SPFA的方式进行转移
#pragma comment(linker, "/STACK:102400000,102400000")
#include <cstdio>
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cctype>
#include <map>
#include <set>
#include <queue>
using namespace std;
typedef pair<int,int> Pii;
typedef long long LL;
typedef unsigned long long ULL;
typedef double DBL;
typedef long double LDBL;
#define MST(a,b) memset(a,b,sizeof(a))
#define CLR(a) MST(a,0)
#define Sqr(a) ((a)*(a))
const int maxn=, maxa=+;
const LL INF=;
int N;
LL bmin, bmax;
LL A[maxn];
LL dp[maxa];
bool inq[maxa];
LL Get(LL,LL,int);
int main()
{
#ifdef LOCAL
freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
int T;
scanf("%d", &T);
for(int ck=; ck<=T; ck++)
{
scanf("%d%lld%lld", &N, &bmin, &bmax);
for(int i=; i<=N; i++) scanf("%lld\n", &A[i]);
sort(A+, A++N);
if(A[]==)
{
puts("0");
continue;
}
MST(dp,);
CLR(inq);
dp[]=;
inq[]=;
queue<LL> que;
que.push();
while(que.size())
{
LL u=que.front(); que.pop();
for(int i=; i<=N; i++)
{
LL v = (u+A[i])%A[];
if(dp[v] > dp[u]+A[i])
{
dp[v] = dp[u]+A[i];
if(!inq[v])
{
inq[v]=;
que.push(v);
}
}
}
inq[u]=;
}
// for(int i=0; i<A[1]; i++) printf("%lld ", dp[i]); puts("");
LL ans=;
for(int i=; i<A[]; i++)
{
ans += Get(bmax,dp[i],A[]) - Get(bmin-,dp[i],A[]);
}
cout << ans << '\n';
}
return ;
}
LL Get(LL R, LL L, int r)
{
if(R<L) return ;
return (R-L)/r+;
}