生活随笔
收集整理的這篇文章主要介紹了
P2371 [国家集训队]墨墨的等式 同余最短路
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
傳送門
題意:
思路:
一個同于最短路的板子題,初始的時候值為0,所以設dis[0]=0dis[0]=0dis[0]=0,讓后選擇一個最小的a[i]a[i]a[i]作為basebasebase,跑一遍同余最短路就好啦。跑完dis[i]dis[i]dis[i]表示在模basebasebase的意義下能到達iii的最小數。讓后問的是區間里的,轉化為[0,r]?[0,l?1][0,r]-[0,l-1][0,r]?[0,l?1]就好啦。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std
;
typedef long long LL
;
typedef unsigned long long ULL
;
typedef pair
<int,int> PII
;const int N
=1000010,mod
=1e9+7,INF
=0x3f3f3f3f;
const double eps
=1e-6;int n
; LL l
,r
;
int a
[N
];
LL dis
[N
];
bool st
[N
];void dijkstra()
{memset(dis
,INF
,sizeof(dis
));queue
<int>q
; q
.push(0);dis
[0]=0;while(q
.size()){int u
=q
.front(); q
.pop(); st
[u
]=false;for(int i
=1;i
<=n
;i
++){int now
=(a
[i
]+u
)%a
[1];if(dis
[now
]>dis
[u
]+a
[i
]){dis
[now
]=dis
[u
]+a
[i
];if(!st
[now
]) q
.push(now
),st
[now
]=true;}}}
}LL
get(LL x
)
{LL sum
=0;for(int i
=0;i
<a
[1];i
++) if(dis
[i
]<=x
) sum
+=(x
-dis
[i
])/a
[1]+1;return sum
;
}int main()
{
scanf("%d%lld%lld",&n
,&l
,&r
);for(int i
=1;i
<=n
;i
++) scanf("%d",&a
[i
]);dijkstra();printf("%lld\n",get(r
)-get(l
-1));return 0;
}
總結
以上是生活随笔為你收集整理的P2371 [国家集训队]墨墨的等式 同余最短路的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。