求助:被Subtask #1卡掉了
查看原帖
求助:被Subtask #1卡掉了
373530
Reply_楼主2024/7/26 08:56

rt

#include<bits/stdc++.h>
#define int long long
#define ll long long 
#define R register
#define F(i,a,b) for(int i = (a);i<=(b);i++)
using namespace std;
inline int read(){R int x=0,t=1;R char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-') t=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*t;}
const int M=1e3+10,N=5e6+10;
int n,l,r;
int a[M],dis[N];
struct Node
{
	int id,dis;
};
bool operator<(const Node &x,const Node &y)
{
	return x.dis>y.dis;
}
priority_queue<Node>q;
vector<int>g[N],w[N];
bool vis[N];
inline void add(int ui,int vi,int wi)
{
	g[ui].push_back(vi);
	w[ui].push_back(wi);
}
signed main()
{
	n=read(),l=read(),r=read();
	if(r<l){
		cout  << 0;
		return 0;
	}
	l--;
	F(i,1,n){
		a[i]=read();
		if(a[i]==1){
			cout  << r-l;
			return 0;;
		}
	}
	sort(a+1,a+1+n); 
	for(int i = 0;i<a[1];i++){
		for(int j = 2;j<=n;j++){
			add(i,(i+a[j])%a[1],a[j]);
		}
	}
	memset(dis,0x3f3f3f3f,sizeof dis);
	q.push({0,0});
	dis[0]=0;
	while(q.size()){
		int u=q.top().id;
		q.pop();
		if(vis[u]) continue; 
		vis[u]=1;
		for(int i = 0;i<g[u].size();i++){
			int v=g[u][i];
			if(dis[v]>dis[u]+w[u][i]){
				dis[v]=dis[u]+w[u][i];
				q.push({v,dis[v]});
			}
		}
	}
	ll ans2=0;
	for(int i = 0;i<a[1];i++){
		if(dis[i]<=l){
			ans2-=1ll*(l-dis[i])/a[1]+1;
		}
		if(dis[i]<=r){
			ans2+=1ll*(r-dis[i])/a[1]+1;
		}
	}
	cout << ans2;
	return 0;
}
//0 3
//5 6 8 9 10

2024/7/26 08:56
加载中...