zoukankan      html  css  js  c++  java
  • bzoj 2118: 墨墨的等式 spfa

    题目:

    墨墨突然对等式很感兴趣,他正在研究(a_1x_1+a_2y_2+ ... +a_nx_n=B)存在非负整数解的条件,他要求你编写一个程序,给定(N,{a_n})以及(B)的取值范围,求出有多少(B)可以使等式存在非负整数解。

    题解:

    首先我们发现 : 如果我们能够通过选取一些数凑到(x),那么我们肯定能够凑到$x + a_1 ,x + 2a_1 ,x + 3a_1, ... ( 所以我们考虑在)mod a_1(的剩余系下进行操作. 记)f[x](表示取到可以用)k*a_1 + x(表示的数的最小的)k$
    这个dp我们可以直接利用最短路算法求解.

    #include <cstdio>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    typedef long long ll;
    inline void read(ll &x){
        x=0;char ch;bool flag = false;
        while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
        while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
    }
    const ll maxn = 500500;
    const ll lim = maxn<<1;
    ll a[maxn],dis[maxn],q[lim + 10],l,r,n;
    bool inq[maxn];
    void spfa(){
        memset(dis,0x3f,sizeof dis);
        l = 0;r = -1;
        dis[0] = 0;q[++r] = 0;
        inq[0] = true;
        while(l <= r){
            ll u = q[l++];
            for(ll i=2;i<=n;++i){
                ll v = (u + a[i]) % a[1];
                if( dis[v] > dis[u] + (u+a[i])/a[1]){
                    dis[v] = dis[u] + (u+a[i])/a[1];
                    if(!inq[v]){
                        q[++r] = v;
                        inq[v] = true;
                    }
                }
            }inq[u] = false;
        }
    }
    inline ll calc(ll x){
        ll ret = 0;
        for(ll i=0;i<a[1];++i){
            ret += max((x/a[1] + ((x % a[1]) >= i)) - dis[i],0LL);
        }return ret;
    }
    int main(){
        ll L,R;read(n);read(L);read(R);
        ll pos = 0;
        for(ll i=1;i<=n;++i){
            read(a[i]);
            if(pos == 0 || a[pos] > a[i]) pos = i;
        }swap(a[pos],a[1]);
        if(a[1] == 0) return puts("0");
        spfa();
        printf("%lld
    ",calc(R) - calc(L-1));
        getchar();getchar();
        return 0;
    }
    
  • 相关阅读:
    Python 模块 itertools
    Python 字符串的encode与decode
    python 模块 hashlib(提供多个不同的加密算法)
    暴力尝试安卓gesture.key
    hdu 1300 Pearls(DP)
    hdu 1232 畅通工程(并查集)
    hdu 1856 More is better(并查集)
    hdu 1198 Farm Irrigation(并查集)
    hdu 3635 Dragon Balls(并查集)
    hdu 3038 How Many Answers Are Wrong(并查集)
  • 原文地址:https://www.cnblogs.com/Skyminer/p/6584462.html
Copyright © 2011-2022 走看看