同余最短路
用法
应用范围其实很狭窄。
- 给定
n 个整数,求这n 个整数能拼凑出多少的其他整数(n 个整数可以重复取) - 给定
n 个整数,求这n 个整数不能拼凑出的最小(大)的整数 - 给定
n 个整数,至少要拼几次才能拼出模p 余k 的数」
算法
::::info[点击查看] 我们先推一下柿子:
| 我们建 |
|---|
例题
P2371 [国家集训队] 墨墨的等式
题解
:::info[题解] 纯板子,套个公式算即可。 :::
std
:::info[代码]
/* 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 */
:::