跳到正文

个人记录

同余最短路

gala_xk约 8 分钟
原文

这是 2025年10月3日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 8 分钟

版本

  1. v2当前
  2. v1

用法

应用范围其实很狭窄。

  1. 给定 nn 个整数,求这 nn 个整数能拼凑出多少的其他整数(nn 个整数可以重复取)
  2. 给定 nn 个整数,求这 nn 个整数不能拼凑出的最小(大)的整数
  3. 给定 nn 个整数,至少要拼几次才能拼出模 pp 余 kk 的数」

算法

点击查看

我们先推一下柿子:

a1x1+a2x2+⋯+an+1xn+1=c  ⟺  a1x1+a2x2+⋯+anxn≡c(mod  an+1)a_1x_1+a_2x_2+\dots+a_{n+1}x_{n+1}=c \iff a_1x_1+a_2x_2+\dots+a_nx_n\equiv c(\mod a_{n+1})

我们建 an+1a_{n+1} 个点,每一个 ii 连 i→i+ajmod  an+1i\to i+a_j \mod a_{n+1} 的边权为 ⌊i+ajan+1⌋\lfloor\frac{i+a_j}{a_{n+1}}\rfloor,00 为起点跑最短路即可。

例题

P2371 [国家集训队] 墨墨的等式

题解

题解

纯板子,套个公式算即可。

std

代码
C++
/* Use the slash-star style comments or the system won't see your
   identification information */
/*
ID: zhuheng1
TASK:
LANG: C++
*/
/* LANG can be C++11 or C++14 for those more recent releases */
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int mod=998244353;
#undef _GLIBCXX_DEBUG
template <typename A, typename B>string to_string(pair<A, B> p);
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p);
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p);
string to_string(const string& s) {  return '"' + s + '"';}
string to_string(const char* s) {  return to_string((string) s);}
string to_string(bool b) {  return (b ? "true" : "false");}
string to_string(vector<bool> v) {  bool first = true;  string res = "{";  for (int i = 0; i < static_cast<int>(v.size()); i++) {  if (!first) {  res += ", ";  }  first = false;  res += to_string(v[i]);  }  res += "}";  return res;}
template <size_t N> string to_string(bitset<N> v) {  string res = ""; for (size_t i = 0; i < N; i++) { res += static_cast<char>('0' + v[i]); } return res; }
template <typename A> string to_string(A v) {bool first = true; string res = "{"; for (const auto &x : v) { if (!first) { res += ", "; } first = false; res += to_string(x); } res += "}"; return res;}
template <typename A, typename B> string to_string(pair<A, B> p) { return "(" + to_string(p.first) + ", " + to_string(p.second) + ")"; }
template <typename A, typename B, typename C>string to_string(tuple<A, B, C> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")"; }
template <typename A, typename B, typename C, typename D>string to_string(tuple<A, B, C, D> p) { return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")"; }
void debug_out() { cerr << endl; }
template <typename Head, typename... Tail>void debug_out(Head H, Tail... T) { cerr << " " << to_string(H); debug_out(T...); }
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)

int n,a[500005];
struct node{
	int u,dis;
	friend bool operator < (node x,node y){
		return x.dis>y.dis;
	}
};
vector<node>ed[500005];
ll dis[500005];
priority_queue<node> q;
ll l,r,N;
void work(){
	/*fill your code here*/
	cin>>N>>l>>r;
	for(int i=1;i<=N;i++){
		int x;
		cin>>x;
		if(x)a[++n]=x;
	}
	for(int i=0;i<a[1];i++){
		for(int l=2;l<=n;l++){
//			if(i+a[l]<a[1])
//			ed[i].push_back({i+a[l],0});
//			else
			ed[i].push_back({(i+a[l])%a[1],(i+a[l])/a[1]});
		}
	}
	
	q.push({0,0});
	memset(dis,-1,sizeof(dis));
	while(!q.empty()){
		node x=q.top();q.pop();
		if(dis[x.u]!=-1) continue;
		dis[x.u]=x.dis;
		for(int i=0;i<ed[x.u].size();i++){
			if(dis[ed[x.u][i].u]==-1){
				q.push({ed[x.u][i].u,x.dis+ed[x.u][i].dis});
			}
		}
	}
//	for(int i=0;i<a[1];i++){
//		cout<<dis[i]<<" ";
//	}
	l--;
	ll ans=0;
	for(int i=0;i<a[1];i++){
		ll cnt=l/a[1];
		if(i%a[1]<=l%a[1]){
			cnt++;
		}
		if(i==0)cnt--;
		if(dis[i]==-1)dis[i]=1e18;
		cnt=max(cnt-dis[i],0ll);
		ans-=cnt;
	}
	for(int i=0;i<a[1];i++){
		ll cnt=r/a[1];
		if(i%a[1]<=r%a[1]){
			cnt++;
		}
		if(i==0)cnt--;
		if(dis[i]==-1)dis[i]=1e18;
		cnt=max(cnt-dis[i],0ll);
		ans+=cnt;
	}
	cout<<ans;
	return;
}

#ifdef int
signed main()
#else
int main()
#endif
{
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	int T=1;
//	cin>>T;  /*If there's only one test case, comment this line.*/
	while(T--){
		work();
	}
    return 0;
}



/* Code written by
		gala_xk */