1. 程式人生 > >BZOJ2118: 墨墨的等式(最短路 數論)

BZOJ2118: 墨墨的等式(最短路 數論)

tails bzoj == sin may 程序 pre mat memset

題意

墨墨突然對等式很感興趣,他正在研究a1x1+a2y2+…+anxn=B存在非負整數解的條件,他要求你編寫一個程序,給定N、{an}、以及B的取值範圍,求出有多少B可以使等式存在非負整數解。

Sol

maya神仙題啊,感覺自己做題難度跨度太大了qwq。

這裏有一篇講的非常好的博客https://blog.csdn.net/w4149/article/details/66476606?locationNum=3&fps=1

思路大概就是 利用取余的性質,把能夠構造出來的解表示成統一的形式

發現該形式可以通過最短路更新

然後就做完了。。

#include<cstdio>
#include
<algorithm> #include<stack> #include<queue> #include<cmath> #include<cstring> #define lb(x) (x & (-x)) #define Pair pair<int, int> #define fi first #define se second #define MP(x, y) make_pair(x, y) #define LL long long using namespace std; const
int MAXN = 1e6 + 10; inline LL read() { char c = getchar(); LL x = 0, f = 1; while(c < 0 || c > 9) {if(c == -) f = -1; c = getchar();} while(c >= 0 && c <= 9) x = x * 10 + c - 0, c = getchar(); return x * f; } int N, vis[MAXN]; LL dis[MAXN], a[MAXN], Mi
= 1e15, Bmin, Bmax; void SPFA() { queue<int> q; memset(dis, 0xf, sizeof(dis)); dis[0] = 0; vis[0] = 1, q.push(0); while(!q.empty()) { int p = q.front(); q.pop(); vis[p] = 1; for(int i = 1; i <= N; i++) { int to = (p + a[i]) % Mi;//tag if(dis[to] > dis[p] + a[i]) { dis[to] = dis[p] + a[i]; if(!vis[to]) vis[to] = 1, q.push(to); } } } } LL Query(LL x) { LL rt = 0; for(int i = 0; i < Mi; i++) if(dis[i] <= x) rt += (x - dis[i]) / Mi + 1; return rt; } int main() { N = read(); Bmin = read(); Bmax = read(); for(int i = 1; i <= N; i++) { a[i] = read(); if(a[i] != 0) Mi = min(Mi, a[i]); } SPFA(); printf("%lld", Query(Bmax) - Query(Bmin - 1)); return 0; } /* */

BZOJ2118: 墨墨的等式(最短路 數論)